Skip to content

Commit a666b5a

Browse files
Chloe Stefantsovacommit-bot@chromium.org
authored andcommitted
[analyzer,parser] Check for named before positional in analyzer
Since the check or the absence of the check for a named argument appearing before a positional parameter is feature-specific considering the language feature that allows placing the named arguments anywhere, it should be performed by the listeners of the parser and guarded by an experiment flag, in alignment with other experimental featuers. This CL moves the check and the reporting of the error from the parser into the Analyzer, so that later this check can be skipped if the corresponding experiment flag is enabled. Part of #47451. Change-Id: Ib795d418af429ee04ecfff6dfa71ec1e836fe798 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/216640 Reviewed-by: Paul Berry <paulberry@google.com> Commit-Queue: Chloe Stefantsova <dmitryas@google.com>
1 parent 8516cdc commit a666b5a

File tree

8 files changed

+13
-718
lines changed

8 files changed

+13
-718
lines changed

pkg/_fe_analyzer_shared/lib/src/parser/parser_impl.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6446,7 +6446,6 @@ class Parser {
64466446
assert(optional('(', begin));
64476447
listener.beginArguments(begin);
64486448
int argumentCount = 0;
6449-
bool hasSeenNamedArgument = false;
64506449
bool old = mayParseFunctionExpressions;
64516450
mayParseFunctionExpressions = true;
64526451
while (true) {
@@ -6461,10 +6460,6 @@ class Parser {
64616460
ensureIdentifier(token, IdentifierContext.namedArgumentReference)
64626461
.next!;
64636462
colon = token;
6464-
hasSeenNamedArgument = true;
6465-
} else if (hasSeenNamedArgument) {
6466-
// Positional argument after named argument.
6467-
reportRecoverableError(next, codes.messagePositionalAfterNamedArgument);
64686463
}
64696464
token = parseExpression(token);
64706465
next = token.next!;

pkg/analyzer/lib/src/fasta/ast_builder.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import 'package:_fe_analyzer_shared/src/messages/codes.dart'
2626
messageMissingAssignableSelector,
2727
messageNativeClauseShouldBeAnnotation,
2828
messageOperatorWithTypeParameters,
29+
messagePositionalAfterNamedArgument,
2930
templateDuplicateLabelInSwitchStatement,
3031
templateExpectedButGot,
3132
templateExpectedIdentifier,
@@ -577,6 +578,18 @@ class AstBuilder extends StackListener {
577578
var expressions = popTypedList2<Expression>(count);
578579
ArgumentList arguments =
579580
ast.argumentList(leftParenthesis, expressions, rightParenthesis);
581+
582+
bool hasSeenNamedArgument = false;
583+
for (Expression expression in expressions) {
584+
if (expression is NamedExpression) {
585+
hasSeenNamedArgument = true;
586+
} else if (hasSeenNamedArgument) {
587+
// Positional argument after named argument.
588+
handleRecoverableError(messagePositionalAfterNamedArgument,
589+
expression.beginToken, expression.endToken);
590+
}
591+
}
592+
580593
push(ast.methodInvocation(
581594
null, null, _tmpSimpleIdentifier(), null, arguments));
582595
}

pkg/front_end/parser_testcases/error_recovery/bracket_mismatch_01.dart.expect

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
Problems reported:
22

3-
parser/error_recovery/bracket_mismatch_01:25:11: Place positional arguments before named arguments.
4-
D(),
5-
^
6-
7-
parser/error_recovery/bracket_mismatch_01:26:11: Place positional arguments before named arguments.
8-
D(),
9-
^
10-
11-
parser/error_recovery/bracket_mismatch_01:27:9: Place positional arguments before named arguments.
12-
]),
13-
^
14-
153
parser/error_recovery/bracket_mismatch_01:27:9: Expected an identifier, but got ']'.
164
]),
175
^
@@ -186,19 +174,16 @@ beginCompilationUnit(class)
186174
endArguments(0, (, ))
187175
handleSend(D, ,)
188176
handleNamedArgument(:)
189-
handleRecoverableError(PositionalAfterNamedArgument, D, D)
190177
handleIdentifier(D, expression)
191178
handleNoTypeArguments(()
192179
beginArguments(()
193180
endArguments(0, (, ))
194181
handleSend(D, ,)
195-
handleRecoverableError(PositionalAfterNamedArgument, D, D)
196182
handleIdentifier(D, expression)
197183
handleNoTypeArguments(()
198184
beginArguments(()
199185
endArguments(0, (, ))
200186
handleSend(D, ,)
201-
handleRecoverableError(PositionalAfterNamedArgument, ], ])
202187
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got ']'., Try inserting an identifier before ']'., {lexeme: ]}], ], ])
203188
handleIdentifier(, expression)
204189
handleNoTypeArguments(])

