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

Commit eca6c89

Browse files
lrhncommit-bot@chromium.org
authored andcommitted
Allow async as an identifier everywhere.
Fixes #37063 Bug: http://dartbug.com/37063 Change-Id: I2253c3088fbe33eca1072f2480cf0cf3cc363362 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103524 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Leaf Petersen <[email protected]> Commit-Queue: Lasse R.H. Nielsen <[email protected]>
1 parent dbd70dd commit eca6c89

File tree

12 files changed

+229
-303
lines changed

12 files changed

+229
-303
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
### Language
66

7+
* The identifier `async` can now be used in asynchronous and generator
8+
functions.
9+
710
### Core library
811

912
#### `dart:io`

pkg/analyzer/lib/src/dart/error/syntactic_errors.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class ParserErrorCode extends ErrorCode {
5151
static const ParserErrorCode ASYNC_KEYWORD_USED_AS_IDENTIFIER =
5252
const ParserErrorCode(
5353
'ASYNC_KEYWORD_USED_AS_IDENTIFIER',
54-
"The keywords 'async', 'await', and 'yield' can't be used as "
55-
"identifiers in an asynchronous or generator function.");
54+
"The keywords 'await' and 'yield' can't be used as "
55+
"identifiers in an asynchronous or generator function.");
5656

5757
static const ParserErrorCode BREAK_OUTSIDE_OF_LOOP = _BREAK_OUTSIDE_OF_LOOP;
5858

@@ -340,7 +340,7 @@ class ParserErrorCode extends ErrorCode {
340340
static const ParserErrorCode INVALID_COMMENT_REFERENCE = const ParserErrorCode(
341341
'INVALID_COMMENT_REFERENCE',
342342
"Comment references should contain a possibly prefixed identifier and "
343-
"can start with 'new', but shouldn't contain anything else.");
343+
"can start with 'new', but shouldn't contain anything else.");
344344

