From c2d76581f574ab1330675ab8471b5b4499d29bc9 Mon Sep 17 00:00:00 2001 From: "Sergey G. Grekhov" Date: Tue, 11 Apr 2023 10:25:30 +0300 Subject: [PATCH] Fixes #2010. Fix exhaustiveness tests for a cast-pattern (#2018) --- .../lifting_cast_pattern_A01_t01.dart | 21 ++----- .../lifting_cast_pattern_A01_t02.dart | 49 ++++------------ .../lifting_cast_pattern_A02_t01.dart | 58 +++++++++++++++++++ 3 files changed, 75 insertions(+), 53 deletions(-) create mode 100644 LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A02_t01.dart diff --git a/LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A01_t01.dart b/LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A01_t01.dart index 9c01ddfbbb..026514dfb2 100644 --- a/LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A01_t01.dart +++ b/LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A01_t01.dart @@ -12,15 +12,14 @@ /// space union of E. /// /// @description Check a lifted space of a cast pattern in case of not sealed -/// type +/// type. Test switch statement /// @author sgrekhov22@gmail.com // SharedOptions=--enable-experiment=patterns -int test1(Object obj) { -// ^^^^^ -// [analyzer] unspecified -// [cfe] unspecified +import "../../../Utils/expect.dart"; + +int test(Object obj) { switch (obj) { case int(isEven: true) as int: return 1; @@ -29,15 +28,7 @@ int test1(Object obj) { } } -int test2(Object obj) => switch (obj) { -// ^^^^^^ -// [analyzer] unspecified -// [cfe] unspecified - int(isEven: true) as int => 1, - int _ => 2 - }; - main() { - test1(1); - test2(2); + Expect.equals(2 ,test(1)); + Expect.equals(1 ,test(2)); } diff --git a/LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A01_t02.dart b/LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A01_t02.dart index 6415151f72..aa915968aa 100644 --- a/LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A01_t02.dart +++ b/LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A01_t02.dart @@ -9,50 +9,23 @@ /// - The lifted space union of the cast's subpattern in context C. /// - For each space E in the expanded spaces of M: /// a. If E is not a subset of C and C is not a subset of M, then the lifted -/// space union of E. +/// space union of E. /// -/// @description Check a lifted space of a cast pattern in case of a sealed type +/// @description Check a lifted space of a cast pattern in case of not sealed +/// type. Test switch element /// @author sgrekhov22@gmail.com -/// @issue 51877 +/// @issue 51986 -// SharedOptions=--enable-experiment=patterns,class-modifiers +// SharedOptions=--enable-experiment=patterns import "../../../Utils/expect.dart"; -sealed class A { - final int field; - A(this.field); -} -class B extends A { - B(int field) : super(field); -} -class C extends A { - C(int field) : super(field); -} - -test1(A a) { - switch (a) { - case C(field: 0) as C: - return 0; - case C _: - return 1; - } -} - -test2(A a) => switch (a) { - C(field: 0) as C => 0, - C _ => 1 -}; +int test(Object obj) => switch (obj) { + int(isEven: true) as int => 1, + int _ => 2 + }; main() { - Expect.equals(0, test1(C(0))); - Expect.equals(1, test1(C(1))); - Expect.throws(() { - test1(B(0)); - }); - Expect.equals(0, test2(C(0))); - Expect.equals(1, test2(C(1))); - Expect.throws(() { - test2(B(0)); - }); + Expect.equals(2 ,test(1)); + Expect.equals(1 ,test(2)); } diff --git a/LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A02_t01.dart b/LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A02_t01.dart new file mode 100644 index 0000000000..6415151f72 --- /dev/null +++ b/LanguageFeatures/Patterns/Exhaustiveness/lifting_cast_pattern_A02_t01.dart @@ -0,0 +1,58 @@ +// 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 The lifted space union for a pattern with matched value type M is +/// ... +/// Cast pattern: +/// The space union spaces for a cast pattern with cast type C is a union of: +/// - The lifted space union of the cast's subpattern in context C. +/// - For each space E in the expanded spaces of M: +/// a. If E is not a subset of C and C is not a subset of M, then the lifted +/// space union of E. +/// +/// @description Check a lifted space of a cast pattern in case of a sealed type +/// @author sgrekhov22@gmail.com +/// @issue 51877 + +// SharedOptions=--enable-experiment=patterns,class-modifiers + +import "../../../Utils/expect.dart"; + +sealed class A { + final int field; + A(this.field); +} +class B extends A { + B(int field) : super(field); +} +class C extends A { + C(int field) : super(field); +} + +test1(A a) { + switch (a) { + case C(field: 0) as C: + return 0; + case C _: + return 1; + } +} + +test2(A a) => switch (a) { + C(field: 0) as C => 0, + C _ => 1 +}; + +main() { + Expect.equals(0, test1(C(0))); + Expect.equals(1, test1(C(1))); + Expect.throws(() { + test1(B(0)); + }); + Expect.equals(0, test2(C(0))); + Expect.equals(1, test2(C(1))); + Expect.throws(() { + test2(B(0)); + }); +}