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

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

Issue 2288983002: Move error checking from scope creation to error reporter (Closed)
Patch Set: Created 4 years, 3 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 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 StaticTypeWarningCode.NON_BOOL_OPERAND, [lexeme]); 380 StaticTypeWarningCode.NON_BOOL_OPERAND, [lexeme]);
381 _checkForAssignability(node.rightOperand, _boolType, 381 _checkForAssignability(node.rightOperand, _boolType,
382 StaticTypeWarningCode.NON_BOOL_OPERAND, [lexeme]); 382 StaticTypeWarningCode.NON_BOOL_OPERAND, [lexeme]);
383 } else { 383 } else {
384 _checkForArgumentTypeNotAssignableForArgument(node.rightOperand); 384 _checkForArgumentTypeNotAssignableForArgument(node.rightOperand);
385 } 385 }
386 return super.visitBinaryExpression(node); 386 return super.visitBinaryExpression(node);
387 } 387 }
388 388
389 @override 389 @override
390 Object visitBlock(Block node) {
391 _checkDuplicateDeclarationInStatements(node.statements);
392 return super.visitBlock(node);
393 }
394
395 @override
390 Object visitBlockFunctionBody(BlockFunctionBody node) { 396 Object visitBlockFunctionBody(BlockFunctionBody node) {
391 bool wasInAsync = _inAsync; 397 bool wasInAsync = _inAsync;
392 bool wasInGenerator = _inGenerator; 398 bool wasInGenerator = _inGenerator;
393 bool previousHasReturnWithoutValue = _hasReturnWithoutValue; 399 bool previousHasReturnWithoutValue = _hasReturnWithoutValue;
394 _hasReturnWithoutValue = false; 400 _hasReturnWithoutValue = false;
395 List<ReturnStatement> previousReturnsWith = _returnsWith; 401 List<ReturnStatement> previousReturnsWith = _returnsWith;
396 List<ReturnStatement> previousReturnsWithout = _returnsWithout; 402 List<ReturnStatement> previousReturnsWithout = _returnsWithout;
397 try { 403 try {
398 _inAsync = node.isAsynchronous; 404 _inAsync = node.isAsynchronous;
399 _inGenerator = node.isGenerator; 405 _inGenerator = node.isGenerator;
(...skipping 19 matching lines...) Expand all
419 if (labelElement is LabelElementImpl && labelElement.isOnSwitchMember) { 425 if (labelElement is LabelElementImpl && labelElement.isOnSwitchMember) {
420 _errorReporter.reportErrorForNode( 426 _errorReporter.reportErrorForNode(
421 ResolverErrorCode.BREAK_LABEL_ON_SWITCH_MEMBER, labelNode); 427 ResolverErrorCode.BREAK_LABEL_ON_SWITCH_MEMBER, labelNode);
422 } 428 }
423 } 429 }
424 return null; 430 return null;
425 } 431 }
426 432
427 @override 433 @override
428 Object visitCatchClause(CatchClause node) { 434 Object visitCatchClause(CatchClause node) {
435 _checkDuplicateDefinitionInCatchClause(node);
429 bool previousIsInCatchClause = _isInCatchClause; 436 bool previousIsInCatchClause = _isInCatchClause;
430 try { 437 try {
431 _isInCatchClause = true; 438 _isInCatchClause = true;
432 _checkForTypeAnnotationDeferredClass(node.exceptionType); 439 _checkForTypeAnnotationDeferredClass(node.exceptionType);
433 return super.visitCatchClause(node); 440 return super.visitCatchClause(node);
434 } finally { 441 } finally {
435 _isInCatchClause = previousIsInCatchClause; 442 _isInCatchClause = previousIsInCatchClause;
436 } 443 }
437 } 444 }
438 445
439 @override 446 @override
440 Object visitClassDeclaration(ClassDeclaration node) { 447 Object visitClassDeclaration(ClassDeclaration node) {
441 ClassElementImpl outerClass = _enclosingClass; 448 ClassElementImpl outerClass = _enclosingClass;
442 try { 449 try {
443 _isInNativeClass = node.nativeClause != null; 450 _isInNativeClass = node.nativeClause != null;
444 _enclosingClass = AbstractClassElementImpl.getImpl(node.element); 451 _enclosingClass = AbstractClassElementImpl.getImpl(node.element);
445 ExtendsClause extendsClause = node.extendsClause; 452 _checkDuplicateClassMembers(node);
446 ImplementsClause implementsClause = node.implementsClause;
447 WithClause withClause = node.withClause;
448 _checkForBuiltInIdentifierAsName( 453 _checkForBuiltInIdentifierAsName(
449 node.name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME); 454 node.name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME);
450 _checkForMemberWithClassName(); 455 _checkForMemberWithClassName();
451 _checkForNoDefaultSuperConstructorImplicit(node); 456 _checkForNoDefaultSuperConstructorImplicit(node);
452 _checkForConflictingTypeVariableErrorCodes(node); 457 _checkForConflictingTypeVariableErrorCodes(node);
458 ExtendsClause extendsClause = node.extendsClause;
459 ImplementsClause implementsClause = node.implementsClause;
460 WithClause withClause = node.withClause;
453 // Only do error checks on the clause nodes if there is a non-null clause 461 // Only do error checks on the clause nodes if there is a non-null clause
454 if (implementsClause != null || 462 if (implementsClause != null ||
455 extendsClause != null || 463 extendsClause != null ||
456 withClause != null) { 464 withClause != null) {
457 // Only check for all of the inheritance logic around clauses if there 465 // Only check for all of the inheritance logic around clauses if there
458 // isn't an error code such as "Cannot extend double" already on the 466 // isn't an error code such as "Cannot extend double" already on the
459 // class. 467 // class.
460 if (!_checkForImplementsDisallowedClass(implementsClause) && 468 if (!_checkForImplementsDisallowedClass(implementsClause) &&
461 !_checkForExtendsDisallowedClass(extendsClause) && 469 !_checkForExtendsDisallowedClass(extendsClause) &&
462 !_checkForAllMixinErrorCodes(withClause)) { 470 !_checkForAllMixinErrorCodes(withClause)) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 _isInComment = true; 545 _isInComment = true;
538 try { 546 try {
539 return super.visitComment(node); 547 return super.visitComment(node);
540 } finally { 548 } finally {
541 _isInComment = false; 549 _isInComment = false;
542 } 550 }
543 } 551 }
544 552
545 @override 553 @override
546 Object visitCompilationUnit(CompilationUnit node) { 554 Object visitCompilationUnit(CompilationUnit node) {
555 _checkDuplicateUnitMembers(node);
547 _checkForDeferredPrefixCollisions(node); 556 _checkForDeferredPrefixCollisions(node);
548 return super.visitCompilationUnit(node); 557 return super.visitCompilationUnit(node);
549 } 558 }
550 559
551 @override 560 @override
552 Object visitConditionalExpression(ConditionalExpression node) { 561 Object visitConditionalExpression(ConditionalExpression node) {
553 _checkForNonBoolCondition(node.condition); 562 _checkForNonBoolCondition(node.condition);
554 return super.visitConditionalExpression(node); 563 return super.visitConditionalExpression(node);
555 } 564 }
556 565
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 return super.visitFieldFormalParameter(node); 721 return super.visitFieldFormalParameter(node);
713 } 722 }
714 723
715 @override 724 @override
716 Object visitForEachStatement(ForEachStatement node) { 725 Object visitForEachStatement(ForEachStatement node) {
717 _checkForInIterable(node); 726 _checkForInIterable(node);
718 return super.visitForEachStatement(node); 727 return super.visitForEachStatement(node);
719 } 728 }
720 729
721 @override 730 @override
731 Object visitFormalParameterList(FormalParameterList node) {
732 _checkDuplicateDefinitionInParameterList(node);
733 return super.visitFormalParameterList(node);
734 }
735
736 @override
722 Object visitForStatement(ForStatement node) { 737 Object visitForStatement(ForStatement node) {
723 if (node.condition != null) { 738 if (node.condition != null) {
724 _checkForNonBoolCondition(node.condition); 739 _checkForNonBoolCondition(node.condition);
725 } 740 }
726 return super.visitForStatement(node); 741 return super.visitForStatement(node);
727 } 742 }
728 743
729 @override 744 @override
730 Object visitFunctionDeclaration(FunctionDeclaration node) { 745 Object visitFunctionDeclaration(FunctionDeclaration node) {
731 ExecutableElement outerFunction = _enclosingFunction; 746 ExecutableElement outerFunction = _enclosingFunction;
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
1103 Object visitSuperConstructorInvocation(SuperConstructorInvocation node) { 1118 Object visitSuperConstructorInvocation(SuperConstructorInvocation node) {
1104 _isInConstructorInitializer = true; 1119 _isInConstructorInitializer = true;
1105 try { 1120 try {
1106 return super.visitSuperConstructorInvocation(node); 1121 return super.visitSuperConstructorInvocation(node);
1107 } finally { 1122 } finally {
1108 _isInConstructorInitializer = false; 1123 _isInConstructorInitializer = false;
1109 } 1124 }
1110 } 1125 }
1111 1126
1112 @override 1127 @override
1128 Object visitSwitchCase(SwitchCase node) {
1129 _checkDuplicateDeclarationInStatements(node.statements);
1130 return super.visitSwitchCase(node);
1131 }
1132
1133 @override
1134 Object visitSwitchDefault(SwitchDefault node) {
1135 _checkDuplicateDeclarationInStatements(node.statements);
1136 return super.visitSwitchDefault(node);
1137 }
1138
1139 @override
1113 Object visitSwitchStatement(SwitchStatement node) { 1140 Object visitSwitchStatement(SwitchStatement node) {
1114 _checkForSwitchExpressionNotAssignable(node); 1141 _checkForSwitchExpressionNotAssignable(node);
1115 _checkForCaseBlocksNotTerminated(node); 1142 _checkForCaseBlocksNotTerminated(node);
1116 _checkForMissingEnumConstantInSwitch(node); 1143 _checkForMissingEnumConstantInSwitch(node);
1117 return super.visitSwitchStatement(node); 1144 return super.visitSwitchStatement(node);
1118 } 1145 }
1119 1146
1120 @override 1147 @override
1121 Object visitThisExpression(ThisExpression node) { 1148 Object visitThisExpression(ThisExpression node) {
1122 _checkForInvalidReferenceToThis(node); 1149 _checkForInvalidReferenceToThis(node);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1155 Object visitTypeParameter(TypeParameter node) { 1182 Object visitTypeParameter(TypeParameter node) {
1156 _checkForBuiltInIdentifierAsName(node.name, 1183 _checkForBuiltInIdentifierAsName(node.name,
1157 CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_PARAMETER_NAME); 1184 CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_PARAMETER_NAME);
1158 _checkForTypeParameterSupertypeOfItsBound(node); 1185 _checkForTypeParameterSupertypeOfItsBound(node);
1159 _checkForTypeAnnotationDeferredClass(node.bound); 1186 _checkForTypeAnnotationDeferredClass(node.bound);
1160 _checkForImplicitDynamicType(node.bound); 1187 _checkForImplicitDynamicType(node.bound);
1161 return super.visitTypeParameter(node); 1188 return super.visitTypeParameter(node);
1162 } 1189 }
1163 1190
1164 @override 1191 @override
1192 Object visitTypeParameterList(TypeParameterList node) {
1193 _checkDuplicateDefinitionInTypeParameterList(node);
1194 return super.visitTypeParameterList(node);
1195 }
1196
1197 @override
1165 Object visitVariableDeclaration(VariableDeclaration node) { 1198 Object visitVariableDeclaration(VariableDeclaration node) {
1166 SimpleIdentifier nameNode = node.name; 1199 SimpleIdentifier nameNode = node.name;
1167 Expression initializerNode = node.initializer; 1200 Expression initializerNode = node.initializer;
1168 // do checks 1201 // do checks
1169 _checkForInvalidAssignment(nameNode, initializerNode); 1202 _checkForInvalidAssignment(nameNode, initializerNode);
1170 _checkForImplicitDynamicIdentifier(node, nameNode); 1203 _checkForImplicitDynamicIdentifier(node, nameNode);
1171 // visit name 1204 // visit name
1172 nameNode.accept(this); 1205 nameNode.accept(this);
1173 // visit initializer 1206 // visit initializer
1174 String name = nameNode.name; 1207 String name = nameNode.name;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1252 Token deferredToken = directives[i].deferredKeyword; 1285 Token deferredToken = directives[i].deferredKeyword;
1253 if (deferredToken != null) { 1286 if (deferredToken != null) {
1254 _errorReporter.reportErrorForToken( 1287 _errorReporter.reportErrorForToken(
1255 CompileTimeErrorCode.SHARED_DEFERRED_PREFIX, deferredToken); 1288 CompileTimeErrorCode.SHARED_DEFERRED_PREFIX, deferredToken);
1256 } 1289 }
1257 } 1290 }
1258 } 1291 }
1259 } 1292 }
1260 1293
1261 /** 1294 /**
1295 * Check that there are no members with the same name.
1296 */
1297 void _checkDuplicateClassMembers(ClassDeclaration node) {
1298 Map<String, Element> definedNames = new HashMap<String, Element>();
1299 for (ClassMember member in node.members) {
1300 // We ignore constructors because they are checked in the method
1301 // _checkForConflictingConstructorNameAndMember.
1302 if (member is FieldDeclaration) {
1303 for (VariableDeclaration field in member.fields.variables) {
1304 _checkDuplicateIdentifier(definedNames, field.name);
1305 }
1306 } else if (member is MethodDeclaration) {
1307 _checkDuplicateIdentifier(definedNames, member.name);
1308 }
1309 }
1310 }
1311
1312 /**
1313 * Check that all of the parameters have unique names.
1314 */
1315 void _checkDuplicateDeclarationInStatements(List<Statement> statements) {
1316 Map<String, Element> definedNames = new HashMap<String, Element>();
1317 for (Statement statement in statements) {
1318 if (statement is VariableDeclarationStatement) {
1319 for (VariableDeclaration variable in statement.variables.variables) {
1320 _checkDuplicateIdentifier(definedNames, variable.name);
1321 }
1322 } else if (statement is FunctionDeclarationStatement) {
1323 _checkDuplicateIdentifier(
1324 definedNames, statement.functionDeclaration.name);
1325 }
1326 }
1327 }
1328
1329 /**
1330 * Check that the exception and stack trace parameters have different names.
1331 */
1332 void _checkDuplicateDefinitionInCatchClause(CatchClause node) {
1333 SimpleIdentifier exceptionParameter = node.exceptionParameter;
1334 SimpleIdentifier stackTraceParameter = node.stackTraceParameter;
1335 if (exceptionParameter != null && stackTraceParameter != null) {
1336 String exceptionName = exceptionParameter.name;
1337 if (exceptionName == stackTraceParameter.name) {
1338 _errorReporter.reportErrorForNode(
1339 CompileTimeErrorCode.DUPLICATE_DEFINITION,
1340 stackTraceParameter,
1341 [exceptionName]);
1342 }
1343 }
1344 }
1345
1346 /**
1347 * Check that all of the parameters have unique names.
1348 */
1349 void _checkDuplicateDefinitionInParameterList(FormalParameterList node) {
1350 Map<String, Element> definedNames = new HashMap<String, Element>();
1351 for (FormalParameter parameter in node.parameters) {
1352 _checkDuplicateIdentifier(definedNames, parameter.identifier);
1353 }
1354 }
1355
1356 /**
1357 * Check that all of the parameters have unique names.
1358 */
1359 void _checkDuplicateDefinitionInTypeParameterList(TypeParameterList node) {
1360 Map<String, Element> definedNames = new HashMap<String, Element>();
1361 for (TypeParameter parameter in node.typeParameters) {
1362 _checkDuplicateIdentifier(definedNames, parameter.name);
1363 }
1364 }
1365
1366 /**
1367 * Check whether the given [identifier] is already in the set of
1368 * [definedNames], and produce an error if it is.
1369 */
1370 void _checkDuplicateIdentifier(
1371 Map<String, Element> definedNames, SimpleIdentifier identifier) {
1372 ErrorCode getError(Element previous, Element current) {
1373 if (previous is MethodElement && current is PropertyAccessorElement) {
1374 if (current.isGetter) {
1375 return CompileTimeErrorCode.GETTER_AND_METHOD_WITH_SAME_NAME;
1376 }
1377 } else if (previous is PropertyAccessorElement &&
1378 current is MethodElement) {
1379 if (previous.isGetter) {
1380 return CompileTimeErrorCode.METHOD_AND_GETTER_WITH_SAME_NAME;
1381 }
1382 } else if (previous is PrefixElement) {
1383 return CompileTimeErrorCode.PREFIX_COLLIDES_WITH_TOP_LEVEL_MEMBER;
1384 }
1385 return CompileTimeErrorCode.DUPLICATE_DEFINITION;
1386 }
1387
1388 Element current = identifier.staticElement;
1389 String name = identifier.name;
1390 if (current is PropertyAccessorElement && current.isSetter) {
1391 name += '=';
1392 } else if (current is MethodElement && current.isOperator && name == '-') {
1393 if (current.parameters.length == 0) {
1394 name = 'unary-';
1395 }
1396 }
1397 Element previous = definedNames[name];
1398 if (previous != null) {
1399 _errorReporter
1400 .reportErrorForNode(getError(previous, current), identifier, [name]);
1401 } else {
1402 definedNames[name] = identifier.staticElement;
1403 }
1404 }
1405
1406 /**
1407 * Check that there are no members with the same name.
1408 */
1409 void _checkDuplicateUnitMembers(CompilationUnit node) {
1410 Map<String, Element> definedNames = new HashMap<String, Element>();
1411 void addWithoutChecking(CompilationUnitElement element) {
1412 for (PropertyAccessorElement accessor in element.accessors) {
1413 String name = accessor.name;
1414 if (accessor.isSetter) {
1415 name += '=';
1416 }
1417 definedNames[name] = accessor;
1418 }
1419 for (ClassElement type in element.enums) {
1420 definedNames[type.name] = type;
1421 }
1422 for (FunctionElement function in element.functions) {
1423 definedNames[function.name] = function;
1424 }
1425 for (FunctionTypeAliasElement alias in element.functionTypeAliases) {
1426 definedNames[alias.name] = alias;
1427 }
1428 for (TopLevelVariableElement variable in element.topLevelVariables) {
1429 definedNames[variable.name] = variable;
1430 }
1431 for (ClassElement type in element.types) {
1432 definedNames[type.name] = type;
1433 }
1434 }
1435
1436 for (ImportElement importElement in _currentLibrary.imports) {
1437 PrefixElement prefix = importElement.prefix;
1438 if (prefix != null) {
1439 definedNames[prefix.name] = prefix;
1440 }
1441 }
1442 CompilationUnitElement element = node.element;
1443 if (element != _currentLibrary.definingCompilationUnit) {
1444 addWithoutChecking(_currentLibrary.definingCompilationUnit);
1445 for (CompilationUnitElement part in _currentLibrary.parts) {
1446 if (element == part) {
1447 break;
1448 }
1449 addWithoutChecking(part);
1450 }
1451 }
1452 for (CompilationUnitMember member in node.declarations) {
1453 if (member is NamedCompilationUnitMember) {
1454 _checkDuplicateIdentifier(definedNames, member.name);
1455 } else if (member is TopLevelVariableDeclaration) {
1456 for (VariableDeclaration variable in member.variables.variables) {
1457 _checkDuplicateIdentifier(definedNames, variable.name);
1458 }
1459 }
1460 }
1461 }
1462
1463 /**
1262 * Verify that the given list of [typeArguments] contains exactly two 1464 * Verify that the given list of [typeArguments] contains exactly two
1263 * elements. 1465 * elements.
1264 * 1466 *
1265 * See [StaticTypeWarningCode.EXPECTED_TWO_MAP_TYPE_ARGUMENTS]. 1467 * See [StaticTypeWarningCode.EXPECTED_TWO_MAP_TYPE_ARGUMENTS].
1266 */ 1468 */
1267 void _checkExpectedTwoMapTypeArguments(TypeArgumentList typeArguments) { 1469 void _checkExpectedTwoMapTypeArguments(TypeArgumentList typeArguments) {
1268 int num = typeArguments.arguments.length; 1470 int num = typeArguments.arguments.length;
1269 if (num != 2) { 1471 if (num != 2) {
1270 _errorReporter.reportErrorForNode( 1472 _errorReporter.reportErrorForNode(
1271 StaticTypeWarningCode.EXPECTED_TWO_MAP_TYPE_ARGUMENTS, 1473 StaticTypeWarningCode.EXPECTED_TWO_MAP_TYPE_ARGUMENTS,
(...skipping 5197 matching lines...) Expand 10 before | Expand all | Expand 10 after
6469 class _InvocationCollector extends RecursiveAstVisitor { 6671 class _InvocationCollector extends RecursiveAstVisitor {
6470 final List<String> superCalls = <String>[]; 6672 final List<String> superCalls = <String>[];
6471 6673
6472 @override 6674 @override
6473 visitMethodInvocation(MethodInvocation node) { 6675 visitMethodInvocation(MethodInvocation node) {
6474 if (node.target is SuperExpression) { 6676 if (node.target is SuperExpression) {
6475 superCalls.add(node.methodName.name); 6677 superCalls.add(node.methodName.name);
6476 } 6678 }
6477 } 6679 }
6478 } 6680 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698