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

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

Issue 2296773002: Revert "Move error checking from scope creation to error reporter" (TBD) (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
396 Object visitBlockFunctionBody(BlockFunctionBody node) { 390 Object visitBlockFunctionBody(BlockFunctionBody node) {
397 bool wasInAsync = _inAsync; 391 bool wasInAsync = _inAsync;
398 bool wasInGenerator = _inGenerator; 392 bool wasInGenerator = _inGenerator;
399 bool previousHasReturnWithoutValue = _hasReturnWithoutValue; 393 bool previousHasReturnWithoutValue = _hasReturnWithoutValue;
400 _hasReturnWithoutValue = false; 394 _hasReturnWithoutValue = false;
401 List<ReturnStatement> previousReturnsWith = _returnsWith; 395 List<ReturnStatement> previousReturnsWith = _returnsWith;
402 List<ReturnStatement> previousReturnsWithout = _returnsWithout; 396 List<ReturnStatement> previousReturnsWithout = _returnsWithout;
403 try { 397 try {
404 _inAsync = node.isAsynchronous; 398 _inAsync = node.isAsynchronous;
405 _inGenerator = node.isGenerator; 399 _inGenerator = node.isGenerator;
(...skipping 19 matching lines...) Expand all
425 if (labelElement is LabelElementImpl && labelElement.isOnSwitchMember) { 419 if (labelElement is LabelElementImpl && labelElement.isOnSwitchMember) {
426 _errorReporter.reportErrorForNode( 420 _errorReporter.reportErrorForNode(
427 ResolverErrorCode.BREAK_LABEL_ON_SWITCH_MEMBER, labelNode); 421 ResolverErrorCode.BREAK_LABEL_ON_SWITCH_MEMBER, labelNode);
428 } 422 }
429 } 423 }
430 return null; 424 return null;
431 } 425 }
432 426
433 @override 427 @override
434 Object visitCatchClause(CatchClause node) { 428 Object visitCatchClause(CatchClause node) {
435 _checkDuplicateDefinitionInCatchClause(node);
436 bool previousIsInCatchClause = _isInCatchClause; 429 bool previousIsInCatchClause = _isInCatchClause;
437 try { 430 try {
438 _isInCatchClause = true; 431 _isInCatchClause = true;
439 _checkForTypeAnnotationDeferredClass(node.exceptionType); 432 _checkForTypeAnnotationDeferredClass(node.exceptionType);
440 return super.visitCatchClause(node); 433 return super.visitCatchClause(node);
441 } finally { 434 } finally {
442 _isInCatchClause = previousIsInCatchClause; 435 _isInCatchClause = previousIsInCatchClause;
443 } 436 }
444 } 437 }
445 438
446 @override 439 @override
447 Object visitClassDeclaration(ClassDeclaration node) { 440 Object visitClassDeclaration(ClassDeclaration node) {
448 ClassElementImpl outerClass = _enclosingClass; 441 ClassElementImpl outerClass = _enclosingClass;
449 try { 442 try {
450 _isInNativeClass = node.nativeClause != null; 443 _isInNativeClass = node.nativeClause != null;
451 _enclosingClass = AbstractClassElementImpl.getImpl(node.element); 444 _enclosingClass = AbstractClassElementImpl.getImpl(node.element);
452 _checkDuplicateClassMembers(node); 445 ExtendsClause extendsClause = node.extendsClause;
446 ImplementsClause implementsClause = node.implementsClause;
447 WithClause withClause = node.withClause;
453 _checkForBuiltInIdentifierAsName( 448 _checkForBuiltInIdentifierAsName(
454 node.name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME); 449 node.name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME);
455 _checkForMemberWithClassName(); 450 _checkForMemberWithClassName();
456 _checkForNoDefaultSuperConstructorImplicit(node); 451 _checkForNoDefaultSuperConstructorImplicit(node);
457 _checkForConflictingTypeVariableErrorCodes(node); 452 _checkForConflictingTypeVariableErrorCodes(node);
458 ExtendsClause extendsClause = node.extendsClause;
459 ImplementsClause implementsClause = node.implementsClause;
460 WithClause withClause = node.withClause;
461 // Only do error checks on the clause nodes if there is a non-null clause 453 // Only do error checks on the clause nodes if there is a non-null clause
462 if (implementsClause != null || 454 if (implementsClause != null ||
463 extendsClause != null || 455 extendsClause != null ||
464 withClause != null) { 456 withClause != null) {
465 // Only check for all of the inheritance logic around clauses if there 457 // Only check for all of the inheritance logic around clauses if there
466 // isn't an error code such as "Cannot extend double" already on the 458 // isn't an error code such as "Cannot extend double" already on the
467 // class. 459 // class.
468 if (!_checkForImplementsDisallowedClass(implementsClause) && 460 if (!_checkForImplementsDisallowedClass(implementsClause) &&
469 !_checkForExtendsDisallowedClass(extendsClause) && 461 !_checkForExtendsDisallowedClass(extendsClause) &&
470 !_checkForAllMixinErrorCodes(withClause)) { 462 !_checkForAllMixinErrorCodes(withClause)) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 _isInComment = true; 537 _isInComment = true;
546 try { 538 try {
547 return super.visitComment(node); 539 return super.visitComment(node);
548 } finally { 540 } finally {
549 _isInComment = false; 541 _isInComment = false;
550 } 542 }
551 } 543 }
552 544
553 @override 545 @override
554 Object visitCompilationUnit(CompilationUnit node) { 546 Object visitCompilationUnit(CompilationUnit node) {
555 _checkDuplicateUnitMembers(node);
556 _checkForDeferredPrefixCollisions(node); 547 _checkForDeferredPrefixCollisions(node);
557 return super.visitCompilationUnit(node); 548 return super.visitCompilationUnit(node);
558 } 549 }
559 550
560 @override 551 @override
561 Object visitConditionalExpression(ConditionalExpression node) { 552 Object visitConditionalExpression(ConditionalExpression node) {
562 _checkForNonBoolCondition(node.condition); 553 _checkForNonBoolCondition(node.condition);
563 return super.visitConditionalExpression(node); 554 return super.visitConditionalExpression(node);
564 } 555 }
565 556
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 return super.visitFieldFormalParameter(node); 712 return super.visitFieldFormalParameter(node);
722 } 713 }
723 714
724 @override 715 @override
725 Object visitForEachStatement(ForEachStatement node) { 716 Object visitForEachStatement(ForEachStatement node) {
726 _checkForInIterable(node); 717 _checkForInIterable(node);
727 return super.visitForEachStatement(node); 718 return super.visitForEachStatement(node);
728 } 719 }
729 720
730 @override 721 @override
731 Object visitFormalParameterList(FormalParameterList node) {
732 _checkDuplicateDefinitionInParameterList(node);
733 return super.visitFormalParameterList(node);
734 }
735
736 @override
737 Object visitForStatement(ForStatement node) { 722 Object visitForStatement(ForStatement node) {
738 if (node.condition != null) { 723 if (node.condition != null) {
739 _checkForNonBoolCondition(node.condition); 724 _checkForNonBoolCondition(node.condition);
740 } 725 }
741 return super.visitForStatement(node); 726 return super.visitForStatement(node);
742 } 727 }
743 728
744 @override 729 @override
745 Object visitFunctionDeclaration(FunctionDeclaration node) { 730 Object visitFunctionDeclaration(FunctionDeclaration node) {
746 ExecutableElement outerFunction = _enclosingFunction; 731 ExecutableElement outerFunction = _enclosingFunction;
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
1118 Object visitSuperConstructorInvocation(SuperConstructorInvocation node) { 1103 Object visitSuperConstructorInvocation(SuperConstructorInvocation node) {
1119 _isInConstructorInitializer = true; 1104 _isInConstructorInitializer = true;
1120 try { 1105 try {
1121 return super.visitSuperConstructorInvocation(node); 1106 return super.visitSuperConstructorInvocation(node);
1122 } finally { 1107 } finally {
1123 _isInConstructorInitializer = false; 1108 _isInConstructorInitializer = false;
1124 } 1109 }
1125 } 1110 }
1126 1111
1127 @override 1112 @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
1140 Object visitSwitchStatement(SwitchStatement node) { 1113 Object visitSwitchStatement(SwitchStatement node) {
1141 _checkForSwitchExpressionNotAssignable(node); 1114 _checkForSwitchExpressionNotAssignable(node);
1142 _checkForCaseBlocksNotTerminated(node); 1115 _checkForCaseBlocksNotTerminated(node);
1143 _checkForMissingEnumConstantInSwitch(node); 1116 _checkForMissingEnumConstantInSwitch(node);
1144 return super.visitSwitchStatement(node); 1117 return super.visitSwitchStatement(node);
1145 } 1118 }
1146 1119
1147 @override 1120 @override
1148 Object visitThisExpression(ThisExpression node) { 1121 Object visitThisExpression(ThisExpression node) {
1149 _checkForInvalidReferenceToThis(node); 1122 _checkForInvalidReferenceToThis(node);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1182 Object visitTypeParameter(TypeParameter node) { 1155 Object visitTypeParameter(TypeParameter node) {
1183 _checkForBuiltInIdentifierAsName(node.name, 1156 _checkForBuiltInIdentifierAsName(node.name,
1184 CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_PARAMETER_NAME); 1157 CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_PARAMETER_NAME);
1185 _checkForTypeParameterSupertypeOfItsBound(node); 1158 _checkForTypeParameterSupertypeOfItsBound(node);
1186 _checkForTypeAnnotationDeferredClass(node.bound); 1159 _checkForTypeAnnotationDeferredClass(node.bound);
1187 _checkForImplicitDynamicType(node.bound); 1160 _checkForImplicitDynamicType(node.bound);
1188 return super.visitTypeParameter(node); 1161 return super.visitTypeParameter(node);
1189 } 1162 }
1190 1163
1191 @override 1164 @override
1192 Object visitTypeParameterList(TypeParameterList node) {
1193 _checkDuplicateDefinitionInTypeParameterList(node);
1194 return super.visitTypeParameterList(node);
1195 }
1196
1197 @override
1198 Object visitVariableDeclaration(VariableDeclaration node) { 1165 Object visitVariableDeclaration(VariableDeclaration node) {
1199 SimpleIdentifier nameNode = node.name; 1166 SimpleIdentifier nameNode = node.name;
1200 Expression initializerNode = node.initializer; 1167 Expression initializerNode = node.initializer;
1201 // do checks 1168 // do checks
1202 _checkForInvalidAssignment(nameNode, initializerNode); 1169 _checkForInvalidAssignment(nameNode, initializerNode);
1203 _checkForImplicitDynamicIdentifier(node, nameNode); 1170 _checkForImplicitDynamicIdentifier(node, nameNode);
1204 // visit name 1171 // visit name
1205 nameNode.accept(this); 1172 nameNode.accept(this);
1206 // visit initializer 1173 // visit initializer
1207 String name = nameNode.name; 1174 String name = nameNode.name;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1285 Token deferredToken = directives[i].deferredKeyword; 1252 Token deferredToken = directives[i].deferredKeyword;
1286 if (deferredToken != null) { 1253 if (deferredToken != null) {
1287 _errorReporter.reportErrorForToken( 1254 _errorReporter.reportErrorForToken(
1288 CompileTimeErrorCode.SHARED_DEFERRED_PREFIX, deferredToken); 1255 CompileTimeErrorCode.SHARED_DEFERRED_PREFIX, deferredToken);
1289 } 1256 }
1290 } 1257 }
1291 } 1258 }
1292 } 1259 }
1293 1260
1294 /** 1261 /**
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 /**
1464 * Verify that the given list of [typeArguments] contains exactly two 1262 * Verify that the given list of [typeArguments] contains exactly two
1465 * elements. 1263 * elements.
1466 * 1264 *
1467 * See [StaticTypeWarningCode.EXPECTED_TWO_MAP_TYPE_ARGUMENTS]. 1265 * See [StaticTypeWarningCode.EXPECTED_TWO_MAP_TYPE_ARGUMENTS].
1468 */ 1266 */
1469 void _checkExpectedTwoMapTypeArguments(TypeArgumentList typeArguments) { 1267 void _checkExpectedTwoMapTypeArguments(TypeArgumentList typeArguments) {
1470 int num = typeArguments.arguments.length; 1268 int num = typeArguments.arguments.length;
1471 if (num != 2) { 1269 if (num != 2) {
1472 _errorReporter.reportErrorForNode( 1270 _errorReporter.reportErrorForNode(
1473 StaticTypeWarningCode.EXPECTED_TWO_MAP_TYPE_ARGUMENTS, 1271 StaticTypeWarningCode.EXPECTED_TWO_MAP_TYPE_ARGUMENTS,
(...skipping 5197 matching lines...) Expand 10 before | Expand all | Expand 10 after
6671 class _InvocationCollector extends RecursiveAstVisitor { 6469 class _InvocationCollector extends RecursiveAstVisitor {
6672 final List<String> superCalls = <String>[]; 6470 final List<String> superCalls = <String>[];
6673 6471
6674 @override 6472 @override
6675 visitMethodInvocation(MethodInvocation node) { 6473 visitMethodInvocation(MethodInvocation node) {
6676 if (node.target is SuperExpression) { 6474 if (node.target is SuperExpression) {
6677 superCalls.add(node.methodName.name); 6475 superCalls.add(node.methodName.name);
6678 } 6476 }
6679 } 6477 }
6680 } 6478 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/resolver/scope.dart ('k') | pkg/analyzer/test/generated/compile_time_error_code_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698