diff --git a/tests/language/mixin_constructor_forwarding/mixin_constructor_parameter_forwarding_helper.dart b/tests/language/mixin_constructor_forwarding/mixin_constructor_parameter_forwarding_helper.dart new file mode 100644 index 00000000000..58d2081a3e0 --- /dev/null +++ b/tests/language/mixin_constructor_forwarding/mixin_constructor_parameter_forwarding_helper.dart @@ -0,0 +1,23 @@ +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import "mixin_constructor_parameter_forwarding_test.dart"; + +// A private class that the mixin application cannot access syntactically, +// yet it needs an instance of it for the default value. +class _Private { + const _Private(); +} + +class B2 implements B { + final T x; + final Object y; + const B2(T x, [Object y = const _Private()]) + : x = x, + y = y; +} + +// Leaking the constant value in a non-constant way which cannot be used +// for the default value of the mixin application. +Object get privateValue => const _Private(); diff --git a/tests/language/mixin_constructor_forwarding/mixin_constructor_parameter_forwarding_test.dart b/tests/language/mixin_constructor_forwarding/mixin_constructor_parameter_forwarding_test.dart new file mode 100644 index 00000000000..0aba8b9ec79 --- /dev/null +++ b/tests/language/mixin_constructor_forwarding/mixin_constructor_parameter_forwarding_test.dart @@ -0,0 +1,77 @@ +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Tests that mixin application forwarding constructors correctly forward +// optional parameter default values. + +import "package:expect/expect.dart"; + +import "mixin_constructor_parameter_forwarding_helper.dart" + show B2, privateValue; + +class B { + final T x; + final Object y; + const B(T x, [Object y = 0]) + : x = x, + y = y; + const B.b1(T x, {Object y = 0}) + : x = x, + y = y; + const B.b2(T x, [Object y = const [0]]) + : x = x, + y = y; +} + +mixin M1 on B { + void check(int x, Object y) { + Expect.identical(x, this.x); + Expect.identical(y, this.y); + } +} + +mixin M2 on B { + void check(T x, Object y) { + Expect.identical(x, this.x); + Expect.identical(y, this.y); + } +} + +class A1 = B with M1; +class A2 = B with M2; +class P1 = B2 with M2; + +main() { + A1(1, 2).check(1, 2); + A1.b1(1, y: 2).check(1, 2); + A1.b2(1, 2).check(1, 2); + A2(1, 2).check(1, 2); + A2.b1(1, y: 2).check(1, 2); + A2.b2(1, 2).check(1, 2); + P1(1, 2).check(1, 2); + + A1(1).check(1, 0); + A1.b1(1).check(1, 0); + A1.b2(1).check(1, const [0]); + A2(1).check(1, 0); + A2.b1(1).check(1, 0); + A2.b2(1).check(1, const [0]); + P1(1).check(1, privateValue); + + const A1(1, 2).check(1, 2); + const A1.b1(1, y: 2).check(1, 2); + const A1.b2(1, 2).check(1, 2); + const A2(1, 2).check(1, 2); + const A2.b1(1, y: 2).check(1, 2); + const A2.b2(1, 2).check(1, 2); + const P1(1, 2).check(1, 2); + + const A1(1).check(1, 0); + const A1.b1(1).check(1, 0); + const A1.b2(1).check(1, const [0]); + const A2(1).check(1, 0); + const A2.b1(1).check(1, 0); + const A2.b2(1).check(1, const [0]); + const P1(1).check(1, privateValue); +} diff --git a/tests/language_2/mixin_constructor_forwarding/mixin_constructor_parameter_forwarding_helper.dart b/tests/language_2/mixin_constructor_forwarding/mixin_constructor_parameter_forwarding_helper.dart new file mode 100644 index 00000000000..58d2081a3e0 --- /dev/null +++ b/tests/language_2/mixin_constructor_forwarding/mixin_constructor_parameter_forwarding_helper.dart @@ -0,0 +1,23 @@ +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import "mixin_constructor_parameter_forwarding_test.dart"; + +// A private class that the mixin application cannot access syntactically, +// yet it needs an instance of it for the default value. +class _Private { + const _Private(); +} + +class B2 implements B { + final T x; + final Object y; + const B2(T x, [Object y = const _Private()]) + : x = x, + y = y; +} + +// Leaking the constant value in a non-constant way which cannot be used +// for the default value of the mixin application. +Object get privateValue => const _Private(); diff --git a/tests/language_2/mixin_constructor_forwarding/mixin_constructor_parameter_forwarding_test.dart b/tests/language_2/mixin_constructor_forwarding/mixin_constructor_parameter_forwarding_test.dart new file mode 100644 index 00000000000..0aba8b9ec79 --- /dev/null +++ b/tests/language_2/mixin_constructor_forwarding/mixin_constructor_parameter_forwarding_test.dart @@ -0,0 +1,77 @@ +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Tests that mixin application forwarding constructors correctly forward +// optional parameter default values. + +import "package:expect/expect.dart"; + +import "mixin_constructor_parameter_forwarding_helper.dart" + show B2, privateValue; + +class B { + final T x; + final Object y; + const B(T x, [Object y = 0]) + : x = x, + y = y; + const B.b1(T x, {Object y = 0}) + : x = x, + y = y; + const B.b2(T x, [Object y = const [0]]) + : x = x, + y = y; +} + +mixin M1 on B { + void check(int x, Object y) { + Expect.identical(x, this.x); + Expect.identical(y, this.y); + } +} + +mixin M2 on B { + void check(T x, Object y) { + Expect.identical(x, this.x); + Expect.identical(y, this.y); + } +} + +class A1 = B with M1; +class A2 = B with M2; +class P1 = B2 with M2; + +main() { + A1(1, 2).check(1, 2); + A1.b1(1, y: 2).check(1, 2); + A1.b2(1, 2).check(1, 2); + A2(1, 2).check(1, 2); + A2.b1(1, y: 2).check(1, 2); + A2.b2(1, 2).check(1, 2); + P1(1, 2).check(1, 2); + + A1(1).check(1, 0); + A1.b1(1).check(1, 0); + A1.b2(1).check(1, const [0]); + A2(1).check(1, 0); + A2.b1(1).check(1, 0); + A2.b2(1).check(1, const [0]); + P1(1).check(1, privateValue); + + const A1(1, 2).check(1, 2); + const A1.b1(1, y: 2).check(1, 2); + const A1.b2(1, 2).check(1, 2); + const A2(1, 2).check(1, 2); + const A2.b1(1, y: 2).check(1, 2); + const A2.b2(1, 2).check(1, 2); + const P1(1, 2).check(1, 2); + + const A1(1).check(1, 0); + const A1.b1(1).check(1, 0); + const A1.b2(1).check(1, const [0]); + const A2(1).check(1, 0); + const A2.b1(1).check(1, 0); + const A2.b2(1).check(1, const [0]); + const P1(1).check(1, privateValue); +}