Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 5d89a1a

Browse files
lrhncommit-bot@chromium.org
authored andcommitted
Add tests for mixin application constructor forwarding.
Forwarding should work for constructors with optional parameters, and for const constructors as long as the mixin doesn't declare any instance variables. Add tests for forwarding of constructors in mixin applications. Bug: http://dartbug.com/31543, http://dartbug.com/32223 Change-Id: Iaf950f48703e14391050338f7ae2d80854ec8286 Reviewed-on: https://dart-review.googlesource.com/50460 Commit-Queue: Lasse R.H. Nielsen <[email protected]> Reviewed-by: Erik Ernst <[email protected]>
1 parent e696279 commit 5d89a1a

File tree

5 files changed

+352
-0
lines changed

5 files changed

+352
-0
lines changed

tests/language_2/language_2.status

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# for details. All rights reserved. Use of this source code is governed by a
33
# BSD-style license that can be found in the LICENSE file.
44

5+
mixin_constructor_forwarding/const_constructor_test/none: CompileTimeError # Issue 32223
6+
mixin_constructor_forwarding/const_constructor_with_field_test/none: CompileTimeError # Issue 32223
7+
mixin_constructor_forwarding/optional_named_parameters_test/none: CompileTimeError # Issue 31543
8+
mixin_constructor_forwarding/optional_positional_parameters_test/none: CompileTimeError # Issue 31543
9+
510
[ $compiler == app_jit ]
611
deferred_inheritance_constraints_test/redirecting_constructor: Crash
712

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright (c) 2018, 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+
import "package:expect/expect.dart";
6+
7+
abstract class Mixin {
8+
// Declares no instance variable.
9+
int get x;
10+
int get m => x;
11+
}
12+
13+
class Base {
14+
final int x;
15+
16+
// Non-const constructors.
17+
Base.c1(this.x);
18+
Base.c2([this.x = 37]);
19+
Base.c3({this.x = 37});
20+
21+
// Non-forwarding generative const constructors.
22+
const Base.c4(this.x);
23+
const Base.c5([this.x = 37]);
24+
const Base.c6({this.x = 37});
25+
26+
// Forwarding generative const constructors.
27+
const Base.c7(int x) : this.c4(x);
28+
const Base.c8([int x = 87]) : this.c4(x);
29+
const Base.c9({int x = 87}) : this.c4(x);
30+
const Base.c10(int x) : this.c5(x);
31+
const Base.c11([int x = 87]) : this.c5(x);
32+
const Base.c12({int x = 87}) : this.c5(x);
33+
const Base.c13(int x) : this.c6(x: x);
34+
const Base.c14([int x = 87]) : this.c6(x: x);
35+
const Base.c15({int x = 87}) : this.c6(x: x);
36+
37+
// Non-generative constructor.
38+
const factory Base() = Base.c5;
39+
}
40+
41+
class Application = Base with Mixin;
42+
43+
class Application2 extends Base with Mixin {}
44+
45+
main() {
46+
Expect.equals(42, new Application.c1(42).m);
47+
Expect.equals(42, new Application.c2(42).m);
48+
Expect.equals(42, new Application.c3(x: 42).m);
49+
Expect.equals(42, const Application.c4(42).m);
50+
Expect.equals(42, const Application.c5(42).m);
51+
Expect.equals(42, const Application.c6(x: 42).m);
52+
Expect.equals(42, const Application.c7(42).m);
53+
Expect.equals(42, const Application.c8(42).m);
54+
Expect.equals(42, const Application.c9(x: 42).m);
55+
Expect.equals(42, const Application.c10(42).m);
56+
Expect.equals(42, const Application.c11(42).m);
57+
Expect.equals(42, const Application.c12(x: 42).m);
58+
Expect.equals(42, const Application.c13(42).m);
59+
Expect.equals(42, const Application.c14(42).m);
60+
Expect.equals(42, const Application.c15(x: 42).m);
61+
62+
Expect.equals(37, new Application.c2().m);
63+
Expect.equals(37, new Application.c3().m);
64+
Expect.equals(37, const Application.c5().m);
65+
Expect.equals(37, const Application.c6().m);
66+
Expect.equals(87, const Application.c8().m);
67+
Expect.equals(87, const Application.c9().m);
68+
Expect.equals(87, const Application.c11().m);
69+
Expect.equals(87, const Application.c12().m);
70+
Expect.equals(87, const Application.c14().m);
71+
Expect.equals(87, const Application.c15().m);
72+
73+
Expect.equals(42, new Application2.c1(42).m);
74+
Expect.equals(42, new Application2.c2(42).m);
75+
Expect.equals(42, new Application2.c3(x: 42).m);
76+
Expect.equals(42, const Application2.c4(42).m);
77+
Expect.equals(42, const Application2.c5(42).m);
78+
Expect.equals(42, const Application2.c6(x: 42).m);
79+
Expect.equals(42, const Application2.c7(42).m);
80+
Expect.equals(42, const Application2.c8(42).m);
81+
Expect.equals(42, const Application2.c9(x: 42).m);
82+
Expect.equals(42, const Application2.c10(42).m);
83+
Expect.equals(42, const Application2.c11(42).m);
84+
Expect.equals(42, const Application2.c12(x: 42).m);
85+
Expect.equals(42, const Application2.c13(42).m);
86+
Expect.equals(42, const Application2.c14(42).m);
87+
Expect.equals(42, const Application2.c15(x: 42).m);
88+
89+
Expect.equals(37, new Application2.c2().m);
90+
Expect.equals(37, new Application2.c3().m);
91+
Expect.equals(37, const Application2.c5().m);
92+
Expect.equals(37, const Application2.c6().m);
93+
Expect.equals(87, const Application2.c8().m);
94+
Expect.equals(87, const Application2.c9().m);
95+
Expect.equals(87, const Application2.c11().m);
96+
Expect.equals(87, const Application2.c12().m);
97+
Expect.equals(87, const Application2.c14().m);
98+
Expect.equals(87, const Application2.c15().m);
99+
100+
// Only make forwarders const if original constructor is const.
101+
const Application.c1(0); //# 01: compile-time error
102+
const Application.c2(0); //# 02: compile-time error
103+
const Application.c3(x: 0); //# 03: compile-time error
104+
const Application2.c1(0); //# 04: compile-time error
105+
const Application2.c2(0); //# 05: compile-time error
106+
const Application2.c3(x: 0); //# 06: compile-time error
107+
108+
// Only insert forwarders for generative constructors.
109+
new Application(); //# 07: compile-time error
110+
new Application2(); //# 08: compile-time error
111+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright (c) 2018, 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+
import "package:expect/expect.dart";
6+
7+
abstract class Mixin {
8+
// Declares an instance variable.
9+
// Declaration would be valid in a "const class", but mixin application
10+
// won't get const constructor forwarders.
11+
final int y = 0;
12+
13+
int get x;
14+
int get m => x;
15+
}
16+
17+
class Base {
18+
final int x;
19+
20+
// Non-const constructors.
21+
Base.c1(this.x);
22+
Base.c2([this.x = 37]);
23+
Base.c3({this.x = 37});
24+
25+
// Non-forwarding generative const constructors.
26+
const Base.c4(this.x);
27+
const Base.c5([this.x = 37]);
28+
const Base.c6({this.x = 37});
29+
30+
// Forwarding generative const constructors.
31+
const Base.c7(int x) : this.c4(x);
32+
const Base.c8([int x = 87]) : this.c4(x);
33+
const Base.c9({int x = 87}) : this.c4(x);
34+
const Base.c10(int x) : this.c5(x);
35+
const Base.c11([int x = 87]) : this.c5(x);
36+
const Base.c12({int x = 87}) : this.c5(x);
37+
const Base.c13(int x) : this.c6(x: x);
38+
const Base.c14([int x = 87]) : this.c6(x: x);
39+
const Base.c15({int x = 87}) : this.c6(x: x);
40+
41+
// Non-generative constructor.
42+
const factory Base() = Base.c5;
43+
}
44+
45+
class Application = Base with Mixin;
46+
47+
class Application2 extends Base with Mixin {}
48+
49+
main() {
50+
Expect.equals(42, new Application.c1(42).m);
51+
Expect.equals(42, new Application.c2(42).m);
52+
Expect.equals(42, new Application.c3(x: 42).m);
53+
Expect.equals(42, new Application.c4(42).m);
54+
Expect.equals(42, new Application.c5(42).m);
55+
Expect.equals(42, new Application.c6(x: 42).m);
56+
Expect.equals(42, new Application.c7(42).m);
57+
Expect.equals(42, new Application.c8(42).m);
58+
Expect.equals(42, new Application.c9(x: 42).m);
59+
Expect.equals(42, new Application.c10(42).m);
60+
Expect.equals(42, new Application.c11(42).m);
61+
Expect.equals(42, new Application.c12(x: 42).m);
62+
Expect.equals(42, new Application.c13(42).m);
63+
Expect.equals(42, new Application.c14(42).m);
64+
Expect.equals(42, new Application.c15(x: 42).m);
65+
66+
Expect.equals(37, new Application.c2().m);
67+
Expect.equals(37, new Application.c3().m);
68+
Expect.equals(37, new Application.c5().m);
69+
Expect.equals(37, new Application.c6().m);
70+
Expect.equals(87, new Application.c8().m);
71+
Expect.equals(87, new Application.c9().m);
72+
Expect.equals(87, new Application.c11().m);
73+
Expect.equals(87, new Application.c12().m);
74+
Expect.equals(87, new Application.c14().m);
75+
Expect.equals(87, new Application.c15().m);
76+
77+
Expect.equals(42, new Application2.c1(42).m);
78+
Expect.equals(42, new Application2.c2(42).m);
79+
Expect.equals(42, new Application2.c3(x: 42).m);
80+
Expect.equals(42, new Application2.c4(42).m);
81+
Expect.equals(42, new Application2.c5(42).m);
82+
Expect.equals(42, new Application2.c6(x: 42).m);
83+
Expect.equals(42, new Application2.c7(42).m);
84+
Expect.equals(42, new Application2.c8(42).m);
85+
Expect.equals(42, new Application2.c9(x: 42).m);
86+
Expect.equals(42, new Application2.c10(42).m);
87+
Expect.equals(42, new Application2.c11(42).m);
88+
Expect.equals(42, new Application2.c12(x: 42).m);
89+
Expect.equals(42, new Application2.c13(42).m);
90+
Expect.equals(42, new Application2.c14(42).m);
91+
Expect.equals(42, new Application2.c15(x: 42).m);
92+
93+
Expect.equals(37, new Application2.c2().m);
94+
Expect.equals(37, new Application2.c3().m);
95+
Expect.equals(37, new Application2.c5().m);
96+
Expect.equals(37, new Application2.c6().m);
97+
Expect.equals(87, new Application2.c8().m);
98+
Expect.equals(87, new Application2.c9().m);
99+
Expect.equals(87, new Application2.c11().m);
100+
Expect.equals(87, new Application2.c12().m);
101+
Expect.equals(87, new Application2.c14().m);
102+
Expect.equals(87, new Application2.c15().m);
103+
104+
// Don't make constructors const if mixin declares instance variable.
105+
const Application.c4(42); //# 00: compile-time error
106+
const Application.c5(42); //# 01: compile-time error
107+
const Application.c6(x: 42); //# 02: compile-time error
108+
const Application.c7(42); //# 03: compile-time error
109+
const Application.c8(42); //# 04: compile-time error
110+
const Application.c9(x: 42); //# 05: compile-time error
111+
const Application.c10(42); //# 06: compile-time error
112+
const Application.c11(42); //# 07: compile-time error
113+
const Application.c12(x: 42); //# 08: compile-time error
114+
const Application.c13(42); //# 09: compile-time error
115+
const Application.c14(42); //# 10: compile-time error
116+
const Application.c15(x: 42); //# 11: compile-time error
117+
118+
const Application2.c4(42); //# 12: compile-time error
119+
const Application2.c5(42); //# 13: compile-time error
120+
const Application2.c6(x: 42); //# 14: compile-time error
121+
const Application2.c7(42); //# 15: compile-time error
122+
const Application2.c8(42); //# 16: compile-time error
123+
const Application2.c9(x: 42); //# 17: compile-time error
124+
const Application2.c10(42); //# 18: compile-time error
125+
const Application2.c11(42); //# 19: compile-time error
126+
const Application2.c12(x: 42); //# 20: compile-time error
127+
const Application2.c13(42); //# 21: compile-time error
128+
const Application2.c14(42); //# 22: compile-time error
129+
const Application2.c15(x: 42); //# 23: compile-time error
130+
131+
// Only insert forwarders for generative constructors.
132+
new Application(); //# 24: compile-time error
133+
new Application2(); //# 25: compile-time error
134+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2018, 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+
import "package:expect/expect.dart";
6+
7+
abstract class Mixin {
8+
int get x;
9+
int get m => x;
10+
}
11+
12+
class Base {
13+
final int x;
14+
Base.c1(this.x);
15+
Base.c2({this.x = 37});
16+
Base.c3(int x) : this.c1(x);
17+
Base.c4({int x = 37}) : this.c1(x);
18+
Base.c5(int x) : this.c2(x: x);
19+
Base.c6({int x = 37}) : this.c2(x: x);
20+
factory Base() = Base.c2;
21+
}
22+
23+
class Application = Base with Mixin;
24+
25+
class Application2 extends Base with Mixin {}
26+
27+
main() {
28+
Expect.equals(42, new Application.c1(42).m);
29+
Expect.equals(42, new Application.c2(x: 42).m);
30+
Expect.equals(42, new Application.c3(42).m);
31+
Expect.equals(42, new Application.c4(x: 42).m);
32+
Expect.equals(42, new Application.c5(42).m);
33+
Expect.equals(42, new Application.c6(x: 42).m);
34+
Expect.equals(37, new Application.c2().m);
35+
Expect.equals(37, new Application.c4().m);
36+
Expect.equals(37, new Application.c6().m);
37+
38+
Expect.equals(42, new Application2.c1(42).m);
39+
Expect.equals(42, new Application2.c2(x: 42).m);
40+
Expect.equals(42, new Application2.c3(42).m);
41+
Expect.equals(42, new Application2.c4(x: 42).m);
42+
Expect.equals(42, new Application2.c5(42).m);
43+
Expect.equals(42, new Application2.c6(x: 42).m);
44+
Expect.equals(37, new Application2.c2().m);
45+
Expect.equals(37, new Application2.c4().m);
46+
Expect.equals(37, new Application2.c6().m);
47+
48+
// Only insert forwarders for generative constructors.
49+
new Application(); //# 01: compile-time error
50+
new Application2(); //# 02: compile-time error
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2018, 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+
import "package:expect/expect.dart";
6+
7+
abstract class Mixin {
8+
int get x;
9+
int get m => x;
10+
}
11+
12+
class Base {
13+
final int x;
14+
Base.c1(this.x);
15+
Base.c2([this.x = 37]);
16+
Base.c3(int x) : this.c1(x);
17+
Base.c4([int x = 37]) : this.c1(x);
18+
Base.c5(int x) : this.c2(x);
19+
Base.c6([int x = 37]) : this.c2(x);
20+
factory Base() = Base.c2;
21+
}
22+
23+
class Application = Base with Mixin;
24+
25+
class Application2 extends Base with Mixin {}
26+
27+
main() {
28+
Expect.equals(42, new Application.c1(42).m);
29+
Expect.equals(42, new Application.c2(42).m);
30+
Expect.equals(42, new Application.c3(42).m);
31+
Expect.equals(42, new Application.c4(42).m);
32+
Expect.equals(42, new Application.c5(42).m);
33+
Expect.equals(42, new Application.c6(42).m);
34+
Expect.equals(37, new Application.c2().m);
35+
Expect.equals(37, new Application.c4().m);
36+
Expect.equals(37, new Application.c6().m);
37+
38+
Expect.equals(42, new Application2.c1(42).m);
39+
Expect.equals(42, new Application2.c2(42).m);
40+
Expect.equals(42, new Application2.c3(42).m);
41+
Expect.equals(42, new Application2.c4(42).m);
42+
Expect.equals(42, new Application2.c5(42).m);
43+
Expect.equals(42, new Application2.c6(42).m);
44+
Expect.equals(37, new Application2.c2().m);
45+
Expect.equals(37, new Application2.c4().m);
46+
Expect.equals(37, new Application2.c6().m);
47+
48+
// Only insert forwarders for generative constructors.
49+
new Application(); //# 01: compile-time error
50+
new Application2(); //# 02: compile-time error
51+
}

0 commit comments

Comments
 (0)