OLD | NEW |
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 1187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1198 Object visitTypeParameterList(TypeParameterList node) { | 1198 Object visitTypeParameterList(TypeParameterList node) { |
1199 _checkDuplicateDefinitionInTypeParameterList(node); | 1199 _checkDuplicateDefinitionInTypeParameterList(node); |
1200 return super.visitTypeParameterList(node); | 1200 return super.visitTypeParameterList(node); |
1201 } | 1201 } |
1202 | 1202 |
1203 @override | 1203 @override |
1204 Object visitVariableDeclaration(VariableDeclaration node) { | 1204 Object visitVariableDeclaration(VariableDeclaration node) { |
1205 SimpleIdentifier nameNode = node.name; | 1205 SimpleIdentifier nameNode = node.name; |
1206 Expression initializerNode = node.initializer; | 1206 Expression initializerNode = node.initializer; |
1207 // do checks | 1207 // do checks |
1208 _checkForInvalidAssignment(nameNode, initializerNode); | 1208 _checkForInvalidAssignment(nameNode, initializerNode, |
| 1209 isDeclarationCast: true); |
1209 _checkForImplicitDynamicIdentifier(node, nameNode); | 1210 _checkForImplicitDynamicIdentifier(node, nameNode); |
1210 // visit name | 1211 // visit name |
1211 nameNode.accept(this); | 1212 nameNode.accept(this); |
1212 // visit initializer | 1213 // visit initializer |
1213 String name = nameNode.name; | 1214 String name = nameNode.name; |
1214 _namesForReferenceToDeclaredVariableInInitializer.add(name); | 1215 _namesForReferenceToDeclaredVariableInInitializer.add(name); |
1215 bool wasInInstanceVariableInitializer = _isInInstanceVariableInitializer; | 1216 bool wasInInstanceVariableInitializer = _isInInstanceVariableInitializer; |
1216 _isInInstanceVariableInitializer = _isInInstanceVariableDeclaration; | 1217 _isInInstanceVariableInitializer = _isInInstanceVariableDeclaration; |
1217 try { | 1218 try { |
1218 if (initializerNode != null) { | 1219 if (initializerNode != null) { |
(...skipping 1278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2497 if (expressionType == null) { | 2498 if (expressionType == null) { |
2498 return; | 2499 return; |
2499 } | 2500 } |
2500 if (_expressionIsAssignableAtType(expression, expressionType, type)) { | 2501 if (_expressionIsAssignableAtType(expression, expressionType, type)) { |
2501 return; | 2502 return; |
2502 } | 2503 } |
2503 _errorReporter.reportErrorForNode(errorCode, expression, arguments); | 2504 _errorReporter.reportErrorForNode(errorCode, expression, arguments); |
2504 } | 2505 } |
2505 | 2506 |
2506 bool _checkForAssignableExpression( | 2507 bool _checkForAssignableExpression( |
2507 Expression expression, DartType expectedStaticType, ErrorCode errorCode) { | 2508 Expression expression, DartType expectedStaticType, ErrorCode errorCode, |
| 2509 {bool isDeclarationCast = false}) { |
2508 DartType actualStaticType = getStaticType(expression); | 2510 DartType actualStaticType = getStaticType(expression); |
2509 return actualStaticType != null && | 2511 return actualStaticType != null && |
2510 _checkForAssignableExpressionAtType( | 2512 _checkForAssignableExpressionAtType( |
2511 expression, actualStaticType, expectedStaticType, errorCode); | 2513 expression, actualStaticType, expectedStaticType, errorCode, |
| 2514 isDeclarationCast: isDeclarationCast); |
2512 } | 2515 } |
2513 | 2516 |
2514 bool _checkForAssignableExpressionAtType( | 2517 bool _checkForAssignableExpressionAtType( |
2515 Expression expression, | 2518 Expression expression, |
2516 DartType actualStaticType, | 2519 DartType actualStaticType, |
2517 DartType expectedStaticType, | 2520 DartType expectedStaticType, |
2518 ErrorCode errorCode) { | 2521 ErrorCode errorCode, |
| 2522 {bool isDeclarationCast = false}) { |
2519 if (!_expressionIsAssignableAtType( | 2523 if (!_expressionIsAssignableAtType( |
2520 expression, actualStaticType, expectedStaticType)) { | 2524 expression, actualStaticType, expectedStaticType, |
| 2525 isDeclarationCast: isDeclarationCast)) { |
2521 _errorReporter.reportTypeErrorForNode( | 2526 _errorReporter.reportTypeErrorForNode( |
2522 errorCode, expression, [actualStaticType, expectedStaticType]); | 2527 errorCode, expression, [actualStaticType, expectedStaticType]); |
2523 return false; | 2528 return false; |
2524 } | 2529 } |
2525 return true; | 2530 return true; |
2526 } | 2531 } |
2527 | 2532 |
2528 /** | 2533 /** |
2529 * Verify that the given [expression] is not final. | 2534 * Verify that the given [expression] is not final. |
2530 * | 2535 * |
(...skipping 1734 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4265 if (_typeSystem.isSubtypeOf(loopType, iterableType)) { | 4270 if (_typeSystem.isSubtypeOf(loopType, iterableType)) { |
4266 bestIterableType = DynamicTypeImpl.instance; | 4271 bestIterableType = DynamicTypeImpl.instance; |
4267 } | 4272 } |
4268 } | 4273 } |
4269 | 4274 |
4270 if (bestIterableType == null) { | 4275 if (bestIterableType == null) { |
4271 _errorReporter.reportTypeErrorForNode( | 4276 _errorReporter.reportTypeErrorForNode( |
4272 StaticTypeWarningCode.FOR_IN_OF_INVALID_TYPE, | 4277 StaticTypeWarningCode.FOR_IN_OF_INVALID_TYPE, |
4273 node.iterable, | 4278 node.iterable, |
4274 [iterableType, loopTypeName]); | 4279 [iterableType, loopTypeName]); |
4275 } else if (!_typeSystem.isAssignableTo(bestIterableType, variableType)) { | 4280 } else if (!_typeSystem.isAssignableTo(bestIterableType, variableType, |
| 4281 isDeclarationCast: true)) { |
4276 _errorReporter.reportTypeErrorForNode( | 4282 _errorReporter.reportTypeErrorForNode( |
4277 StaticTypeWarningCode.FOR_IN_OF_INVALID_ELEMENT_TYPE, | 4283 StaticTypeWarningCode.FOR_IN_OF_INVALID_ELEMENT_TYPE, |
4278 node.iterable, | 4284 node.iterable, |
4279 [iterableType, loopTypeName, variableType]); | 4285 [iterableType, loopTypeName, variableType]); |
4280 } | 4286 } |
4281 } | 4287 } |
4282 | 4288 |
4283 /** | 4289 /** |
4284 * Check that the given [typeReference] is not a type reference and that then | 4290 * Check that the given [typeReference] is not a type reference and that then |
4285 * the [name] is reference to an instance member. | 4291 * the [name] is reference to an instance member. |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4419 annotation.name); | 4425 annotation.name); |
4420 } | 4426 } |
4421 } | 4427 } |
4422 | 4428 |
4423 /** | 4429 /** |
4424 * Verify that the given left hand side ([lhs]) and right hand side ([rhs]) | 4430 * Verify that the given left hand side ([lhs]) and right hand side ([rhs]) |
4425 * represent a valid assignment. | 4431 * represent a valid assignment. |
4426 * | 4432 * |
4427 * See [StaticTypeWarningCode.INVALID_ASSIGNMENT]. | 4433 * See [StaticTypeWarningCode.INVALID_ASSIGNMENT]. |
4428 */ | 4434 */ |
4429 void _checkForInvalidAssignment(Expression lhs, Expression rhs) { | 4435 void _checkForInvalidAssignment(Expression lhs, Expression rhs, |
| 4436 {bool isDeclarationCast = false}) { |
4430 if (lhs == null || rhs == null) { | 4437 if (lhs == null || rhs == null) { |
4431 return; | 4438 return; |
4432 } | 4439 } |
4433 VariableElement leftVariableElement = getVariableElement(lhs); | 4440 VariableElement leftVariableElement = getVariableElement(lhs); |
4434 DartType leftType = (leftVariableElement == null) | 4441 DartType leftType = (leftVariableElement == null) |
4435 ? getStaticType(lhs) | 4442 ? getStaticType(lhs) |
4436 : leftVariableElement.type; | 4443 : leftVariableElement.type; |
4437 _checkForAssignableExpression( | 4444 _checkForAssignableExpression( |
4438 rhs, leftType, StaticTypeWarningCode.INVALID_ASSIGNMENT); | 4445 rhs, leftType, StaticTypeWarningCode.INVALID_ASSIGNMENT, |
| 4446 isDeclarationCast: isDeclarationCast); |
4439 } | 4447 } |
4440 | 4448 |
4441 /** | 4449 /** |
4442 * Given an [assignment] using a compound assignment operator, this verifies | 4450 * Given an [assignment] using a compound assignment operator, this verifies |
4443 * that the given assignment is valid. The [lhs] is the left hand side | 4451 * that the given assignment is valid. The [lhs] is the left hand side |
4444 * expression. The [rhs] is the right hand side expression. | 4452 * expression. The [rhs] is the right hand side expression. |
4445 * | 4453 * |
4446 * See [StaticTypeWarningCode.INVALID_ASSIGNMENT]. | 4454 * See [StaticTypeWarningCode.INVALID_ASSIGNMENT]. |
4447 */ | 4455 */ |
4448 void _checkForInvalidCompoundAssignment( | 4456 void _checkForInvalidCompoundAssignment( |
(...skipping 1862 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6311 } | 6319 } |
6312 DartType staticReturnType = getStaticType(returnExpression); | 6320 DartType staticReturnType = getStaticType(returnExpression); |
6313 if (staticReturnType != null && _enclosingFunction.isAsynchronous) { | 6321 if (staticReturnType != null && _enclosingFunction.isAsynchronous) { |
6314 return _typeProvider.futureType.instantiate( | 6322 return _typeProvider.futureType.instantiate( |
6315 <DartType>[staticReturnType.flattenFutures(_typeSystem)]); | 6323 <DartType>[staticReturnType.flattenFutures(_typeSystem)]); |
6316 } | 6324 } |
6317 return staticReturnType; | 6325 return staticReturnType; |
6318 } | 6326 } |
6319 | 6327 |
6320 bool _expressionIsAssignableAtType(Expression expression, | 6328 bool _expressionIsAssignableAtType(Expression expression, |
6321 DartType actualStaticType, DartType expectedStaticType) { | 6329 DartType actualStaticType, DartType expectedStaticType, |
| 6330 {isDeclarationCast: false}) { |
6322 bool concrete = _options.strongMode && checker.hasStrictArrow(expression); | 6331 bool concrete = _options.strongMode && checker.hasStrictArrow(expression); |
6323 if (concrete && actualStaticType is FunctionType) { | 6332 if (concrete && actualStaticType is FunctionType) { |
6324 actualStaticType = | 6333 actualStaticType = |
6325 _typeSystem.functionTypeToConcreteType(actualStaticType); | 6334 _typeSystem.functionTypeToConcreteType(actualStaticType); |
6326 } | 6335 } |
6327 return _typeSystem.isAssignableTo(actualStaticType, expectedStaticType); | 6336 return _typeSystem.isAssignableTo(actualStaticType, expectedStaticType, |
| 6337 isDeclarationCast: isDeclarationCast); |
6328 } | 6338 } |
6329 | 6339 |
6330 MethodElement _findOverriddenMemberThatMustCallSuper(MethodDeclaration node) { | 6340 MethodElement _findOverriddenMemberThatMustCallSuper(MethodDeclaration node) { |
6331 ExecutableElement overriddenMember = _getOverriddenMember(node.element); | 6341 ExecutableElement overriddenMember = _getOverriddenMember(node.element); |
6332 List<ExecutableElement> seen = <ExecutableElement>[]; | 6342 List<ExecutableElement> seen = <ExecutableElement>[]; |
6333 while ( | 6343 while ( |
6334 overriddenMember is MethodElement && !seen.contains(overriddenMember)) { | 6344 overriddenMember is MethodElement && !seen.contains(overriddenMember)) { |
6335 for (ElementAnnotation annotation in overriddenMember.metadata) { | 6345 for (ElementAnnotation annotation in overriddenMember.metadata) { |
6336 if (annotation.isMustCallSuper) { | 6346 if (annotation.isMustCallSuper) { |
6337 return overriddenMember; | 6347 return overriddenMember; |
(...skipping 810 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7148 class _InvocationCollector extends RecursiveAstVisitor { | 7158 class _InvocationCollector extends RecursiveAstVisitor { |
7149 final List<String> superCalls = <String>[]; | 7159 final List<String> superCalls = <String>[]; |
7150 | 7160 |
7151 @override | 7161 @override |
7152 visitMethodInvocation(MethodInvocation node) { | 7162 visitMethodInvocation(MethodInvocation node) { |
7153 if (node.target is SuperExpression) { | 7163 if (node.target is SuperExpression) { |
7154 superCalls.add(node.methodName.name); | 7164 superCalls.add(node.methodName.name); |
7155 } | 7165 } |
7156 } | 7166 } |
7157 } | 7167 } |
OLD | NEW |