Skip to content

Commit 07a4556

Browse files
authored
#3315. Move/add mixins and generative constructors tests to Primary-constructors. (#3394)
Move/add mixin tests and generative constructor tests to `Primary-constructors`
1 parent a4b5c97 commit 07a4556

13 files changed

+525
-0
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, mixin class, enum, or extension type
6+
/// declaration `D` with a primary constructor (note that it cannot be a
7+
/// `<mixinApplicationClass>`, because that kind of declaration does not
8+
/// syntactically support primary constructors).
9+
///
10+
/// @description Check that it is a compile-time error to declare a mixin with a
11+
/// primary constructor.
12+
/// @author sgrekhov22@gmail.com
13+
14+
// SharedOptions=--enable-experiment=declaring-constructors
15+
16+
mixin M1() {}
17+
// ^^
18+
// [analyzer] unspecified
19+
// [cfe] unspecified
20+
21+
mixin M2;
22+
// ^^
23+
// [analyzer] unspecified
24+
// [cfe] unspecified
25+
26+
mixin const M3(final int v);
27+
// ^^
28+
// [analyzer] unspecified
29+
// [cfe] unspecified
30+
31+
mixin M4(var int v) {}
32+
// ^^
33+
// [analyzer] unspecified
34+
// [cfe] unspecified
35+
36+
mixin M5.someName() {
37+
// ^^^^^^^^
38+
// [analyzer] unspecified
39+
// [cfe] unspecified
40+
}
41+
42+
main() {
43+
print(M1);
44+
print(M2);
45+
print(M3);
46+
print(M4);
47+
print(M5);
48+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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, mixin class, enum, or extension type
6+
/// declaration `D` with a primary constructor (note that it cannot be a
7+
/// `<mixinApplicationClass>`, because that kind of declaration does not
8+
/// syntactically support primary constructors).
9+
///
10+
/// @description Check that it is a compile-time error to declare a mixin class
11+
/// with a non-trivial primary constructor.
12+
/// @author sgrekhov22@gmail.com
13+
14+
// SharedOptions=--enable-experiment=declaring-constructors
15+
16+
mixin class M1() {} // Ok, this declares a "trivial" constructor
17+
18+
base mixin class M2;
19+
20+
mixin class const M3(final int v);
21+
// ^^
22+
// [analyzer] unspecified
23+
// [cfe] unspecified
24+
25+
base mixin M4(var int v) {}
26+
// ^^
27+
// [analyzer] unspecified
28+
// [cfe] unspecified
29+
30+
abstract mixin class M5(); // Ok, "trivial" constructor
31+
32+
abstract base mixin class M6.someName(final int v) {}
33+
// ^^^^^^^^^^^
34+
// [analyzer] unspecified
35+
// [cfe] unspecified
36+
37+
base mixin M7.someName(); // Constructor is "trivial" but it's not a "mixin class"
38+
// ^^^^^^^^
39+
// [analyzer] unspecified
40+
// [cfe] unspecified
41+
42+
mixin class M8.someName() { // Ok
43+
int x;
44+
this: x = 0;
45+
}
46+
47+
main() {
48+
print(M1);
49+
print(M2);
50+
print(M3);
51+
print(M4);
52+
print(M5);
53+
print(M6);
54+
print(M7);
55+
print(M8);
56+
}
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 Consider a class, mixin class, enum, or extension type
6+
/// declaration `D` with a primary constructor (note that it cannot be a
7+
/// `<mixinApplicationClass>`, because that kind of declaration does not
8+
/// syntactically support primary constructors).
9+
///
10+
/// @description Check that it is a syntax error to declare a mixin application
11+
/// class with a primary constructor.
12+
/// @author sgrekhov22@gmail.com
13+
14+
// SharedOptions=--enable-experiment=declaring-constructors
15+
16+
mixin M {}
17+
18+
class C1() = Object with M;
19+
// ^^
20+
// [analyzer] unspecified
21+
// [cfe] unspecified
22+
23+
class C2.someName() = Object with M;
24+
// ^^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
28+
class const C3.someName(final int v) = Object with M;
29+
// ^
30+
// [analyzer] unspecified
31+
// [cfe] unspecified
32+
33+
class C4.new(var int v) = Object with M;
34+
// ^^
35+
// [analyzer] unspecified
36+
// [cfe] unspecified
37+
38+
class C5._() = Object with M;
39+
// ^^
40+
// [analyzer] unspecified
41+
// [cfe] unspecified
42+
43+
main() {
44+
print(C1);
45+
print(C2);
46+
print(C3);
47+
print(C4);
48+
print(C5);
49+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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, mixin class, enum, or extension type
6+
/// declaration `D` with a primary constructor (note that it cannot be a
7+
/// `<mixinApplicationClass>`, because that kind of declaration does not
8+
/// syntactically support primary constructors). This declaration is treated as
9+
/// a class, mixin class, enum, respectively extension type declaration without
10+
/// a primary constructor which is obtained as described in the following. This
11+
/// determines the dynamic semantics of a primary constructor.
12+
///
13+
/// A compile-time error occurs if the body of `D` contains a non-redirecting
14+
/// generative constructor, unless `D` is an extension type.
15+
///
16+
/// @description Check that it is a compile-time error if the body of `D`
17+
/// contains a non-redirecting generative constructor. Test a class.
18+
/// @author sgrekhov22@gmail.com
19+
20+
// SharedOptions=--enable-experiment=declaring-constructors
21+
22+
class C1(var int v) {
23+
C1.someName(this.v);
24+
// ^^^^^^^^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
}
28+
29+
class C2.someName(final int v) {
30+
C2(int x) : v = 42;
31+
//^^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
}
35+
36+
main() {
37+
print(C1);
38+
print(C2);
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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, mixin class, enum, or extension type
6+
/// declaration `D` with a primary constructor (note that it cannot be a
7+
/// `<mixinApplicationClass>`, because that kind of declaration does not
8+
/// syntactically support primary constructors). This declaration is treated as
9+
/// a class, mixin class, enum, respectively extension type declaration without
10+
/// a primary constructor which is obtained as described in the following. This
11+
/// determines the dynamic semantics of a primary constructor.
12+
///
13+
/// A compile-time error occurs if the body of `D` contains a non-redirecting
14+
/// generative constructor, unless `D` is an extension type.
15+
///
16+
/// @description Check that it is a compile-time error if the body of `D`
17+
/// contains a non-redirecting generative constructor. Test a mixin class.
18+
/// @author sgrekhov22@gmail.com
19+
20+
// SharedOptions=--enable-experiment=declaring-constructors
21+
22+
mixin class C1() {
23+
C1.someName();
24+
// ^^^^^^^^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
}
28+
29+
class C2.someName() {
30+
C2();
31+
//^^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
}
35+
36+
main() {
37+
print(C1);
38+
print(C2);
39+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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, mixin class, enum, or extension type
6+
/// declaration `D` with a primary constructor (note that it cannot be a
7+
/// `<mixinApplicationClass>`, because that kind of declaration does not
8+
/// syntactically support primary constructors). This declaration is treated as
9+
/// a class, mixin class, enum, respectively extension type declaration without
10+
/// a primary constructor which is obtained as described in the following. This
11+
/// determines the dynamic semantics of a primary constructor.
12+
///
13+
/// A compile-time error occurs if the body of `D` contains a non-redirecting
14+
/// generative constructor, unless `D` is an extension type.
15+
///
16+
/// @description Check that it is a compile-time error if the body of `D`
17+
/// contains a non-redirecting generative constructor. Test an enum.
18+
/// @author sgrekhov22@gmail.com
19+
20+
// SharedOptions=--enable-experiment=declaring-constructors
21+
22+
enum const E1(final int v) {
23+
e0(1);
24+
25+
const E1.someName(this.v);
26+
// ^^^^^^^^
27+
// [analyzer] unspecified
28+
// [cfe] unspecified
29+
}
30+
31+
enum const E2.someName(final int v) {
32+
e0(2);
33+
34+
const E2(int x) : v = 42;
35+
// ^^
36+
// [analyzer] unspecified
37+
// [cfe] unspecified
38+
}
39+
40+
main() {
41+
print(E1);
42+
print(E2);
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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, mixin class, enum, or extension type
6+
/// declaration `D` with a primary constructor (note that it cannot be a
7+
/// `<mixinApplicationClass>`, because that kind of declaration does not
8+
/// syntactically support primary constructors). This declaration is treated as
9+
/// a class, mixin class, enum, respectively extension type declaration without
10+
/// a primary constructor which is obtained as described in the following. This
11+
/// determines the dynamic semantics of a primary constructor.
12+
///
13+
/// A compile-time error occurs if the body of `D` contains a non-redirecting
14+
/// generative constructor, unless `D` is an extension type.
15+
///
16+
/// @description Check that it is not an error if `D` is an extension type and
17+
/// the body of `D` contains a non-redirecting generative constructor.
18+
/// @author sgrekhov22@gmail.com
19+
20+
// SharedOptions=--enable-experiment=declaring-constructors
21+
22+
import '../../Utils/expect.dart';
23+
24+
extension type ET1(int v) {
25+
ET1.someName(this.v);
26+
}
27+
28+
extension type const ET2.someName(final int v) {
29+
const ET2(int v) : this.v = v;
30+
}
31+
32+
main() {
33+
Expect.equals(1, ET1(1).v);
34+
Expect.equals(2, ET2(2).v);
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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, mixin class, enum, or extension type
6+
/// declaration `D` with a primary constructor (note that it cannot be a
7+
/// `<mixinApplicationClass>`, because that kind of declaration does not
8+
/// syntactically support primary constructors). This declaration is treated as
9+
/// a class, mixin class, enum, respectively extension type declaration without
10+
/// a primary constructor which is obtained as described in the following. This
11+
/// determines the dynamic semantics of a primary constructor.
12+
///
13+
/// A compile-time error occurs if the body of `D` contains a non-redirecting
14+
/// generative constructor, unless `D` is an extension type.
15+
///
16+
/// @description Check that it is not an error if the body of `D` contains a
17+
/// redirecting generative constructor. Test a class.
18+
/// @author sgrekhov22@gmail.com
19+
20+
// SharedOptions=--enable-experiment=declaring-constructors
21+
22+
import '../../Utils/expect.dart';
23+
24+
class C1(var int v) {
25+
C1.someName(int v) : this(v);
26+
}
27+
28+
class const C2.someName(final int v) {
29+
const C2(int v) : this.someName(v);
30+
}
31+
32+
main() {
33+
Expect.equals(1, C1(1).v);
34+
Expect.equals(2, C2(2).v);
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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, mixin class, enum, or extension type
6+
/// declaration `D` with a primary constructor (note that it cannot be a
7+
/// `<mixinApplicationClass>`, because that kind of declaration does not
8+
/// syntactically support primary constructors). This declaration is treated as
9+
/// a class, mixin class, enum, respectively extension type declaration without
10+
/// a primary constructor which is obtained as described in the following. This
11+
/// determines the dynamic semantics of a primary constructor.
12+
///
13+
/// A compile-time error occurs if the body of `D` contains a non-redirecting
14+
/// generative constructor, unless `D` is an extension type.
15+
///
16+
/// @description Check that it is not an error if the body of `D` contains a
17+
/// redirecting generative constructor. Test an enum.
18+
/// @author sgrekhov22@gmail.com
19+
20+
// SharedOptions=--enable-experiment=declaring-constructors
21+
22+
import '../../Utils/expect.dart';
23+
24+
enum E1(final int v) {
25+
e0(1), e1.someName(2);
26+
27+
E1.someName(int v) : this(v);
28+
}
29+
30+
enum const E2.someName(final int v) {
31+
e0.someName(1), e1(2);
32+
33+
const C2(int v) : this.someName(v);
34+
}
35+
36+
main() {
37+
Expect.equals(1, E1.e0.v);
38+
Expect.equals(2, E1.e1.v);
39+
Expect.equals(1, E2.e0.v);
40+
Expect.equals(2, E2.e1.v);
41+
}

0 commit comments

Comments
 (0)