Skip to content

Commit 112f987

Browse files
chloestefantsovaCommit Bot
authored and
Commit Bot
committed
[cfe] Report errors on super parameters not in a constructor
Part of #47525 Change-Id: I9ac7f5a49c09755445b6b407af5abf1037cba2e0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/237687 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent da691ea commit 112f987

13 files changed

+311
-0
lines changed

pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9771,6 +9771,17 @@ const MessageCode messageSuperNullAware = const MessageCode("SuperNullAware",
97719771
r"""The operator '?.' cannot be used with 'super' because 'super' cannot be null.""",
97729772
correctionMessage: r"""Try replacing '?.' with '.'""");
97739773

9774+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
9775+
const Code<Null> codeSuperParameterInitializerOutsideConstructor =
9776+
messageSuperParameterInitializerOutsideConstructor;
9777+
9778+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
9779+
const MessageCode messageSuperParameterInitializerOutsideConstructor =
9780+
const MessageCode("SuperParameterInitializerOutsideConstructor",
9781+
problemMessage:
9782+
r"""Super-initializer formal parameters can only be used in generative constructors.""",
9783+
correctionMessage: r"""Try removing 'super.'.""");
9784+
97749785
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
97759786
const Template<Message Function(String name)>
97769787
templateSuperclassHasNoConstructor =

