Skip to content

Commit e9a44ba

Browse files
Dan Rubelcommit-bot@chromium.org
authored andcommitted
fix extension instance field error
This changes EXTENSION_DECLARES_INSTANCE_FIELD to be a parser error and moves it into the parser so that it is generated for all of CFE. See #37945 Change-Id: Ib49d49a20d670e0dd7d9a4cd870237d2cf87dd00 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/114882 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Dan Rubel <[email protected]>
1 parent 15a7b57 commit e9a44ba

File tree

10 files changed

+63
-30
lines changed

10 files changed

+63
-30
lines changed

pkg/analyzer/lib/error/error.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ const List<ErrorCode> errorCodeValues = const [
138138
CompileTimeErrorCode.EXTENDS_NON_CLASS,
139139
CompileTimeErrorCode.EXTENSION_CONFLICTING_STATIC_AND_INSTANCE,
140140
CompileTimeErrorCode.EXTENSION_DECLARES_ABSTRACT_MEMBER,
141-
CompileTimeErrorCode.EXTENSION_DECLARES_INSTANCE_FIELD,
142141
CompileTimeErrorCode.EXTENSION_DECLARES_MEMBER_OF_OBJECT,
143142
CompileTimeErrorCode.EXTENSION_OVERRIDE_ACCESS_TO_STATIC_MEMBER,
144143
CompileTimeErrorCode.EXTENSION_OVERRIDE_ARGUMENT_NOT_ASSIGNABLE,
@@ -464,6 +463,7 @@ const List<ErrorCode> errorCodeValues = const [
464463
ParserErrorCode.EXPERIMENT_NOT_ENABLED,
465464
ParserErrorCode.EXPORT_DIRECTIVE_AFTER_PART_DIRECTIVE,
466465
ParserErrorCode.EXTENSION_DECLARES_CONSTRUCTOR,
466+
ParserErrorCode.EXTENSION_DECLARES_INSTANCE_FIELD,
467467
ParserErrorCode.EXTERNAL_AFTER_CONST,
468468
ParserErrorCode.EXTERNAL_AFTER_FACTORY,
469469
ParserErrorCode.EXTERNAL_AFTER_STATIC,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ class ParserErrorCode extends ErrorCode {
210210
static const ParserErrorCode EXTENSION_DECLARES_CONSTRUCTOR =
211211
_EXTENSION_DECLARES_CONSTRUCTOR;
212212

213+
static const ParserErrorCode EXTENSION_DECLARES_INSTANCE_FIELD =
214+
_EXTENSION_DECLARES_INSTANCE_FIELD;
215+
213216
static const ParserErrorCode EXTERNAL_AFTER_CONST = _MODIFIER_OUT_OF_ORDER;
214217

215218
static const ParserErrorCode EXTERNAL_AFTER_FACTORY = _MODIFIER_OUT_OF_ORDER;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ final fastaAnalyzerErrorCodes = <ErrorCode>[
100100
_INVALID_INITIALIZER,
101101
_ANNOTATION_WITH_TYPE_ARGUMENTS,
102102
_EXTENSION_DECLARES_CONSTRUCTOR,
103+
_EXTENSION_DECLARES_INSTANCE_FIELD,
103104
];
104105

105106
const ParserErrorCode _ABSTRACT_CLASS_MEMBER = const ParserErrorCode(
@@ -249,6 +250,12 @@ const ParserErrorCode _EXTENSION_DECLARES_CONSTRUCTOR = const ParserErrorCode(
249250
'EXTENSION_DECLARES_CONSTRUCTOR', r"Extensions can't declare constructors.",
250251
correction: "Try removing the constructor declaration.");
251252

253+
const ParserErrorCode _EXTENSION_DECLARES_INSTANCE_FIELD =
254+
const ParserErrorCode('EXTENSION_DECLARES_INSTANCE_FIELD',
255+
r"Extensions can't declare instance fields",
256+
correction:
257+
"Try removing the field declaration or making it a static field");
258+
252259
const ParserErrorCode _EXTERNAL_CLASS = const ParserErrorCode(
253260
'EXTERNAL_CLASS', r"Classes can't be declared to be 'external'.",
254261
correction: "Try removing the keyword 'external'.");

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,16 +1283,6 @@ class CompileTimeErrorCode extends ErrorCode {
12831283
"Extensions can't declare abstract members.",
12841284
correction: "Try providing an implementation for the member.");
12851285

1286-
/**
1287-
* No parameters.
1288-
*/
1289-
static const CompileTimeErrorCode EXTENSION_DECLARES_INSTANCE_FIELD =
1290-
const CompileTimeErrorCode('EXTENSION_DECLARES_INSTANCE_FIELD',
1291-
"Extensions can't declare instance fields.",
1292-
correction:
1293-
"Try removing the field declaration or making it a static "
1294-
"field.");
1295-
12961286
/**
12971287
* No parameters.
12981288
*/

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -946,17 +946,6 @@ class AstBuilder extends StackListener {
946946
Token covariantKeyword = covariantToken;
947947
List<Annotation> metadata = pop();
948948
Comment comment = _findComment(metadata, beginToken);
949-
if (extensionDeclaration != null && staticToken == null) {
950-
// TODO(brianwilkerson) Decide how to handle constructor and field
951-
// declarations within extensions. They are invalid, but we might want to
952-
// resolve them in order to get navigation, search, etc.
953-
for (VariableDeclaration variable in variables) {
954-
errorReporter.errorReporter.reportErrorForNode(
955-
CompileTimeErrorCode.EXTENSION_DECLARES_INSTANCE_FIELD,
956-
variable.name);
957-
}
958-
return;
959-
}
960949
currentDeclarationMembers.add(ast.fieldDeclaration2(
961950
comment: comment,
962951
metadata: metadata,
@@ -966,6 +955,32 @@ class AstBuilder extends StackListener {
966955
semicolon: semicolon));
967956
}
968957

958+
@override
959+
void endMixinFields(Token staticToken, Token covariantToken, Token lateToken,
960+
Token varFinalOrConst, int count, Token beginToken, Token endToken) {
961+
endClassFields(staticToken, covariantToken, lateToken, varFinalOrConst,
962+
count, beginToken, endToken);
963+
}
964+
965+
@override
966+
void endExtensionFields(
967+
Token staticToken,
968+
Token covariantToken,
969+
Token lateToken,
970+
Token varFinalOrConst,
971+
int count,
972+
Token beginToken,
973+
Token endToken) {
974+
if (staticToken == null) {
975+
// TODO(danrubel) Decide how to handle instance field declarations
976+
// within extensions. They are invalid and the parser has already reported
977+
// an error at this point, but we include them in order to get navigation,
978+
// search, etc.
979+
}
980+
endClassFields(staticToken, covariantToken, lateToken, varFinalOrConst,
981+
count, beginToken, endToken);
982+
}
983+
969984
@override
970985
void endForControlFlow(Token token) {
971986
debugEvent("endForControlFlow");

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/analysis/features.dart';
6-
import 'package:analyzer/src/error/codes.dart';
6+
import 'package:analyzer/src/dart/error/syntactic_errors.dart';
77
import 'package:analyzer/src/generated/engine.dart';
88
import 'package:test_reflective_loader/test_reflective_loader.dart';
99

@@ -27,11 +27,7 @@ class ExtensionDeclaresFieldTest extends DriverResolutionTest {
2727
extension E on String {
2828
String one, two, three;
2929
}
30-
''', [
31-
error(CompileTimeErrorCode.EXTENSION_DECLARES_INSTANCE_FIELD, 33, 3),
32-
error(CompileTimeErrorCode.EXTENSION_DECLARES_INSTANCE_FIELD, 38, 3),
33-
error(CompileTimeErrorCode.EXTENSION_DECLARES_INSTANCE_FIELD, 43, 5)
34-
]);
30+
''', [error(ParserErrorCode.EXTENSION_DECLARES_INSTANCE_FIELD, 33, 3)]);
3531
}
3632

3733
test_none() async {
@@ -45,7 +41,7 @@ extension E on String {}
4541
extension E on String {
4642
String s;
4743
}
48-
''', [error(CompileTimeErrorCode.EXTENSION_DECLARES_INSTANCE_FIELD, 33, 1)]);
44+
''', [error(ParserErrorCode.EXTENSION_DECLARES_INSTANCE_FIELD, 33, 1)]);
4945
}
5046

5147
test_static() async {

pkg/front_end/lib/src/fasta/fasta_codes_generated.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,6 +3400,17 @@ const MessageCode messageExtensionDeclaresConstructor = const MessageCode(
34003400
message: r"""Extensions can't declare constructors.""",
34013401
tip: r"""Try removing the constructor declaration.""");
34023402

3403+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
3404+
const Code<Null> codeExtensionDeclaresInstanceField =
3405+
messageExtensionDeclaresInstanceField;
3406+
3407+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
3408+
const MessageCode messageExtensionDeclaresInstanceField = const MessageCode(
3409+
"ExtensionDeclaresInstanceField",
3410+
index: 93,
3411+
message: r"""Extensions can't declare instance fields""",
3412+
tip: r"""Try removing the field declaration or making it a static field""");
3413+
34033414
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
34043415
const Code<Null> codeExternalClass = messageExternalClass;
34053416

pkg/front_end/lib/src/fasta/parser/parser.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2400,7 +2400,7 @@ class Parser {
24002400
IdentifierContext context = kind == DeclarationKind.TopLevel
24012401
? IdentifierContext.topLevelVariableDeclaration
24022402
: IdentifierContext.fieldDeclaration;
2403-
name = ensureIdentifier(token, context);
2403+
Token firstName = name = ensureIdentifier(token, context);
24042404

24052405
int fieldCount = 1;
24062406
token =
@@ -2446,6 +2446,10 @@ class Parser {
24462446
varFinalOrConst, fieldCount, beforeStart.next, token);
24472447
break;
24482448
case DeclarationKind.Extension:
2449+
if (staticToken == null) {
2450+
reportRecoverableError(
2451+
firstName, fasta.messageExtensionDeclaresInstanceField);
2452+
}
24492453
listener.endExtensionFields(staticToken, covariantToken, lateToken,
24502454
varFinalOrConst, fieldCount, beforeStart.next, token);
24512455
break;

pkg/front_end/messages.status

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ ExternalFactoryRedirection/example: Fail
206206
ExternalFactoryWithBody/part_wrapped_script1: Fail
207207
ExternalFactoryWithBody/script1: Fail
208208
ExtensionDeclaresConstructor/example: Fail
209+
ExtensionDeclaresInstanceField/example: Fail
209210
ExtraneousModifier/part_wrapped_script1: Fail
210211
ExtraneousModifier/part_wrapped_script2: Fail
211212
ExtraneousModifier/part_wrapped_script3: Fail

pkg/front_end/messages.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,12 @@ ExtensionDeclaresConstructor:
18711871
tip: "Try removing the constructor declaration."
18721872
analyzerCode: ParserErrorCode.EXTENSION_DECLARES_CONSTRUCTOR
18731873

1874+
ExtensionDeclaresInstanceField:
1875+
index: 93
1876+
template: "Extensions can't declare instance fields"
1877+
tip: "Try removing the field declaration or making it a static field"
1878+
analyzerCode: ParserErrorCode.EXTENSION_DECLARES_INSTANCE_FIELD
1879+
18741880
ConflictsWithConstructor:
18751881
template: "Conflicts with constructor '#name'."
18761882
analyzerCode: CONFLICTS_WITH_CONSTRUCTOR

0 commit comments

Comments
 (0)