Skip to content

Commit 87098a9

Browse files
authored
#3315. Add tests for formal parameters. Part 4. (#3346)
Add tests for formal parameters. Part 4.
1 parent 2a8dd57 commit 87098a9

10 files changed

+1021
-4
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2025, 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+
/// @assertion Consider a class, enum, or extension type declaration `D` with a
6+
/// declaring header constructor, also known as a primary constructor.
7+
/// ...
8+
/// If `D` is an extension type, it is a compile-time error if `D`
9+
/// does not contain a declaring constructor that has exactly one declaring
10+
/// parameter which is `final`.
11+
///
12+
/// @description Check that it is a compile-time error if an extension type does
13+
/// not contain a declaring constructor that has exactly one declaring parameter
14+
/// which is `final`.
15+
/// @author sgrekhov22@gmail.com
16+
17+
// SharedOptions=--enable-experiment=declaring-constructors
18+
19+
extension type const ET1();
20+
// ^^^
21+
// [analyzer] unspecified
22+
// [cfe] unspecified
23+
24+
extension type ET2.someName();
25+
// ^^^^^^^^
26+
// [analyzer] unspecified
27+
// [cfe] unspecified
28+
29+
extension type ET3 {
30+
this();
31+
//^^^^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
}
35+
36+
extension type ET4 {
37+
const this.someName();
38+
// ^^^^^^^^
39+
// [analyzer] unspecified
40+
// [cfe] unspecified
41+
}
42+
43+
main() {
44+
print(ET1);
45+
print(ET2);
46+
print(ET3);
47+
print(ET4);
48+
}

LanguageFeatures/Declaring-constructors/static_processing_A08_t02.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,29 @@ extension type ET4({required covariant String x}) {}
4040
// [cfe] unspecified
4141

4242
extension type ET5 {
43-
this(covariant String x);
43+
this(covariant final String x);
4444
// ^^^^^^^^^
4545
// [analyzer] unspecified
4646
// [cfe] unspecified
4747
}
4848

4949
extension type ET6 {
5050
this([covariant final String x = ""]);
51-
// ^^^^^
51+
// ^^^^^^^^^
5252
// [analyzer] unspecified
5353
// [cfe] unspecified
5454
}
5555

5656
extension type ET7 {
57-
this({covariant String x = ""});
57+
this({covariant final String x = ""});
5858
// ^^^^^^^^^
5959
// [analyzer] unspecified
6060
// [cfe] unspecified
6161
}
6262

