Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f6b0a70

Browse files
munificentcommit-bot@chromium.org
authored andcommitted
Migrate language_2/await to NNBD.
Change-Id: I59ae5b01c00f3bebda0ba74eb5eb1950ade59925 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/137720 Reviewed-by: Erik Ernst <[email protected]> Commit-Queue: Bob Nystrom <[email protected]> Auto-Submit: Bob Nystrom <[email protected]>
1 parent 1c64138 commit f6b0a70

15 files changed

+1175
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2017, 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+
import 'dart:async';
6+
import 'package:expect/expect.dart';
7+
8+
Future<String?> foo(bool x) async => x ? "foo" : null;
9+
10+
Future<String> bar(bool x) async {
11+
return ((await foo(x)) ?? "bar").toUpperCase();
12+
}
13+
14+
main() async {
15+
Expect.equals(await bar(true), "FOO");
16+
Expect.equals(await bar(false), "BAR");
17+
}

tests/language/await/await_test.dart

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
// Copyright (c) 2014, 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+
// VMOptions=---optimization-counter-threshold=10
6+
7+
import 'package:expect/expect.dart';
8+
import 'package:async_helper/async_helper.dart';
9+
10+
import 'dart:async';
11+
12+
int globalVariable = 1;
13+
int topLevelFoo(int param) => 1;
14+
int get topLevelGetter => globalVariable;
15+
void set topLevelSetter(val) {
16+
globalVariable = val;
17+
}
18+
19+
class C {
20+
static int staticField = 1;
21+
static int get staticGetter => staticField;
22+
static void set staticSetter(val) {
23+
staticField = val;
24+
}
25+
26+
static int staticFoo(int param) => param;
27+
28+
int field = 1;
29+
int get getter => field;
30+
void set setter(val) {
31+
field = val;
32+
}
33+
34+
int foo(int param) => param;
35+
}
36+
37+
dummy() => 1;
38+
39+
staticMembers() async {
40+
var a = C.staticField + await dummy();
41+
Expect.equals(a, 2);
42+
var f = (C.staticField = 1) + await dummy();
43+
Expect.equals(f, 2);
44+
var b = C.staticGetter + await dummy();
45+
Expect.equals(b, 2);
46+
var c = (C.staticSetter = 1) + await dummy();
47+
Expect.equals(c, 2);
48+
var d = C.staticFoo(2) + await dummy();
49+
Expect.equals(d, 3);
50+
var e = C.staticField +
51+
C.staticGetter +
52+
(C.staticSetter = 1) +
53+
C.staticFoo(1) +
54+
await dummy();
55+
Expect.equals(e, 5);
56+
}
57+
58+
topLevelMembers() async {
59+
var a = globalVariable + await dummy();
60+
Expect.equals(a, 2);
61+
var b = topLevelGetter + await dummy();
62+
Expect.equals(b, 2);
63+
var c = (topLevelSetter = 1) + await dummy();
64+
Expect.equals(c, 2);
65+
var d = topLevelFoo(1) + await dummy();
66+
Expect.equals(d, 2);
67+
var e = globalVariable +
68+
topLevelGetter +
69+
(topLevelSetter = 1) +
70+
topLevelFoo(1) +
71+
await dummy();
72+
Expect.equals(e, 5);
73+
}
74+
75+
instanceMembers() async {
76+
var inst = new C();
77+
var a = inst.field + await dummy();
78+
Expect.equals(a, 2);
79+
var b = inst.getter + await dummy();
80+
Expect.equals(b, 2);
81+
var c = (inst.setter = 1) + await dummy();
82+
Expect.equals(c, 2);
83+
var d = inst.foo(1) + await dummy();
84+
Expect.equals(d, 2);
85+
var e = inst.field +
86+
inst.getter +
87+
(inst.setter = 1) +
88+
inst.foo(1) +
89+
await dummy();
90+
Expect.equals(e, 5);
91+
}
92+
93+
others() async {
94+
var a = "${globalVariable} ${await dummy()} " + await "someString";
95+
Expect.equals(a, "1 1 someString");
96+
var c = new C();
97+
var d = c.field + await dummy();
98+
var cnt = 2;
99+
var b = [1, 2, 3];
100+
b[cnt] = await dummy();
101+
Expect.equals(b[cnt], 1);
102+
var e = b[0] + await dummy();
103+
Expect.equals(e, 2);
104+
}
105+
106+
conditionals() async {
107+
var a = false;
108+
var b = true;
109+
var c = (a || b) || await dummy();
110+
Expect.isTrue(c);
111+
var d = (a || b) ? a : await dummy();
112+
Expect.isFalse(d);
113+
var e = (a is int) ? await dummy() : 2;
114+
Expect.equals(e, 2);
115+
try {
116+
var f = (a is int) ? await dummy() : 2;
117+
} catch (e) {}
118+
}
119+
120+
asserts() async {
121+
for (final FutureOr<T> Function<T>(T) func in <dynamic>[id, future]) {
122+
assert(await func(true));
123+
assert(id(true) as bool, await func("message"));
124+
assert(await func(true), await (func("message")));
125+
bool success = true;
126+
try {
127+
assert(await func(false), await (func("message")));
128+
if (assertStatementsEnabled) Expect.fail("Didn't throw");
129+
} on AssertionError catch (e) {
130+
Expect.equals("message", e.message);
131+
}
132+
}
133+
}
134+
135+
controlFlow() async {
136+
for (final FutureOr<T> Function<T>(T) func in <dynamic>[id, future]) {
137+
// For.
138+
var c = 0;
139+
for (var i = await (func(0)); await func(i < 5); await func(i++)) {
140+
c++;
141+
}
142+
Expect.equals(5, c);
143+
// While.
144+
c = 0;
145+
while (await func(c < 5)) c++;
146+
Expect.equals(5, c);
147+
// Do-while.
148+
c = 0;
149+
do {
150+
c++;
151+
} while (await func(c < 5));
152+
Expect.equals(5, c);
153+
// If.
154+
if (await func(c == 5)) {
155+
Expect.equals(5, c);
156+
} else {
157+
Expect.fail("unreachable");
158+
}
159+
// Throw.
160+
try {
161+
throw await func("string");
162+
} on String {
163+
// OK.
164+
}
165+
166+
try {
167+
await (throw "string");
168+
} on String {
169+
// OK.
170+
}
171+
// Try/catch/finally
172+
try {
173+
try {
174+
throw "string";
175+
} catch (e) {
176+
Expect.equals("string", e);
177+
Expect.equals(0, await func(0));
178+
rethrow;
179+
} finally {
180+
Expect.equals(0, await func(0));
181+
}
182+
} catch (e) {
183+
Expect.equals(0, await func(0));
184+
Expect.equals("string", e);
185+
} finally {
186+
Expect.equals(0, await func(0));
187+
}
188+
// Switch
189+
switch (await func(2)) {
190+
case 2:
191+
break;
192+
default:
193+
Expect.fail("unreachable");
194+
}
195+
// Return.
196+
Expect.equals(
197+
42,
198+
await () async {
199+
return await func(42);
200+
}());
201+
Expect.equals(
202+
42,
203+
await () async {
204+
return func(42);
205+
}());
206+
// Yield.
207+
Stream<int> testStream1() async* {
208+
yield await func(42);
209+
}
210+
211+
Expect.listEquals([42], await testStream1().toList());
212+
// Yield*
213+
Stream<int> testStream2() async* {
214+
yield* await func(intStream());
215+
}
216+
217+
Expect.listEquals([42], await testStream2().toList());
218+
}
219+
}
220+
221+
FutureOr<T> future<T>(T value) async => value;
222+
FutureOr<T> id<T>(T value) => value;
223+
224+
Stream<int> intStream() async* {
225+
yield 42;
226+
}
227+
228+
main() {
229+
asyncStart();
230+
for (int i = 0; i < 11; i++) {
231+
asyncTest(staticMembers);
232+
asyncTest(topLevelMembers);
233+
asyncTest(instanceMembers);
234+
asyncTest(conditionals);
235+
asyncTest(others);
236+
asyncTest(asserts);
237+
asyncTest(controlFlow);
238+
}
239+
asyncEnd();
240+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// TODO(multitest): This was automatically migrated from a multitest and may
2+
// contain strange or dead code.
3+
4+
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
5+
// for details. All rights reserved. Use of this source code is governed by a
6+
// BSD-style license that can be found in the LICENSE file.
7+
8+
import 'dart:async';
9+
10+
import 'package:expect/expect.dart';
11+
12+
get await => 4;
13+
14+
// For functions that are declared with the async modifier we treat await as
15+
// keyword.
16+
17+
test0() async {
18+
var x = await 7;
19+
Expect.equals(7, x);
20+
21+
}
22+
23+
test1() async {
24+
var x = await 9;
25+
Expect.equals(9, x);
26+
27+
}
28+
29+
// For functions that are not declared with the async modifier we allow await to
30+
// be used as an identifier.
31+
32+
test2() {
33+
var y = await;
34+
Expect.equals(4, y);
35+
36+
}
37+
38+
test3() {
39+
var await = 3;
40+
Expect.equals(3, await);
41+
42+
}
43+
44+
main() {
45+
test0();
46+
test1();
47+
test2();
48+
test3();
49+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) 2014, 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+
import 'dart:async';
6+
7+
import 'package:expect/expect.dart';
8+
9+
get await => 4;
10+
11+
// For functions that are declared with the async modifier we treat await as
12+
// keyword.
13+
14+
test0() async {
15+
var x = await 7;
16+
Expect.equals(7, x);
17+
var await = 1;
18+
// ^^^^^
19+
// [analyzer] SYNTACTIC_ERROR.ASYNC_KEYWORD_USED_AS_IDENTIFIER
20+
// [cfe] 'await' can't be used as an identifier in 'async', 'async*', or 'sync*' methods.
21+
}
22+
23+
test1() async {
24+
var x = await 9;
25+
Expect.equals(9, x);
26+
var y = await;
27+
// ^
28+
// [analyzer] SYNTACTIC_ERROR.MISSING_IDENTIFIER
29+
// [cfe] Expected an identifier, but got ';'.
30+
}
31+
32+
// For functions that are not declared with the async modifier we allow await to
33+
// be used as an identifier.
34+
35+
test2() {
36+
var y = await;
37+
Expect.equals(4, y);
38+
var x = await 1;
39+
// ^^^^^
40+
// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN
41+
// [cfe] Expected ';' after this.
42+
}
43+
44+
test3() {
45+
var await = 3;
46+
Expect.equals(3, await);
47+
var x = await 1;
48+
// ^^^^^
49+
// [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN
50+
// [cfe] Expected ';' after this.
51+
}
52+
53+
main() {
54+
test0();
55+
test1();
56+
test2();
57+
test3();
58+
}

0 commit comments

Comments
 (0)