| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 import 'dart:collection'; | 6 import 'dart:collection'; |
| 7 | 7 |
| 8 import "../../../sdk/lib/_internal/compiler/implementation/resolution/resolution
.dart"; | 8 import "../../../sdk/lib/_internal/compiler/implementation/resolution/resolution
.dart"; |
| 9 import "compiler_helper.dart"; | 9 import "compiler_helper.dart"; |
| 10 import "parser_helper.dart"; | 10 import "parser_helper.dart"; |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 testLocals([["foo", false], ["bar", true]]); | 245 testLocals([["foo", false], ["bar", true]]); |
| 246 testLocals([["foo", true], ["bar", true]]); | 246 testLocals([["foo", true], ["bar", true]]); |
| 247 | 247 |
| 248 testLocals([["foo", false], ["bar", false], ["foobar", true]]); | 248 testLocals([["foo", false], ["bar", false], ["foobar", true]]); |
| 249 testLocals([["foo", false], ["bar", true], ["foobar", true]]); | 249 testLocals([["foo", false], ["bar", true], ["foobar", true]]); |
| 250 testLocals([["foo", true], ["bar", true], ["foobar", true]]); | 250 testLocals([["foo", true], ["bar", true], ["foobar", true]]); |
| 251 | 251 |
| 252 MockCompiler compiler = testLocals([["foo", false], ["foo", false]]); | 252 MockCompiler compiler = testLocals([["foo", false], ["foo", false]]); |
| 253 Expect.equals(1, compiler.errors.length); | 253 Expect.equals(1, compiler.errors.length); |
| 254 Expect.equals( | 254 Expect.equals( |
| 255 new Message(MessageKind.DUPLICATE_DEFINITION, {'name': 'foo'}), | 255 new Message(MessageKind.DUPLICATE_DEFINITION, {'name': 'foo'}, false), |
| 256 compiler.errors[0].message); | 256 compiler.errors[0].message); |
| 257 } | 257 } |
| 258 | 258 |
| 259 | 259 |
| 260 testLocalsTwo() { | 260 testLocalsTwo() { |
| 261 MockCompiler compiler = new MockCompiler(); | 261 MockCompiler compiler = new MockCompiler(); |
| 262 ResolverVisitor visitor = compiler.resolverVisitor(); | 262 ResolverVisitor visitor = compiler.resolverVisitor(); |
| 263 Node tree = parseStatement("if (true) { var a = 1; var b = 2; }"); | 263 Node tree = parseStatement("if (true) { var a = 1; var b = 2; }"); |
| 264 Element element = visitor.visit(tree); | 264 Element element = visitor.visit(tree); |
| 265 Expect.equals(null, element); | 265 Expect.equals(null, element); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 // Test that we get a warning when Foo is not defined. | 428 // Test that we get a warning when Foo is not defined. |
| 429 Map mapping = compiler.resolveStatement(statement).map; | 429 Map mapping = compiler.resolveStatement(statement).map; |
| 430 | 430 |
| 431 Expect.equals(2, mapping.length); // Both Foo and bar have an element. | 431 Expect.equals(2, mapping.length); // Both Foo and bar have an element. |
| 432 Expect.equals(1, compiler.warnings.length); | 432 Expect.equals(1, compiler.warnings.length); |
| 433 | 433 |
| 434 Node warningNode = compiler.warnings[0].node; | 434 Node warningNode = compiler.warnings[0].node; |
| 435 | 435 |
| 436 Expect.equals( | 436 Expect.equals( |
| 437 new Message( | 437 new Message( |
| 438 MessageKind.CANNOT_RESOLVE_TYPE.warning, {'typeName': 'Foo'}), | 438 MessageKind.CANNOT_RESOLVE_TYPE.warning, {'typeName': 'Foo'}, false), |
| 439 compiler.warnings[0].message); | 439 compiler.warnings[0].message); |
| 440 VariableDefinitions definition = compiler.parsedTree; | 440 VariableDefinitions definition = compiler.parsedTree; |
| 441 Expect.equals(warningNode, definition.type); | 441 Expect.equals(warningNode, definition.type); |
| 442 compiler.clearWarnings(); | 442 compiler.clearWarnings(); |
| 443 | 443 |
| 444 // Test that there is no warning after defining Foo. | 444 // Test that there is no warning after defining Foo. |
| 445 compiler.parseScript("class Foo {}"); | 445 compiler.parseScript("class Foo {}"); |
| 446 mapping = compiler.resolveStatement(statement).map; | 446 mapping = compiler.resolveStatement(statement).map; |
| 447 Expect.equals(2, mapping.length); | 447 Expect.equals(2, mapping.length); |
| 448 Expect.equals(0, compiler.warnings.length); | 448 Expect.equals(0, compiler.warnings.length); |
| 449 | 449 |
| 450 // Test that 'var' does not create a warning. | 450 // Test that 'var' does not create a warning. |
| 451 mapping = compiler.resolveStatement("var foo;").map; | 451 mapping = compiler.resolveStatement("var foo;").map; |
| 452 Expect.equals(1, mapping.length); | 452 Expect.equals(1, mapping.length); |
| 453 Expect.equals(0, compiler.warnings.length); | 453 Expect.equals(0, compiler.warnings.length); |
| 454 } | 454 } |
| 455 | 455 |
| 456 testSuperclass() { | 456 testSuperclass() { |
| 457 MockCompiler compiler = new MockCompiler(); | 457 MockCompiler compiler = new MockCompiler(); |
| 458 compiler.parseScript("class Foo extends Bar {}"); | 458 compiler.parseScript("class Foo extends Bar {}"); |
| 459 compiler.resolveStatement("Foo bar;"); | 459 compiler.resolveStatement("Foo bar;"); |
| 460 // TODO(ahe): We get the same error twice: once from | 460 // TODO(ahe): We get the same error twice: once from |
| 461 // ClassResolverVisitor, and once from ClassSupertypeResolver. We | 461 // ClassResolverVisitor, and once from ClassSupertypeResolver. We |
| 462 // should only the get the error once. | 462 // should only the get the error once. |
| 463 Expect.equals(2, compiler.errors.length); | 463 Expect.equals(2, compiler.errors.length); |
| 464 var cannotResolveBar = new Message(MessageKind.CANNOT_RESOLVE_TYPE.error, | 464 var cannotResolveBar = new Message(MessageKind.CANNOT_RESOLVE_TYPE.error, |
| 465 {'typeName': 'Bar'}); | 465 {'typeName': 'Bar'}, false); |
| 466 Expect.equals(cannotResolveBar, compiler.errors[0].message); | 466 Expect.equals(cannotResolveBar, compiler.errors[0].message); |
| 467 Expect.equals(cannotResolveBar, compiler.errors[1].message); | 467 Expect.equals(cannotResolveBar, compiler.errors[1].message); |
| 468 compiler.clearErrors(); | 468 compiler.clearErrors(); |
| 469 | 469 |
| 470 compiler = new MockCompiler(); | 470 compiler = new MockCompiler(); |
| 471 compiler.parseScript("class Foo extends Bar {}"); | 471 compiler.parseScript("class Foo extends Bar {}"); |
| 472 compiler.parseScript("class Bar {}"); | 472 compiler.parseScript("class Bar {}"); |
| 473 Map mapping = compiler.resolveStatement("Foo bar;").map; | 473 Map mapping = compiler.resolveStatement("Foo bar;").map; |
| 474 Expect.equals(2, mapping.length); | 474 Expect.equals(2, mapping.length); |
| 475 | 475 |
| 476 ClassElement fooElement = compiler.mainApp.find(buildSourceString('Foo')); | 476 ClassElement fooElement = compiler.mainApp.find(buildSourceString('Foo')); |
| 477 ClassElement barElement = compiler.mainApp.find(buildSourceString('Bar')); | 477 ClassElement barElement = compiler.mainApp.find(buildSourceString('Bar')); |
| 478 Expect.equals(barElement.computeType(compiler), | 478 Expect.equals(barElement.computeType(compiler), |
| 479 fooElement.supertype); | 479 fooElement.supertype); |
| 480 Expect.isTrue(fooElement.interfaces.isEmpty); | 480 Expect.isTrue(fooElement.interfaces.isEmpty); |
| 481 Expect.isTrue(barElement.interfaces.isEmpty); | 481 Expect.isTrue(barElement.interfaces.isEmpty); |
| 482 } | 482 } |
| 483 | 483 |
| 484 testVarSuperclass() { | 484 testVarSuperclass() { |
| 485 MockCompiler compiler = new MockCompiler(); | 485 MockCompiler compiler = new MockCompiler(); |
| 486 compiler.parseScript("class Foo extends var {}"); | 486 compiler.parseScript("class Foo extends var {}"); |
| 487 compiler.resolveStatement("Foo bar;"); | 487 compiler.resolveStatement("Foo bar;"); |
| 488 Expect.equals(1, compiler.errors.length); | 488 Expect.equals(1, compiler.errors.length); |
| 489 Expect.equals( | 489 Expect.equals( |
| 490 new Message(MessageKind.CANNOT_RESOLVE_TYPE.warning, {'typeName': 'var'}), | 490 new Message( |
| 491 MessageKind.CANNOT_RESOLVE_TYPE.warning, {'typeName': 'var'}, false), |
| 491 compiler.errors[0].message); | 492 compiler.errors[0].message); |
| 492 compiler.clearErrors(); | 493 compiler.clearErrors(); |
| 493 } | 494 } |
| 494 | 495 |
| 495 testOneInterface() { | 496 testOneInterface() { |
| 496 MockCompiler compiler = new MockCompiler(); | 497 MockCompiler compiler = new MockCompiler(); |
| 497 compiler.parseScript("class Foo implements Bar {}"); | 498 compiler.parseScript("class Foo implements Bar {}"); |
| 498 compiler.resolveStatement("Foo bar;"); | 499 compiler.resolveStatement("Foo bar;"); |
| 499 Expect.equals(1, compiler.errors.length); | 500 Expect.equals(1, compiler.errors.length); |
| 500 Expect.equals( | 501 Expect.equals( |
| 501 new Message(MessageKind.CANNOT_RESOLVE_TYPE.warning, {'typeName': 'bar'}), | 502 new Message( |
| 503 MessageKind.CANNOT_RESOLVE_TYPE.warning, {'typeName': 'bar'}, false), |
| 502 compiler.errors[0].message); | 504 compiler.errors[0].message); |
| 503 compiler.clearErrors(); | 505 compiler.clearErrors(); |
| 504 | 506 |
| 505 // Add the abstract class to the world and make sure everything is setup | 507 // Add the abstract class to the world and make sure everything is setup |
| 506 // correctly. | 508 // correctly. |
| 507 compiler.parseScript("abstract class Bar {}"); | 509 compiler.parseScript("abstract class Bar {}"); |
| 508 | 510 |
| 509 ResolverVisitor visitor = | 511 ResolverVisitor visitor = |
| 510 new ResolverVisitor(compiler, null, new CollectingTreeElements(null)); | 512 new ResolverVisitor(compiler, null, new CollectingTreeElements(null)); |
| 511 compiler.resolveStatement("Foo bar;"); | 513 compiler.resolveStatement("Foo bar;"); |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 804 class Dynamic_ {} | 806 class Dynamic_ {} |
| 805 class Type {} | 807 class Type {} |
| 806 class Object { Object() : super(); }'''; | 808 class Object { Object() : super(); }'''; |
| 807 resolveConstructor(script, "Object o = new Object();", "Object", "", 1, | 809 resolveConstructor(script, "Object o = new Object();", "Object", "", 1, |
| 808 expectedWarnings: [], | 810 expectedWarnings: [], |
| 809 expectedErrors: [MessageKind.SUPER_INITIALIZER_IN_OBJECT], | 811 expectedErrors: [MessageKind.SUPER_INITIALIZER_IN_OBJECT], |
| 810 corelib: CORELIB_WITH_INVALID_OBJECT); | 812 corelib: CORELIB_WITH_INVALID_OBJECT); |
| 811 } | 813 } |
| 812 | 814 |
| 813 map(ResolverVisitor visitor) { | 815 map(ResolverVisitor visitor) { |
| 814 TreeElementMapping elements = visitor.mapping; | 816 CollectingTreeElements elements = visitor.mapping; |
| 815 return elements.map; | 817 return elements.map; |
| 816 } | 818 } |
| 817 | 819 |
| 818 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1); | 820 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1); |
| 819 | 821 |
| 820 List<String> asSortedStrings(Link link) { | 822 List<String> asSortedStrings(Link link) { |
| 821 List<String> result = <String>[]; | 823 List<String> result = <String>[]; |
| 822 for (; !link.isEmpty; link = link.tail) result.add(link.head.toString()); | 824 for (; !link.isEmpty; link = link.tail) result.add(link.head.toString()); |
| 823 result.sort((s1, s2) => s1.compareTo(s2)); | 825 result.sort((s1, s2) => s1.compareTo(s2)); |
| 824 return result; | 826 return result; |
| 825 } | 827 } |
| 826 | 828 |
| 827 compileScript(String source) { | 829 compileScript(String source) { |
| 828 Uri uri = new Uri(scheme: 'source'); | 830 Uri uri = new Uri(scheme: 'source'); |
| 829 MockCompiler compiler = compilerFor(source, uri); | 831 MockCompiler compiler = compilerFor(source, uri); |
| 830 compiler.runCompiler(uri); | 832 compiler.runCompiler(uri); |
| 831 return compiler; | 833 return compiler; |
| 832 } | 834 } |
| 833 | 835 |
| 834 checkMemberResolved(compiler, className, memberName) { | 836 checkMemberResolved(compiler, className, memberName) { |
| 835 Element memberElement = findElement(compiler, className) | 837 ClassElement cls = findElement(compiler, className); |
| 836 .lookupLocalMember(memberName); | 838 Element memberElement = cls.lookupLocalMember(memberName); |
| 837 Expect.isNotNull(memberElement); | 839 Expect.isNotNull(memberElement); |
| 838 Expect.isNotNull( | 840 Expect.isNotNull( |
| 839 compiler.enqueuer.resolution.getCachedElements(memberElement)); | 841 compiler.enqueuer.resolution.getCachedElements(memberElement)); |
| 840 } | 842 } |
| 841 | 843 |
| 842 testToString() { | 844 testToString() { |
| 843 final script = r"class C { toString() => 'C'; } main() { '${new C()}'; }"; | 845 final script = r"class C { toString() => 'C'; } main() { '${new C()}'; }"; |
| 844 final compiler = compileScript(script); | 846 final compiler = compileScript(script); |
| 845 | 847 |
| 846 checkMemberResolved(compiler, 'C', buildSourceString('toString')); | 848 checkMemberResolved(compiler, 'C', buildSourceString('toString')); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 898 } | 900 } |
| 899 main() { | 901 main() { |
| 900 new A() == new B(); | 902 new A() == new B(); |
| 901 }"""; | 903 }"""; |
| 902 final compiler = compileScript(script); | 904 final compiler = compileScript(script); |
| 903 Expect.equals(1, compiler.warnings.length); | 905 Expect.equals(1, compiler.warnings.length); |
| 904 Expect.equals(MessageKind.OVERRIDE_EQUALS_NOT_HASH_CODE, | 906 Expect.equals(MessageKind.OVERRIDE_EQUALS_NOT_HASH_CODE, |
| 905 compiler.warnings[0].message.kind); | 907 compiler.warnings[0].message.kind); |
| 906 Expect.equals(0, compiler.errors.length); | 908 Expect.equals(0, compiler.errors.length); |
| 907 } | 909 } |
| OLD | NEW |