Skip to content

Commit

Permalink
[wildcards] fix super formal positional param handling
Browse files Browse the repository at this point in the history
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 <brianwilkerson@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
  • Loading branch information
pq authored and Commit Queue committed Aug 16, 2024
1 parent 7600900 commit 4ef9223
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
20 changes: 13 additions & 7 deletions pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:collection';

import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/error/error.dart';
Expand Down Expand Up @@ -71,14 +72,10 @@ class DuplicateDefinitionVerifier {
// function type.

// Skip wildcard `super._`.
if (parameter is SuperFormalParameter &&
identifier.lexeme == '_' &&
_currentLibrary.hasWildcardVariablesFeatureEnabled) {
continue;
if (!_isSuperFormalWildcard(parameter, identifier)) {
_checkDuplicateIdentifier(definedNames, identifier,
element: parameter.declaredElement!);
}

_checkDuplicateIdentifier(definedNames, identifier,
element: parameter.declaredElement!);
}
}
}
Expand Down Expand Up @@ -266,6 +263,15 @@ class DuplicateDefinitionVerifier {
}
}

bool _isSuperFormalWildcard(FormalParameter parameter, Token identifier) {
if (parameter is DefaultFormalParameter) {
parameter = parameter.parameter;
}
return parameter is SuperFormalParameter &&
identifier.lexeme == '_' &&
_currentLibrary.featureSet.isEnabled(Feature.wildcard_variables);
}

bool _isWildCardFunction(FunctionDeclarationStatement statement) =>
statement.functionDeclaration.name.lexeme == '_' &&
_currentLibrary.hasWildcardVariablesFeatureEnabled;
Expand Down
27 changes: 18 additions & 9 deletions pkg/analyzer/lib/src/generated/error_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6230,15 +6230,17 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
.MISSING_DEFAULT_VALUE_FOR_PARAMETER_WITH_ANNOTATION,
);
} else {
errorReporter.atEntity(
errorTarget,
parameterElement.isPositional
? CompileTimeErrorCode
.MISSING_DEFAULT_VALUE_FOR_PARAMETER_POSITIONAL
: CompileTimeErrorCode
.MISSING_DEFAULT_VALUE_FOR_PARAMETER,
arguments: [parameterName?.lexeme ?? '?'],
);
if (!_isWildcardSuperFormalPositionalParameter(parameter)) {
errorReporter.atEntity(
errorTarget,
parameterElement.isPositional
? CompileTimeErrorCode
.MISSING_DEFAULT_VALUE_FOR_PARAMETER_POSITIONAL
: CompileTimeErrorCode
.MISSING_DEFAULT_VALUE_FOR_PARAMETER,
arguments: [parameterName?.lexeme ?? '?'],
);
}
}
}
}
Expand Down Expand Up @@ -6381,6 +6383,13 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
return false;
}

bool _isWildcardSuperFormalPositionalParameter(
DefaultFormalParameter parameter) =>
parameter.parameter is SuperFormalParameter &&
parameter.isPositional &&
parameter.name?.lexeme == '_' &&
_currentLibrary.featureSet.isEnabled(Feature.wildcard_variables);

/// Checks whether a `final`, `base` or `interface` modifier can be ignored.
///
/// Checks whether a subclass in the current library
Expand Down
17 changes: 17 additions & 0 deletions pkg/analyzer/test/src/diagnostics/duplicate_definition_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2715,6 +2715,23 @@ class B extends A {
]);
}

test_parameters_constructor_this_super_wildcard() async {
await assertErrorsInCode(r'''
class A {
final int x, y;
A(this.x, [this.y = 0]);
}
class C extends A {
final int _;
C(this._, super._, [super._]);
}
''', [
error(WarningCode.UNUSED_FIELD, 90, 1),
]);
}

test_parameters_functionTypeAlias() async {
await assertErrorsInCode(r'''
typedef void F(int a, double a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,20 @@ class A<T extends Object?> {
43, 1),
]);
}

test_super_forward_wildcards() async {
await assertNoErrorsInCode('''
class A {
final int x, y;
A(this.x, [this.y = 0]);
}
class C extends A {
final int c;
C(this.c, super._, [super._]);
}
''');
}
}

@reflectiveTest
Expand Down

0 comments on commit 4ef9223

Please sign in to comment.