From 396ed51cf04cfdfd3f33e6803a32e6a9cbfeefcf Mon Sep 17 00:00:00 2001 From: John Messerly Date: Sat, 24 Oct 2015 12:48:53 -0700 Subject: [PATCH] fixes #24713, remove old generic substitution code in _getTypeOfProperty it looks like some old code was hanging around that tried to substitute generic type parameters by name. This is unnecessary because we already have a substituted type at this point, and the type variable we were substituting had no relationship at all with the parameter. After removing the code, all tests seem to be passing. R=brianwilkerson@google.com Review URL: https://codereview.chromium.org/1409143009 . --- .../src/generated/static_type_analyzer.dart | 49 ++++--------------- .../static_type_warning_code_test.dart | 21 ++++++++ 2 files changed, 30 insertions(+), 40 deletions(-) diff --git a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart index ce0016c586a..3f8c0ef4759 100644 --- a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart +++ b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart @@ -888,7 +888,7 @@ class StaticTypeAnalyzer extends SimpleAstVisitor { } else if (staticElement is MethodElement) { staticType = staticElement.type; } else if (staticElement is PropertyAccessorElement) { - staticType = _getTypeOfProperty(staticElement, node.prefix.staticType); + staticType = _getTypeOfProperty(staticElement); propagatedType = _getPropertyPropagatedType(staticElement, propagatedType); } else if (staticElement is ExecutableElement) { @@ -921,8 +921,7 @@ class StaticTypeAnalyzer extends SimpleAstVisitor { } else if (propagatedElement is MethodElement) { propagatedType = propagatedElement.type; } else if (propagatedElement is PropertyAccessorElement) { - propagatedType = - _getTypeOfProperty(propagatedElement, node.prefix.staticType); + propagatedType = _getTypeOfProperty(propagatedElement); propagatedType = _getPropertyPropagatedType(propagatedElement, propagatedType); } else if (propagatedElement is ExecutableElement) { @@ -1024,9 +1023,7 @@ class StaticTypeAnalyzer extends SimpleAstVisitor { if (staticElement is MethodElement) { staticType = staticElement.type; } else if (staticElement is PropertyAccessorElement) { - Expression realTarget = node.realTarget; - staticType = _getTypeOfProperty(staticElement, - realTarget != null ? _getStaticType(realTarget) : null); + staticType = _getTypeOfProperty(staticElement); } else { // TODO(brianwilkerson) Report this internal error. } @@ -1040,8 +1037,7 @@ class StaticTypeAnalyzer extends SimpleAstVisitor { propagatedType = propagatedElement.type; } else if (propagatedElement is PropertyAccessorElement) { Expression realTarget = node.realTarget; - propagatedType = _getTypeOfProperty( - propagatedElement, realTarget != null ? realTarget.bestType : null); + propagatedType = _getTypeOfProperty(propagatedElement); } else { // TODO(brianwilkerson) Report this internal error. } @@ -1117,7 +1113,7 @@ class StaticTypeAnalyzer extends SimpleAstVisitor { } else if (element is MethodElement) { staticType = element.type; } else if (element is PropertyAccessorElement) { - staticType = _getTypeOfProperty(element, null); + staticType = _getTypeOfProperty(element); } else if (element is ExecutableElement) { staticType = element.type; } else if (element is TypeParameterElement) { @@ -1628,14 +1624,9 @@ class StaticTypeAnalyzer extends SimpleAstVisitor { * Return the type that should be recorded for a node that resolved to the given accessor. * * @param accessor the accessor that the node resolved to - * @param context if the accessor element has context [by being the RHS of a - * [PrefixedIdentifier] or [PropertyAccess]], and the return type of the - * accessor is a parameter type, then the type of the LHS can be used to get more - * specific type information * @return the type that should be recorded for a node that resolved to the given accessor */ - DartType _getTypeOfProperty( - PropertyAccessorElement accessor, DartType context) { + DartType _getTypeOfProperty(PropertyAccessorElement accessor) { FunctionType functionType = accessor.type; if (functionType == null) { // TODO(brianwilkerson) Report this internal error. This happens when we @@ -1658,29 +1649,7 @@ class StaticTypeAnalyzer extends SimpleAstVisitor { } return _dynamicType; } - DartType returnType = functionType.returnType; - if (returnType is TypeParameterType && context is InterfaceType) { - // if the return type is a TypeParameter, we try to use the context [that - // the function is being called on] to get a more accurate returnType type - InterfaceType interfaceTypeContext = context; - // Type[] argumentTypes = interfaceTypeContext.getTypeArguments(); - List typeParameterElements = - interfaceTypeContext.element != null - ? interfaceTypeContext.element.typeParameters - : null; - if (typeParameterElements != null) { - for (int i = 0; i < typeParameterElements.length; i++) { - TypeParameterElement typeParameterElement = typeParameterElements[i]; - if (returnType.name == typeParameterElement.name) { - return interfaceTypeContext.typeArguments[i]; - } - } - // TODO(jwren) troubleshoot why call to substitute doesn't work -// Type[] parameterTypes = TypeParameterTypeImpl.getTypes(parameterElements); -// return returnType.substitute(argumentTypes, parameterTypes); - } - } - return returnType; + return functionType.returnType; } /** @@ -1699,8 +1668,8 @@ class StaticTypeAnalyzer extends SimpleAstVisitor { LocalVariableElementImpl element = loopVariable.element; DartType exprType = expr.staticType; DartType targetType = (loop.awaitKeyword == null) - ? _typeProvider.iterableType - : _typeProvider.streamType; + ? _typeProvider.iterableType + : _typeProvider.streamType; DartType iteratedType = _findIteratedType(exprType, targetType); if (element != null && iteratedType != null) { element.type = iteratedType; diff --git a/pkg/analyzer/test/generated/static_type_warning_code_test.dart b/pkg/analyzer/test/generated/static_type_warning_code_test.dart index 79ccd5800ce..99ab84c21c7 100644 --- a/pkg/analyzer/test/generated/static_type_warning_code_test.dart +++ b/pkg/analyzer/test/generated/static_type_warning_code_test.dart @@ -832,6 +832,27 @@ class A { verify([source]); } + // https://github.com/dart-lang/sdk/issues/24713 + void test_returnOfInvalidType_not_issued_for_valid_generic_return() { + Source source = addSource(r''' +abstract class F { + U get value; +} + +abstract class G { + T test(F arg) => arg.value; +} + +abstract class H { + S test(F arg) => arg.value; +} + +void main() { }'''); + computeLibrarySourceErrors(source); + assertNoErrors(source); + verify([source]); + } + void test_returnOfInvalidType_void() { Source source = addSource("void f() { return 42; }"); computeLibrarySourceErrors(source);