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