Skip to content

Commit 3020dd1

Browse files
committed
#3315. Add tests for new() and factory() constructors
1 parent 59bc36e commit 3020dd1

11 files changed

+618
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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` or `factory` as a keyword and omitting
6+
/// the class name in declarations of ordinary (non-primary) constructors is
7+
/// purely syntactic. The static analysis and meaning of such constructors is
8+
/// identical to the form that uses the class name.
9+
///
10+
/// @description Check that `new` can be used instead of the class name in
11+
/// declarations of ordinary constructors. Test classes.
12+
/// @author [email protected]
13+
14+
// SharedOptions=--enable-experiment=declaring-constructors
15+
16+
import '../../Utils/expect.dart';
17+
18+
const metadata = 42;
19+
20+
class C1 {
21+
int? v;
22+
@metadata new() {
23+
v = 1;
24+
}
25+
}
26+
27+
class C2 {
28+
final int v;
29+
@metadata const new(int v) : v = v;
30+
}
31+
32+
class C3 {
33+
final int v;
34+
new(this.v);
35+
}
36+
37+
class A {
38+
int v;
39+
A(this.v);
40+
}
41+
42+
class C4 extends A {
43+
new(super.v);
44+
}
45+
46+
class C5 {
47+
final int v;
48+
new([int v = 0]) : v = v;
49+
}
50+
51+
class C6 {
52+
final int v;
53+
const new({int v = 0}) : v = v;
54+
}
55+
56+
class C7 {
57+
final int v;
58+
new({required int v}) : v = v;
59+
}
60+
61+
main() {
62+
Expect.equals(1, C1().v);
63+
Expect.equals(2, C2(2).v);
64+
Expect.equals(3, C3(3).v);
65+
Expect.equals(4, C4(4).v);
66+
Expect.equals(0, C5().v);
67+
Expect.equals(5, C5(5).v);
68+
Expect.equals(0, C6().v);
69+
Expect.equals(6, C6(v: 6).v);
70+
Expect.equals(7, C7(v: 7).v);
71+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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` or `factory` as a keyword and omitting
6+
/// the class name in declarations of ordinary (non-primary) constructors is
7+
/// purely syntactic. The static analysis and meaning of such constructors is
8+
/// identical to the form that uses the class name.
9+
///
10+
/// @description Check that `new` can be used instead of the class name in
11+
/// declarations of ordinary constructors. Test extension types.
12+
/// @author [email protected]
13+
14+
// SharedOptions=--enable-experiment=declaring-constructors
15+
16+
import '../../Utils/expect.dart';
17+
18+
const metadata = 42;
19+
20+
extension type ET1._(int v) {
21+
@metadata const new(this.v);
22+
}
23+
24+
extension type ET2._(int v) {
25+
@metadata new() : v = 2;
26+
}
27+
28+
extension type ET3._(int v) {
29+
const new([int v = 0]) : v = v;
30+
}
31+
32+
extension type ET4._(int v) {
33+
new({int v = 0}) : v = v;
34+
}
35+
36+
extension type ET5._(int v) {
37+
const new({required int v}) : v = v;
38+
}
39+
40+
main() {
41+
Expect.equals(1, ET1(1).v);
42+
Expect.equals(2, ET2().v);
43+
Expect.equals(3, ET3(3).v);
44+
Expect.equals(0, ET3().v);
45+
Expect.equals(4, ET4(v: 4).v);
46+
Expect.equals(0, ET4().v);
47+
Expect.equals(5, ET5(v: 5).v);
48+
Expect.equals(0, ET5().v);
49+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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` or `factory` as a keyword and omitting
6+
/// the class name in declarations of ordinary (non-primary) constructors is
7+
/// purely syntactic. The static analysis and meaning of such constructors is
8+
/// identical to the form that uses the class name.
9+
///
10+
/// @description Check that `new` can be used instead of the class name in
11+
/// declarations of ordinary constructors. Test enums.
12+
/// @author [email protected]
13+
14+
// SharedOptions=--enable-experiment=declaring-constructors
15+
16+
import '../../Utils/expect.dart';
17+
18+
const metadata = 42;
19+
20+
enum E1 {
21+
a(1);
22+
23+
final int v;
24+
@metadata const new(this.v);
25+
}
26+
27+
enum E2 {
28+
a();
29+
30+
final int v;
31+
const new() : v = 2;
32+
}
33+
34+
enum E3 {
35+
a(1), b();
36+
37+
final int v;
38+
const new([int v = 0]) : v = v;
39+
}
40+
41+
enum E4 {
42+
a(v: 1), b();
43+
44+
final int v;
45+
const new({int v = 0}) : v = v;
46+
}
47+
48+
enum E5 {
49+
a(v: 1);
50+
51+
final int v;
52+
const new({required int v}) : v = v;
53+
}
54+
55+
main() {
56+
Expect.equals(1, E1.a.v);
57+
Expect.equals(2, E2.a.v);
58+
Expect.equals(1, E3.a.v);
59+
Expect.equals(0, E3.b.v);
60+
Expect.equals(1, E4.a.v);
61+
Expect.equals(0, E4.b.v);
62+
Expect.equals(1, E5.a.v);
63+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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` or `factory` as a keyword and omitting
6+
/// the class name in declarations of ordinary (non-primary) constructors is
7+
/// purely syntactic. The static analysis and meaning of such constructors is
8+
/// identical to the 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 factory constructor redirection.
12+
/// @author [email protected]
13+
14+
// SharedOptions=--enable-experiment=declaring-constructors
15+
16+
class C1 {
17+
C1();
18+
factory C1.someName() = new;
19+
// ^^^
20+
// [analyzer] unspecified
21+
// [cfe] unspecified
22+
}
23+
24+
class C2 {
25+
factory 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+
factory ET1.someName(int v) = new;
35+
// ^^^
36+
// [analyzer] unspecified
37+
// [cfe] unspecified
38+
}
39+
40+
extension type ET2._(int v) {
41+
factory ET2(int v) = new.someName;
42+
// ^^^
43+
// [analyzer] unspecified
44+
// [cfe] unspecified
45+
ET2.someName(this.v);
46+
}
47+
48+
main() {
49+
print(C1);
50+
print(C2);
51+
print(ET1);
52+
print(ET2);
53+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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` or `factory` as a keyword and omitting
6+
/// the class name in declarations of ordinary (non-primary) constructors is
7+
/// purely syntactic. The static analysis and meaning of such constructors is
8+
/// identical to the 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 [email protected]
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.someName() : new(0);
34+
// ^^^
35+
// [analyzer] unspecified
36+
// [cfe] unspecified
37+
}
38+
39+
extension type ET2.someName(int v) {
40+
ET2() : new.someName(0);
41+
// ^^^
42+
// [analyzer] unspecified
43+
// [cfe] unspecified
44+
}
45+
46+
enum E1 {
47+
e0;
48+
49+
const E1();
50+
const E1.someName() : new();
51+
// ^^^
52+
// [analyzer] unspecified
53+
// [cfe] unspecified
54+
}
55+
56+
enum E2 {
57+
e0;
58+
59+
const ET2() : new.someName();
60+
// ^^^
61+
// [analyzer] unspecified
62+
// [cfe] unspecified
63+
const ET2.someName();
64+
}
65+
66+
main() {
67+
print(C1);
68+
print(C2);
69+
print(ET1);
70+
print(ET2);
71+
print(E1);
72+
print(E2);
73+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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` or `factory` as a keyword and omitting
6+
/// the class name in declarations of ordinary (non-primary) constructors is
7+
/// purely syntactic. The static analysis and meaning of such constructors is
8+
/// identical to the form that uses the class name.
9+
///
10+
/// @description Check that `factory` can be used instead of the class name in
11+
/// declarations of ordinary constructors.
12+
/// @author [email protected]
13+
14+
// SharedOptions=--enable-experiment=declaring-constructors
15+
16+
import '../../Utils/expect.dart';
17+
18+
class C1 {
19+
int v;
20+
C1.create(this.v);
21+
22+
factory() => C1.create(1);
23+
}
24+
25+
class C2 {
26+
final int v;
27+
const C2.create(this.v);
28+
29+
const factory(int v) = C2.create;
30+
}
31+
32+
extension type ET1.create(int v) {
33+
factory() => ET1.create(1);
34+
}
35+
36+
extension type const ET2.create(int v) {
37+
const factory ET2 (int v) = ET2.create;
38+
}
39+
40+
main() {
41+
Expect.equals(1, C1().v);
42+
Expect.equals(2, C2(2).v);
43+
Expect.equals(1, ET1().v);
44+
Expect.equals(2, ET2(2).v);
45+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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` or `factory` as a keyword and omitting
6+
/// the class name in declarations of ordinary (non-primary) constructors is
7+
/// purely syntactic. The static analysis and meaning of such constructors is
8+
/// identical to the form that uses the class name.
9+
///
10+
/// @description Check that it is a compile-time error if a keyword `factory` is
11+
/// used to declare a constant non-redirecting constructor.
12+
/// @author [email protected]
13+
14+
// SharedOptions=--enable-experiment=declaring-constructors
15+
16+
class C {
17+
int v;
18+
C.create(this.v);
19+
20+
const factory C() => C.create(1);
21+
//^^^^^
22+
// [analyzer] unspecified
23+
// [cfe] unspecified
24+
}
25+
26+
extension type const ET(int _) {
27+
const factory() => ET(0);
28+
//^^^^^
29+
// [analyzer] unspecified
30+
// [cfe] unspecified
31+
}
32+
33+
main() {
34+
print(C);
35+
print(ET);
36+
}

0 commit comments

Comments
 (0)