| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 import 'package:expect/expect.dart'; |
| 6 import "package:async_helper/async_helper.dart"; |
| 7 |
| 8 import 'compiler_helper.dart'; |
| 9 import 'type_mask_test_helper.dart'; |
| 10 import 'package:compiler/src/types/types.dart'; |
| 11 |
| 12 bool isContainer(TypeMask mask) { |
| 13 return mask is ContainerTypeMask; |
| 14 } |
| 15 |
| 16 const String TEST = ''' |
| 17 |
| 18 foo1() { |
| 19 final methods = []; |
| 20 var res, sum; |
| 21 for (int i = 0; i != 3; i++) { |
| 22 methods.add((int x) { res = x; sum = x + i; }); |
| 23 } |
| 24 methods[0](499); |
| 25 probe1res(res); |
| 26 probe1sum(sum); |
| 27 probe1methods(methods); |
| 28 } |
| 29 probe1res(x) => x; |
| 30 probe1sum(x) => x; |
| 31 probe1methods(x) => x; |
| 32 |
| 33 nonContainer(choice) { |
| 34 var m = choice == 0 ? [] : "<String>"; |
| 35 if (m is !List) throw 123; |
| 36 // The union then filter leaves us with a non-container type. |
| 37 return m; |
| 38 } |
| 39 |
| 40 foo2(int choice) { |
| 41 final methods = nonContainer(choice); |
| 42 var res, sum; |
| 43 for (int i = 0; i != 3; i++) { |
| 44 methods.add((int x) { res = x; sum = x + i; }); |
| 45 } |
| 46 methods[0](499); |
| 47 probe2res(res); |
| 48 probe2methods(methods); |
| 49 } |
| 50 probe2res(x) => x; |
| 51 probe2methods(x) => x; |
| 52 |
| 53 main() { |
| 54 foo1(); |
| 55 foo2(0); |
| 56 foo2(1); |
| 57 } |
| 58 '''; |
| 59 |
| 60 void main() { |
| 61 Uri uri = new Uri(scheme: 'source'); |
| 62 var compiler = compilerFor(TEST, uri); |
| 63 asyncTest(() => compiler.run(uri).then((_) { |
| 64 var typesInferrer = compiler.globalInference.typesInferrerInternal; |
| 65 var closedWorld = typesInferrer.closedWorld; |
| 66 var commonMasks = closedWorld.commonMasks; |
| 67 |
| 68 typeOf(String name) { |
| 69 return typesInferrer |
| 70 .getReturnTypeOfElement(findElement(compiler, name)); |
| 71 } |
| 72 |
| 73 checkType(String name, type) { |
| 74 var mask = typeOf(name); |
| 75 Expect.equals(type.nullable(), simplify(mask, closedWorld), name); |
| 76 } |
| 77 |
| 78 checkContainer(String name, bool value) { |
| 79 var mask = typeOf(name); |
| 80 Expect.equals( |
| 81 value, isContainer(mask), '$name is container (mask: $mask)'); |
| 82 } |
| 83 |
| 84 checkContainer('probe1methods', true); |
| 85 checkType('probe1res', commonMasks.uint31Type); |
| 86 checkType('probe1sum', commonMasks.positiveIntType); |
| 87 |
| 88 checkContainer('probe2methods', false); |
| 89 checkType('probe2res', commonMasks.dynamicType); |
| 90 })); |
| 91 } |
| OLD | NEW |