Skip to content

Commit 1971d61

Browse files
pqCommit Queue
authored and
Commit Queue
committed
[wildcards] duplicate field name support for record positional fields
See: #56271 Change-Id: If471de5174f0ea1fae9daf6fbe3956a3abce40a4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/376460 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 62f6c37 commit 1971d61

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

pkg/analyzer/lib/src/dart/resolver/record_type_annotation_resolver.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ class RecordTypeAnnotationResolver {
3030
bool get isWildCardVariablesEnabled =>
3131
libraryElement.featureSet.isEnabled(Feature.wildcard_variables);
3232

33+
bool isPositionalWildCard(AstNode field, String name) =>
34+
field is RecordTypeAnnotationPositionalField &&
35+
name == '_' &&
36+
isWildCardVariablesEnabled;
37+
3338
/// Report any named fields in the record type [node] that use a previously
3439
/// defined name.
3540
void reportDuplicateFieldDefinitions(RecordTypeAnnotationImpl node) {
3641
var usedNames = <String, RecordTypeAnnotationField>{};
3742
for (var field in node.fields) {
3843
var name = field.name?.lexeme;
3944
if (name != null) {
45+
// Multiple positional `_`s are legal with wildcards.
46+
if (isPositionalWildCard(field, name)) continue;
47+
4048
var previousField = usedNames[name];
4149
if (previousField != null) {
4250
errorReporter.reportError(DiagnosticFactory()
@@ -58,11 +66,8 @@ class RecordTypeAnnotationResolver {
5866
if (nameToken != null) {
5967
var name = nameToken.lexeme;
6068
if (name.startsWith('_')) {
61-
if (field is RecordTypeAnnotationPositionalField &&
62-
name.length == 1 &&
63-
isWildCardVariablesEnabled) {
64-
// Positional record fields named `_` are legal w/ wildcards.
65-
} else {
69+
// Positional record fields named `_` are legal w/ wildcards.
70+
if (!isPositionalWildCard(field, name)) {
6671
errorReporter.atToken(
6772
nameToken,
6873
CompileTimeErrorCode.INVALID_FIELD_NAME_PRIVATE,

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,38 @@ void f((int a, {int a}) r) {}
6262
]);
6363
}
6464

65+
void test_duplicated_wildcard_named() async {
66+
await assertErrorsInCode(r'''
67+
void f(({int _, int _}) r) {}
68+
''', [
69+
// Only positional wildcard fields can be duplicated.
70+
error(CompileTimeErrorCode.INVALID_FIELD_NAME_PRIVATE, 13, 1),
71+
error(CompileTimeErrorCode.DUPLICATE_FIELD_NAME, 20, 1,
72+
contextMessages: [message(testFile, 13, 1)]),
73+
error(CompileTimeErrorCode.INVALID_FIELD_NAME_PRIVATE, 20, 1),
74+
]);
75+
}
76+
77+
void test_duplicated_wildcard_positional() async {
78+
await assertNoErrorsInCode(r'''
79+
void f((int _, int _) r) {}
80+
''');
81+
}
82+
83+
void test_duplicated_wildcard_positional_preWildcards() async {
84+
await assertErrorsInCode(r'''
85+
// @dart = 3.4
86+
// (pre wildcard-variables)
87+
88+
void f((int _, int _) r) {}
89+
''', [
90+
error(CompileTimeErrorCode.INVALID_FIELD_NAME_PRIVATE, 56, 1),
91+
error(CompileTimeErrorCode.DUPLICATE_FIELD_NAME, 63, 1,
92+
contextMessages: [message(testFile, 56, 1)]),
93+
error(CompileTimeErrorCode.INVALID_FIELD_NAME_PRIVATE, 63, 1),
94+
]);
95+
}
96+
6597
void test_notDuplicated_named() async {
6698
await assertNoErrorsInCode(r'''
6799
void f(({int a, int b}) r) {}

0 commit comments

Comments
 (0)