Skip to content

Commit 9453462

Browse files
scheglovCommit Queue
authored andcommitted
Move check for setter formal parameters to AstBuilder.
Now every setter after parsing has exactly one required positional formal parameter. This will cause setter elements have exactly one formal parameter, and let us avoid checking for it in multiple places. Remove CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER Add ParserErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER Change-Id: I80a6f3f6e51ac2ed2f6ddb49d059c2d78ff64b2e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/441830 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 9f72f10 commit 9453462

21 files changed

+437
-302
lines changed

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,8 +1662,6 @@ CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_OPERATOR_MINUS:
16621662
notes: |-
16631663
Fixes could include (1) removing all past the first parameter, and (2)
16641664
removing all but a singular used parameter, if only one is used.
1665-
CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER:
1666-
status: needsFix
16671665
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS:
16681666
status: hasFix
16691667
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_ANONYMOUS_FUNCTION:
@@ -3319,6 +3317,8 @@ ParserErrorCode.WITH_BEFORE_EXTENDS:
33193317
status: needsFix
33203318
notes: |-
33213319
Move the extends clause.
3320+
ParserErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER:
3321+
status: needsFix
33223322
ParserErrorCode.WRONG_SEPARATOR_FOR_POSITIONAL_PARAMETER:
33233323
status: hasFix
33243324
ParserErrorCode.WRONG_TERMINATOR_FOR_PARAMETER_GROUP:

