Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: pkg/analyzer/lib/src/generated/error_verifier.dart

Issue 2622303006: Reapply "Add support for generic function type syntax, part 1" (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library analyzer.src.generated.error_verifier; 5 library analyzer.src.generated.error_verifier;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 import "dart:math" as math; 8 import "dart:math" as math;
9 9
10 import 'package:analyzer/dart/ast/ast.dart'; 10 import 'package:analyzer/dart/ast/ast.dart';
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after
761 _hiddenElements.declare(functionElement); 761 _hiddenElements.declare(functionElement);
762 } 762 }
763 ExecutableElement outerFunction = _enclosingFunction; 763 ExecutableElement outerFunction = _enclosingFunction;
764 try { 764 try {
765 SimpleIdentifier identifier = node.name; 765 SimpleIdentifier identifier = node.name;
766 String methodName = ""; 766 String methodName = "";
767 if (identifier != null) { 767 if (identifier != null) {
768 methodName = identifier.name; 768 methodName = identifier.name;
769 } 769 }
770 _enclosingFunction = functionElement; 770 _enclosingFunction = functionElement;
771 TypeName returnType = node.returnType; 771 TypeAnnotation returnType = node.returnType;
772 if (node.isSetter || node.isGetter) { 772 if (node.isSetter || node.isGetter) {
773 _checkForMismatchedAccessorTypes(node, methodName); 773 _checkForMismatchedAccessorTypes(node, methodName);
774 if (node.isSetter) { 774 if (node.isSetter) {
775 FunctionExpression functionExpression = node.functionExpression; 775 FunctionExpression functionExpression = node.functionExpression;
776 if (functionExpression != null) { 776 if (functionExpression != null) {
777 _checkForWrongNumberOfParametersForSetter( 777 _checkForWrongNumberOfParametersForSetter(
778 identifier, functionExpression.parameters); 778 identifier, functionExpression.parameters);
779 } 779 }
780 _checkForNonVoidReturnTypeForSetter(returnType); 780 _checkForNonVoidReturnTypeForSetter(returnType);
781 } 781 }
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
855 [node.identifier]); 855 [node.identifier]);
856 } 856 }
857 } 857 }
858 return super.visitFunctionTypedFormalParameter(node); 858 return super.visitFunctionTypedFormalParameter(node);
859 } finally { 859 } finally {
860 _isInFunctionTypedFormalParameter = old; 860 _isInFunctionTypedFormalParameter = old;
861 } 861 }
862 } 862 }
863 863
864 @override 864 @override
865 Object visitGenericFunctionType(GenericFunctionType node) {
866 throw new StateError(
867 'Support for generic function types is not yet implemented');
868 // return super.visitGenericFunctionType(node);
869 }
870
871 @override
872 Object visitGenericTypeAlias(GenericTypeAlias node) {
873 throw new StateError(
874 'Support for generic type aliases is not yet implemented');
875 // return super.visitGenericTypeAlias(node);
876 }
877
878 @override
865 Object visitIfStatement(IfStatement node) { 879 Object visitIfStatement(IfStatement node) {
866 _checkForNonBoolCondition(node.condition); 880 _checkForNonBoolCondition(node.condition);
867 return super.visitIfStatement(node); 881 return super.visitIfStatement(node);
868 } 882 }
869 883
870 @override 884 @override
871 Object visitImplementsClause(ImplementsClause node) { 885 Object visitImplementsClause(ImplementsClause node) {
872 node.interfaces.forEach(_checkForImplicitDynamicType); 886 node.interfaces.forEach(_checkForImplicitDynamicType);
873 return super.visitImplementsClause(node); 887 return super.visitImplementsClause(node);
874 } 888 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
924 _checkForTypeAnnotationDeferredClass(node.type); 938 _checkForTypeAnnotationDeferredClass(node.type);
925 _checkForTypeAnnotationGenericFunctionParameter(node.type); 939 _checkForTypeAnnotationGenericFunctionParameter(node.type);
926 return super.visitIsExpression(node); 940 return super.visitIsExpression(node);
927 } 941 }
928 942
929 @override 943 @override
930 Object visitListLiteral(ListLiteral node) { 944 Object visitListLiteral(ListLiteral node) {
931 TypeArgumentList typeArguments = node.typeArguments; 945 TypeArgumentList typeArguments = node.typeArguments;
932 if (typeArguments != null) { 946 if (typeArguments != null) {
933 if (!_options.strongMode && node.constKeyword != null) { 947 if (!_options.strongMode && node.constKeyword != null) {
934 NodeList<TypeName> arguments = typeArguments.arguments; 948 NodeList<TypeAnnotation> arguments = typeArguments.arguments;
935 if (arguments.isNotEmpty) { 949 if (arguments.isNotEmpty) {
936 _checkForInvalidTypeArgumentInConstTypedLiteral(arguments, 950 _checkForInvalidTypeArgumentInConstTypedLiteral(arguments,
937 CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_LIST); 951 CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_LIST);
938 } 952 }
939 } 953 }
940 _checkForExpectedOneListTypeArgument(node, typeArguments); 954 _checkForExpectedOneListTypeArgument(node, typeArguments);
941 } 955 }
942 _checkForImplicitDynamicTypedLiteral(node); 956 _checkForImplicitDynamicTypedLiteral(node);
943 _checkForListElementTypeNotAssignable(node); 957 _checkForListElementTypeNotAssignable(node);
944 return super.visitListLiteral(node); 958 return super.visitListLiteral(node);
945 } 959 }
946 960
947 @override 961 @override
948 Object visitMapLiteral(MapLiteral node) { 962 Object visitMapLiteral(MapLiteral node) {
949 TypeArgumentList typeArguments = node.typeArguments; 963 TypeArgumentList typeArguments = node.typeArguments;
950 if (typeArguments != null) { 964 if (typeArguments != null) {
951 NodeList<TypeName> arguments = typeArguments.arguments; 965 NodeList<TypeAnnotation> arguments = typeArguments.arguments;
952 if (!_options.strongMode && arguments.isNotEmpty) { 966 if (!_options.strongMode && arguments.isNotEmpty) {
953 if (node.constKeyword != null) { 967 if (node.constKeyword != null) {
954 _checkForInvalidTypeArgumentInConstTypedLiteral(arguments, 968 _checkForInvalidTypeArgumentInConstTypedLiteral(arguments,
955 CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_MAP); 969 CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_MAP);
956 } 970 }
957 } 971 }
958 _checkExpectedTwoMapTypeArguments(typeArguments); 972 _checkExpectedTwoMapTypeArguments(typeArguments);
959 } 973 }
960 _checkForImplicitDynamicTypedLiteral(node); 974 _checkForImplicitDynamicTypedLiteral(node);
961 _checkForMapTypeNotAssignable(node); 975 _checkForMapTypeNotAssignable(node);
962 _checkForNonConstMapAsExpressionStatement(node); 976 _checkForNonConstMapAsExpressionStatement(node);
963 return super.visitMapLiteral(node); 977 return super.visitMapLiteral(node);
964 } 978 }
965 979
966 @override 980 @override
967 Object visitMethodDeclaration(MethodDeclaration node) { 981 Object visitMethodDeclaration(MethodDeclaration node) {
968 ExecutableElement previousFunction = _enclosingFunction; 982 ExecutableElement previousFunction = _enclosingFunction;
969 try { 983 try {
970 _isInStaticMethod = node.isStatic; 984 _isInStaticMethod = node.isStatic;
971 _enclosingFunction = node.element; 985 _enclosingFunction = node.element;
972 SimpleIdentifier identifier = node.name; 986 SimpleIdentifier identifier = node.name;
973 String methodName = ""; 987 String methodName = "";
974 if (identifier != null) { 988 if (identifier != null) {
975 methodName = identifier.name; 989 methodName = identifier.name;
976 } 990 }
977 TypeName returnTypeName = node.returnType; 991 TypeAnnotation returnType = node.returnType;
978 if (node.isSetter || node.isGetter) { 992 if (node.isSetter || node.isGetter) {
979 _checkForMismatchedAccessorTypes(node, methodName); 993 _checkForMismatchedAccessorTypes(node, methodName);
980 } 994 }
981 if (node.isGetter) { 995 if (node.isGetter) {
982 _checkForVoidReturnType(node); 996 _checkForVoidReturnType(node);
983 _checkForConflictingStaticGetterAndInstanceSetter(node); 997 _checkForConflictingStaticGetterAndInstanceSetter(node);
984 } else if (node.isSetter) { 998 } else if (node.isSetter) {
985 _checkForInvalidModifierOnBody( 999 _checkForInvalidModifierOnBody(
986 node.body, CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER); 1000 node.body, CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER);
987 _checkForWrongNumberOfParametersForSetter(node.name, node.parameters); 1001 _checkForWrongNumberOfParametersForSetter(node.name, node.parameters);
988 _checkForNonVoidReturnTypeForSetter(returnTypeName); 1002 _checkForNonVoidReturnTypeForSetter(returnType);
989 _checkForConflictingStaticSetterAndInstanceMember(node); 1003 _checkForConflictingStaticSetterAndInstanceMember(node);
990 } else if (node.isOperator) { 1004 } else if (node.isOperator) {
991 _checkForOptionalParameterInOperator(node); 1005 _checkForOptionalParameterInOperator(node);
992 _checkForWrongNumberOfParametersForOperator(node); 1006 _checkForWrongNumberOfParametersForOperator(node);
993 _checkForNonVoidReturnTypeForOperator(node); 1007 _checkForNonVoidReturnTypeForOperator(node);
994 } 1008 }
995 _checkForConcreteClassWithAbstractMember(node); 1009 _checkForConcreteClassWithAbstractMember(node);
996 _checkForAllInvalidOverrideErrorCodesForMethod(node); 1010 _checkForAllInvalidOverrideErrorCodesForMethod(node);
997 _checkForTypeAnnotationDeferredClass(returnTypeName); 1011 _checkForTypeAnnotationDeferredClass(returnType);
998 _checkForIllegalReturnType(returnTypeName); 1012 _checkForIllegalReturnType(returnType);
999 _checkForImplicitDynamicReturn(node.name, node.element); 1013 _checkForImplicitDynamicReturn(node, node.element);
1000 _checkForMustCallSuper(node); 1014 _checkForMustCallSuper(node);
1001 return super.visitMethodDeclaration(node); 1015 return super.visitMethodDeclaration(node);
1002 } finally { 1016 } finally {
1003 _enclosingFunction = previousFunction; 1017 _enclosingFunction = previousFunction;
1004 _isInStaticMethod = false; 1018 _isInStaticMethod = false;
1005 } 1019 }
1006 } 1020 }
1007 1021
1008 @override 1022 @override
1009 Object visitMethodInvocation(MethodInvocation node) { 1023 Object visitMethodInvocation(MethodInvocation node) {
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
1180 } 1194 }
1181 1195
1182 @override 1196 @override
1183 Object visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { 1197 Object visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
1184 _checkForFinalNotInitialized(node.variables); 1198 _checkForFinalNotInitialized(node.variables);
1185 return super.visitTopLevelVariableDeclaration(node); 1199 return super.visitTopLevelVariableDeclaration(node);
1186 } 1200 }
1187 1201
1188 @override 1202 @override
1189 Object visitTypeArgumentList(TypeArgumentList node) { 1203 Object visitTypeArgumentList(TypeArgumentList node) {
1190 NodeList<TypeName> list = node.arguments; 1204 NodeList<TypeAnnotation> list = node.arguments;
1191 for (TypeName typeName in list) { 1205 for (TypeAnnotation type in list) {
1192 _checkForTypeAnnotationDeferredClass(typeName); 1206 _checkForTypeAnnotationDeferredClass(type);
1193 } 1207 }
1194 return super.visitTypeArgumentList(node); 1208 return super.visitTypeArgumentList(node);
1195 } 1209 }
1196 1210
1197 @override 1211 @override
1198 Object visitTypeName(TypeName node) { 1212 Object visitTypeName(TypeName node) {
1199 _checkForTypeArgumentNotMatchingBounds(node); 1213 _checkForTypeArgumentNotMatchingBounds(node);
1200 _checkForTypeParameterReferencedByStatic(node); 1214 _checkForTypeParameterReferencedByStatic(node);
1201 return super.visitTypeName(node); 1215 return super.visitTypeName(node);
1202 } 1216 }
(...skipping 2010 matching lines...) Expand 10 before | Expand all | Expand 10 after
3213 */ 3227 */
3214 void _checkForConstWithNonConst(InstanceCreationExpression expression) { 3228 void _checkForConstWithNonConst(InstanceCreationExpression expression) {
3215 ConstructorElement constructorElement = expression.staticElement; 3229 ConstructorElement constructorElement = expression.staticElement;
3216 if (constructorElement != null && !constructorElement.isConst) { 3230 if (constructorElement != null && !constructorElement.isConst) {
3217 _errorReporter.reportErrorForNode( 3231 _errorReporter.reportErrorForNode(
3218 CompileTimeErrorCode.CONST_WITH_NON_CONST, expression); 3232 CompileTimeErrorCode.CONST_WITH_NON_CONST, expression);
3219 } 3233 }
3220 } 3234 }
3221 3235
3222 /** 3236 /**
3223 * Verify that the given [typeName] does not reference any type parameters. 3237 * Verify that the given [type] does not reference any type parameters.
3224 * 3238 *
3225 * See [CompileTimeErrorCode.CONST_WITH_TYPE_PARAMETERS]. 3239 * See [CompileTimeErrorCode.CONST_WITH_TYPE_PARAMETERS].
3226 */ 3240 */
3227 void _checkForConstWithTypeParameters(TypeName typeName) { 3241 void _checkForConstWithTypeParameters(TypeAnnotation type) {
3228 // something wrong with AST 3242 // something wrong with AST
3229 if (typeName == null) { 3243 if (type is! TypeName) {
3230 return; 3244 return;
3231 } 3245 }
3246 TypeName typeName = type;
3232 Identifier name = typeName.name; 3247 Identifier name = typeName.name;
3233 if (name == null) { 3248 if (name == null) {
3234 return; 3249 return;
3235 } 3250 }
3236 // should not be a type parameter 3251 // should not be a type parameter
3237 if (name.staticElement is TypeParameterElement) { 3252 if (name.staticElement is TypeParameterElement) {
3238 _errorReporter.reportErrorForNode( 3253 _errorReporter.reportErrorForNode(
3239 CompileTimeErrorCode.CONST_WITH_TYPE_PARAMETERS, name); 3254 CompileTimeErrorCode.CONST_WITH_TYPE_PARAMETERS, name);
3240 } 3255 }
3241 // check type arguments 3256 // check type arguments
3242 TypeArgumentList typeArguments = typeName.typeArguments; 3257 TypeArgumentList typeArguments = typeName.typeArguments;
3243 if (typeArguments != null) { 3258 if (typeArguments != null) {
3244 for (TypeName argument in typeArguments.arguments) { 3259 for (TypeAnnotation argument in typeArguments.arguments) {
3245 _checkForConstWithTypeParameters(argument); 3260 _checkForConstWithTypeParameters(argument);
3246 } 3261 }
3247 } 3262 }
3248 } 3263 }
3249 3264
3250 /** 3265 /**
3251 * Verify that if the given 'const' instance creation [expression] is being 3266 * Verify that if the given 'const' instance creation [expression] is being
3252 * invoked on the resolved constructor. The [constructorName] is the 3267 * invoked on the resolved constructor. The [constructorName] is the
3253 * constructor name, always non-`null`. The [typeName] is the name of the type 3268 * constructor name, always non-`null`. The [typeName] is the name of the type
3254 * defining the constructor, always non-`null`. 3269 * defining the constructor, always non-`null`.
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after
3799 _checkForFinalNotInitialized(classMember.fields); 3814 _checkForFinalNotInitialized(classMember.fields);
3800 } 3815 }
3801 } 3816 }
3802 } 3817 }
3803 3818
3804 /** 3819 /**
3805 * If the current function is async, async*, or sync*, verify that its 3820 * If the current function is async, async*, or sync*, verify that its
3806 * declared return type is assignable to Future, Stream, or Iterable, 3821 * declared return type is assignable to Future, Stream, or Iterable,
3807 * respectively. If not, report the error using [returnType]. 3822 * respectively. If not, report the error using [returnType].
3808 */ 3823 */
3809 void _checkForIllegalReturnType(TypeName returnType) { 3824 void _checkForIllegalReturnType(TypeAnnotation returnType) {
3810 if (returnType == null) { 3825 if (returnType == null) {
3811 // No declared return type, so the return type must be dynamic, which is 3826 // No declared return type, so the return type must be dynamic, which is
3812 // assignable to everything. 3827 // assignable to everything.
3813 return; 3828 return;
3814 } 3829 }
3815 if (_enclosingFunction.isAsynchronous) { 3830 if (_enclosingFunction.isAsynchronous) {
3816 if (_enclosingFunction.isGenerator) { 3831 if (_enclosingFunction.isGenerator) {
3817 _checkForIllegalReturnTypeCode( 3832 _checkForIllegalReturnTypeCode(
3818 returnType, 3833 returnType,
3819 _typeProvider.streamDynamicType, 3834 _typeProvider.streamDynamicType,
(...skipping 12 matching lines...) Expand all
3832 } 3847 }
3833 } 3848 }
3834 3849
3835 /** 3850 /**
3836 * If the current function is async, async*, or sync*, verify that its 3851 * If the current function is async, async*, or sync*, verify that its
3837 * declared return type is assignable to Future, Stream, or Iterable, 3852 * declared return type is assignable to Future, Stream, or Iterable,
3838 * respectively. This is called by [_checkForIllegalReturnType] to check if 3853 * respectively. This is called by [_checkForIllegalReturnType] to check if
3839 * the declared [returnTypeName] is assignable to the required [expectedType] 3854 * the declared [returnTypeName] is assignable to the required [expectedType]
3840 * and if not report [errorCode]. 3855 * and if not report [errorCode].
3841 */ 3856 */
3842 void _checkForIllegalReturnTypeCode(TypeName returnTypeName, 3857 void _checkForIllegalReturnTypeCode(TypeAnnotation returnTypeName,
3843 DartType expectedType, StaticTypeWarningCode errorCode) { 3858 DartType expectedType, StaticTypeWarningCode errorCode) {
3844 DartType returnType = _enclosingFunction.returnType; 3859 DartType returnType = _enclosingFunction.returnType;
3845 if (_options.strongMode) { 3860 if (_options.strongMode) {
3846 // 3861 //
3847 // When checking an async/sync*/async* method, we know the exact type 3862 // When checking an async/sync*/async* method, we know the exact type
3848 // that will be returned (e.g. Future, Iterable, or Stream). 3863 // that will be returned (e.g. Future, Iterable, or Stream).
3849 // 3864 //
3850 // For example an `async` function body will return a `Future<T>` for 3865 // For example an `async` function body will return a `Future<T>` for
3851 // some `T` (possibly `dynamic`). 3866 // some `T` (possibly `dynamic`).
3852 // 3867 //
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
3979 return; 3994 return;
3980 } 3995 }
3981 if (element != null && 3996 if (element != null &&
3982 element.hasImplicitReturnType && 3997 element.hasImplicitReturnType &&
3983 element.returnType.isDynamic) { 3998 element.returnType.isDynamic) {
3984 _errorReporter.reportErrorForNode(StrongModeCode.IMPLICIT_DYNAMIC_RETURN, 3999 _errorReporter.reportErrorForNode(StrongModeCode.IMPLICIT_DYNAMIC_RETURN,
3985 functionName, [element.displayName]); 4000 functionName, [element.displayName]);
3986 } 4001 }
3987 } 4002 }
3988 4003
3989 void _checkForImplicitDynamicType(TypeName node) { 4004 void _checkForImplicitDynamicType(TypeAnnotation node) {
3990 if (_options.implicitDynamic || 4005 if (_options.implicitDynamic ||
3991 node == null || 4006 node == null ||
3992 node.typeArguments != null) { 4007 (node is TypeName && node.typeArguments != null)) {
3993 return; 4008 return;
3994 } 4009 }
3995 DartType type = node.type; 4010 DartType type = node.type;
3996 if (type is ParameterizedType && 4011 if (type is ParameterizedType &&
3997 type.typeArguments.isNotEmpty && 4012 type.typeArguments.isNotEmpty &&
3998 type.typeArguments.any((t) => t.isDynamic)) { 4013 type.typeArguments.any((t) => t.isDynamic)) {
3999 _errorReporter.reportErrorForNode( 4014 _errorReporter.reportErrorForNode(
4000 StrongModeCode.IMPLICIT_DYNAMIC_TYPE, node, [type]); 4015 StrongModeCode.IMPLICIT_DYNAMIC_TYPE, node, [type]);
4001 } 4016 }
4002 } 4017 }
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after
4450 } 4465 }
4451 } 4466 }
4452 4467
4453 /** 4468 /**
4454 * Checks to ensure that the given list of type [arguments] does not have a 4469 * Checks to ensure that the given list of type [arguments] does not have a
4455 * type parameter as a type argument. The [errorCode] is either 4470 * type parameter as a type argument. The [errorCode] is either
4456 * [CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_LIST] or 4471 * [CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_LIST] or
4457 * [CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_MAP]. 4472 * [CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_MAP].
4458 */ 4473 */
4459 void _checkForInvalidTypeArgumentInConstTypedLiteral( 4474 void _checkForInvalidTypeArgumentInConstTypedLiteral(
4460 NodeList<TypeName> arguments, ErrorCode errorCode) { 4475 NodeList<TypeAnnotation> arguments, ErrorCode errorCode) {
4461 for (TypeName typeName in arguments) { 4476 for (TypeAnnotation type in arguments) {
4462 if (typeName.type is TypeParameterType) { 4477 if (type is TypeName && type.type is TypeParameterType) {
4463 _errorReporter.reportErrorForNode(errorCode, typeName, [typeName.name]); 4478 _errorReporter.reportErrorForNode(errorCode, type, [type.name]);
4464 } 4479 }
4465 } 4480 }
4466 } 4481 }
4467 4482
4468 /** 4483 /**
4469 * Verify that the elements given list [literal] are subtypes of the list's 4484 * Verify that the elements given list [literal] are subtypes of the list's
4470 * static type. 4485 * static type.
4471 * 4486 *
4472 * See [CompileTimeErrorCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE], and 4487 * See [CompileTimeErrorCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE], and
4473 * [StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE]. 4488 * [StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE].
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after
5134 * 5149 *
5135 * See [StaticWarningCode.NON_VOID_RETURN_FOR_OPERATOR]. 5150 * See [StaticWarningCode.NON_VOID_RETURN_FOR_OPERATOR].
5136 */ 5151 */
5137 void _checkForNonVoidReturnTypeForOperator(MethodDeclaration declaration) { 5152 void _checkForNonVoidReturnTypeForOperator(MethodDeclaration declaration) {
5138 // check that []= operator 5153 // check that []= operator
5139 SimpleIdentifier name = declaration.name; 5154 SimpleIdentifier name = declaration.name;
5140 if (name.name != "[]=") { 5155 if (name.name != "[]=") {
5141 return; 5156 return;
5142 } 5157 }
5143 // check return type 5158 // check return type
5144 TypeName typeName = declaration.returnType; 5159 TypeAnnotation annotation = declaration.returnType;
5145 if (typeName != null) { 5160 if (annotation != null) {
5146 DartType type = typeName.type; 5161 DartType type = annotation.type;
5147 if (type != null && !type.isVoid) { 5162 if (type != null && !type.isVoid) {
5148 _errorReporter.reportErrorForNode( 5163 _errorReporter.reportErrorForNode(
5149 StaticWarningCode.NON_VOID_RETURN_FOR_OPERATOR, typeName); 5164 StaticWarningCode.NON_VOID_RETURN_FOR_OPERATOR, annotation);
5150 } 5165 }
5151 } 5166 }
5152 } 5167 }
5153 5168
5154 /** 5169 /**
5155 * Verify the [typeName], used as the return type of a setter, is valid 5170 * Verify the [typeName], used as the return type of a setter, is valid
5156 * (either `null` or the type 'void'). 5171 * (either `null` or the type 'void').
5157 * 5172 *
5158 * See [StaticWarningCode.NON_VOID_RETURN_FOR_SETTER]. 5173 * See [StaticWarningCode.NON_VOID_RETURN_FOR_SETTER].
5159 */ 5174 */
5160 void _checkForNonVoidReturnTypeForSetter(TypeName typeName) { 5175 void _checkForNonVoidReturnTypeForSetter(TypeAnnotation typeName) {
5161 if (typeName != null) { 5176 if (typeName != null) {
5162 DartType type = typeName.type; 5177 DartType type = typeName.type;
5163 if (type != null && !type.isVoid) { 5178 if (type != null && !type.isVoid) {
5164 _errorReporter.reportErrorForNode( 5179 _errorReporter.reportErrorForNode(
5165 StaticWarningCode.NON_VOID_RETURN_FOR_SETTER, typeName); 5180 StaticWarningCode.NON_VOID_RETURN_FOR_SETTER, typeName);
5166 } 5181 }
5167 } 5182 }
5168 } 5183 }
5169 5184
5170 /** 5185 /**
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
5572 _errorReporter.reportErrorForNode( 5587 _errorReporter.reportErrorForNode(
5573 CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF, alias); 5588 CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF, alias);
5574 } 5589 }
5575 } 5590 }
5576 5591
5577 /** 5592 /**
5578 * Verify that the given type [name] is not a deferred type. 5593 * Verify that the given type [name] is not a deferred type.
5579 * 5594 *
5580 * See [StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS]. 5595 * See [StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS].
5581 */ 5596 */
5582 void _checkForTypeAnnotationDeferredClass(TypeName name) { 5597 void _checkForTypeAnnotationDeferredClass(TypeAnnotation type) {
5583 if (name != null && name.isDeferred) { 5598 if (type is TypeName && type.isDeferred) {
5584 _errorReporter.reportErrorForNode( 5599 _errorReporter.reportErrorForNode(
5585 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS, name, [name.name]); 5600 StaticWarningCode.TYPE_ANNOTATION_DEFERRED_CLASS, type, [type.name]);
5586 } 5601 }
5587 } 5602 }
5588 5603
5589 /** 5604 /**
5590 * Verify that the given type [name] is not a type parameter in a generic 5605 * Verify that the given type [name] is not a type parameter in a generic
5591 * method. 5606 * method.
5592 * 5607 *
5593 * See [StaticWarningCode.TYPE_ANNOTATION_GENERIC_FUNCTION_PARAMETER]. 5608 * See [StaticWarningCode.TYPE_ANNOTATION_GENERIC_FUNCTION_PARAMETER].
5594 */ 5609 */
5595 void _checkForTypeAnnotationGenericFunctionParameter(TypeName typeName) { 5610 void _checkForTypeAnnotationGenericFunctionParameter(TypeAnnotation type) {
5596 if (typeName == null) { 5611 if (type is TypeName) {
5597 return; 5612 Identifier name = type.name;
5598 } 5613 if (name is SimpleIdentifier) {
5599 Identifier name = typeName.name; 5614 Element element = name.staticElement;
5600 if (name is SimpleIdentifier) { 5615 if (element is TypeParameterElement &&
5601 Element element = name.staticElement; 5616 element.enclosingElement is ExecutableElement) {
5602 if (element is TypeParameterElement && 5617 _errorReporter.reportErrorForNode(
5603 element.enclosingElement is ExecutableElement) { 5618 StaticWarningCode.TYPE_ANNOTATION_GENERIC_FUNCTION_PARAMETER,
5604 _errorReporter.reportErrorForNode( 5619 name,
5605 StaticWarningCode.TYPE_ANNOTATION_GENERIC_FUNCTION_PARAMETER, 5620 [name.name]);
5606 name, 5621 }
5607 [name.name]);
5608 } 5622 }
5609 } 5623 }
5610 } 5624 }
5611 5625
5612 /** 5626 /**
5613 * Verify that the type arguments in the given [typeName] are all within 5627 * Verify that the type arguments in the given [typeName] are all within
5614 * their bounds. 5628 * their bounds.
5615 * 5629 *
5616 * See [StaticTypeWarningCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS]. 5630 * See [StaticTypeWarningCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS].
5617 */ 5631 */
5618 void _checkForTypeArgumentNotMatchingBounds(TypeName typeName) { 5632 void _checkForTypeArgumentNotMatchingBounds(TypeName typeName) {
5619 if (typeName.typeArguments == null) { 5633 if (typeName.typeArguments == null) {
5620 return; 5634 return;
5621 } 5635 }
5622 // prepare Type 5636 // prepare Type
5623 DartType type = typeName.type; 5637 DartType type = typeName.type;
5624 if (type == null) { 5638 if (type == null) {
5625 return; 5639 return;
5626 } 5640 }
5627 Element element = type.element; 5641 Element element = type.element;
5628 if (element is ClassElement) { 5642 if (element is ClassElement) {
5629 // prepare type parameters 5643 // prepare type parameters
5630 List<TypeParameterElement> parameterElements = element.typeParameters; 5644 List<TypeParameterElement> parameterElements = element.typeParameters;
5631 List<DartType> parameterTypes = element.type.typeArguments; 5645 List<DartType> parameterTypes = element.type.typeArguments;
5632 List<DartType> arguments = (type as ParameterizedType).typeArguments; 5646 List<DartType> arguments = (type as ParameterizedType).typeArguments;
5633 // iterate over each bounded type parameter and corresponding argument 5647 // iterate over each bounded type parameter and corresponding argument
5634 NodeList<TypeName> typeNameArgList = typeName.typeArguments.arguments; 5648 NodeList<TypeAnnotation> argumentNodes = typeName.typeArguments.arguments;
5635 int loopThroughIndex = 5649 int loopThroughIndex =
5636 math.min(typeNameArgList.length, parameterElements.length); 5650 math.min(argumentNodes.length, parameterElements.length);
5637 bool shouldSubstitute = 5651 bool shouldSubstitute =
5638 arguments.length != 0 && arguments.length == parameterTypes.length; 5652 arguments.length != 0 && arguments.length == parameterTypes.length;
5639 for (int i = 0; i < loopThroughIndex; i++) { 5653 for (int i = 0; i < loopThroughIndex; i++) {
5640 TypeName argTypeName = typeNameArgList[i]; 5654 TypeAnnotation argumentNode = argumentNodes[i];
5641 DartType argType = argTypeName.type; 5655 DartType argType = argumentNode.type;
5642 DartType boundType = parameterElements[i].bound; 5656 DartType boundType = parameterElements[i].bound;
5643 if (argType != null && boundType != null) { 5657 if (argType != null && boundType != null) {
5644 if (shouldSubstitute) { 5658 if (shouldSubstitute) {
5645 boundType = boundType.substitute2(arguments, parameterTypes); 5659 boundType = boundType.substitute2(arguments, parameterTypes);
5646 } 5660 }
5647 if (!_typeSystem.isSubtypeOf(argType, boundType)) { 5661 if (!_typeSystem.isSubtypeOf(argType, boundType)) {
5648 ErrorCode errorCode; 5662 ErrorCode errorCode;
5649 if (_isInConstInstanceCreation) { 5663 if (_isInConstInstanceCreation) {
5650 errorCode = 5664 errorCode =
5651 CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS; 5665 CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS;
5652 } else { 5666 } else {
5653 errorCode = 5667 errorCode =
5654 StaticTypeWarningCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS; 5668 StaticTypeWarningCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS;
5655 } 5669 }
5656 _errorReporter.reportTypeErrorForNode( 5670 _errorReporter.reportTypeErrorForNode(
5657 errorCode, argTypeName, [argType, boundType]); 5671 errorCode, argumentNode, [argType, boundType]);
5658 } 5672 }
5659 } 5673 }
5660 } 5674 }
5661 } 5675 }
5662 } 5676 }
5663 5677
5664 /** 5678 /**
5665 * Check whether the given type [name] is a type parameter being used to 5679 * Check whether the given type [name] is a type parameter being used to
5666 * define a static member. 5680 * define a static member.
5667 * 5681 *
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
5857 // // (Either here or in ErrorVerifier.checkForAllFinalInitializedErrorCo des) 5871 // // (Either here or in ErrorVerifier.checkForAllFinalInitializedErrorCo des)
5858 // } 5872 // }
5859 } 5873 }
5860 5874
5861 /** 5875 /**
5862 * Verify that the given [getter] does not have a return type of 'void'. 5876 * Verify that the given [getter] does not have a return type of 'void'.
5863 * 5877 *
5864 * See [StaticWarningCode.VOID_RETURN_FOR_GETTER]. 5878 * See [StaticWarningCode.VOID_RETURN_FOR_GETTER].
5865 */ 5879 */
5866 void _checkForVoidReturnType(MethodDeclaration getter) { 5880 void _checkForVoidReturnType(MethodDeclaration getter) {
5867 TypeName returnType = getter.returnType; 5881 TypeAnnotation returnType = getter.returnType;
5868 if (returnType == null || returnType.name.name != "void") { 5882 if (returnType is TypeName && returnType.name.name == "void") {
5869 return; 5883 _errorReporter.reportErrorForNode(
5884 StaticWarningCode.VOID_RETURN_FOR_GETTER, returnType);
5870 } 5885 }
5871 _errorReporter.reportErrorForNode(
5872 StaticWarningCode.VOID_RETURN_FOR_GETTER, returnType);
5873 } 5886 }
5874 5887
5875 /** 5888 /**
5876 * Verify the given operator-method [declaration], has correct number of 5889 * Verify the given operator-method [declaration], has correct number of
5877 * parameters. 5890 * parameters.
5878 * 5891 *
5879 * This method assumes that the method declaration was tested to be an 5892 * This method assumes that the method declaration was tested to be an
5880 * operator declaration before being called. 5893 * operator declaration before being called.
5881 * 5894 *
5882 * See [CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_OPERATOR]. 5895 * See [CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_OPERATOR].
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
6070 * Verify that the given [typeArguments] are all within their bounds, as 6083 * Verify that the given [typeArguments] are all within their bounds, as
6071 * defined by the given [element]. 6084 * defined by the given [element].
6072 * 6085 *
6073 * See [StaticTypeWarningCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS]. 6086 * See [StaticTypeWarningCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS].
6074 */ 6087 */
6075 void _checkTypeArguments(Element element, TypeArgumentList typeArguments, 6088 void _checkTypeArguments(Element element, TypeArgumentList typeArguments,
6076 [DartType targetType]) { 6089 [DartType targetType]) {
6077 if (element == null || typeArguments == null) { 6090 if (element == null || typeArguments == null) {
6078 return; 6091 return;
6079 } 6092 }
6080 void reportError( 6093 void reportError(TypeAnnotation argument, DartType argumentType,
6081 TypeName argument, DartType argumentType, DartType parameterType) { 6094 DartType parameterType) {
6082 _errorReporter.reportTypeErrorForNode( 6095 _errorReporter.reportTypeErrorForNode(
6083 StaticTypeWarningCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, 6096 StaticTypeWarningCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS,
6084 argument, 6097 argument,
6085 [argumentType, parameterType]); 6098 [argumentType, parameterType]);
6086 } 6099 }
6087 6100
6088 if (element is FunctionTypedElement) { 6101 if (element is FunctionTypedElement) {
6089 _checkTypeArgumentsAgainstBounds( 6102 _checkTypeArgumentsAgainstBounds(
6090 element.typeParameters, typeArguments, targetType, reportError); 6103 element.typeParameters, typeArguments, targetType, reportError);
6091 } else if (element is ClassElement) { 6104 } else if (element is ClassElement) {
6092 _checkTypeArgumentsAgainstBounds( 6105 _checkTypeArgumentsAgainstBounds(
6093 element.typeParameters, typeArguments, targetType, reportError); 6106 element.typeParameters, typeArguments, targetType, reportError);
6094 } else if (element is ParameterElement || element is LocalVariableElement) { 6107 } else if (element is ParameterElement || element is LocalVariableElement) {
6095 // TODO(brianwilkerson) Implement this case 6108 // TODO(brianwilkerson) Implement this case
6096 } else { 6109 } else {
6097 print('Unhandled element type: ${element.runtimeType}'); 6110 print('Unhandled element type: ${element.runtimeType}');
6098 } 6111 }
6099 } 6112 }
6100 6113
6101 void _checkTypeArgumentsAgainstBounds( 6114 void _checkTypeArgumentsAgainstBounds(
6102 List<TypeParameterElement> typeParameters, 6115 List<TypeParameterElement> typeParameters,
6103 TypeArgumentList typeArgumentList, 6116 TypeArgumentList typeArgumentList,
6104 DartType targetType, 6117 DartType targetType,
6105 void reportError( 6118 void reportError(TypeAnnotation argument, DartType argumentType,
6106 TypeName argument, DartType argumentType, DartType parameterType)) { 6119 DartType parameterType)) {
6107 NodeList<TypeName> typeArguments = typeArgumentList.arguments; 6120 NodeList<TypeAnnotation> typeArguments = typeArgumentList.arguments;
6108 int argumentsLength = typeArguments.length; 6121 int argumentsLength = typeArguments.length;
6109 int maxIndex = math.min(typeParameters.length, argumentsLength); 6122 int maxIndex = math.min(typeParameters.length, argumentsLength);
6110 6123
6111 bool shouldSubstitute = 6124 bool shouldSubstitute =
6112 argumentsLength != 0 && argumentsLength == typeParameters.length; 6125 argumentsLength != 0 && argumentsLength == typeParameters.length;
6113 List<DartType> argumentTypes = shouldSubstitute 6126 List<DartType> argumentTypes = shouldSubstitute
6114 ? typeArguments.map((TypeName typeName) => typeName.type).toList() 6127 ? typeArguments.map((TypeAnnotation type) => type.type).toList()
6115 : null; 6128 : null;
6116 List<DartType> parameterTypes = shouldSubstitute 6129 List<DartType> parameterTypes = shouldSubstitute
6117 ? typeParameters 6130 ? typeParameters
6118 .map((TypeParameterElement element) => element.type) 6131 .map((TypeParameterElement element) => element.type)
6119 .toList() 6132 .toList()
6120 : null; 6133 : null;
6121 List<DartType> targetTypeParameterTypes = null; 6134 List<DartType> targetTypeParameterTypes = null;
6122 for (int i = 0; i < maxIndex; i++) { 6135 for (int i = 0; i < maxIndex; i++) {
6123 TypeName argTypeName = typeArguments[i]; 6136 TypeAnnotation argument = typeArguments[i];
6124 DartType argType = argTypeName.type; 6137 DartType argType = argument.type;
6125 DartType boundType = typeParameters[i].bound; 6138 DartType boundType = typeParameters[i].bound;
6126 if (argType != null && boundType != null) { 6139 if (argType != null && boundType != null) {
6127 if (targetType is ParameterizedType) { 6140 if (targetType is ParameterizedType) {
6128 if (targetTypeParameterTypes == null) { 6141 if (targetTypeParameterTypes == null) {
6129 targetTypeParameterTypes = targetType.typeParameters 6142 targetTypeParameterTypes = targetType.typeParameters
6130 .map((TypeParameterElement element) => element.type) 6143 .map((TypeParameterElement element) => element.type)
6131 .toList(); 6144 .toList();
6132 } 6145 }
6133 boundType = boundType.substitute2( 6146 boundType = boundType.substitute2(
6134 targetType.typeArguments, targetTypeParameterTypes); 6147 targetType.typeArguments, targetTypeParameterTypes);
6135 } 6148 }
6136 if (shouldSubstitute) { 6149 if (shouldSubstitute) {
6137 boundType = boundType.substitute2(argumentTypes, parameterTypes); 6150 boundType = boundType.substitute2(argumentTypes, parameterTypes);
6138 } 6151 }
6139 if (!_typeSystem.isSubtypeOf(argType, boundType)) { 6152 if (!_typeSystem.isSubtypeOf(argType, boundType)) {
6140 reportError(argTypeName, argType, boundType); 6153 reportError(argument, argType, boundType);
6141 } 6154 }
6142 } 6155 }
6143 } 6156 }
6144 } 6157 }
6145 6158
6146 DartType _computeReturnTypeForMethod(Expression returnExpression) { 6159 DartType _computeReturnTypeForMethod(Expression returnExpression) {
6147 // This method should never be called for generators, since generators are 6160 // This method should never be called for generators, since generators are
6148 // never allowed to contain return statements with expressions. 6161 // never allowed to contain return statements with expressions.
6149 assert(!_inGenerator); 6162 assert(!_inGenerator);
6150 if (returnExpression == null) { 6163 if (returnExpression == null) {
(...skipping 842 matching lines...) Expand 10 before | Expand all | Expand 10 after
6993 class _InvocationCollector extends RecursiveAstVisitor { 7006 class _InvocationCollector extends RecursiveAstVisitor {
6994 final List<String> superCalls = <String>[]; 7007 final List<String> superCalls = <String>[];
6995 7008
6996 @override 7009 @override
6997 visitMethodInvocation(MethodInvocation node) { 7010 visitMethodInvocation(MethodInvocation node) {
6998 if (node.target is SuperExpression) { 7011 if (node.target is SuperExpression) {
6999 superCalls.add(node.methodName.name); 7012 superCalls.add(node.methodName.name);
7000 } 7013 }
7001 } 7014 }
7002 } 7015 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/element_resolver.dart ('k') | pkg/analyzer/lib/src/generated/resolver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698