pkg/front_end/lib/src/fasta/kernel/body_builder.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4352,6 +4352,15 @@ class BodyBuilder extends StackListenerImpl
43524352
thisKeyword = null;
43534353
}
43544354
}
4355+
if (superKeyword != null) {
4356+
if (!inConstructor) {
4357+
handleRecoverableError(
4358+
fasta.messageSuperParameterInitializerOutsideConstructor,
4359+
superKeyword,
4360+
superKeyword);
4361+
superKeyword = null;
4362+
}
4363+
}
43554364
Object? nameNode = pop();
43564365
TypeBuilder? type = pop() as TypeBuilder?;
43574366
if (functionNestingLevel == 0 && type != null) {

pkg/front_end/messages.status

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,8 @@ StrongWithWeakDillLibrary/spelling: Fail
809809
SuperAsExpression/example: Fail
810810
SuperAsIdentifier/example: Fail
811811
SuperNullAware/example: Fail
812+
SuperParameterInitializerOutsideConstructor/analyzerCode: Fail
813+
SuperParameterInitializerOutsideConstructor/example: Fail
812814
SuperclassHasNoDefaultConstructor/example: Fail
813815
SuperclassHasNoGetter/example: Fail
814816
SuperclassHasNoMember/example: Fail

pkg/front_end/messages.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,10 @@ FieldInitializerOutsideConstructor:
17541754
script:
17551755
- "class C { void m(this.x); }"
17561756

1757+
SuperParameterInitializerOutsideConstructor:
1758+
problemMessage: "Super-initializer formal parameters can only be used in generative constructors."
1759+
correctionMessage: "Try removing 'super.'."
1760+
17571761
RedirectionTargetNotFound:
17581762
problemMessage: "Redirection constructor target not found: '#name'"
17591763
analyzerCode: REDIRECT_TO_MISSING_CONSTRUCTOR
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2022, 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+
class A {
6+
A(int x);
7+
}
8+
9+
class B extends A {
10+
B() : super(42);
11+
factory B.foo(super.x) => new B();
12+
}
13+
14+
foo(super.x) {}
15+
16+
class C {
17+
void set foo(super.value) {}
18+
}
19+
20+
main() {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:11:17: Error: Super-initializer formal parameters can only be used in generative constructors.
6+
// Try removing 'super.'.
7+
// factory B.foo(super.x) => new B();
8+
// ^^^^^
9+
//
10+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:14:5: Error: Super-initializer formal parameters can only be used in generative constructors.
11+
// Try removing 'super.'.
12+
// foo(super.x) {}
13+
// ^^^^^
14+
//
15+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:17:16: Error: Super-initializer formal parameters can only be used in generative constructors.
16+
// Try removing 'super.'.
17+
// void set foo(super.value) {}
18+
// ^^^^^
19+
//
20+
import self as self;
21+
import "dart:core" as core;
22+
23+
class A extends core::Object {
24+
constructor •(core::int x) → self::A
25+
: super core::Object::•()
26+
;
27+
}
28+
class B extends self::A {
29+
constructor •() → self::B
30+
: super self::A::•(42)
31+
;
32+
static factory foo(dynamic x) → self::B
33+
return new self::B::•();
34+
}
35+
class C extends core::Object {
36+
synthetic constructor •() → self::C
37+
: super core::Object::•()
38+
;
39+
set foo(dynamic value) → void {}
40+
}
41+
static method foo(dynamic x) → dynamic {}
42+
static method main() → dynamic {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:11:17: Error: Super-initializer formal parameters can only be used in generative constructors.
6+
// Try removing 'super.'.
7+
// factory B.foo(super.x) => new B();
8+
// ^^^^^
9+
//
10+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:14:5: Error: Super-initializer formal parameters can only be used in generative constructors.
11+
// Try removing 'super.'.
12+
// foo(super.x) {}
13+
// ^^^^^
14+
//
15+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:17:16: Error: Super-initializer formal parameters can only be used in generative constructors.
16+
// Try removing 'super.'.
17+
// void set foo(super.value) {}
18+
// ^^^^^
19+
//
20+
import self as self;
21+
import "dart:core" as core;
22+
23+
class A extends core::Object {
24+
constructor •(core::int x) → self::A
25+
: super core::Object::•()
26+
;
27+
}
28+
class B extends self::A {
29+
constructor •() → self::B
30+
: super self::A::•(42)
31+
;
32+
static factory foo(dynamic x) → self::B
33+
return new self::B::•();
34+
}
35+
class C extends core::Object {
36+
synthetic constructor •() → self::C
37+
: super core::Object::•()
38+
;
39+
set foo(dynamic value) → void {}
40+
}
41+
static method foo(dynamic x) → dynamic {}
42+
static method main() → dynamic {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class A {
2+
A(int x);
3+
}
4+
5+
class B extends A {
6+
B() : super(42);
7+
factory B.foo(super.x) => new B();
8+
}
9+
10+
foo(super.x) {}
11+
12+
class C {
13+
void set foo(super.value) {}
14+
}
15+
16+
main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class A {
2+
A(int x);
3+
}
4+
5+
class B extends A {
6+
B() : super(42);
7+
factory B.foo(super.x) => new B();
8+
}
9+
10+
class C {
11+
void set foo(super.value) {}
12+
}
13+
14+
foo(super.x) {}
15+
main() {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:11:17: Error: Super-initializer formal parameters can only be used in generative constructors.
6+
// Try removing 'super.'.
7+
// factory B.foo(super.x) => new B();
8+
// ^^^^^
9+
//
10+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:14:5: Error: Super-initializer formal parameters can only be used in generative constructors.
11+
// Try removing 'super.'.
12+
// foo(super.x) {}
13+
// ^^^^^
14+
//
15+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:17:16: Error: Super-initializer formal parameters can only be used in generative constructors.
16+
// Try removing 'super.'.
17+
// void set foo(super.value) {}
18+
// ^^^^^
19+
//
20+
import self as self;
21+
import "dart:core" as core;
22+
23+
class A extends core::Object {
24+
constructor •(core::int x) → self::A
25+
: super core::Object::•()
26+
;
27+
}
28+
class B extends self::A {
29+
constructor •() → self::B
30+
: super self::A::•(42)
31+
;
32+
static factory foo(dynamic x) → self::B
33+
return new self::B::•();
34+
}
35+
class C extends core::Object {
36+
synthetic constructor •() → self::C
37+
: super core::Object::•()
38+
;
39+
set foo(dynamic value) → void {}
40+
}
41+
static method foo(dynamic x) → dynamic {}
42+
static method main() → dynamic {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:11:17: Error: Super-initializer formal parameters can only be used in generative constructors.
6+
// Try removing 'super.'.
7+
// factory B.foo(super.x) => new B();
8+
// ^^^^^
9+
//
10+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:14:5: Error: Super-initializer formal parameters can only be used in generative constructors.
11+
// Try removing 'super.'.
12+
// foo(super.x) {}
13+
// ^^^^^
14+
//
15+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:17:16: Error: Super-initializer formal parameters can only be used in generative constructors.
16+
// Try removing 'super.'.
17+
// void set foo(super.value) {}
18+
// ^^^^^
19+
//
20+
import self as self;
21+
import "dart:core" as core;
22+
23+
class A extends core::Object {
24+
constructor •(core::int x) → self::A
25+
: super core::Object::•()
26+
;
27+
}
28+
class B extends self::A {
29+
constructor •() → self::B
30+
: super self::A::•(42)
31+
;
32+
static factory foo(dynamic x) → self::B
33+
return new self::B::•();
34+
}
35+
class C extends core::Object {
36+
synthetic constructor •() → self::C
37+
: super core::Object::•()
38+
;
39+
set foo(dynamic value) → void {}
40+
}
41+
static method foo(dynamic x) → dynamic {}
42+
static method main() → dynamic {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class A extends core::Object {
6+
constructor •(core::int x) → self::A
7+
;
8+
}
9+
class B extends self::A {
10+
constructor •() → self::B
11+
;
12+
static factory foo(dynamic x) → self::B
13+
;
14+
}
15+
class C extends core::Object {
16+
synthetic constructor •() → self::C
17+
;
18+
set foo(dynamic value) → void
19+
;
20+
}
21+
static method foo(dynamic x) → dynamic
22+
;
23+
static method main() → dynamic
24+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:11:17: Error: Super-initializer formal parameters can only be used in generative constructors.
6+
// Try removing 'super.'.
7+
// factory B.foo(super.x) => new B();
8+
// ^^^^^
9+
//
10+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:14:5: Error: Super-initializer formal parameters can only be used in generative constructors.
11+
// Try removing 'super.'.
12+
// foo(super.x) {}
13+
// ^^^^^
14+
//
15+
// pkg/front_end/testcases/super_parameters/super_parameters_outside_constructor.dart:17:16: Error: Super-initializer formal parameters can only be used in generative constructors.
16+
// Try removing 'super.'.
17+
// void set foo(super.value) {}
18+
// ^^^^^
19+
//
20+
import self as self;
21+
import "dart:core" as core;
22+
23+
class A extends core::Object {
24+
constructor •(core::int x) → self::A
25+
: super core::Object::•()
26+
;
27+
}
28+
class B extends self::A {
29+
constructor •() → self::B
30+
: super self::A::•(42)
31+
;
32+
static factory foo(dynamic x) → self::B
33+
return new self::B::•();
34+
}
35+
class C extends core::Object {
36+
synthetic constructor •() → self::C
37+
: super core::Object::•()
38+
;
39+
set foo(dynamic value) → void {}
40+
}
41+
static method foo(dynamic x) → dynamic {}
42+
static method main() → dynamic {}

0 commit comments

Comments
 (0)