6363
extension type ET8 {
6464
this({required covariant final String x});
65-
// ^^^^^
65+
// ^^^^^^^^^
6666
// [analyzer] unspecified
6767
// [cfe] unspecified
6868
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Copyright (c) 2025, 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+
/// @assertion The semantics of the declaring constructor is found in the
6+
/// following steps, where `D` is the class, extension type, or enum declaration
7+
/// in the program that includes a declaring constructor `k`, and `D2` is the
8+
/// result of the derivation of the semantics of `D`. The derivation step will
9+
/// delete elements that amount to the declaring constructor. Semantically, it
10+
/// will add a new constructor `k2`, and it will add zero or more instance
11+
/// variable declarations.
12+
/// ...
13+
/// Otherwise, a formal parameter (named or positional) of the form` var T p` or
14+
/// `final T p` where `T` is a type and `p` is an identifier is replaced in `L2`
15+
/// by `this.p`, along with its default value, if any. Next, a semantic instance
16+
/// variable declaration corresponding to the syntax `T p;` or `final T p;` is
17+
/// added to `D2`. It includes the modifier `final` if the parameter in `L` has
18+
/// the modifier `final`, or `D` is an extension type declaration and `k` is a
19+
/// declaring header constructor. In all cases, if `p` has the modifier
20+
/// `covariant` then this modifier is removed from the parameter in `L2`, and it
21+
/// is added to the instance variable declaration named `p`.
22+
///
23+
/// @description Check that it is a compile-time error if a declaring
24+
/// constructor contains a formal parameter of the form `var T p` or
25+
/// `final T p` and the enclosing class/enum/extension type contains an instance
26+
/// member named `p`. Test mandatory parameters.
27+
/// @author sgrekhov22@gmail.com
28+
29+
// SharedOptions=--enable-experiment=declaring-constructors
30+
31+
class C1<T>(var String v1, final T v2, var bool v3) {
32+
String get v1 => "";
33+
// ^^
34+
// [analyzer] unspecified
35+
// [cfe] unspecified
36+
T v2;
37+
// ^^
38+
// [analyzer] unspecified
39+
// [cfe] unspecified
40+
void v3() {}
41+
// ^^
42+
// [analyzer] unspecified
43+
// [cfe] unspecified
44+
}
45+
46+
class C2<T> {
47+
this(final String v1, var T v2, final bool v3);
48+
String get v1 => "";
49+
// ^^
50+
// [analyzer] unspecified
51+
// [cfe] unspecified
52+
T v2;
53+
// ^^
54+
// [analyzer] unspecified
55+
// [cfe] unspecified
56+
void v3() {}
57+
// ^^
58+
// [analyzer] unspecified
59+
// [cfe] unspecified
60+
}
61+
62+
extension type ET1<T>(final T v) {
63+
void v() {}
64+
// ^
65+
// [analyzer] unspecified
66+
// [cfe] unspecified
67+
}
68+
69+
extension type ET2(String v) {
70+
String get v => "";
71+
// ^
72+
// [analyzer] unspecified
73+
// [cfe] unspecified
74+
}
75+
76+
extension type ET3<T> {
77+
this(final T v);
78+
79+
void v() {}
80+
// ^
81+
// [analyzer] unspecified
82+
// [cfe] unspecified
83+
}
84+
85+
extension type ET4 {
86+
this(final String v);
87+
88+
String get v => "";
89+
// ^
90+
// [analyzer] unspecified
91+
// [cfe] unspecified
92+
}
93+
94+
enum E1<T>(final T v1, final String v2, final bool v3) {
95+
e0<int>(1, "E1", true);
96+
97+
final T v1;
98+
// ^^
99+
// [analyzer] unspecified
100+
// [cfe] unspecified
101+
String get v2 => "";
102+
// ^^
103+
// [analyzer] unspecified
104+
// [cfe] unspecified
105+
void v3() {}
106+
// ^^
107+
// [analyzer] unspecified
108+
// [cfe] unspecified
109+
}
110+
111+
enum E2<T> {
112+
e0<int>(1, "E1", true);
113+
114+
this(final T v1, final String v2, final bool v3);
115+
116+
final T v1;
117+
// ^^
118+
// [analyzer] unspecified
119+
// [cfe] unspecified
120+
String get v2 => "";
121+
// ^^
122+
// [analyzer] unspecified
123+
// [cfe] unspecified
124+
void v3() {}
125+
// ^^
126+
// [analyzer] unspecified
127+
// [cfe] unspecified
128+
}
129+
130+
main() {
131+
print(C1);
132+
print(C2);
133+
print(ET1);
134+
print(ET2);
135+
print(ET3);
136+
print(ET4);
137+
print(E1);
138+
print(E2);
139+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Copyright (c) 2025, 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+
/// @assertion The semantics of the declaring constructor is found in the
6+
/// following steps, where `D` is the class, extension type, or enum declaration
7+
/// in the program that includes a declaring constructor `k`, and `D2` is the
8+
/// result of the derivation of the semantics of `D`. The derivation step will
9+
/// delete elements that amount to the declaring constructor. Semantically, it
10+
/// will add a new constructor `k2`, and it will add zero or more instance
11+
/// variable declarations.
12+
/// ...
13+
/// Otherwise, a formal parameter (named or positional) of the form` var T p` or
14+
/// `final T p` where `T` is a type and `p` is an identifier is replaced in `L2`
15+
/// by `this.p`, along with its default value, if any. Next, a semantic instance
16+
/// variable declaration corresponding to the syntax `T p;` or `final T p;` is
17+
/// added to `D2`. It includes the modifier `final` if the parameter in `L` has
18+
/// the modifier `final`, or `D` is an extension type declaration and `k` is a
19+
/// declaring header constructor. In all cases, if `p` has the modifier
20+
/// `covariant` then this modifier is removed from the parameter in `L2`, and it
21+
/// is added to the instance variable declaration named `p`.
22+
///
23+
/// @description Check that it is a compile-time error if a declaring
24+
/// constructor contains a formal parameter of the form `var T p` or
25+
/// `final T p` and the enclosing class/enum/extension type contains an instance
26+
/// member named `p`. Test optional positional parameters.
27+
/// @author sgrekhov22@gmail.com
28+
29+
// SharedOptions=--enable-experiment=declaring-constructors
30+
31+
class C1<T>([var String v1 = "", final T? v2, var bool v3 = true]) {
32+
String get v1 => "";
33+
// ^^
34+
// [analyzer] unspecified
35+
// [cfe] unspecified
36+
T v2;
37+
// ^^
38+
// [analyzer] unspecified
39+
// [cfe] unspecified
40+
void v3() {}
41+
// ^^
42+
// [analyzer] unspecified
43+
// [cfe] unspecified
44+
}
45+
46+
class C2<T> {
47+
this([var String v1 = "", final T? v2, var bool v3 = true]);
48+
String get v1 => "";
49+
// ^^
50+
// [analyzer] unspecified
51+
// [cfe] unspecified
52+
T v2;
53+
// ^^
54+
// [analyzer] unspecified
55+
// [cfe] unspecified
56+
57+
void v3() {}
58+
// ^^
59+
// [analyzer] unspecified
60+
// [cfe] unspecified
61+
}
62+
63+
extension type ET1<T>([final T? v]) {
64+
void v() {}
65+
// ^
66+
// [analyzer] unspecified
67+
// [cfe] unspecified
68+
}
69+
70+
extension type ET2([String v = ""]) {
71+
String get v => "";
72+
// ^
73+
// [analyzer] unspecified
74+
// [cfe] unspecified
75+
}
76+
77+
extension type ET3<T> {
78+
this([final T? v]);
79+
80+
void v() {}
81+
// ^
82+
// [analyzer] unspecified
83+
// [cfe] unspecified
84+
}
85+
86+
extension type ET4 {
87+
this([final String v = ""]);
88+
89+
String get v => "";
90+
// ^
91+
// [analyzer] unspecified
92+
// [cfe] unspecified
93+
}
94+
95+
enum E1<T>([final T? v1, final String v2 = "", final bool v3 = true]) {
96+
e0<int>(1, "E1", true);
97+
98+
final T v1;
99+
// ^^
100+
// [analyzer] unspecified
101+
// [cfe] unspecified
102+
String get v2 => "";
103+
// ^^
104+
// [analyzer] unspecified
105+
// [cfe] unspecified
106+
void v3() {}
107+
// ^^
108+
// [analyzer] unspecified
109+
// [cfe] unspecified
110+
}
111+
112+
enum E2<T> {
113+
e0<int>(1, "E1", true);
114+
115+
this([final T? v1, final String v2 = "", final bool v3 = true]);
116+
117+
final T v1;
118+
// ^^
119+
// [analyzer] unspecified
120+
// [cfe] unspecified
121+
String get v2 => "";
122+
// ^^
123+
// [analyzer] unspecified
124+
// [cfe] unspecified
125+
void v3() {}
126+
// ^^
127+
// [analyzer] unspecified
128+
// [cfe] unspecified
129+
}
130+
131+
main() {
132+
print(C1);
133+
print(C2);
134+
print(ET1);
135+
print(ET2);
136+
print(ET3);
137+
print(ET4);
138+
print(E1);
139+
print(E2);
140+
}

0 commit comments

Comments
 (0)