| 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 "dart:uri"; | 5 import "dart:uri"; |
| 6 import "../../../sdk/lib/_internal/compiler/implementation/elements/elements.dar
t"; | 6 import "../../../sdk/lib/_internal/compiler/implementation/elements/elements.dar
t"; |
| 7 import '../../../sdk/lib/_internal/compiler/implementation/scanner/scannerlib.da
rt'; | 7 import '../../../sdk/lib/_internal/compiler/implementation/scanner/scannerlib.da
rt'; |
| 8 import '../../../sdk/lib/_internal/compiler/implementation/source_file.dart'; | 8 import '../../../sdk/lib/_internal/compiler/implementation/source_file.dart'; |
| 9 import '../../../sdk/lib/_internal/compiler/implementation/types/types.dart'; | 9 import '../../../sdk/lib/_internal/compiler/implementation/types/types.dart'; |
| 10 import '../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart'; | 10 import '../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart'; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 } | 84 } |
| 85 | 85 |
| 86 /** | 86 /** |
| 87 * Finds the [Element] corresponding to [: className#fieldName :]. | 87 * Finds the [Element] corresponding to [: className#fieldName :]. |
| 88 */ | 88 */ |
| 89 Element findField(String className, String fieldName) { | 89 Element findField(String className, String fieldName) { |
| 90 ClassElement element = compiler.mainApp.find(buildSourceString(className)); | 90 ClassElement element = compiler.mainApp.find(buildSourceString(className)); |
| 91 return element.lookupLocalMember(buildSourceString(fieldName)); | 91 return element.lookupLocalMember(buildSourceString(fieldName)); |
| 92 } | 92 } |
| 93 | 93 |
| 94 static ConcreteType concreteFrom(List<BaseType> baseTypes) { | 94 ConcreteType concreteFrom(List<BaseType> baseTypes) { |
| 95 ConcreteType result = new ConcreteType.empty(); | 95 ConcreteType result = inferrer.emptyConcreteType; |
| 96 for (final baseType in baseTypes) { | 96 for (final baseType in baseTypes) { |
| 97 result = result.union(new ConcreteType.singleton(baseType)); | 97 result = result.union(compiler.maxConcreteTypeSize, |
| 98 inferrer.singletonConcreteType(baseType)); |
| 98 } | 99 } |
| 100 // We make sure the concrete types expected by the tests don't default to |
| 101 // dynamic because of widening. |
| 102 assert(!result.isUnkown()); |
| 99 return result; | 103 return result; |
| 100 } | 104 } |
| 101 | 105 |
| 102 /** | 106 /** |
| 103 * Checks that the inferred type of the node corresponding to the last | 107 * Checks that the inferred type of the node corresponding to the last |
| 104 * occurence of [: variable; :] in the program is the concrete type | 108 * occurence of [: variable; :] in the program is the concrete type |
| 105 * made of [baseTypes]. | 109 * made of [baseTypes]. |
| 106 */ | 110 */ |
| 107 void checkNodeHasType(String variable, List<BaseType> baseTypes) { | 111 void checkNodeHasType(String variable, List<BaseType> baseTypes) { |
| 108 return Expect.equals( | 112 return Expect.equals( |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 class Object {} | 159 class Object {} |
| 156 class Function {} | 160 class Function {} |
| 157 abstract class List {} | 161 abstract class List {} |
| 158 abstract class Map {} | 162 abstract class Map {} |
| 159 class Closure {} | 163 class Closure {} |
| 160 class Null {} | 164 class Null {} |
| 161 class Type {} | 165 class Type {} |
| 162 class Dynamic_ {} | 166 class Dynamic_ {} |
| 163 bool identical(Object a, Object b) {}'''; | 167 bool identical(Object a, Object b) {}'''; |
| 164 | 168 |
| 165 AnalysisResult analyze(String code) { | 169 AnalysisResult analyze(String code, {int maxConcreteTypeSize: 1000}) { |
| 166 Uri uri = new Uri.fromComponents(scheme: 'source'); | 170 Uri uri = new Uri.fromComponents(scheme: 'source'); |
| 167 MockCompiler compiler = new MockCompiler(coreSource: CORELIB, | 171 MockCompiler compiler = new MockCompiler( |
| 168 enableConcreteTypeInference: true); | 172 coreSource: CORELIB, |
| 173 enableConcreteTypeInference: true, |
| 174 maxConcreteTypeSize: maxConcreteTypeSize); |
| 169 compiler.sourceFiles[uri.toString()] = new SourceFile(uri.toString(), code); | 175 compiler.sourceFiles[uri.toString()] = new SourceFile(uri.toString(), code); |
| 170 compiler.typesTask.concreteTypesInferrer.testMode = true; | 176 compiler.typesTask.concreteTypesInferrer.testMode = true; |
| 171 compiler.runCompiler(uri); | 177 compiler.runCompiler(uri); |
| 172 return new AnalysisResult(compiler); | 178 return new AnalysisResult(compiler); |
| 173 } | 179 } |
| 174 | 180 |
| 175 testDynamicBackDoor() { | 181 testDynamicBackDoor() { |
| 176 final String source = r""" | 182 final String source = r""" |
| 177 main () { | 183 main () { |
| 178 var x = "__dynamic_for_test"; | 184 var x = "__dynamic_for_test"; |
| (...skipping 600 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 779 x; y; z; w; | 785 x; y; z; w; |
| 780 } | 786 } |
| 781 """; | 787 """; |
| 782 AnalysisResult result = analyze(source); | 788 AnalysisResult result = analyze(source); |
| 783 result.checkNodeHasType('x', []); | 789 result.checkNodeHasType('x', []); |
| 784 result.checkNodeHasType('y', []); | 790 result.checkNodeHasType('y', []); |
| 785 result.checkNodeHasType('z', []); | 791 result.checkNodeHasType('z', []); |
| 786 result.checkNodeHasType('w', []); | 792 result.checkNodeHasType('w', []); |
| 787 } | 793 } |
| 788 | 794 |
| 795 testBigTypesWidening1() { |
| 796 final String source = r""" |
| 797 small() => true ? 1 : 'abc'; |
| 798 big() => true ? 1 : (true ? 'abc' : false); |
| 799 main () { |
| 800 var x = small(); |
| 801 var y = big(); |
| 802 x; y; |
| 803 } |
| 804 """; |
| 805 AnalysisResult result = analyze(source, maxConcreteTypeSize: 2); |
| 806 result.checkNodeHasType('x', [result.int, result.string]); |
| 807 result.checkNodeHasUnknownType('y'); |
| 808 } |
| 809 |
| 810 testBigTypesWidening2() { |
| 811 final String source = r""" |
| 812 class A { |
| 813 var x, y; |
| 814 A(this.x, this.y); |
| 815 } |
| 816 main () { |
| 817 var a = new A(1, 1); |
| 818 a.x = 'abc'; |
| 819 a.y = 'abc'; |
| 820 a.y = true; |
| 821 } |
| 822 """; |
| 823 AnalysisResult result = analyze(source, maxConcreteTypeSize: 2); |
| 824 result.checkFieldHasType('A', 'x', [result.int, result.string]); |
| 825 result.checkFieldHasUknownType('A', 'y'); |
| 826 } |
| 827 |
| 789 testDynamicIsAbsorbing() { | 828 testDynamicIsAbsorbing() { |
| 790 final String source = r""" | 829 final String source = r""" |
| 791 main () { | 830 main () { |
| 792 var x = 1; | 831 var x = 1; |
| 793 if (true) { | 832 if (true) { |
| 794 x = "__dynamic_for_test"; | 833 x = "__dynamic_for_test"; |
| 795 } else { | 834 } else { |
| 796 x = 42; | 835 x = 42; |
| 797 } | 836 } |
| 798 x; | 837 x; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 828 testReturn(); | 867 testReturn(); |
| 829 // testNoReturn(); // right now we infer the empty type instead of null | 868 // testNoReturn(); // right now we infer the empty type instead of null |
| 830 testArithmeticOperators(); | 869 testArithmeticOperators(); |
| 831 testOperators(); | 870 testOperators(); |
| 832 testCompoundOperators1(); | 871 testCompoundOperators1(); |
| 833 testCompoundOperators2(); | 872 testCompoundOperators2(); |
| 834 testSetIndexOperator(); | 873 testSetIndexOperator(); |
| 835 testInequality(); | 874 testInequality(); |
| 836 // testFieldInitialization(); // TODO(polux) | 875 // testFieldInitialization(); // TODO(polux) |
| 837 testSendWithWrongArity(); | 876 testSendWithWrongArity(); |
| 877 testBigTypesWidening1(); |
| 878 testBigTypesWidening2(); |
| 838 testDynamicIsAbsorbing(); | 879 testDynamicIsAbsorbing(); |
| 839 } | 880 } |
| OLD | NEW |