| 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 // Dart test for a closure result type test that cannot be eliminated at compile | 4 // Dart test for a closure result type test that cannot be eliminated at compile |
| 5 // time. | 5 // time. |
| 6 | 6 |
| 7 #library('closure_type_test'); | 7 #library('closure_type_test'); |
| 8 #import('dart:math', prefix: 'math'); | 8 #import('dart:math', prefix: 'math'); |
| 9 | 9 |
| 10 class Math { | 10 class Math { |
| 11 static int sqrt(x) => math.sqrt(x); | 11 static int sqrt(x) => math.sqrt(x); |
| 12 } | 12 } |
| 13 | 13 |
| 14 isCheckedMode() { | 14 isCheckedMode() { |
| 15 try { | 15 try { |
| 16 var i = 1; | 16 var i = 1; |
| 17 String s = i; | 17 String s = i; |
| 18 return false; | 18 return false; |
| 19 } catch (e) { | 19 } catch (e) { |
| 20 return true; | 20 return true; |
| 21 } | 21 } |
| 22 } | 22 } |
| 23 | 23 |
| 24 | 24 |
| 25 void test(int func(int value), int value) { | 25 void test(int func(int value), int value) { |
| 26 bool got_type_error = false; | 26 bool got_type_error = false; |
| 27 try { | 27 try { |
| 28 // Because of function subtyping rules, the static return type of a closure | 28 // Because of function subtyping rules, the static return type of a closure |
| 29 // call cannot be relied upon for static type analysis. For example, a | 29 // call cannot be relied upon for static type analysis. For example, a |
| 30 // function returning Dynamic (function 'root') can be assigned to a closure | 30 // function returning dynamic (function 'root') can be assigned to a closure |
| 31 // variable declared to return int (closure 'func') and may actually return | 31 // variable declared to return int (closure 'func') and may actually return |
| 32 // a double at run-time. | 32 // a double at run-time. |
| 33 // Therefore, eliminating the run-time type check would be wrong. | 33 // Therefore, eliminating the run-time type check would be wrong. |
| 34 int x = func(value); | 34 int x = func(value); |
| 35 Expect.equals(value, x * x); | 35 Expect.equals(value, x * x); |
| 36 } on TypeError catch (error) { | 36 } on TypeError catch (error) { |
| 37 got_type_error = true; | 37 got_type_error = true; |
| 38 } | 38 } |
| 39 // Type error expected in checked mode only. | 39 // Type error expected in checked mode only. |
| 40 Expect.isTrue(got_type_error == isCheckedMode()); | 40 Expect.isTrue(got_type_error == isCheckedMode()); |
| 41 } | 41 } |
| 42 | 42 |
| 43 root(x) => Math.sqrt(x); | 43 root(x) => Math.sqrt(x); |
| 44 | 44 |
| 45 main() => test(root, 4); | 45 main() => test(root, 4); |
| OLD | NEW |