From 6dbe0dc268d574d9be9c427cc046ae6435b65710 Mon Sep 17 00:00:00 2001 From: "Sergey G. Grekhov" Date: Fri, 23 Feb 2024 13:01:37 +0200 Subject: [PATCH] #2548. Add tests for out parameter in a contrvariant position (#2555) Add tests for out parameter in a contravariant position. --- .../out_non_covariant_A01_t01.dart | 88 ++++++++++++++++++ .../out_non_covariant_A01_t02.dart | 68 ++++++++++++++ .../out_non_covariant_A01_t03.dart | 68 ++++++++++++++ .../out_non_covariant_A01_t04.dart | 89 ++++++++++++++++++ .../out_non_covariant_A01_t05.dart | 85 ++++++++++++++++++ .../out_non_covariant_A01_t06.dart | 90 +++++++++++++++++++ 6 files changed, 488 insertions(+) create mode 100644 TypeSystem/type-variance/out_non_covariant_A01_t01.dart create mode 100644 TypeSystem/type-variance/out_non_covariant_A01_t02.dart create mode 100644 TypeSystem/type-variance/out_non_covariant_A01_t03.dart create mode 100644 TypeSystem/type-variance/out_non_covariant_A01_t04.dart create mode 100644 TypeSystem/type-variance/out_non_covariant_A01_t05.dart create mode 100644 TypeSystem/type-variance/out_non_covariant_A01_t06.dart diff --git a/TypeSystem/type-variance/out_non_covariant_A01_t01.dart b/TypeSystem/type-variance/out_non_covariant_A01_t01.dart new file mode 100644 index 0000000000..74be320db6 --- /dev/null +++ b/TypeSystem/type-variance/out_non_covariant_A01_t01.dart @@ -0,0 +1,88 @@ +// Copyright (c) 2024, 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. + +/// @assertion Let `D` be the declaration of a class or mixin, enum or a type +/// extension and let `X` be a type parameter declared by `D`. +/// +/// If `X` has the variance modifier `out` then it is a compile-time error for +/// `X` to occur in a non-covariant position in a member signature in the body +/// of `D`, except that it is not an error if it occurs in a covariant position +/// in the type annotation of a formal parameter which is covariant (this is a +/// contravariant position in the member signature as a whole). +/// +/// In particular, `X` can not be the type of a method parameter (unless it is +/// covariant). It can never be the bound of a type parameter of a generic +/// method +/// +/// @description Check that it is a compile-time error if a type parameter has a +/// variance modifier `out` and occurs in a contravariant position in a member +/// signature in the body of `D` +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=variance,inline-class + +class C { + C(X x) {} // No error here + factory C.f(X x) => C(x); // And here + + void f(X x) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + void set s(X x) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +mixin M { + void f(X x); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + void set s(X x); +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +enum E { + e; + + const E(); + const E.n(X x); // No error here + + void f(X x) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + void set s(X x) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +extension type ET(X x) { + ET.n(X x) : this.x = x; // No error here + + void f(X x) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + void set s(X x) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void main() { + print(C); + print(M); + print(E); + print(ET); +} diff --git a/TypeSystem/type-variance/out_non_covariant_A01_t02.dart b/TypeSystem/type-variance/out_non_covariant_A01_t02.dart new file mode 100644 index 0000000000..254b8c6a06 --- /dev/null +++ b/TypeSystem/type-variance/out_non_covariant_A01_t02.dart @@ -0,0 +1,68 @@ +// Copyright (c) 2024, 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. + +/// @assertion Let `D` be the declaration of a class or mixin, enum or a type +/// extension and let `X` be a type parameter declared by `D`. +/// +/// If `X` has the variance modifier `out` then it is a compile-time error for +/// `X` to occur in a non-covariant position in a member signature in the body +/// of `D`, except that it is not an error if it occurs in a covariant position +/// in the type annotation of a formal parameter which is covariant (this is a +/// contravariant position in the member signature as a whole). +/// +/// In particular, `X` can not be the type of a method parameter (unless it is +/// covariant). It can never be the bound of a type parameter of a generic +/// method +/// +/// @description Check that it is a compile-time error if a type parameter has a +/// variance modifier `out` and occurs in a contravariant position in a member +/// signature in the body of `D` +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=variance,inline-class + +class C { + C([X? x]) {} // No error here + factory C.f([X? x]) => C(x); // And here + + void f([X? x]) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +mixin M { + void f([X? x]); +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +enum E { + e; + + const E(); + const E.n([X? x]); // No error here + + void f([X? x]) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +extension type ET(X? x) { + ET.n([X? x]) : this.x = x; // No error here + + void f([X? x]) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void main() { + print(C); + print(M); + print(E); + print(ET); +} diff --git a/TypeSystem/type-variance/out_non_covariant_A01_t03.dart b/TypeSystem/type-variance/out_non_covariant_A01_t03.dart new file mode 100644 index 0000000000..cad709985d --- /dev/null +++ b/TypeSystem/type-variance/out_non_covariant_A01_t03.dart @@ -0,0 +1,68 @@ +// Copyright (c) 2024, 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. + +/// @assertion Let `D` be the declaration of a class or mixin, enum or a type +/// extension and let `X` be a type parameter declared by `D`. +/// +/// If `X` has the variance modifier `out` then it is a compile-time error for +/// `X` to occur in a non-covariant position in a member signature in the body +/// of `D`, except that it is not an error if it occurs in a covariant position +/// in the type annotation of a formal parameter which is covariant (this is a +/// contravariant position in the member signature as a whole). +/// +/// In particular, `X` can not be the type of a method parameter (unless it is +/// covariant). It can never be the bound of a type parameter of a generic +/// method +/// +/// @description Check that it is a compile-time error if a type parameter has a +/// variance modifier `out` and occurs in a contravariant position in a member +/// signature in the body of `D` +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=variance,inline-class + +class C { + C({X? x}) {} // No error here + factory C.f({X? x}) => C(x); // And here + + void f({X? x}) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +mixin M { + void f({X? x}); +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +enum E { + e; + + const E(); + const E.n({X? x}); // No error here + + void f({X? x}) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +extension type ET(X? x) { + ET.n({X? x}) : this.x = x; // No error here + + void f({X? x}) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void main() { + print(C); + print(M); + print(E); + print(ET); +} diff --git a/TypeSystem/type-variance/out_non_covariant_A01_t04.dart b/TypeSystem/type-variance/out_non_covariant_A01_t04.dart new file mode 100644 index 0000000000..d67dd531aa --- /dev/null +++ b/TypeSystem/type-variance/out_non_covariant_A01_t04.dart @@ -0,0 +1,89 @@ +// Copyright (c) 2024, 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. + +/// @assertion Let `D` be the declaration of a class or mixin, enum or a type +/// extension and let `X` be a type parameter declared by `D`. +/// +/// If `X` has the variance modifier `out` then it is a compile-time error for +/// `X` to occur in a non-covariant position in a member signature in the body +/// of `D`, except that it is not an error if it occurs in a covariant position +/// in the type annotation of a formal parameter which is covariant (this is a +/// contravariant position in the member signature as a whole). +/// +/// In particular, `X` can not be the type of a method parameter (unless it is +/// covariant). It can never be the bound of a type parameter of a generic +/// method +/// +/// @description Check that it is a compile-time error if a type parameter has a +/// variance modifier `out` and occurs in a contravariant position in a member +/// signature in the body of `D` (it occurs in a contravariant position in a +/// covariant parameter). +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=variance,inline-class + +typedef C = T; +typedef ContravariantT = void Function(T); + +class A { + final C>? v = null; +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + C>? f() {} +//^^^^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + C>? get g => null; +//^^^^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +mixin M { + C>? f() {} +//^^^^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + C>? get g => null; +//^^^^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +enum E { + e; + + ContravariantT? f() {} +//^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + ContravariantT? get g => null; +//^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +extension type ET(X x) { + ContravariantT? f() {} +//^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + ContravariantT? get g => null; +//^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void main() { + print(C); + print(M); + print(E); + print(ET); +} diff --git a/TypeSystem/type-variance/out_non_covariant_A01_t05.dart b/TypeSystem/type-variance/out_non_covariant_A01_t05.dart new file mode 100644 index 0000000000..f2c87647f1 --- /dev/null +++ b/TypeSystem/type-variance/out_non_covariant_A01_t05.dart @@ -0,0 +1,85 @@ +// Copyright (c) 2024, 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. + +/// @assertion Let `D` be the declaration of a class or mixin, enum or a type +/// extension and let `X` be a type parameter declared by `D`. +/// +/// If `X` has the variance modifier `out` then it is a compile-time error for +/// `X` to occur in a non-covariant position in a member signature in the body +/// of `D`, except that it is not an error if it occurs in a covariant position +/// in the type annotation of a formal parameter which is covariant (this is a +/// contravariant position in the member signature as a whole). +/// +/// In particular, `X` can not be the type of a method parameter (unless it is +/// covariant). It can never be the bound of a type parameter of a generic +/// method +/// +/// @description Check that it is a compile-time error if a type parameter has a +/// variance modifier `out` and occurs in a contravariant position in a member +/// signature in the body of `D` (it occurs in a covariant position in a +/// contravariant parameter). +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=variance,inline-class + +typedef CovariantT = T Function(); + +class C { + final CovariantT? v = null; // Ok, covariant + + f(CovariantT v) {} +// ^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + void set s(CovariantT v) {} +// ^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +mixin M { + f(CovariantT v) {} +// ^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + void set s(CovariantT v) {} +// ^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +enum E { + e; + + f(CovariantT v) {} +// ^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + void set s(CovariantT v) {} +// ^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +extension type ET(X x) { + f(CovariantT v) {} +// ^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + void set s(CovariantT v) {} +// ^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void main() { + print(C); + print(M); + print(E); + print(ET); +} diff --git a/TypeSystem/type-variance/out_non_covariant_A01_t06.dart b/TypeSystem/type-variance/out_non_covariant_A01_t06.dart new file mode 100644 index 0000000000..8768a650ec --- /dev/null +++ b/TypeSystem/type-variance/out_non_covariant_A01_t06.dart @@ -0,0 +1,90 @@ +// Copyright (c) 2024, 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. + +/// @assertion Let `D` be the declaration of a class or mixin, enum or a type +/// extension and let `X` be a type parameter declared by `D`. +/// +/// If `X` has the variance modifier `out` then it is a compile-time error for +/// `X` to occur in a non-covariant position in a member signature in the body +/// of `D`, except that it is not an error if it occurs in a covariant position +/// in the type annotation of a formal parameter which is covariant (this is a +/// contravariant position in the member signature as a whole). +/// +/// In particular, `X` can not be the type of a method parameter (unless it is +/// covariant). It can never be the bound of a type parameter of a generic +/// method +/// +/// @description Check that it is a compile-time error if a type parameter has a +/// variance modifier `out` and occurs in a contravariant position in a member +/// signature in the body of `D` (it occurs in a contravariant position in a +/// `FutureOr` type). +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=variance,inline-class + +import 'dart:async'; + +typedef ContravariantT = void Function(T); + +class C { + final FutureOr>? v = null; +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + FutureOr>? f() {} +//^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + FutureOr>? get g => null; +//^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +mixin M { + FutureOr>? f() {} +//^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + FutureOr>? get g => null; +//^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +enum E { + e; + + FutureOr>? f() {} +//^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + FutureOr>? get g => null; +//^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +extension type ET(X x) { + FutureOr>? f() {} +//^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + FutureOr>? get g => null; +//^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void main() { + print(C); + print(M); + print(E); + print(ET); +}