Skip to content

Commit a945888

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Discard constructors and fields temporarily to get valid code compiling
Change-Id: If3e15a1f3164ee23a5e54e3a04b777de8e0189c9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108380 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 06c3d7a commit a945888

File tree

6 files changed

+154
-6
lines changed

6 files changed

+154
-6
lines changed

pkg/analyzer/lib/error/error.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ const List<ErrorCode> errorCodeValues = const [
135135
CompileTimeErrorCode.EXTENDS_DEFERRED_CLASS,
136136
CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS,
137137
CompileTimeErrorCode.EXTENDS_NON_CLASS,
138+
CompileTimeErrorCode.EXTENSION_DECLARES_CONSTRUCTOR,
139+
CompileTimeErrorCode.EXTENSION_DECLARES_FIELD,
138140
CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS,
139141
CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS_COULD_BE_NAMED,
140142
CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS,

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,22 @@ class CompileTimeErrorCode extends ErrorCode {
11741174
correction: "Try specifying a different superclass, or "
11751175
"removing the extends clause.");
11761176

1177+
/**
1178+
* No parameters.
1179+
*/
1180+
static const CompileTimeErrorCode EXTENSION_DECLARES_CONSTRUCTOR =
1181+
const CompileTimeErrorCode('EXTENSION_DECLARES_CONSTRUCTOR',
1182+
"Extensions can't declare constructors.",
1183+
correction: "Try removing the constructor declaration.");
1184+
1185+
/**
1186+
* No parameters.
1187+
*/
1188+
static const CompileTimeErrorCode EXTENSION_DECLARES_FIELD =
1189+
const CompileTimeErrorCode(
1190+
'EXTENSION_DECLARES_FIELD', "Extensions can't declare fields.",
1191+
correction: "Try removing the field declaration.");
1192+
11771193
/**
11781194
* 12.14.2 Binding Actuals to Formals: It is a static warning if <i>m &lt;
11791195
* h</i> or if <i>m &gt; n</i>.

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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:analyzer/src/dart/ast/ast.dart'
1515
CompilationUnitImpl,
1616
ExtensionDeclarationImpl,
1717
MixinDeclarationImpl;
18+
import 'package:analyzer/src/error/codes.dart';
1819
import 'package:analyzer/src/fasta/error_converter.dart';
1920
import 'package:analyzer/src/generated/utilities_dart.dart';
2021
import 'package:front_end/src/fasta/messages.dart'
@@ -563,12 +564,6 @@ class AstBuilder extends StackListener {
563564
push(ast.awaitExpression(awaitKeyword, pop()));
564565
}
565566

566-
void endInvalidAwaitExpression(
567-
Token awaitKeyword, Token endToken, MessageCode errorCode) {
568-
debugEvent("InvalidAwaitExpression");
569-
endAwaitExpression(awaitKeyword, endToken);
570-
}
571-
572567
@override
573568
void endBinaryExpression(Token operatorToken) {
574569
assert(operatorToken.isOperator ||
@@ -875,6 +870,15 @@ class AstBuilder extends StackListener {
875870
ast.simpleIdentifier(typeName.identifier.token, isDeclaration: true);
876871
}
877872

873+
if (extensionDeclaration != null) {
874+
// TODO(brianwilkerson) Decide how to handle constructor and field
875+
// declarations within extensions. They are invalid, but we might want to
876+
// resolve them in order to get navigation, search, etc.
877+
errorReporter.errorReporter.reportErrorForNode(
878+
CompileTimeErrorCode.EXTENSION_DECLARES_CONSTRUCTOR,
879+
name ?? returnType);
880+
return;
881+
}
878882
currentDeclarationMembers.add(ast.constructorDeclaration(
879883
comment,
880884
metadata,
@@ -917,6 +921,16 @@ class AstBuilder extends StackListener {
917921
Token covariantKeyword = covariantToken;
918922
List<Annotation> metadata = pop();
919923
Comment comment = _findComment(metadata, beginToken);
924+
if (extensionDeclaration != null) {
925+
// TODO(brianwilkerson) Decide how to handle constructor and field
926+
// declarations within extensions. They are invalid, but we might want to
927+
// resolve them in order to get navigation, search, etc.
928+
for (VariableDeclaration variable in variables) {
929+
errorReporter.errorReporter.reportErrorForNode(
930+
CompileTimeErrorCode.EXTENSION_DECLARES_FIELD, variable.name);
931+
}
932+
return;
933+
}
920934
currentDeclarationMembers.add(ast.fieldDeclaration2(
921935
comment: comment,
922936
metadata: metadata,
@@ -1338,6 +1352,12 @@ class AstBuilder extends StackListener {
13381352
push(initializers);
13391353
}
13401354

1355+
void endInvalidAwaitExpression(
1356+
Token awaitKeyword, Token endToken, MessageCode errorCode) {
1357+
debugEvent("InvalidAwaitExpression");
1358+
endAwaitExpression(awaitKeyword, endToken);
1359+
}
1360+
13411361
@override
13421362
void endLabeledStatement(int labelCount) {
13431363
debugEvent("LabeledStatement");
@@ -1522,6 +1542,15 @@ class AstBuilder extends StackListener {
15221542
initializers,
15231543
redirectedConstructor,
15241544
body);
1545+
if (extensionDeclaration != null) {
1546+
// TODO(brianwilkerson) Decide how to handle constructor and field
1547+
// declarations within extensions. They are invalid, but we might want
1548+
// to resolve them in order to get navigation, search, etc.
1549+
errorReporter.errorReporter.reportErrorForNode(
1550+
CompileTimeErrorCode.EXTENSION_DECLARES_CONSTRUCTOR,
1551+
name ?? prefixOrName);
1552+
return;
1553+
}
15251554
currentDeclarationMembers.add(constructor);
15261555
if (mixinDeclaration != null) {
15271556
// TODO (danrubel): Report an error if this is a mixin declaration.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/dart/analysis/features.dart';
6+
import 'package:analyzer/src/error/codes.dart';
7+
import 'package:analyzer/src/generated/engine.dart';
8+
import 'package:test_reflective_loader/test_reflective_loader.dart';
9+
10+
import '../dart/resolution/driver_resolution.dart';
11+
12+
main() {
13+
defineReflectiveSuite(() {
14+
defineReflectiveTests(ExtensionDeclaresConstructorTest);
15+
});
16+
}
17+
18+
@reflectiveTest
19+
class ExtensionDeclaresConstructorTest extends DriverResolutionTest {
20+
@override
21+
AnalysisOptionsImpl get analysisOptions => AnalysisOptionsImpl()
22+
..contextFeatures = new FeatureSet.forTesting(
23+
sdkVersion: '2.3.0', additionalFeatures: [Feature.extension_methods]);
24+
25+
test_named() {
26+
assertErrorsInCode('''
27+
extension E on String {
28+
E.named() : super();
29+
}
30+
''', [error(CompileTimeErrorCode.EXTENSION_DECLARES_CONSTRUCTOR, 28, 5)]);
31+
}
32+
33+
test_none() {
34+
assertNoErrorsInCode('''
35+
extension E on String {}
36+
''');
37+
}
38+
39+
test_unnamed() {
40+
assertErrorsInCode('''
41+
extension E on String {
42+
E() : super();
43+
}
44+
''', [error(CompileTimeErrorCode.EXTENSION_DECLARES_CONSTRUCTOR, 26, 1)]);
45+
}
46+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/dart/analysis/features.dart';
6+
import 'package:analyzer/src/error/codes.dart';
7+
import 'package:analyzer/src/generated/engine.dart';
8+
import 'package:test_reflective_loader/test_reflective_loader.dart';
9+
10+
import '../dart/resolution/driver_resolution.dart';
11+
12+
main() {
13+
defineReflectiveSuite(() {
14+
defineReflectiveTests(ExtensionDeclaresFieldTest);
15+
});
16+
}
17+
18+
@reflectiveTest
19+
class ExtensionDeclaresFieldTest extends DriverResolutionTest {
20+
@override
21+
AnalysisOptionsImpl get analysisOptions => AnalysisOptionsImpl()
22+
..contextFeatures = new FeatureSet.forTesting(
23+
sdkVersion: '2.3.0', additionalFeatures: [Feature.extension_methods]);
24+
25+
test_multiple() {
26+
assertErrorsInCode('''
27+
extension E on String {
28+
String one, two, three;
29+
}
30+
''', [
31+
error(CompileTimeErrorCode.EXTENSION_DECLARES_FIELD, 33, 3),
32+
error(CompileTimeErrorCode.EXTENSION_DECLARES_FIELD, 38, 3),
33+
error(CompileTimeErrorCode.EXTENSION_DECLARES_FIELD, 43, 5)
34+
]);
35+
}
36+
37+
test_none() {
38+
assertNoErrorsInCode('''
39+
extension E on String {}
40+
''');
41+
}
42+
43+
test_one() {
44+
assertErrorsInCode('''
45+
extension E on String {
46+
String s;
47+
}
48+
''', [error(CompileTimeErrorCode.EXTENSION_DECLARES_FIELD, 33, 1)]);
49+
}
50+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ import 'equal_elements_in_const_set_test.dart' as equal_elements_in_const_set;
3333
import 'equal_keys_in_const_map_test.dart' as equal_keys_in_const_map;
3434
import 'expression_in_map_test.dart' as expression_in_map;
3535
import 'extends_non_class_test.dart' as extends_non_class;
36+
import 'extension_declares_constructor_test.dart'
37+
as extension_declares_constructor;
38+
import 'extension_declares_field_test.dart' as extension_declares_field;
3639
import 'final_not_initialized_test.dart' as final_not_initialized;
3740
import 'implements_non_class_test.dart' as implements_non_class;
3841
import 'implicit_this_reference_in_initializer_test.dart'
@@ -197,6 +200,8 @@ main() {
197200
equal_keys_in_const_map.main();
198201
expression_in_map.main();
199202
extends_non_class.main();
203+
extension_declares_constructor.main();
204+
extension_declares_field.main();
200205
final_not_initialized.main();
201206
implements_non_class.main();
202207
implicit_this_reference_in_initializer.main();

0 commit comments

Comments
 (0)