Skip to content

Commit a97ef31

Browse files
pqCommit Queue
authored and
Commit Queue
committed
[wildcards] local function DUPLICATE_DEFINITION support
See: #56089 Fixes: CompileTimeError -> Pass (expected Pass) co19/LanguageFeatures/Wildcards/binding_A02_t05 Change-Id: Icf996952a4f87f71cc64cecf7ac539df363c3db6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/380045 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent d6432ed commit a97ef31

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,13 @@ class DuplicateDefinitionVerifier {
9393
element: variable.declaredElement!);
9494
}
9595
} else if (statement is FunctionDeclarationStatement) {
96-
_checkDuplicateIdentifier(
97-
definedNames,
98-
statement.functionDeclaration.name,
99-
element: statement.functionDeclaration.declaredElement!,
100-
);
96+
if (!_isWildCardFunction(statement)) {
97+
_checkDuplicateIdentifier(
98+
definedNames,
99+
statement.functionDeclaration.name,
100+
element: statement.functionDeclaration.declaredElement!,
101+
);
102+
}
101103
} else if (statement is PatternVariableDeclarationStatementImpl) {
102104
for (var variable in statement.declaration.elements) {
103105
_checkDuplicateIdentifier(definedNames, variable.node.name,
@@ -264,6 +266,10 @@ class DuplicateDefinitionVerifier {
264266
}
265267
}
266268

269+
bool _isWildCardFunction(FunctionDeclarationStatement statement) =>
270+
statement.functionDeclaration.name.lexeme == '_' &&
271+
_currentLibrary.hasWildcardVariablesFeatureEnabled;
272+
267273
static bool _isGetterSetterPair(Element a, Element b) {
268274
if (a is PropertyAccessorElement && b is PropertyAccessorElement) {
269275
return a.isGetter && b.isSetter || a.isSetter && b.isGetter;

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,6 +2384,41 @@ mixin M {
23842384

23852385
@reflectiveTest
23862386
class DuplicateDefinitionTest extends PubPackageResolutionTest {
2387+
test_block_localFunction_wildcard() async {
2388+
await assertErrorsInCode(r'''
2389+
void f() {
2390+
void _() {}
2391+
int _(int _) => 42;
2392+
String _(int _) => "42";
2393+
}
2394+
''', [
2395+
error(WarningCode.DEAD_CODE, 13, 11),
2396+
error(WarningCode.DEAD_CODE, 27, 19),
2397+
error(WarningCode.DEAD_CODE, 49, 24),
2398+
]);
2399+
}
2400+
2401+
test_block_localFunction_wildcard_preWildcards() async {
2402+
await assertErrorsInCode(r'''
2403+
// @dart = 3.4
2404+
// (pre wildcard-variables)
2405+
2406+
void f() {
2407+
void _() {}
2408+
int _(int _) => 42;
2409+
String _(int _) => "42";
2410+
}
2411+
''', [
2412+
error(WarningCode.UNUSED_ELEMENT, 62, 1),
2413+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 75, 1,
2414+
contextMessages: [message(testFile, 62, 1)]),
2415+
error(WarningCode.UNUSED_ELEMENT, 75, 1),
2416+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 100, 1,
2417+
contextMessages: [message(testFile, 62, 1)]),
2418+
error(WarningCode.UNUSED_ELEMENT, 100, 1),
2419+
]);
2420+
}
2421+
23872422
test_block_localVariable_localVariable() async {
23882423
await assertErrorsInCode(r'''
23892424
void f() {

0 commit comments

Comments
 (0)