pkg/analysis_server/test/lsp/completion_dart_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3306,8 +3306,8 @@ void f() {
33063306
.toList();
33073307
expect(setters, [
33083308
'stringSetter (String)',
3309-
'noArgSetter',
3310-
'multiArgSetter',
3309+
'noArgSetter (dynamic)',
3310+
'multiArgSetter (dynamic)',
33113311
// Because of how we extract the type name, we don't currently support
33123312
// this.
33133313
'functionSetter',

pkg/analyzer/lib/src/dart/ast/extensions.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ extension FormalParameterExtension on FormalParameter {
197197
}
198198
}
199199

200+
extension FormalParameterImplExtension on FormalParameterImpl {
201+
FormalParameterImpl get notDefault {
202+
var self = this;
203+
if (self is DefaultFormalParameterImpl) {
204+
return self.parameter;
205+
}
206+
return self;
207+
}
208+
}
209+
200210
// TODO(scheglov): https://github.com/dart-lang/sdk/issues/43608
201211
extension IdentifierExtension on Identifier {
202212
Element? get readElement {

pkg/analyzer/lib/src/dart/error/syntactic_errors.g.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,13 @@ class ParserErrorCode extends DiagnosticCode {
20382038
correctionMessage: "Try moving the extends clause before the with clause.",
20392039
);
20402040

2041+
static const ParserErrorCode WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER =
2042+
ParserErrorCode(
2043+
'WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER',
2044+
"Setters must declare exactly one required positional parameter.",
2045+
hasPublishedDocs: true,
2046+
);
2047+
20412048
static const ParserErrorCode
20422049
WRONG_SEPARATOR_FOR_POSITIONAL_PARAMETER = ParserErrorCode(
20432050
'WRONG_SEPARATOR_FOR_POSITIONAL_PARAMETER',

pkg/analyzer/lib/src/diagnostic/diagnostic_code_values.g.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,6 @@ const List<DiagnosticCode> diagnosticCodeValues = [
583583
CompileTimeErrorCode.WRONG_EXPLICIT_TYPE_PARAMETER_VARIANCE_IN_SUPERINTERFACE,
584584
CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_OPERATOR,
585585
CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_OPERATOR_MINUS,
586-
CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER,
587586
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS,
588587
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_ANONYMOUS_FUNCTION,
589588
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_CONSTRUCTOR,
@@ -924,6 +923,7 @@ const List<DiagnosticCode> diagnosticCodeValues = [
924923
ParserErrorCode.VAR_TYPEDEF,
925924
ParserErrorCode.VOID_WITH_TYPE_ARGUMENTS,
926925
ParserErrorCode.WITH_BEFORE_EXTENDS,
926+
ParserErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER,
927927
ParserErrorCode.WRONG_SEPARATOR_FOR_POSITIONAL_PARAMETER,
928928
ParserErrorCode.WRONG_TERMINATOR_FOR_PARAMETER_GROUP,
929929
PubspecWarningCode.ASSET_DIRECTORY_DOES_NOT_EXIST,

pkg/analyzer/lib/src/error/codes.g.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5879,14 +5879,6 @@ class CompileTimeErrorCode extends DiagnosticCode {
58795879
uniqueName: 'WRONG_NUMBER_OF_PARAMETERS_FOR_OPERATOR_MINUS',
58805880
);
58815881

5882-
/// No parameters.
5883-
static const CompileTimeErrorCode WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER =
5884-
CompileTimeErrorCode(
5885-
'WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER',
5886-
"Setters must declare exactly one required positional parameter.",
5887-
hasPublishedDocs: true,
5888-
);
5889-
58905882
/// Parameters:
58915883
/// 0: the name of the type being referenced (<i>G</i>)
58925884
/// 1: the number of type parameters that were declared

pkg/analyzer/lib/src/fasta/ast_builder.dart

Lines changed: 86 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,31 +1378,15 @@ class AstBuilder extends StackListener {
13781378
var bodyObject = pop();
13791379
pop(); // initializers
13801380
pop(); // separator
1381-
var parameters = pop() as FormalParameterListImpl?;
1381+
var formalParameters = pop() as FormalParameterListImpl?;
13821382
var typeParameters = pop() as TypeParameterListImpl?;
13831383
var name = pop();
13841384
var returnType = pop() as TypeAnnotationImpl?;
13851385
var modifiers = pop() as _Modifiers?;
13861386
var metadata = pop() as List<AnnotationImpl>?;
13871387
var comment = _findComment(metadata, beginToken);
13881388

1389-
assert(parameters != null || optional('get', getOrSet!));
1390-
1391-
FunctionBodyImpl body;
1392-
if (bodyObject is FunctionBodyImpl) {
1393-
body = bodyObject;
1394-
} else if (bodyObject is _RedirectingFactoryBody) {
1395-
body = EmptyFunctionBodyImpl(semicolon: endToken);
1396-
} else {
1397-
internalProblem(
1398-
templateInternalProblemUnhandled.withArguments(
1399-
"${bodyObject.runtimeType}",
1400-
"bodyObject",
1401-
),
1402-
beginToken.charOffset,
1403-
uri,
1404-
);
1405-
}
1389+
assert(formalParameters != null || optional('get', getOrSet!));
14061390

14071391
Token? operatorKeyword;
14081392
SimpleIdentifierImpl nameId;
@@ -1424,7 +1408,27 @@ class AstBuilder extends StackListener {
14241408
);
14251409
}
14261410

1427-
checkFieldFormalParameters(parameters);
1411+
if (getOrSet?.keyword == Keyword.SET) {
1412+
formalParameters = _ensureSetterFormalParameter(nameId, formalParameters);
1413+
}
1414+
1415+
FunctionBodyImpl body;
1416+
if (bodyObject is FunctionBodyImpl) {
1417+
body = bodyObject;
1418+
} else if (bodyObject is _RedirectingFactoryBody) {
1419+
body = EmptyFunctionBodyImpl(semicolon: endToken);
1420+
} else {
1421+
internalProblem(
1422+
templateInternalProblemUnhandled.withArguments(
1423+
"${bodyObject.runtimeType}",
1424+
"bodyObject",
1425+
),
1426+
beginToken.charOffset,
1427+
uri,
1428+
);
1429+
}
1430+
1431+
checkFieldFormalParameters(formalParameters);
14281432
_classLikeBuilder?.members.add(
14291433
MethodDeclarationImpl(
14301434
comment: comment,
@@ -1437,7 +1441,7 @@ class AstBuilder extends StackListener {
14371441
operatorKeyword: operatorKeyword,
14381442
name: nameId.token,
14391443
typeParameters: typeParameters,
1440-
parameters: parameters,
1444+
parameters: formalParameters,
14411445
body: body,
14421446
),
14431447
);
@@ -3615,7 +3619,7 @@ class AstBuilder extends StackListener {
36153619
debugEvent("TopLevelMethod");
36163620

36173621
var body = pop() as FunctionBodyImpl;
3618-
var parameters = pop() as FormalParameterListImpl?;
3622+
var formalParameters = pop() as FormalParameterListImpl?;
36193623
var typeParameters = pop() as TypeParameterListImpl?;
36203624
var name = pop() as SimpleIdentifierImpl;
36213625
var returnType = pop() as TypeAnnotationImpl?;
@@ -3624,6 +3628,11 @@ class AstBuilder extends StackListener {
36243628
var externalKeyword = modifiers?.externalKeyword;
36253629
var metadata = pop() as List<AnnotationImpl>?;
36263630
var comment = _findComment(metadata, beginToken);
3631+
3632+
if (getOrSet?.keyword == Keyword.SET) {
3633+
formalParameters = _ensureSetterFormalParameter(name, formalParameters);
3634+
}
3635+
36273636
declarations.add(
36283637
FunctionDeclarationImpl(
36293638
comment: comment,
@@ -3635,7 +3644,7 @@ class AstBuilder extends StackListener {
36353644
name: name.token,
36363645
functionExpression: FunctionExpressionImpl(
36373646
typeParameters: typeParameters,
3638-
parameters: parameters,
3647+
parameters: formalParameters,
36393648
body: body,
36403649
),
36413650
),
@@ -6229,6 +6238,61 @@ class AstBuilder extends StackListener {
62296238
return constructor;
62306239
}
62316240

6241+
FormalParameterListImpl? _ensureSetterFormalParameter(
6242+
SimpleIdentifierImpl setterName,
6243+
FormalParameterListImpl? formalParameters,
6244+
) {
6245+
formalParameters ??=
6246+
throw StateError('Parser has recovery, this never happens.');
6247+
6248+
var valueFormalParameter = formalParameters.parameters.firstOrNull;
6249+
if (valueFormalParameter == null) {
6250+
if (!formalParameters.leftParenthesis.isSynthetic) {
6251+
diagnosticReporter.diagnosticReporter?.atToken(
6252+
setterName.token,
6253+
ParserErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER,
6254+
);
6255+
}
6256+
var valueNameToken = parser.rewriter.insertSyntheticIdentifier(
6257+
formalParameters.leftParenthesis,
6258+
);
6259+
return FormalParameterListImpl(
6260+
leftParenthesis: formalParameters.leftParenthesis,
6261+
parameters: [
6262+
SimpleFormalParameterImpl(
6263+
comment: null,
6264+
metadata: null,
6265+
covariantKeyword: null,
6266+
requiredKeyword: null,
6267+
keyword: null,
6268+
type: null,
6269+
name: valueNameToken,
6270+
),
6271+
],
6272+
leftDelimiter: null,
6273+
rightDelimiter: null,
6274+
rightParenthesis: formalParameters.rightParenthesis,
6275+
);
6276+
}
6277+
6278+
if (valueFormalParameter.isRequiredPositional &&
6279+
formalParameters.parameters.length == 1) {
6280+
return formalParameters;
6281+
}
6282+
6283+
diagnosticReporter.diagnosticReporter?.atToken(
6284+
setterName.token,
6285+
ParserErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER,
6286+
);
6287+
return FormalParameterListImpl(
6288+
leftParenthesis: formalParameters.leftParenthesis,
6289+
parameters: [valueFormalParameter.notDefault],
6290+
leftDelimiter: null,
6291+
rightDelimiter: null,
6292+
rightParenthesis: formalParameters.rightParenthesis,
6293+
);
6294+
}
6295+
62326296
CommentImpl? _findComment(
62336297
List<AnnotationImpl>? metadata,
62346298
Token tokenAfterMetadata,

pkg/analyzer/lib/src/fasta/error_converter.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -500,14 +500,6 @@ class FastaErrorReporter {
500500
diagnosticCode: ScannerErrorCode.UNTERMINATED_STRING_LITERAL,
501501
);
502502
return;
503-
case "WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER":
504-
diagnosticReporter?.atOffset(
505-
offset: offset,
506-
length: length,
507-
diagnosticCode:
508-
CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER,
509-
);
510-
return;
511503
case "WRONG_SEPARATOR_FOR_POSITIONAL_PARAMETER":
512504
diagnosticReporter?.atOffset(
513505
offset: offset,

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -996,11 +996,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
996996
() {
997997
TypeAnnotation? returnType = node.returnType;
998998
if (node.isSetter) {
999-
FunctionExpression functionExpression = node.functionExpression;
1000-
_checkForWrongNumberOfParametersForSetter(
1001-
node.name,
1002-
functionExpression.parameters,
1003-
);
1004999
_checkForNonVoidReturnTypeForSetter(returnType);
10051000
}
10061001
_checkForTypeAnnotationDeferredClass(returnType);
@@ -1234,7 +1229,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
12341229
() {
12351230
var returnType = node.returnType;
12361231
if (node.isSetter) {
1237-
_checkForWrongNumberOfParametersForSetter(node.name, node.parameters);
12381232
_checkForNonVoidReturnTypeForSetter(returnType);
12391233
} else if (node.isOperator) {
12401234
var hasWrongNumberOfParameters =
@@ -5782,31 +5776,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
57825776
return false;
57835777
}
57845778

5785-
/// Verify that the given setter [parameterList] has only one required
5786-
/// parameter. The [setterName] is the name of the setter to report problems
5787-
/// on.
5788-
///
5789-
/// This method assumes that the method declaration was tested to be a setter
5790-
/// before being called.
5791-
///
5792-
/// See [CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER].
5793-
void _checkForWrongNumberOfParametersForSetter(
5794-
Token setterName,
5795-
FormalParameterList? parameterList,
5796-
) {
5797-
if (parameterList == null) {
5798-
return;
5799-
}
5800-
5801-
NodeList<FormalParameter> parameters = parameterList.parameters;
5802-
if (parameters.length != 1 || !parameters[0].isRequiredPositional) {
5803-
diagnosticReporter.atToken(
5804-
setterName,
5805-
CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER,
5806-
);
5807-
}
5808-
}
5809-
58105779
void _checkForWrongTypeParameterVarianceInField(FieldDeclarationImpl node) {
58115780
if (_enclosingClass != null) {
58125781
for (var typeParameter in _enclosingClass!.asElement.typeParameters) {

0 commit comments

Comments
 (0)