Skip to content

Commit 8796382

Browse files
committed
Analyzer: Move invalid_super_invocation to compile-time error
Change-Id: Id8be8473b2516cfc15c226edaaf25dadf3a90d3d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155549 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent cece303 commit 8796382

File tree

8 files changed

+62
-21
lines changed

8 files changed

+62
-21
lines changed

pkg/analyzer/lib/error/error.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ const List<ErrorCode> errorCodeValues = [
212212
CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER,
213213
CompileTimeErrorCode.INVALID_OVERRIDE,
214214
CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS,
215+
CompileTimeErrorCode.INVALID_SUPER_INVOCATION,
215216
CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_LIST,
216217
CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_MAP,
217218
CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_SET,
@@ -838,7 +839,6 @@ const List<ErrorCode> errorCodeValues = [
838839
StrongModeCode.IMPLICIT_DYNAMIC_TYPE,
839840
StrongModeCode.IMPLICIT_DYNAMIC_VARIABLE,
840841
StrongModeCode.INVALID_PARAMETER_DECLARATION,
841-
StrongModeCode.INVALID_SUPER_INVOCATION,
842842
StrongModeCode.TOP_LEVEL_CYCLE,
843843
StrongModeCode.TOP_LEVEL_FUNCTION_LITERAL_BLOCK,
844844
StrongModeCode.TOP_LEVEL_IDENTIFIER_NO_TYPE,

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3773,6 +3773,10 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
37733773
"Invalid reference to 'this' expression.",
37743774
hasPublishedDocs: true);
37753775

3776+
static const CompileTimeErrorCode INVALID_SUPER_INVOCATION =
3777+
CompileTimeErrorCode('INVALID_SUPER_INVOCATION',
3778+
"The super call must be last in an initializer list: '{0}'.");
3779+
37763780
/**
37773781
* 12.6 Lists: It is a compile time error if the type argument of a constant
37783782
* list literal includes a type parameter.
@@ -10787,12 +10791,6 @@ class StrongModeCode extends ErrorCode {
1078710791
'COULD_NOT_INFER',
1078810792
"Couldn't infer type parameter '{0}'.{1}");
1078910793

10790-
static const StrongModeCode INVALID_SUPER_INVOCATION = StrongModeCode(
10791-
ErrorType.COMPILE_TIME_ERROR,
10792-
'INVALID_SUPER_INVOCATION',
10793-
"The super call must be last in an initializer "
10794-
"list (see https://goo.gl/EY6hDP): '{0}'.");
10795-
1079610794
static const StrongModeCode IMPLICIT_DYNAMIC_PARAMETER = StrongModeCode(
1079710795
ErrorType.COMPILE_TIME_ERROR,
1079810796
'IMPLICIT_DYNAMIC_PARAMETER',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class FastaErrorReporter {
197197
return;
198198
case "INVALID_SUPER_INVOCATION":
199199
errorReporter?.reportErrorForOffset(
200-
StrongModeCode.INVALID_SUPER_INVOCATION, offset, length);
200+
CompileTimeErrorCode.INVALID_SUPER_INVOCATION, offset, length);
201201
return;
202202
case "MISSING_DIGIT":
203203
errorReporter?.reportErrorForOffset(

pkg/analyzer/lib/src/task/strong/checker.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,12 @@ class CodeChecker extends RecursiveAstVisitor {
307307
for (int i = 0, last = init.length - 1; i < last; i++) {
308308
final node = init[i];
309309
if (node is SuperConstructorInvocation) {
310-
_recordMessage(node, StrongModeCode.INVALID_SUPER_INVOCATION, [node]);
310+
// TODO(srawlins): Don't report this when
311+
// [CompileTimeErrorCode.SUPER_IN_REDIRECTING_CONSTRUCTOR] or
312+
// [CompileTimeErrorCode.MULTIPLE_SUPER_INITIALIZERS] is reported for
313+
// this constructor.
314+
_recordMessage(
315+
node, CompileTimeErrorCode.INVALID_SUPER_INVOCATION, [node]);
311316
}
312317
}
313318
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2020, 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/src/error/codes.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/driver_resolution.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(InvalidSuperInvocationTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class InvalidSuperInvocationTest extends DriverResolutionTest {
18+
test_superBeforeAssert() async {
19+
await assertErrorsInCode(r'''
20+
class A {
21+
A(int x) : super(), assert(x != null);
22+
}
23+
''', [
24+
error(CompileTimeErrorCode.INVALID_SUPER_INVOCATION, 23, 7),
25+
]);
26+
}
27+
28+
test_superBeforeAssignment() async {
29+
await assertErrorsInCode(r'''
30+
class A {
31+
final int x;
32+
A() : super(), x = 1;
33+
}
34+
''', [
35+
error(CompileTimeErrorCode.INVALID_SUPER_INVOCATION, 33, 7),
36+
]);
37+
}
38+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class B extends A {
2222
B() : super(), super() {}
2323
}
2424
''', [
25-
error(StrongModeCode.INVALID_SUPER_INVOCATION, 39, 7),
25+
error(CompileTimeErrorCode.INVALID_SUPER_INVOCATION, 39, 7),
2626
error(CompileTimeErrorCode.MULTIPLE_SUPER_INITIALIZERS, 48, 7),
2727
]);
2828
}

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,24 @@ main() {
1717
class SuperInRedirectingConstructorTest extends DriverResolutionTest {
1818
test_redirectionSuper() async {
1919
await assertErrorsInCode(r'''
20-
class A {}
21-
class B {
22-
B() : this.name(), super();
23-
B.name() {}
20+
class A {
21+
A() : this.name(), super();
22+
A.name() {}
2423
}
2524
''', [
26-
error(CompileTimeErrorCode.SUPER_IN_REDIRECTING_CONSTRUCTOR, 42, 7),
25+
error(CompileTimeErrorCode.SUPER_IN_REDIRECTING_CONSTRUCTOR, 31, 7),
2726
]);
2827
}
2928

3029
test_superRedirection() async {
3130
await assertErrorsInCode(r'''
32-
class A {}
33-
class B {
34-
B() : super(), this.name();
35-
B.name() {}
31+
class A {
32+
A() : super(), this.name();
33+
A.name() {}
3634
}
3735
''', [
38-
error(StrongModeCode.INVALID_SUPER_INVOCATION, 29, 7),
39-
error(CompileTimeErrorCode.SUPER_IN_REDIRECTING_CONSTRUCTOR, 29, 7),
36+
error(CompileTimeErrorCode.INVALID_SUPER_INVOCATION, 18, 7),
37+
error(CompileTimeErrorCode.SUPER_IN_REDIRECTING_CONSTRUCTOR, 18, 7),
4038
]);
4139
}
4240
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ import 'invalid_required_optional_positional_param_test.dart'
280280
import 'invalid_required_positional_param_test.dart'
281281
as invalid_required_positional_param;
282282
import 'invalid_sealed_annotation_test.dart' as invalid_sealed_annotation;
283+
import 'invalid_super_invocation_test.dart' as invalid_super_invocation;
283284
import 'invalid_type_argument_in_const_list_test.dart'
284285
as invalid_type_argument_in_const_list;
285286
import 'invalid_type_argument_in_const_map_test.dart'
@@ -785,6 +786,7 @@ main() {
785786
invalid_required_optional_positional_param.main();
786787
invalid_required_positional_param.main();
787788
invalid_sealed_annotation.main();
789+
invalid_super_invocation.main();
788790
invalid_type_argument_in_const_list.main();
789791
invalid_type_argument_in_const_map.main();
790792
invalid_type_argument_in_const_set.main();

0 commit comments

Comments
 (0)