pkg/front_end/parser_testcases/error_recovery/bracket_mismatch_01.dart.intertwined.expect

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,6 @@ parseUnit(class)
414414
listener: endArguments(0, (, ))
415415
listener: handleSend(D, ,)
416416
listener: handleNamedArgument(:)
417-
reportRecoverableError(D, PositionalAfterNamedArgument)
418-
listener: handleRecoverableError(PositionalAfterNamedArgument, D, D)
419417
parseExpression(,)
420418
parsePrecedenceExpression(,, 1, true)
421419
parseUnaryExpression(,, true)
@@ -433,8 +431,6 @@ parseUnit(class)
433431
listener: beginArguments(()
434432
listener: endArguments(0, (, ))
435433
listener: handleSend(D, ,)
436-
reportRecoverableError(D, PositionalAfterNamedArgument)
437-
listener: handleRecoverableError(PositionalAfterNamedArgument, D, D)
438434
parseExpression(,)
439435
parsePrecedenceExpression(,, 1, true)
440436
parseUnaryExpression(,, true)
@@ -452,8 +448,6 @@ parseUnit(class)
452448
listener: beginArguments(()
453449
listener: endArguments(0, (, ))
454450
listener: handleSend(D, ,)
455-
reportRecoverableError(], PositionalAfterNamedArgument)
456-
listener: handleRecoverableError(PositionalAfterNamedArgument, ], ])
457451
parseExpression(,)
458452
parsePrecedenceExpression(,, 1, true)
459453
parseUnaryExpression(,, true)

pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart.strong.expect

