Skip to content

Commit d98753d

Browse files
authored
#3315. Add more declaring constructors tests (#3351)
1 parent 137d34b commit d98753d

File tree

4 files changed

+187
-2
lines changed

4 files changed

+187
-2
lines changed

LanguageFeatures/Declaring-constructors/grammar_A04_t01.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class const C3.someName(final int v) {
4040
// [cfe] unspecified
4141
}
4242

43-
class const C4() {
43+
class const C4.someName() {
4444
int? v;
4545
const this.someName : v = 0;
4646
// ^

LanguageFeatures/Declaring-constructors/grammar_A04_t03.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ enum E2.someName(final int v) {
3636
// [cfe] unspecified
3737
}
3838

39-
enum E3() {
39+
enum E3.someName() {
4040
e0;
4141
final int v;
4242
// ^
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 keyword `const` can be specified in the class header when it
6+
/// contains a primary constructor, and in this case `const` can not be
7+
/// specified in the part of the primary constructor that occurs in the body
8+
/// (that is, the declaration that starts with this and contains an initializer
9+
/// list and/or a constructor body, if any). The rationale is that when the
10+
/// class header contains any parts of a declaring constructor, the class header
11+
/// must be the location where all parts of the signature of that primary
12+
/// constructor are specified.
13+
///
14+
/// @description Check that it is a compile-time error if a class header
15+
/// contains a primary constructor and the keyword const is specified before its
16+
/// `this` part in the body.
17+
/// @author sgrekhov22@gmail.com
18+
19+
// SharedOptions=--enable-experiment=declaring-constructors
20+
21+
class C1() {
22+
final int v;
23+
const this : v = 0;
24+
//^^^^^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
}
28+
29+
class C2(int v) {
30+
const this : assert(v > 0);
31+
//^^^^^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
}
35+
36+
class C3.someName(final int v) {
37+
const this.someName : assert(v > 0);
38+
//^^^^^
39+
// [analyzer] unspecified
40+
// [cfe] unspecified
41+
}
42+
43+
class C4.someName() {
44+
int v;
45+
const this.someName : v = 0;
46+
//^^^^^
47+
// [analyzer] unspecified
48+
// [cfe] unspecified
49+
}
50+
51+
extension type ET1(int v) {
52+
const this: assert(v > 0);
53+
//^^^^^
54+
// [analyzer] unspecified
55+
// [cfe] unspecified
56+
}
57+
58+
extension type ET2.someName(int v) {
59+
const this.someName: assert(v > 0);
60+
//^^^^^
61+
// [analyzer] unspecified
62+
// [cfe] unspecified
63+
}
64+
65+
enum E1() {
66+
e0;
67+
final int v;
68+
const this : v = 0;
69+
//^^^^^
70+
// [analyzer] unspecified
71+
// [cfe] unspecified
72+
}
73+
74+
enum E2(int v) {
75+
e0(1);
76+
const this : assert(v > 0);
77+
//^^^^^
78+
// [analyzer] unspecified
79+
// [cfe] unspecified
80+
}
81+
82+
enum E3.someName(final int v) {
83+
e0.someName(0);
84+
const this.someName : assert(v > 0);
85+
//^^^^^
86+
// [analyzer] unspecified
87+
// [cfe] unspecified
88+
}
89+
90+
enum E4.someName() {
91+
e0.someName();
92+
final int v;
93+
const this.someName : v = 0;
94+
//^^^^^
95+
// [analyzer] unspecified
96+
// [cfe] unspecified
97+
}
98+
99+
main() {
100+
print(C1);
101+
print(C2);
102+
print(C3);
103+
print(C4);
104+
print(ET1);
105+
print(ET2);
106+
print(E1);
107+
print(E2);
108+
print(E3);
109+
print(E4);
110+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 ability to use `new` rather than the class name in
6+
/// declarations of ordinary (non-declaring) constructors is purely syntactic.
7+
/// The static analysis and meaning of such constructors is identical to the
8+
/// form that uses the class name.
9+
///
10+
/// @description Check that it is a compile-time error if a `new` keyword is
11+
/// used as a constructor name in the initializer list.
12+
/// @author sgrekhov22@gmail.com
13+
14+
// SharedOptions=--enable-experiment=declaring-constructors
15+
16+
class C1 {
17+
C1();
18+
C1.someName() : new();
19+
// ^^^
20+
// [analyzer] unspecified
21+
// [cfe] unspecified
22+
}
23+
24+
class C2 {
25+
C2() : new.someName();
26+
// ^^^
27+
// [analyzer] unspecified
28+
// [cfe] unspecified
29+
C2.someName();
30+
}
31+
32+
extension type ET1._(int v) {
33+
ET1(this.v);
34+
ET1.someName() : new(0);
35+
// ^^^
36+
// [analyzer] unspecified
37+
// [cfe] unspecified
38+
}
39+
40+
extension type ET2._(int v) {
41+
ET2() : new.someName(0);
42+
// ^^^
43+
// [analyzer] unspecified
44+
// [cfe] unspecified
45+
ET2.someName(this.v);
46+
}
47+
48+
enum E1 {
49+
e0;
50+
51+
const E1();
52+
const E1.someName() : new();
53+
// ^^^
54+
// [analyzer] unspecified
55+
// [cfe] unspecified
56+
}
57+
58+
enum E2 {
59+
e0;
60+
61+
const ET2() : new.someName();
62+
// ^^^
63+
// [analyzer] unspecified
64+
// [cfe] unspecified
65+
const ET2.someName();
66+
}
67+
68+
main() {
69+
print(C1);
70+
print(C2);
71+
print(ET1);
72+
print(ET2);
73+
print(E1);
74+
print(E2);
75+
}

0 commit comments

Comments
 (0)