From a4f8f9c07bb080edabf51f86b533541ee82e02bf Mon Sep 17 00:00:00 2001 From: sgrekhov Date: Thu, 28 Dec 2023 11:48:55 +0200 Subject: [PATCH] #1400. Add object pattern tests for extension types --- ...atic_analysis_extension_types_A32_t01.dart | 38 ++++++++++++++ ...atic_analysis_extension_types_A32_t02.dart | 39 +++++++++++++++ ...atic_analysis_extension_types_A32_t03.dart | 50 +++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 LanguageFeatures/Extension-types/static_analysis_extension_types_A32_t01.dart create mode 100644 LanguageFeatures/Extension-types/static_analysis_extension_types_A32_t02.dart create mode 100644 LanguageFeatures/Extension-types/static_analysis_extension_types_A32_t03.dart diff --git a/LanguageFeatures/Extension-types/static_analysis_extension_types_A32_t01.dart b/LanguageFeatures/Extension-types/static_analysis_extension_types_A32_t01.dart new file mode 100644 index 0000000000..0e9ae91bd3 --- /dev/null +++ b/LanguageFeatures/Extension-types/static_analysis_extension_types_A32_t01.dart @@ -0,0 +1,38 @@ +// Copyright (c) 2023, 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 An extension type V (which may include actual type arguments) can +/// be used in an object pattern (e.g., case V(): ... where V is an extension +/// type). Exhaustiveness analysis will treat such patterns as if they had been +/// an object pattern matching the extension type erasure of V. +/// +/// @description Check that an extension type can be used in an object pattern +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=inline-class + +import "../../Utils/expect.dart"; + +class C { + int x; + C(this.x); +} + +extension type ET1(C id) {} +extension type ET2(C id) implements C {} + +main() { + var ET1() = ET1(C(0)); + var ET2(x: v1) = ET2(C(1)); + Expect.equals(1, v1); + + var C(x: v2) = ET2(C(2)); + Expect.equals(2, v2); + + var ET1(id: v3) = ET1(C(3)); + Expect.equals(3, v3.x); + + var ET2(id: v4) = ET2(C(4)); + Expect.equals(4, v4.x); +} diff --git a/LanguageFeatures/Extension-types/static_analysis_extension_types_A32_t02.dart b/LanguageFeatures/Extension-types/static_analysis_extension_types_A32_t02.dart new file mode 100644 index 0000000000..e02a3b4473 --- /dev/null +++ b/LanguageFeatures/Extension-types/static_analysis_extension_types_A32_t02.dart @@ -0,0 +1,39 @@ +// Copyright (c) 2023, 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 An extension type V (which may include actual type arguments) can +/// be used in an object pattern (e.g., case V(): ... where V is an extension +/// type). Exhaustiveness analysis will treat such patterns as if they had been +/// an object pattern matching the extension type erasure of V. +/// +/// @description Check that an extension type can be used in an object pattern. +/// Test generics +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=inline-class + +import "../../Utils/expect.dart"; + +class C { + T x; + C(this.x); +} + +extension type ET1(C id) {} +extension type ET2(C id) implements C {} + +main() { + var ET1() = ET1(C(3.14)); + var ET2(x: v1) = ET2(C(1)); + Expect.equals(1, v1); + + var C(x: v2) = ET2(C("2")); + Expect.equals("2", v2); + + var ET1(id: v3) = ET1(C("3")); + Expect.equals("3", v3.x); + + var ET2(id: v4) = ET2(C(4)); + Expect.equals(4, v4.x); +} diff --git a/LanguageFeatures/Extension-types/static_analysis_extension_types_A32_t03.dart b/LanguageFeatures/Extension-types/static_analysis_extension_types_A32_t03.dart new file mode 100644 index 0000000000..6c46d076b3 --- /dev/null +++ b/LanguageFeatures/Extension-types/static_analysis_extension_types_A32_t03.dart @@ -0,0 +1,50 @@ +// Copyright (c) 2023, 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 An extension type V (which may include actual type arguments) can +/// be used in an object pattern (e.g., case V(): ... where V is an extension +/// type). Exhaustiveness analysis will treat such patterns as if they had been +/// an object pattern matching the extension type erasure of V. +/// +/// @description Check that an extension type erasure is not used in an object +/// pattern outside of switch statements and expressions +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=inline-class + +class C { + int x; + C(this.x); +} + +extension type ET1(C id) {} +extension type ET2(C id) {} +extension type ET3(C id) implements C {} + +main() { + var C(x: v1) = ET1(C(1)); +// ^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + var ET1(x: v2) = ET1(C(2)); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + var ET1() = C(42); +// ^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + var ET1() = ET2(C(42)); +// ^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + var ET3(x: v3) = C(42); +// ^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + var ET3(x: v4) = ET1(C(42)); +// ^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +}