|
| 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 | +} |
0 commit comments