Skip to content

Commit ec575f8

Browse files
pqCommit Queue
authored and
Commit Queue
committed
[wildcards] field name support for record positional fields
See: #56271 `DUPLICATE_FIELD_NAME` to be handled in a follow-up Change-Id: I44b7302cc3d522636075e4ffccbf88c117306730 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/376203 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 547bf4a commit ec575f8

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/dart/analysis/features.dart';
6+
import 'package:analyzer/dart/element/element.dart';
57
import 'package:analyzer/dart/element/nullability_suffix.dart';
68
import 'package:analyzer/error/listener.dart';
79
import 'package:analyzer/src/dart/ast/ast.dart';
@@ -17,12 +19,17 @@ import 'package:analyzer/src/error/codes.g.dart';
1719
class RecordTypeAnnotationResolver {
1820
final TypeProviderImpl typeProvider;
1921
final ErrorReporter errorReporter;
22+
final LibraryElement libraryElement;
2023

2124
RecordTypeAnnotationResolver({
2225
required this.typeProvider,
2326
required this.errorReporter,
27+
required this.libraryElement,
2428
});
2529

30+
bool get isWildCardVariablesEnabled =>
31+
libraryElement.featureSet.isEnabled(Feature.wildcard_variables);
32+
2633
/// Report any named fields in the record type [node] that use a previously
2734
/// defined name.
2835
void reportDuplicateFieldDefinitions(RecordTypeAnnotationImpl node) {
@@ -51,10 +58,16 @@ class RecordTypeAnnotationResolver {
5158
if (nameToken != null) {
5259
var name = nameToken.lexeme;
5360
if (name.startsWith('_')) {
54-
errorReporter.atToken(
55-
nameToken,
56-
CompileTimeErrorCode.INVALID_FIELD_NAME_PRIVATE,
57-
);
61+
if (field is RecordTypeAnnotationPositionalField &&
62+
name.length == 1 &&
63+
isWildCardVariablesEnabled) {
64+
// Positional record fields named `_` are legal w/ wildcards.
65+
} else {
66+
errorReporter.atToken(
67+
nameToken,
68+
CompileTimeErrorCode.INVALID_FIELD_NAME_PRIVATE,
69+
);
70+
}
5871
} else {
5972
var index = RecordTypeExtension.positionalFieldIndex(name);
6073
if (index != null) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
138138
var recordTypeResolver = RecordTypeAnnotationResolver(
139139
typeProvider: typeProvider,
140140
errorReporter: errorReporter,
141+
libraryElement: libraryElement,
141142
);
142143

143144
return ResolutionVisitor._(

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,40 @@ void f((int _a, int b) r) {}
167167
error(CompileTimeErrorCode.INVALID_FIELD_NAME_PRIVATE, 12, 2),
168168
]);
169169
}
170+
171+
void test_wildcard_named() async {
172+
await assertErrorsInCode(r'''
173+
void f(({int _, int b}) r) {}
174+
''', [
175+
error(CompileTimeErrorCode.INVALID_FIELD_NAME_PRIVATE, 13, 1),
176+
]);
177+
}
178+
179+
void test_wildcard_named_preWildcards() async {
180+
await assertErrorsInCode(r'''
181+
// @dart = 3.4
182+
// (pre wildcard-variables)
183+
184+
void f(({int _, int b}) r) {}
185+
''', [
186+
error(CompileTimeErrorCode.INVALID_FIELD_NAME_PRIVATE, 57, 1),
187+
]);
188+
}
189+
190+
void test_wildcard_positional() async {
191+
await assertNoErrorsInCode(r'''
192+
void f((int _, int b) r) {}
193+
''');
194+
}
195+
196+
void test_wildcard_positional_preWildcards() async {
197+
await assertErrorsInCode(r'''
198+
// @dart = 3.4
199+
// (pre wildcard-variables)
200+
201+
void f((int _, int b) r) {}
202+
''', [
203+
error(CompileTimeErrorCode.INVALID_FIELD_NAME_PRIVATE, 56, 1),
204+
]);
205+
}
170206
}

0 commit comments

Comments
 (0)