345345
static const ParserErrorCode INVALID_CONSTRUCTOR_NAME = const ParserErrorCode(
346346
'INVALID_CONSTRUCTOR_NAME',
@@ -564,7 +564,7 @@ class ParserErrorCode extends ErrorCode {
564564
const ParserErrorCode(
565565
'MULTIPLE_VARIABLES_IN_FOR_EACH',
566566
"A single loop variable must be declared in a for-each loop before "
567-
"the 'in', but {0} were found.",
567+
"the 'in', but {0} were found.",
568568
correction:
569569
"Try moving all but one of the declarations inside the loop body.");
570570

@@ -588,14 +588,14 @@ class ParserErrorCode extends ErrorCode {
588588
const ParserErrorCode(
589589
'NATIVE_CLAUSE_IN_NON_SDK_CODE',
590590
"Native clause can only be used in the SDK and code that is loaded "
591-
"through native extensions.",
591+
"through native extensions.",
592592
correction: "Try removing the native clause.");
593593

594594
static const ParserErrorCode NATIVE_FUNCTION_BODY_IN_NON_SDK_CODE =
595595
const ParserErrorCode(
596596
'NATIVE_FUNCTION_BODY_IN_NON_SDK_CODE',
597597
"Native functions can only be declared in the SDK and code that is "
598-
"loaded through native extensions.",
598+
"loaded through native extensions.",
599599
correction: "Try removing the word 'native'.");
600600

601601
static const ParserErrorCode NATIVE_CLAUSE_SHOULD_BE_ANNOTATION =

pkg/analyzer/lib/src/generated/parser.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6976,8 +6976,7 @@ class Parser {
69766976
SimpleIdentifier _parseSimpleIdentifierUnchecked(
69776977
{bool isDeclaration: false}) {
69786978
String lexeme = _currentToken.lexeme;
6979-
if ((_inAsync || _inGenerator) &&
6980-
(lexeme == ASYNC || lexeme == _AWAIT || lexeme == _YIELD)) {
6979+
if ((_inAsync || _inGenerator) && (lexeme == _AWAIT || lexeme == _YIELD)) {
69816980
_reportErrorForCurrentToken(
69826981
ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER);
69836982
}

pkg/analyzer/test/generated/compile_time_error_code.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2726,7 +2726,6 @@ class A {
27262726
}
27272727
''', [
27282728
error(HintCode.UNUSED_LOCAL_VARIABLE, 32, 5),
2729-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 32, 5),
27302729
]);
27312730
}
27322731

pkg/analyzer/test/src/diagnostics/async_keyword_used_as_identifier_test.dart

Lines changed: 0 additions & 272 deletions
Original file line numberDiff line numberDiff line change
@@ -16,278 +16,6 @@ main() {
1616

1717
@reflectiveTest
1818
class AsyncKeywordUsedAsIdentifierTest extends DriverResolutionTest {
19-
test_async_annotation() async {
20-
await assertErrorsInCode('''
21-
const int async = 0;
22-
f() async {
23-
g(@async x) {}
24-
g(0);
25-
}
26-
''', [
27-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 38, 5),
28-
]);
29-
}
30-
31-
test_async_argumentLabel() async {
32-
await assertErrorsInCode('''
33-
f(c) async {
34-
c.g(async: 0);
35-
}
36-
''', [
37-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 19, 5),
38-
]);
39-
}
40-
41-
test_async_async() async {
42-
await assertErrorsInCode('''
43-
f() async {
44-
var async = 1;
45-
print(async);
46-
}
47-
''', [
48-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 18, 5),
49-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 37, 5),
50-
]);
51-
}
52-
53-
test_async_asyncStar() async {
54-
await assertErrorsInCode('''
55-
f() async* {
56-
var async = 1;
57-
print(async);
58-
}
59-
''', [
60-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 19, 5),
61-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 38, 5),
62-
]);
63-
}
64-
65-
test_async_break() async {
66-
await assertErrorsInCode('''
67-
f() async {
68-
while (true) {
69-
break async;
70-
}
71-
}
72-
''', [
73-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 39, 5),
74-
error(CompileTimeErrorCode.LABEL_UNDEFINED, 39, 5),
75-
]);
76-
}
77-
78-
test_async_catchException() async {
79-
await assertErrorsInCode('''
80-
g() {}
81-
f() async {
82-
try {
83-
g();
84-
} catch (async) { }
85-
}
86-
''', [
87-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 47, 5),
88-
]);
89-
}
90-
91-
test_async_catchStacktrace() async {
92-
await assertErrorsInCode('''
93-
g() {}
94-
f() async {
95-
try {
96-
g();
97-
} catch (e, async) { }
98-
}
99-
''', [
100-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 50, 5),
101-
error(HintCode.UNUSED_CATCH_STACK, 50, 5),
102-
]);
103-
}
104-
105-
test_async_continue() async {
106-
await assertErrorsInCode('''
107-
f() async {
108-
while (true) {
109-
continue async;
110-
}
111-
}
112-
''', [
113-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 42, 5),
114-
error(CompileTimeErrorCode.LABEL_UNDEFINED, 42, 5),
115-
]);
116-
}
117-
118-
test_async_for() async {
119-
await assertErrorsInCode('''
120-
var async;
121-
f() async {
122-
for (async in []) {}
123-
}
124-
''', [
125-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 30, 5),
126-
]);
127-
}
128-
129-
test_async_formalParameter() async {
130-
await assertErrorsInCode('''
131-
f() async {
132-
g(int async) {}
133-
g(0);
134-
}
135-
''', [
136-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 20, 5),
137-
]);
138-
}
139-
140-
test_async_getter() async {
141-
await assertErrorsInCode('''
142-
class C {
143-
int get async => 1;
144-
}
145-
f() async {
146-
return new C().async;
147-
}
148-
''', [
149-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 63, 5),
150-
]);
151-
}
152-
153-
test_async_invocation() async {
154-
await assertErrorsInCode('''
155-
class C {
156-
int async() => 1;
157-
}
158-
f() async {
159-
return new C().async();
160-
}
161-
''', [
162-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 61, 5),
163-
]);
164-
}
165-
166-
test_async_invocation_cascaded() async {
167-
await assertErrorsInCode('''
168-
class C {
169-
int async() => 1;
170-
}
171-
f() async {
172-
return new C()..async();
173-
}
174-
''', [
175-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 62, 5),
176-
]);
177-
}
178-
179-
test_async_label() async {
180-
await assertErrorsInCode('''
181-
f() async {
182-
async: g();
183-
}
184-
g() {}
185-
''', [
186-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 14, 5),
187-
error(HintCode.UNUSED_LABEL, 14, 6),
188-
]);
189-
}
190-
191-
test_async_localFunction() async {
192-
await assertErrorsInCode('''
193-
f() async {
194-
int async() => null;
195-
async();
196-
}
197-
''', [
198-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 18, 5),
199-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 37, 5),
200-
]);
201-
}
202-
203-
test_async_prefix() async {
204-
await assertErrorsInCode('''
205-
import 'dart:async' as async;
206-
f() async {
207-
return new async.Future.value(0);
208-
}
209-
''', [
210-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 55, 5),
211-
]);
212-
}
213-
214-
test_async_setter() async {
215-
await assertErrorsInCode('''
216-
class C {
217-
void set async(int i) {}
218-
}
219-
f() async {
220-
new C().async = 1;
221-
}
222-
''', [
223-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 61, 5),
224-
]);
225-
}
226-
227-
test_async_setter_cascaded() async {
228-
await assertErrorsInCode('''
229-
class C {
230-
void set async(int i) {}
231-
}
232-
f() async {
233-
return new C()..async = 1;
234-
}
235-
''', [
236-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 69, 5),
237-
]);
238-
}
239-
240-
test_async_stringInterpolation() async {
241-
await assertErrorsInCode(r'''
242-
int async = 1;
243-
f() async {
244-
return "$async";
245-
}
246-
''', [
247-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 38, 5),
248-
]);
249-
}
250-
251-
test_async_suffix() async {
252-
newFile("/test/lib/lib1.dart", content: r'''
253-
library lib1;
254-
int async;
255-
''');
256-
await assertErrorsInCode('''
257-
import 'lib1.dart' as l;
258-
f() async {
259-
return l.async;
260-
}
261-
''', [
262-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 48, 5),
263-
]);
264-
}
265-
266-
test_async_switchLabel() async {
267-
await assertErrorsInCode('''
268-
f() async {
269-
switch (0) {
270-
async: case 0: break;
271-
}
272-
}
273-
''', [
274-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 31, 5),
275-
error(HintCode.UNUSED_LABEL, 31, 6),
276-
]);
277-
}
278-
279-
test_async_syncStar() async {
280-
await assertErrorsInCode('''
281-
f() sync* {
282-
var async = 1;
283-
print(async);
284-
}
285-
''', [
286-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 18, 5),
287-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 37, 5),
288-
]);
289-
}
290-
29119
test_await_async() async {
29220
await assertErrorsInCode('''
29321
f() async {

pkg/front_end/lib/src/fasta/fasta_codes_generated.dart

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,6 @@ const MessageCode messageAssertExtraneousArgument = const MessageCode(
232232
"AssertExtraneousArgument",
233233
message: r"""`assert` can't have more than two arguments.""");
234234

235-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
236-
const Code<Null> codeAsyncAsIdentifier = messageAsyncAsIdentifier;
237-
238-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
239-
const MessageCode messageAsyncAsIdentifier = const MessageCode(
240-
"AsyncAsIdentifier",
241-
analyzerCodes: <String>["ASYNC_KEYWORD_USED_AS_IDENTIFIER"],
242-
message:
243-
r"""'async' can't be used as an identifier in 'async', 'async*', or 'sync*' methods.""");
244-
245235
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
246236
const Code<Null> codeAwaitAsIdentifier = messageAwaitAsIdentifier;
247237

pkg/front_end/lib/src/fasta/parser/identifier_context_impl.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,8 +940,6 @@ void checkAsyncAwaitYieldAsIdentifier(Token identifier, Parser parser) {
940940
parser.reportRecoverableError(identifier, fasta.messageAwaitAsIdentifier);
941941
} else if (optional('yield', identifier)) {
942942
parser.reportRecoverableError(identifier, fasta.messageYieldAsIdentifier);
943-
} else if (optional('async', identifier)) {
944-
parser.reportRecoverableError(identifier, fasta.messageAsyncAsIdentifier);
945943
}
946944
}
947945
}

pkg/front_end/messages.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,10 +1165,6 @@ AbstractNotSync:
11651165
template: "Abstract methods can't use 'async', 'async*', or 'sync*'."
11661166
analyzerCode: NON_SYNC_ABSTRACT_METHOD
11671167

1168-
AsyncAsIdentifier:
1169-
analyzerCode: ASYNC_KEYWORD_USED_AS_IDENTIFIER
1170-
template: "'async' can't be used as an identifier in 'async', 'async*', or 'sync*' methods."
1171-
11721168
AwaitAsIdentifier:
11731169
template: "'await' can't be used as an identifier in 'async', 'async*', or 'sync*' methods."
11741170
analyzerCode: ASYNC_KEYWORD_USED_AS_IDENTIFIER

0 commit comments

Comments
 (0)