Lines changed: 0 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,4 @@
11
library /*isNonNullableByDefault*/;
2-
//
3-
// Problems in library:
4-
//
5-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:17:21: Error: Place positional arguments before named arguments.
6-
// Try moving the positional argument before the named arguments, or add a name to the argument.
7-
// method2(foo: 1, 2); // This call.
8-
// ^
9-
//
10-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:27:16: Error: Place positional arguments before named arguments.
11-
// Try moving the positional argument before the named arguments, or add a name to the argument.
12-
// foo(1, z: 2, 3);
13-
// ^
14-
//
15-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:28:13: Error: Place positional arguments before named arguments.
16-
// Try moving the positional argument before the named arguments, or add a name to the argument.
17-
// foo(z: 1, 2, 3);
18-
// ^
19-
//
20-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:28:16: Error: Place positional arguments before named arguments.
21-
// Try moving the positional argument before the named arguments, or add a name to the argument.
22-
// foo(z: 1, 2, 3);
23-
// ^
24-
//
25-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:32:22: Error: Place positional arguments before named arguments.
26-
// Try moving the positional argument before the named arguments, or add a name to the argument.
27-
// new A.foo(1, z: 2, 3);
28-
// ^
29-
//
30-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:33:19: Error: Place positional arguments before named arguments.
31-
// Try moving the positional argument before the named arguments, or add a name to the argument.
32-
// new A.foo(z: 1, 2, 3);
33-
// ^
34-
//
35-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:33:22: Error: Place positional arguments before named arguments.
36-
// Try moving the positional argument before the named arguments, or add a name to the argument.
37-
// new A.foo(z: 1, 2, 3);
38-
// ^
39-
//
40-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:35:22: Error: Place positional arguments before named arguments.
41-
// Try moving the positional argument before the named arguments, or add a name to the argument.
42-
// new B.foo(1, z: 2, 3);
43-
// ^
44-
//
45-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:36:19: Error: Place positional arguments before named arguments.
46-
// Try moving the positional argument before the named arguments, or add a name to the argument.
47-
// new B.foo(z: 1, 2, 3);
48-
// ^
49-
//
50-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:36:22: Error: Place positional arguments before named arguments.
51-
// Try moving the positional argument before the named arguments, or add a name to the argument.
52-
// new B.foo(z: 1, 2, 3);
53-
// ^
54-
//
55-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:40:18: Error: Place positional arguments before named arguments.
56-
// Try moving the positional argument before the named arguments, or add a name to the argument.
57-
// new A(1, z: 2, 3);
58-
// ^
59-
//
60-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:41:15: Error: Place positional arguments before named arguments.
61-
// Try moving the positional argument before the named arguments, or add a name to the argument.
62-
// new A(z: 1, 2, 3);
63-
// ^
64-
//
65-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:41:18: Error: Place positional arguments before named arguments.
66-
// Try moving the positional argument before the named arguments, or add a name to the argument.
67-
// new A(z: 1, 2, 3);
68-
// ^
69-
//
70-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:43:18: Error: Place positional arguments before named arguments.
71-
// Try moving the positional argument before the named arguments, or add a name to the argument.
72-
// new B(1, z: 2, 3);
73-
// ^
74-
//
75-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:44:15: Error: Place positional arguments before named arguments.
76-
// Try moving the positional argument before the named arguments, or add a name to the argument.
77-
// new B(z: 1, 2, 3);
78-
// ^
79-
//
80-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:44:18: Error: Place positional arguments before named arguments.
81-
// Try moving the positional argument before the named arguments, or add a name to the argument.
82-
// new B(z: 1, 2, 3);
83-
// ^
84-
//
85-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:48:14: Error: Place positional arguments before named arguments.
86-
// Try moving the positional argument before the named arguments, or add a name to the argument.
87-
// d(1, z: 2, 3);
88-
// ^
89-
//
90-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:49:11: Error: Place positional arguments before named arguments.
91-
// Try moving the positional argument before the named arguments, or add a name to the argument.
92-
// d(z: 1, 2, 3);
93-
// ^
94-
//
95-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:49:14: Error: Place positional arguments before named arguments.
96-
// Try moving the positional argument before the named arguments, or add a name to the argument.
97-
// d(z: 1, 2, 3);
98-
// ^
99-
//
100-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:53:14: Error: Place positional arguments before named arguments.
101-
// Try moving the positional argument before the named arguments, or add a name to the argument.
102-
// f(1, z: 2, 3);
103-
// ^
104-
//
105-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:54:11: Error: Place positional arguments before named arguments.
106-
// Try moving the positional argument before the named arguments, or add a name to the argument.
107-
// f(z: 1, 2, 3);
108-
// ^
109-
//
110-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:54:14: Error: Place positional arguments before named arguments.
111-
// Try moving the positional argument before the named arguments, or add a name to the argument.
112-
// f(z: 1, 2, 3);
113-
// ^
114-
//
115-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:58:23: Error: Place positional arguments before named arguments.
116-
// Try moving the positional argument before the named arguments, or add a name to the argument.
117-
// a.property(1, z: 2, 3);
118-
// ^
119-
//
120-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:59:20: Error: Place positional arguments before named arguments.
121-
// Try moving the positional argument before the named arguments, or add a name to the argument.
122-
// a.property(z: 1, 2, 3);
123-
// ^
124-
//
125-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:59:23: Error: Place positional arguments before named arguments.
126-
// Try moving the positional argument before the named arguments, or add a name to the argument.
127-
// a.property(z: 1, 2, 3);
128-
// ^
129-
//
130-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:63:18: Error: Place positional arguments before named arguments.
131-
// Try moving the positional argument before the named arguments, or add a name to the argument.
132-
// a.bar(1, z: 2, 3);
133-
// ^
134-
//
135-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:64:15: Error: Place positional arguments before named arguments.
136-
// Try moving the positional argument before the named arguments, or add a name to the argument.
137-
// a.bar(z: 1, 2, 3);
138-
// ^
139-
//
140-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:64:18: Error: Place positional arguments before named arguments.
141-
// Try moving the positional argument before the named arguments, or add a name to the argument.
142-
// a.bar(z: 1, 2, 3);
143-
// ^
144-
//
145-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:68:18: Error: Place positional arguments before named arguments.
146-
// Try moving the positional argument before the named arguments, or add a name to the argument.
147-
// local(1, z: 2, 3);
148-
// ^
149-
//
150-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:69:15: Error: Place positional arguments before named arguments.
151-
// Try moving the positional argument before the named arguments, or add a name to the argument.
152-
// local(z: 1, 2, 3);
153-
// ^
154-
//
155-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:69:18: Error: Place positional arguments before named arguments.
156-
// Try moving the positional argument before the named arguments, or add a name to the argument.
157-
// local(z: 1, 2, 3);
158-
// ^
159-
//
160-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:77:24: Error: Place positional arguments before named arguments.
161-
// Try moving the positional argument before the named arguments, or add a name to the argument.
162-
// super.bar(1, z: 2, 3);
163-
// ^
164-
//
165-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:78:21: Error: Place positional arguments before named arguments.
166-
// Try moving the positional argument before the named arguments, or add a name to the argument.
167-
// super.bar(z: 1, 2, 3);
168-
// ^
169-
//
170-
// pkg/front_end/testcases/unscheduled_experiments/named_arguments_anywhere/all_kinds.dart:78:24: Error: Place positional arguments before named arguments.
171-
// Try moving the positional argument before the named arguments, or add a name to the argument.
172-
// super.bar(z: 1, 2, 3);
173-
// ^
174-
//
1752
import self as self;
1763
import "dart:core" as core;
1774

0 commit comments

Comments
 (0)