Skip to content

Commit 4ef9223

Browse files
pqCommit Queue
authored and
Commit Queue
committed
[wildcards] fix super formal positional param handling
Fixes `co19/src/LanguageFeatures/Wildcards/super_parameters_A04_t02.dart` ``` class A { final int x, y; A(this.x, [this.y = 0]); } class C extends A { final int _; String log = ""; C(this._, super._, [super._]) { log = "_=$_, x=$x, y=$y"; } C.n(this._, super._, [super._ = 1]) { log = "_=$_, x=$x, y=$y"; } } main() { Expect.equals("_=1, x=2, y=3", C(1, 2, 3).log); Expect.equals("_=1, x=2, y=0", C(1, 2).log); Expect.equals("_=1, x=2, y=1", C.n(1, 2).log); } ``` See: #55680 Change-Id: Ia87ae6a73816a08ca83d77c7f077e9ed7c153ebb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/380883 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 7600900 commit 4ef9223

File tree

4 files changed

+62
-16
lines changed

4 files changed

+62
-16
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:collection';
66

7+
import 'package:analyzer/dart/analysis/features.dart';
78
import 'package:analyzer/dart/ast/token.dart';
89
import 'package:analyzer/dart/element/element.dart';
910
import 'package:analyzer/error/error.dart';
@@ -71,14 +72,10 @@ class DuplicateDefinitionVerifier {
7172
// function type.
7273

7374
// Skip wildcard `super._`.
74-
if (parameter is SuperFormalParameter &&
75-
identifier.lexeme == '_' &&
76-
_currentLibrary.hasWildcardVariablesFeatureEnabled) {
77-
continue;
75+
if (!_isSuperFormalWildcard(parameter, identifier)) {
76+
_checkDuplicateIdentifier(definedNames, identifier,
77+
element: parameter.declaredElement!);
7878
}
79-
80-
_checkDuplicateIdentifier(definedNames, identifier,
81-
element: parameter.declaredElement!);
8279
}
8380
}
8481
}
@@ -266,6 +263,15 @@ class DuplicateDefinitionVerifier {
266263
}
267264
}
268265

266+
bool _isSuperFormalWildcard(FormalParameter parameter, Token identifier) {
267+
if (parameter is DefaultFormalParameter) {
268+
parameter = parameter.parameter;
269+
}
270+
return parameter is SuperFormalParameter &&
271+
identifier.lexeme == '_' &&
272+
_currentLibrary.featureSet.isEnabled(Feature.wildcard_variables);
273+
}
274+
269275
bool _isWildCardFunction(FunctionDeclarationStatement statement) =>
270276
statement.functionDeclaration.name.lexeme == '_' &&
271277
_currentLibrary.hasWildcardVariablesFeatureEnabled;

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6230,15 +6230,17 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
62306230
.MISSING_DEFAULT_VALUE_FOR_PARAMETER_WITH_ANNOTATION,
62316231
);
62326232
} else {
6233-
errorReporter.atEntity(
6234-
errorTarget,
6235-
parameterElement.isPositional
6236-
? CompileTimeErrorCode
6237-
.MISSING_DEFAULT_VALUE_FOR_PARAMETER_POSITIONAL
6238-
: CompileTimeErrorCode
6239-
.MISSING_DEFAULT_VALUE_FOR_PARAMETER,
6240-
arguments: [parameterName?.lexeme ?? '?'],
6241-
);
6233+
if (!_isWildcardSuperFormalPositionalParameter(parameter)) {
6234+
errorReporter.atEntity(
6235+
errorTarget,
6236+
parameterElement.isPositional
6237+
? CompileTimeErrorCode
6238+
.MISSING_DEFAULT_VALUE_FOR_PARAMETER_POSITIONAL
6239+
: CompileTimeErrorCode
6240+
.MISSING_DEFAULT_VALUE_FOR_PARAMETER,
6241+
arguments: [parameterName?.lexeme ?? '?'],
6242+
);
6243+
}
62426244
}
62436245
}
62446246
}
@@ -6381,6 +6383,13 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
63816383
return false;
63826384
}
63836385

6386+
bool _isWildcardSuperFormalPositionalParameter(
6387+
DefaultFormalParameter parameter) =>
6388+
parameter.parameter is SuperFormalParameter &&
6389+
parameter.isPositional &&
6390+
parameter.name?.lexeme == '_' &&
6391+
_currentLibrary.featureSet.isEnabled(Feature.wildcard_variables);
6392+
63846393
/// Checks whether a `final`, `base` or `interface` modifier can be ignored.
63856394
///
63866395
/// Checks whether a subclass in the current library

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,6 +2715,23 @@ class B extends A {
27152715
]);
27162716
}
27172717

2718+
test_parameters_constructor_this_super_wildcard() async {
2719+
await assertErrorsInCode(r'''
2720+
class A {
2721+
final int x, y;
2722+
A(this.x, [this.y = 0]);
2723+
}
2724+
2725+
class C extends A {
2726+
final int _;
2727+
2728+
C(this._, super._, [super._]);
2729+
}
2730+
''', [
2731+
error(WarningCode.UNUSED_FIELD, 90, 1),
2732+
]);
2733+
}
2734+
27182735
test_parameters_functionTypeAlias() async {
27192736
await assertErrorsInCode(r'''
27202737
typedef void F(int a, double a);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,20 @@ class A<T extends Object?> {
720720
43, 1),
721721
]);
722722
}
723+
724+
test_super_forward_wildcards() async {
725+
await assertNoErrorsInCode('''
726+
class A {
727+
final int x, y;
728+
A(this.x, [this.y = 0]);
729+
}
730+
731+
class C extends A {
732+
final int c;
733+
C(this.c, super._, [super._]);
734+
}
735+
''');
736+
}
723737
}
724738

725739
@reflectiveTest

0 commit comments

Comments
 (0)