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 // Test compile-time errors for illegal variable declarations if the name | 5 // Test compile-time errors for illegal variable declarations if the name |
6 // has been referenced before the variable is declared. | 6 // has been referenced before the variable is declared. |
7 | 7 |
8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
9 | 9 |
10 use(value) => value; | 10 use(value) => value; |
11 | 11 |
12 var x = 0; | 12 var x = 0; |
13 final y = 0; | 13 final y = 0; |
14 | 14 |
15 class C { | 15 class C { |
16 var f; | 16 var f; |
17 C() : f = 'How do you spell PTSD?'; | 17 C() : f = 'How do you spell PTSD?'; |
18 | 18 |
19 void test1() { | 19 void test1() { |
20 use(f); // Refers to instance field f. | 20 use(f); // Refers to instance field f. |
21 var f = 'A shut mouth gathers no foot.'; /// 00: compile-time error | 21 var f = 'A shut mouth gathers no foot.'; //# 00: compile-time error |
22 } | 22 } |
23 | 23 |
24 void test2() { | 24 void test2() { |
25 void localFunc() { | 25 void localFunc() { |
26 use(f); // Refers to instance field f. | 26 use(f); // Refers to instance field f. |
27 } | 27 } |
28 | 28 |
29 var f = 'When chemists die, they barium.'; /// 01: compile-time error | 29 var f = 'When chemists die, they barium.'; //# 01: compile-time error |
30 if (true) { | 30 if (true) { |
31 var f = 1; // ok, shadows outer f and instance field f. | 31 var f = 1; // ok, shadows outer f and instance field f. |
32 } | 32 } |
33 } | 33 } |
34 | 34 |
35 void test3() { | 35 void test3() { |
36 if (true) { | 36 if (true) { |
37 use(x); // Refers to top-level x. | 37 use(x); // Refers to top-level x. |
38 use(y); // Refers to top-level y. | 38 use(y); // Refers to top-level y. |
39 } | 39 } |
40 final x = "I have not yet begun to procrastinate."; /// 02: compile-time er
ror | 40 final x = "I have not yet begun to procrastinate."; //# 02: compile-time er
ror |
41 const y = "Honk if you like peace and quiet!"; /// 03: compile-time error | 41 const y = "Honk if you like peace and quiet!"; //# 03: compile-time error |
42 } | 42 } |
43 | 43 |
44 void test4() { | 44 void test4() { |
45 void Q() { | 45 void Q() { |
46 P(); // Refers to non-existing top-level function P | 46 P(); // Refers to non-existing top-level function P |
47 } | 47 } |
48 void P() { /// 06: compile-time error | 48 void P() { //# 06: compile-time error |
49 Q(); /// 06: continued | 49 Q(); //# 06: continued |
50 } /// 06: continued | 50 } //# 06: continued |
51 | 51 |
52 Function f = () {x = f;}; /// 07: compile-time error | 52 Function f = () {x = f;}; //# 07: compile-time error |
53 } | 53 } |
54 | 54 |
55 test() { | 55 test() { |
56 test1(); | 56 test1(); |
57 test2(); | 57 test2(); |
58 test3(); | 58 test3(); |
59 test4(); | 59 test4(); |
60 } | 60 } |
61 } | 61 } |
62 | 62 |
63 void testTypeRef() { | 63 void testTypeRef() { |
64 String s = 'Can vegetarians eat animal crackers?'; | 64 String s = 'Can vegetarians eat animal crackers?'; |
65 var String = "I distinctly remember forgetting that."; /// 04: compile-time e
rror | 65 var String = "I distinctly remember forgetting that."; //# 04: compile-time e
rror |
66 } | 66 } |
67 | 67 |
68 void testLibPrefix() { | 68 void testLibPrefix() { |
69 var pie = math.PI; | 69 var pie = math.PI; |
70 final math = 0; /// 05: compile-time error | 70 final math = 0; //# 05: compile-time error |
71 } | 71 } |
72 | 72 |
73 | 73 |
74 void noErrorsExpected() { | 74 void noErrorsExpected() { |
75 use(x); | 75 use(x); |
76 for (var x = 0; x < 10; x++) use(x); | 76 for (var x = 0; x < 10; x++) use(x); |
77 for (var i = 0; i < 10; i++) var x = 0; | 77 for (var i = 0; i < 10; i++) var x = 0; |
78 if (true) var x = 0; | 78 if (true) var x = 0; |
79 while (false) var x = 0; | 79 while (false) var x = 0; |
80 try { throw "ball"; } catch (x) { use(x); } | 80 try { throw "ball"; } catch (x) { use(x); } |
81 switch (x) { | 81 switch (x) { |
82 case 0: var x = 'Does fuzzy logic tickle?'; | 82 case 0: var x = 'Does fuzzy logic tickle?'; |
83 } | 83 } |
84 var int = 007; | 84 var int = 007; |
85 } | 85 } |
86 | 86 |
87 void main() { | 87 void main() { |
88 var c = new C(); | 88 var c = new C(); |
89 c.test(); | 89 c.test(); |
90 testTypeRef(); | 90 testTypeRef(); |
91 testLibPrefix(); | 91 testLibPrefix(); |
92 noErrorsExpected(); | 92 noErrorsExpected(); |
93 } | 93 } |
OLD | NEW |