| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 'compiler_helper.dart'; | 6 import 'compiler_helper.dart'; |
| 7 | 7 |
| 8 const String TEST = """ | 8 const String TEST = """ |
| 9 returnInt1() { | 9 returnInt1() { |
| 10 var a = 42; | 10 var a = 42; |
| 11 var f = () { | 11 var f = () { |
| 12 return a; | 12 return a; |
| 13 }; | 13 }; |
| 14 return a; | 14 return a; |
| 15 } | 15 } |
| 16 | 16 |
| 17 returnDyn1() { | 17 returnDyn1() { |
| 18 var a = 42; | 18 var a = 42; |
| 19 var f = () { | 19 var f = () { |
| 20 a = 'foo'; | 20 a = {}; |
| 21 }; | 21 }; |
| 22 return a; | 22 return a; |
| 23 } | 23 } |
| 24 | 24 |
| 25 returnInt2() { | 25 returnInt2() { |
| 26 var a = 42; | 26 var a = 42; |
| 27 var f = () { | 27 var f = () { |
| 28 a = 54; | 28 a = 54; |
| 29 }; | 29 }; |
| 30 return a; | 30 return a; |
| 31 } | 31 } |
| 32 | 32 |
| 33 returnDyn2() { | 33 returnDyn2() { |
| 34 var a = 42; | 34 var a = 42; |
| 35 var f = () { | 35 var f = () { |
| 36 a = 54; | 36 a = 54; |
| 37 }; | 37 }; |
| 38 var g = () { | 38 var g = () { |
| 39 a = 'foo'; | 39 a = {}; |
| 40 }; | 40 }; |
| 41 return a; | 41 return a; |
| 42 } | 42 } |
| 43 | 43 |
| 44 returnInt3() { | 44 returnInt3() { |
| 45 var a = 42; | 45 var a = 42; |
| 46 if (a == 53) { | 46 if (a == 53) { |
| 47 var f = () { | 47 var f = () { |
| 48 return a; | 48 return a; |
| 49 }; | 49 }; |
| 50 } | 50 } |
| 51 return a; | 51 return a; |
| 52 } | 52 } |
| 53 | 53 |
| 54 returnDyn3() { | 54 returnDyn3() { |
| 55 var a = 42; | 55 var a = 42; |
| 56 if (a == 53) { | 56 if (a == 53) { |
| 57 var f = () { | 57 var f = () { |
| 58 a = 'foo'; | 58 a = {}; |
| 59 }; | 59 }; |
| 60 } | 60 } |
| 61 return a; | 61 return a; |
| 62 } | 62 } |
| 63 | 63 |
| 64 returnInt4() { | 64 returnInt4() { |
| 65 var a = 42; | 65 var a = 42; |
| 66 g() { return a; } | 66 g() { return a; } |
| 67 return g(); | 67 return g(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 returnNum1() { |
| 71 var a = 42.5; |
| 72 try { |
| 73 g() { |
| 74 var b = {}; |
| 75 b = 42; |
| 76 return b; |
| 77 } |
| 78 a = g(); |
| 79 } finally { |
| 80 } |
| 81 return a; |
| 82 } |
| 83 |
| 70 main() { | 84 main() { |
| 71 returnInt1(); | 85 returnInt1(); |
| 72 returnDyn1(); | 86 returnDyn1(); |
| 73 returnInt2(); | 87 returnInt2(); |
| 74 returnDyn2(); | 88 returnDyn2(); |
| 75 returnInt3(); | 89 returnInt3(); |
| 76 returnDyn3(); | 90 returnDyn3(); |
| 77 returnInt4(); | 91 returnInt4(); |
| 92 returnNum1(); |
| 78 } | 93 } |
| 79 """; | 94 """; |
| 80 | 95 |
| 81 | 96 |
| 82 void main() { | 97 void main() { |
| 83 Uri uri = new Uri(scheme: 'source'); | 98 Uri uri = new Uri(scheme: 'source'); |
| 84 var compiler = compilerFor(TEST, uri); | 99 var compiler = compilerFor(TEST, uri); |
| 85 compiler.runCompiler(uri); | 100 compiler.runCompiler(uri); |
| 86 var typesInferrer = compiler.typesTask.typesInferrer; | 101 var typesInferrer = compiler.typesTask.typesInferrer; |
| 87 | 102 |
| 88 checkReturn(String name, type) { | 103 checkReturn(String name, type) { |
| 89 var element = findElement(compiler, name); | 104 var element = findElement(compiler, name); |
| 90 Expect.equals(type, typesInferrer.getReturnTypeOfElement(element)); | 105 Expect.equals(type, |
| 106 typesInferrer.getReturnTypeOfElement(element).simplify(compiler)); |
| 91 } | 107 } |
| 92 | 108 |
| 93 checkReturn('returnInt1', compiler.typesTask.intType); | 109 checkReturn('returnInt1', compiler.typesTask.intType); |
| 94 // TODO(ngeoffray): We don't use types of mutated captured | 110 checkReturn('returnInt2', compiler.typesTask.intType.nullable()); |
| 95 // variables anymore, because they could lead to optimistic results | |
| 96 // needing to be re-analyzed. | |
| 97 checkReturn('returnInt2', compiler.typesTask.dynamicType); | |
| 98 checkReturn('returnInt3', compiler.typesTask.intType); | 111 checkReturn('returnInt3', compiler.typesTask.intType); |
| 99 checkReturn('returnInt4', compiler.typesTask.intType); | 112 checkReturn('returnInt4', compiler.typesTask.intType); |
| 100 | 113 |
| 101 checkReturn('returnDyn1', compiler.typesTask.dynamicType); | 114 checkReturn('returnDyn1', compiler.typesTask.dynamicType); |
| 102 checkReturn('returnDyn2', compiler.typesTask.dynamicType); | 115 checkReturn('returnDyn2', compiler.typesTask.dynamicType); |
| 103 checkReturn('returnDyn3', compiler.typesTask.dynamicType); | 116 checkReturn('returnDyn3', compiler.typesTask.dynamicType); |
| 117 checkReturn('returnNum1', compiler.typesTask.numType); |
| 104 } | 118 } |
| OLD | NEW |