Skip to content

Commit d47cd99

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Report NOT_NAMED_FIELD_IN_OBJECT_PATTERN
Change-Id: Iea58d3cef38c07f5fbac0dcbc8984ea727ffd93e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/293441 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 31095d7 commit d47cd99

File tree

9 files changed

+76
-20
lines changed

9 files changed

+76
-20
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ CompileTimeErrorCode.MISSING_DEFAULT_VALUE_FOR_PARAMETER_POSITIONAL:
857857
status: hasFix
858858
CompileTimeErrorCode.MISSING_DEFAULT_VALUE_FOR_PARAMETER_WITH_ANNOTATION:
859859
status: hasFix
860-
CompileTimeErrorCode.MISSING_OBJECT_PATTERN_GETTER_NAME:
860+
CompileTimeErrorCode.MISSING_NAMED_PATTERN_FIELD_NAME:
861861
status: noFix
862862
CompileTimeErrorCode.MISSING_REQUIRED_ARGUMENT:
863863
status: hasFix
@@ -1078,6 +1078,8 @@ CompileTimeErrorCode.PATTERN_VARIABLE_SHARED_CASE_SCOPE_HAS_LABEL:
10781078
status: needsEvaluation
10791079
CompileTimeErrorCode.PATTERN_VARIABLE_SHARED_CASE_SCOPE_NOT_ALL_CASES:
10801080
status: needsEvaluation
1081+
CompileTimeErrorCode.POSITIONAL_FIELD_IN_OBJECT_PATTERN:
1082+
status: needsEvaluation
10811083
CompileTimeErrorCode.POSITIONAL_SUPER_FORMAL_PARAMETER_WITH_POSITIONAL_ARGUMENT:
10821084
status: needsFix
10831085
since: 2.17

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10020,7 +10020,10 @@ class ObjectPatternImpl extends DartPatternImpl implements ObjectPattern {
1002010020
final result = resolverVisitor.analyzeObjectPattern(
1002110021
context,
1002210022
this,
10023-
fields: resolverVisitor.buildSharedPatternFields(fields),
10023+
fields: resolverVisitor.buildSharedPatternFields(
10024+
fields,
10025+
mustBeNamed: true,
10026+
),
1002410027
);
1002510028

1002610029
resolverVisitor.checkPatternNeverMatchesValueType(
@@ -11123,7 +11126,10 @@ class RecordPatternImpl extends DartPatternImpl implements RecordPattern {
1112311126
@override
1112411127
DartType computePatternSchema(ResolverVisitor resolverVisitor) {
1112511128
return resolverVisitor.analyzeRecordPatternSchema(
11126-
fields: resolverVisitor.buildSharedPatternFields(fields),
11129+
fields: resolverVisitor.buildSharedPatternFields(
11130+
fields,
11131+
mustBeNamed: false,
11132+
),
1112711133
);
1112811134
}
1112911135

@@ -11135,7 +11141,10 @@ class RecordPatternImpl extends DartPatternImpl implements RecordPattern {
1113511141
resolverVisitor.analyzeRecordPattern(
1113611142
context,
1113711143
this,
11138-
fields: resolverVisitor.buildSharedPatternFields(fields),
11144+
fields: resolverVisitor.buildSharedPatternFields(
11145+
fields,
11146+
mustBeNamed: false,
11147+
),
1113911148
);
1114011149
}
1114111150

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,15 +2926,14 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
29262926
);
29272927

29282928
/// No parameters.
2929-
static const CompileTimeErrorCode MISSING_OBJECT_PATTERN_GETTER_NAME =
2929+
static const CompileTimeErrorCode MISSING_NAMED_PATTERN_FIELD_NAME =
29302930
CompileTimeErrorCode(
2931-
'MISSING_OBJECT_PATTERN_GETTER_NAME',
2931+
'MISSING_NAMED_PATTERN_FIELD_NAME',
29322932
"The getter name is not specified explicitly, and the pattern is not a "
29332933
"variable.",
29342934
correctionMessage:
29352935
"Try specifying the getter name explicitly, or using a variable "
29362936
"pattern.",
2937-
hasPublishedDocs: true,
29382937
);
29392938

29402939
/// Parameters:
@@ -3993,6 +3992,14 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
39933992
uniqueName: 'PATTERN_VARIABLE_SHARED_CASE_SCOPE_NOT_ALL_CASES',
39943993
);
39953994

3995+
/// No parameters.
3996+
static const CompileTimeErrorCode POSITIONAL_FIELD_IN_OBJECT_PATTERN =
3997+
CompileTimeErrorCode(
3998+
'POSITIONAL_FIELD_IN_OBJECT_PATTERN',
3999+
"Object patterns can only use named fields.",
4000+
correctionMessage: "Try specifying the field name.",
4001+
);
4002+
39964003
/// No parameters.
39974004
static const CompileTimeErrorCode
39984005
POSITIONAL_SUPER_FORMAL_PARAMETER_WITH_POSITIONAL_ARGUMENT =

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ const List<ErrorCode> errorCodeValues = [
305305
CompileTimeErrorCode.MISSING_DEFAULT_VALUE_FOR_PARAMETER,
306306
CompileTimeErrorCode.MISSING_DEFAULT_VALUE_FOR_PARAMETER_POSITIONAL,
307307
CompileTimeErrorCode.MISSING_DEFAULT_VALUE_FOR_PARAMETER_WITH_ANNOTATION,
308-
CompileTimeErrorCode.MISSING_OBJECT_PATTERN_GETTER_NAME,
308+
CompileTimeErrorCode.MISSING_NAMED_PATTERN_FIELD_NAME,
309309
CompileTimeErrorCode.MISSING_REQUIRED_ARGUMENT,
310310
CompileTimeErrorCode.MISSING_VARIABLE_PATTERN,
311311
CompileTimeErrorCode.MIXINS_SUPER_CLASS,
@@ -405,6 +405,7 @@ const List<ErrorCode> errorCodeValues = [
405405
.PATTERN_VARIABLE_SHARED_CASE_SCOPE_DIFFERENT_FINALITY_OR_TYPE,
406406
CompileTimeErrorCode.PATTERN_VARIABLE_SHARED_CASE_SCOPE_HAS_LABEL,
407407
CompileTimeErrorCode.PATTERN_VARIABLE_SHARED_CASE_SCOPE_NOT_ALL_CASES,
408+
CompileTimeErrorCode.POSITIONAL_FIELD_IN_OBJECT_PATTERN,
408409
CompileTimeErrorCode
409410
.POSITIONAL_SUPER_FORMAL_PARAMETER_WITH_POSITIONAL_ARGUMENT,
410411
CompileTimeErrorCode.PREFIX_COLLIDES_WITH_TOP_LEVEL_MEMBER,

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,9 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
526526
}
527527

528528
List<SharedPatternField> buildSharedPatternFields(
529-
List<PatternFieldImpl> fields,
530-
) {
529+
List<PatternFieldImpl> fields, {
530+
required bool mustBeNamed,
531+
}) {
531532
return fields.map((field) {
532533
Token? nameToken;
533534
var fieldName = field.name;
@@ -537,11 +538,16 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
537538
nameToken = field.pattern.variablePattern?.name;
538539
if (nameToken == null) {
539540
errorReporter.reportErrorForNode(
540-
CompileTimeErrorCode.MISSING_OBJECT_PATTERN_GETTER_NAME,
541+
CompileTimeErrorCode.MISSING_NAMED_PATTERN_FIELD_NAME,
541542
field,
542543
);
543544
}
544545
}
546+
} else if (mustBeNamed) {
547+
errorReporter.reportErrorForNode(
548+
CompileTimeErrorCode.POSITIONAL_FIELD_IN_OBJECT_PATTERN,
549+
field,
550+
);
545551
}
546552
return shared.RecordPatternField(
547553
node: field,
@@ -1605,10 +1611,6 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
16051611
var nameToken = fieldNode.name?.name;
16061612
nameToken ??= field.pattern.variablePattern?.name;
16071613
if (nameToken == null) {
1608-
errorReporter.reportErrorForNode(
1609-
CompileTimeErrorCode.MISSING_OBJECT_PATTERN_GETTER_NAME,
1610-
fieldNode,
1611-
);
16121614
return typeProvider.dynamicType;
16131615
}
16141616

pkg/analyzer/messages.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9245,10 +9245,10 @@ CompileTimeErrorCode:
92459245
correctionMessage: "Try removing the '@'."
92469246
hasPublishedDocs: true
92479247
comment: No parameters.
9248-
MISSING_OBJECT_PATTERN_GETTER_NAME:
9248+
MISSING_NAMED_PATTERN_FIELD_NAME:
92499249
problemMessage: The getter name is not specified explicitly, and the pattern is not a variable.
92509250
correctionMessage: Try specifying the getter name explicitly, or using a variable pattern.
9251-
hasPublishedDocs: true
9251+
hasPublishedDocs: false
92529252
comment: No parameters.
92539253
documentation: |-
92549254
#### Description
@@ -12734,6 +12734,10 @@ CompileTimeErrorCode:
1273412734
comment: |-
1273512735
Parameters:
1273612736
0: the name of the pattern variable
12737+
POSITIONAL_FIELD_IN_OBJECT_PATTERN:
12738+
problemMessage: Object patterns can only use named fields.
12739+
correctionMessage: Try specifying the field name.
12740+
comment: No parameters.
1273712741
POSITIONAL_SUPER_FORMAL_PARAMETER_WITH_POSITIONAL_ARGUMENT:
1273812742
problemMessage: Positional super parameters can't be used when the super constructor invocation has a positional argument.
1273912743
correctionMessage: Try making all the positional parameters passed to the super constructor be either all super parameters or all normal parameters.

pkg/analyzer/test/src/dart/resolution/object_pattern_test.dart

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ void f(x) {
426426
}
427427
}
428428
''', [
429-
error(CompileTimeErrorCode.MISSING_OBJECT_PATTERN_GETTER_NAME, 75, 3),
429+
error(CompileTimeErrorCode.MISSING_NAMED_PATTERN_FIELD_NAME, 75, 3),
430430
]);
431431
final node = findNode.singleGuardedPattern.pattern;
432432
assertResolvedNodeText(node, r'''
@@ -677,6 +677,37 @@ ObjectPattern
677677
''');
678678
}
679679

680+
test_class_notGeneric_positionalField() async {
681+
await assertErrorsInCode(r'''
682+
void f(Object? x) {
683+
if (x case Object(0)) {}
684+
}
685+
''', [
686+
error(CompileTimeErrorCode.POSITIONAL_FIELD_IN_OBJECT_PATTERN, 40, 1),
687+
]);
688+
final node = findNode.singleGuardedPattern.pattern;
689+
assertResolvedNodeText(node, r'''
690+
ObjectPattern
691+
type: NamedType
692+
name: SimpleIdentifier
693+
token: Object
694+
staticElement: dart:core::@class::Object
695+
staticType: null
696+
type: Object
697+
leftParenthesis: (
698+
fields
699+
PatternField
700+
pattern: ConstantPattern
701+
expression: IntegerLiteral
702+
literal: 0
703+
staticType: int
704+
matchedValueType: dynamic
705+
element: <null>
706+
rightParenthesis: )
707+
matchedValueType: Object?
708+
''');
709+
}
710+
680711
test_class_notGeneric_unresolved_hasName() async {
681712
await assertErrorsInCode(r'''
682713
abstract class A {}

pkg/analyzer/test/src/dart/resolution/record_pattern_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ void f(({int foo}) x) {
629629
}
630630
}
631631
''', [
632-
error(CompileTimeErrorCode.MISSING_OBJECT_PATTERN_GETTER_NAME, 49, 3),
632+
error(CompileTimeErrorCode.MISSING_NAMED_PATTERN_FIELD_NAME, 49, 3),
633633
]);
634634
final node = findNode.singleGuardedPattern.pattern;
635635
assertResolvedNodeText(node, r'''

pkg/analyzer/tool/diagnostics/diagnostics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12154,7 +12154,7 @@ dependencies:
1215412154
meta: ^1.0.2
1215512155
```
1215612156

12157-
### missing_object_pattern_getter_name
12157+
### missing_named_pattern_field_name
1215812158

1215912159
_The getter name is not specified explicitly, and the pattern is not a
1216012160
variable._

0 commit comments

Comments
 (0)