OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 // Verify that '??' binds tighter than '?:' and less tightly than '||'. | 5 // Verify that '??' binds tighter than '?:' and less tightly than '||'. |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 | 8 |
9 assertionError(e) => e is AssertionError; | |
10 | |
11 // Determine whether the VM is running in checked mode. | |
12 bool get checkedMode { | |
13 try { | |
14 var x = 'foo'; | |
15 int y = x; | |
16 return false; | |
17 } catch (_) { | |
18 return true; | |
19 } | |
20 } | |
21 | |
22 main() { | 9 main() { |
23 // Make sure the "none" test fails if "??" is not implemented. This makes | 10 // Make sure the "none" test fails if "??" is not implemented. This makes |
24 // status files easier to maintain. | 11 // status files easier to maintain. |
25 var _ = null ?? null; | 12 var _ = null ?? null; |
26 | 13 |
27 // "a ?? b ?? c" should be legal, and should evaluate to the first non-null | 14 // "a ?? b ?? c" should be legal, and should evaluate to the first non-null |
28 // value (or null if there are no non-null values). | 15 // value (or null if there are no non-null values). |
29 Expect.equals(1, 1 ?? 2 ?? 3); //# 01: ok | 16 Expect.equals(1, 1 ?? 2 ?? 3); |
30 Expect.equals(2, null ?? 2 ?? 3); //# 02: ok | 17 Expect.equals(2, null ?? 2 ?? 3); |
31 Expect.equals(3, null ?? null ?? 3); //# 03: ok | 18 Expect.equals(3, null ?? null ?? 3); |
32 Expect.equals(null, null ?? null ?? null); //# 04: ok | 19 Expect.equals(null, null ?? null ?? null); |
33 | 20 |
34 // "a ?? b ? c : d" should parse as "(a ?? b) ? c : d", therefore provided | 21 // "a ?? b ? c : d" should parse as "(a ?? b) ? c : d", therefore provided |
35 // that a is true, b need not be a bool. An incorrect parse of | 22 // that a is true, b need not be a bool. An incorrect parse of |
36 // "a ?? (b ? c : d)" would require b to be a bool to avoid a static type | 23 // "a ?? (b ? c : d)" would require b to be a bool to avoid a static type |
37 // warning. | 24 // warning. |
38 Expect.equals(2, true ?? 1 ? 2 : 3); //# 05: ok | 25 Expect.equals(2, true ?? 1 ? 2 : 3); |
Lasse Reichstein Nielsen
2017/08/29 07:43:40
Should this be a type problem in Dart 2?
Or is it
Bob Nystrom
2017/08/30 18:21:23
Yes, I believe that's right. You get Object as the
| |
39 | 26 |
40 // "a ?? b || c" should parse as "a ?? (b || c)", therefore it's a static | 27 // "a ?? b || c" should parse as "a ?? (b || c)", therefore it's a static |
41 // type warning if b doesn't have type bool. An incorrect parse of | 28 // type warning if b doesn't have type bool. An incorrect parse of |
42 // "(a ?? b) || c" would allow b to have any type provided that a is bool. | 29 // "(a ?? b) || c" would allow b to have any type provided that a is bool. |
43 Expect.equals(false, false ?? 1 || true); //# 06: static type warning | 30 false ?? 1 || true; //# 06: compile-time error |
44 | 31 |
45 // "a || b ?? c" should parse as "(a || b) ?? c", therefore it is a static | 32 // "a || b ?? c" should parse as "(a || b) ?? c", therefore it is a static |
46 // type warning if b doesn't have type bool. An incorrect parse of | 33 // type warning if b doesn't have type bool. An incorrect parse of |
47 // "a || (b ?? c)" would allow b to have any type provided that c is bool. | 34 // "a || (b ?? c)" would allow b to have any type provided that c is bool. |
48 if (checkedMode) { | 35 false || 1 ?? true; //# 07: compile-time error |
49 Expect.throws(() => false || 1 ?? true, assertionError); //# 07: static type warning | |
50 } else { | |
51 Expect.equals(false, false || 1 ?? true); // //# 07: continued | |
52 } | |
53 | 36 |
54 if (checkedMode) { | 37 // An incorrect parse of "a || (b ?? c)" would result in no checked-mode |
55 // An incorrect parse of "a || (b ?? c)" would result in no checked-mode | 38 // error. |
56 // error. | 39 Expect.throwsAssertionError(() => false || null ?? true); |
57 Expect.throws(() => false || null ?? true, assertionError); //# 08: ok | |
58 } else { | |
59 // An incorrect parse of "a || (b ?? c)" would result in c being evaluated. | |
60 int i = 0; // //# 08: contin ued | |
61 Expect.equals(false, false || null ?? i++ == 0); // //# 08: contin ued | |
62 Expect.equals(0, i); // //# 08: contin ued | |
63 } | |
64 } | 40 } |
OLD | NEW |