OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library notnull; | |
6 | |
7 void intAssignments() { | |
8 var i = 0; | |
9 i &= 1; | |
10 i |= 1; | |
11 i ^= 1; | |
12 i >>= 1; | |
13 i <<= 1; | |
14 i -= 1; | |
15 i %= 1; | |
16 i += 1; | |
17 i ??= 1; | |
18 i *= 1; | |
19 i ~/= 1; | |
20 i++; | |
21 --i; | |
22 print(i + 1); | |
23 | |
24 int j = 1; | |
25 j = i < 10 ? 1 : 2; | |
26 print(j + 1); | |
27 } | |
28 | |
29 void doubleAssignments() { | |
30 var d = 0.0; | |
31 d /= 1; | |
32 print(d + 1); | |
33 } | |
34 | |
35 void boolAssignments() { | |
36 var b = true; | |
37 b != b; | |
38 print(b); | |
39 } | |
40 | |
41 void increments() { | |
42 int i = 1; | |
43 print(++i); | |
44 print(i++); | |
45 print(--i); | |
46 print(i--); | |
47 | |
48 int j; | |
49 j = 1; | |
50 print(++j); | |
51 print(j++); | |
52 print(--j); | |
53 print(j--); | |
54 } | |
55 | |
56 void conditionals([bool cond]) { | |
57 int nullable; | |
58 nullable = 1; | |
59 int nonNullable = 1; | |
60 int a = cond ? nullable : nullable; | |
61 int b = cond ? nullable : nonNullable; | |
62 int c = cond ? nonNullable : nonNullable; | |
63 int d = cond ? nonNullable : nullable; | |
64 print(a + b + c + d); | |
65 } | |
66 | |
67 void nullAwareOps() { | |
68 int nullable; | |
69 int nonNullable = 1; | |
70 int a = nullable ?? nullable; | |
71 int b = nullable ?? nonNullable; | |
72 int c = nonNullable ?? nonNullable; | |
73 int d = nonNullable ?? nullable; | |
74 print(a + b + c + d); | |
75 | |
76 var s = ""; | |
77 print(s?.length + 1); | |
78 } | |
79 | |
80 void nullableLocals(int param) { | |
81 print(param + 1); | |
82 | |
83 int i; | |
84 // We could detect that i is effectively non-nullable with flow analysis. | |
85 i = 1; | |
86 print(i + 1); | |
87 | |
88 int j = 1; | |
89 j = i == 1 ? 1 : null; | |
90 print(j + 1); | |
91 } | |
92 | |
93 void optParams([int x, int y = 1]) { | |
94 print(x + y); | |
95 } | |
96 | |
97 void namedParams({int x, int y: 1}) { | |
98 print(x + y); | |
99 } | |
100 | |
101 void forLoops(int length()) { | |
102 for (int i = 0; i < 10; i++) { | |
103 print(i + 1); | |
104 } | |
105 for (int i = 0; i < length(); i++) { | |
106 print(i + 1); | |
107 } | |
108 for (int i = 0, n = length(); i < n; i++) { | |
109 print(i + 1); | |
110 } | |
111 // TODO(ochafik): Special-case `int + 0` to provide a cheap way to coerce | |
112 // ints to notnull in the SDK (like asm.js's `x|0` pattern). | |
113 for (int i = 0, n = length() + 0; i < n; i++) { | |
114 print(i + 1); | |
115 } | |
116 } | |
117 | |
118 void nullableCycle() { | |
119 int x = 1; | |
120 int y = 2; | |
121 int z; | |
122 x = y; | |
123 y = z; | |
124 z = x; | |
125 print(x + y + z); | |
126 | |
127 int s; | |
128 s = s; | |
129 print(s + 1); | |
130 } | |
131 | |
132 void nonNullableCycle() { | |
133 int x = 1; | |
134 int y = 2; | |
135 int z = 3; | |
136 x = y; | |
137 y = z; | |
138 z = x; | |
139 print(x + y + z); | |
140 | |
141 int s = 1; | |
142 s = s; | |
143 print(s + 1); | |
144 } | |
145 | |
146 class Foo { | |
147 int intField; | |
148 var varField; | |
149 f(Foo o) { | |
150 print(1 + varField + 2); | |
151 while (varField < 10) varField++; | |
152 while (varField < 10) varField = varField + 1; | |
153 | |
154 print(1 + intField + 2); | |
155 while (intField < 10) intField++; | |
156 while (intField < 10) intField = intField + 1; | |
157 | |
158 print(1 + o.intField + 2); | |
159 while (o.intField < 10) o.intField++; | |
160 while (o.intField < 10) o.intField = o.intField + 1; | |
161 } | |
162 } | |
163 | |
164 int _foo() => 1; | |
165 calls() { | |
166 int a = 1; | |
167 int b = 1; | |
168 b = ((x) => x)(a); | |
169 print(b + 1); | |
170 | |
171 int c = _foo(); | |
172 print(c + 1); | |
173 } | |
174 | |
175 localEscapes() { | |
176 int a = 1; | |
177 var f = (x) => a = x; | |
178 | |
179 int b = 1; | |
180 g(x) => b = x; | |
181 | |
182 f(1); | |
183 g(1); | |
184 | |
185 print(a + b); | |
186 } | |
187 | |
188 controlFlow() { | |
189 for (int i, j;;) { | |
190 i = j = 1; | |
191 print(i + j + 1); | |
192 break; | |
193 } | |
194 try { | |
195 throw 1; | |
196 } catch (e) { | |
197 print(e + 1); | |
198 } | |
199 try { | |
200 (null as dynamic).foo(); | |
201 } catch (e, trace) { | |
202 print('${(e is String) ? e : e.toString()} at $trace'); | |
203 } | |
204 } | |
205 | |
206 cascadesOnNull() { | |
207 dynamic x = null; | |
208 print((x | |
209 ..toString() | |
210 ..runtimeType) | |
211 .hashCode); | |
212 | |
213 Object y = null; | |
214 print((y | |
215 ..toString() | |
216 ..runtimeType) | |
217 .hashCode); | |
218 } | |
219 | |
220 main() { | |
221 intAssignments(); | |
222 doubleAssignments(); | |
223 boolAssignments(); | |
224 nullableLocals(1); | |
225 optParams(1, 2); | |
226 namedParams(x: 1, y: 2); | |
227 forLoops(() => 10); | |
228 increments(); | |
229 conditionals(true); | |
230 calls(); | |
231 localEscapes(); | |
232 controlFlow(); | |
233 cascadesOnNull(); | |
234 | |
235 nullableCycle(); | |
236 nonNullableCycle(); | |
237 } | |
OLD | NEW |