From 150b77600ac179042ceb530c702adb0a78ec324e Mon Sep 17 00:00:00 2001 From: sgrekhov Date: Fri, 24 Mar 2023 15:05:42 +0300 Subject: [PATCH] #1401. If- and for-elements tests for maps and sets added --- ...execution_pattern_for_element_A01_t06.dart | 31 +++++ ...execution_pattern_for_element_A01_t07.dart | 36 +++++ ...execution_pattern_for_element_A01_t08.dart | 39 ++++++ ...execution_pattern_for_element_A01_t09.dart | 36 +++++ ...execution_pattern_for_element_A01_t10.dart | 31 +++++ ...execution_pattern_for_element_A01_t11.dart | 27 ++++ ...execution_pattern_for_element_A01_t12.dart | 27 ++++ ...execution_pattern_for_element_A01_t13.dart | 30 ++++ ...execution_pattern_for_element_A01_t14.dart | 23 ++++ ...execution_pattern_for_element_A01_t15.dart | 31 +++++ ...execution_pattern_for_element_A02_t01.dart | 2 +- ...execution_pattern_for_element_A02_t02.dart | 54 ++++++++ ...execution_pattern_for_element_A02_t03.dart | 54 ++++++++ ...cution_pattern_for_in_element_A01_t05.dart | 128 ++++++++++++++++++ ...cution_pattern_for_in_element_A01_t06.dart | 122 +++++++++++++++++ ...cution_pattern_for_in_element_A01_t07.dart | 84 ++++++++++++ ...cution_pattern_for_in_element_A01_t08.dart | 74 ++++++++++ ...cution_pattern_for_in_element_A01_t09.dart | 128 ++++++++++++++++++ ...cution_pattern_for_in_element_A01_t10.dart | 122 +++++++++++++++++ ...cution_pattern_for_in_element_A01_t11.dart | 83 ++++++++++++ ...cution_pattern_for_in_element_A01_t12.dart | 74 ++++++++++ ...cution_pattern_for_in_element_A02_t01.dart | 2 +- ...cution_pattern_for_in_element_A02_t02.dart | 74 ++++++++++ ...cution_pattern_for_in_element_A02_t03.dart | 74 ++++++++++ ...cution_pattern_for_in_element_A03_t01.dart | 2 +- ...cution_pattern_for_in_element_A03_t02.dart | 74 ++++++++++ ...cution_pattern_for_in_element_A03_t03.dart | 74 ++++++++++ ...cution_pattern_for_in_element_A04_t01.dart | 2 +- ...cution_pattern_for_in_element_A04_t02.dart | 74 ++++++++++ ...cution_pattern_for_in_element_A04_t03.dart | 74 ++++++++++ ...ution_pattern_if_case_element_A01_t01.dart | 4 +- ...ution_pattern_if_case_element_A01_t04.dart | 88 ++++++++++++ ...ution_pattern_if_case_element_A01_t05.dart | 70 ++++++++++ ...ution_pattern_if_case_element_A01_t06.dart | 43 ++++++ ...ution_pattern_if_case_element_A01_t07.dart | 87 ++++++++++++ ...ution_pattern_if_case_element_A01_t08.dart | 70 ++++++++++ ...ution_pattern_if_case_element_A01_t09.dart | 43 ++++++ ...ution_pattern_if_case_element_A02_t02.dart | 53 ++++++++ ...ution_pattern_if_case_element_A02_t03.dart | 52 +++++++ ...ution_pattern_if_case_element_A03_t01.dart | 4 +- ...ution_pattern_if_case_element_A03_t02.dart | 42 ++++++ ...ution_pattern_if_case_element_A03_t03.dart | 42 ++++++ .../Patterns/invocation_keys_A07_t08.dart | 2 +- 43 files changed, 2278 insertions(+), 8 deletions(-) create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_element_A01_t06.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_element_A01_t07.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_element_A01_t08.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_element_A01_t09.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_element_A01_t10.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_element_A01_t11.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_element_A01_t12.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_element_A01_t13.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_element_A01_t14.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_element_A01_t15.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_element_A02_t02.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_element_A02_t03.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t05.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t06.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t07.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t08.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t09.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t10.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t11.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t12.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A02_t02.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A02_t03.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A03_t02.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A03_t03.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A04_t02.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_for_in_element_A04_t03.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t04.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t05.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t06.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t07.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t08.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t09.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_if_case_element_A02_t02.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_if_case_element_A02_t03.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_if_case_element_A03_t02.dart create mode 100644 LanguageFeatures/Patterns/execution_pattern_if_case_element_A03_t03.dart diff --git a/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t06.dart b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t06.dart new file mode 100644 index 0000000000..f0a8b63dac --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t06.dart @@ -0,0 +1,31 @@ +// 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 +/// Likewise, a collection element of the form: +/// +/// for (; ; ) +/// Is executed like a traditional for loop though is more likely to declare +/// multiple variables +/// +/// @description Check that a pattern-for element is executed like a traditional +/// for loop. Test variable and identifier subpatterns of a parenthesized +/// pattern. Test pattern-for element in a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns + +import "../../Utils/expect.dart"; + +main() { + var map1 = { + "k0": 0, + for (var (int i) = 1; i <= 3; (i) = (i + 1)) "k$i": i, + "k4": 4 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2, "k3": 3, "k4": 4}, map1); + int j = -1; + var map2 = {"k0": 0, for ((j) = 1; j < 4; (j) = (j + 1)) "k$j": j, "k4": 4}; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2, "k3": 3, "k4": 4}, map2); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t07.dart b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t07.dart new file mode 100644 index 0000000000..d6042cabcc --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t07.dart @@ -0,0 +1,36 @@ +// 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 +/// Likewise, a collection element of the form: +/// +/// for (; ; ) +/// Is executed like a traditional for loop though is more likely to declare +/// multiple variables +/// +/// @description Check that a pattern-for element is executed like a traditional +/// for loop. Test a list pattern in a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns + +import "../../Utils/expect.dart"; + +main() { + var map = { + "k-1": -1, + for (var [a, b] = [0, 1]; a <= 8; [a, b] = [b, a + b]) "k$a": a, + "k13": 13 + }; + Expect.mapEquals({ + "k-1": -1, + "k0": 0, + "k1": 1, + "k2": 2, + "k3": 3, + "k5": 5, + "k8": 8, + "k13": 13 + }, map); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t08.dart b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t08.dart new file mode 100644 index 0000000000..907aa365d6 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t08.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 +/// Likewise, a collection element of the form: +/// +/// for (; ; ) +/// Is executed like a traditional for loop though is more likely to declare +/// multiple variables +/// +/// @description Check that a pattern-for element is executed like a traditional +/// for loop. Test a map pattern in a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns + +import "../../Utils/expect.dart"; + +main() { + var map = { + "k-1": -1, + for (var {"k1": a, "k2": b} = {"k1": 0, "k2": 1}; + a <= 8; + {"k1": a, "k2": b} = {"k1": b, "k2": a + b}) + "k$a": a, + "k13": 13 + }; + Expect.mapEquals({ + "k-1": -1, + "k0": 0, + "k1": 1, + "k2": 2, + "k3": 3, + "k5": 5, + "k8": 8, + "k13": 13 + }, map); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t09.dart b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t09.dart new file mode 100644 index 0000000000..f0227638bb --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t09.dart @@ -0,0 +1,36 @@ +// 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 +/// Likewise, a collection element of the form: +/// +/// for (; ; ) +/// Is executed like a traditional for loop though is more likely to declare +/// multiple variables +/// +/// @description Check that a pattern-for element is executed like a traditional +/// for loop. Test a record pattern in a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; + +main() { + var map = { + "k-1": -1, + for (var (a, b) = (0, 1); a <= 8; (a, b) = (b, a + b)) "k$a": a, + "k13": 13 + }; + Expect.mapEquals({ + "k-1": -1, + "k0": 0, + "k1": 1, + "k2": 2, + "k3": 3, + "k5": 5, + "k8": 8, + "k13": 13 + }, map); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t10.dart b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t10.dart new file mode 100644 index 0000000000..3d476eaca9 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t10.dart @@ -0,0 +1,31 @@ +// 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 +/// Likewise, a collection element of the form: +/// +/// for (; ; ) +/// Is executed like a traditional for loop though is more likely to declare +/// multiple variables +/// +/// @description Check that a pattern-for element is executed like a traditional +/// for loop. Test an object pattern in a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "patterns_lib.dart"; + +main() { + var map = { + "k0": 0, + for (var Square(:areaAsInt) = Square(1); + areaAsInt <= 9; + Square(:areaAsInt) = Square((++areaAsInt).toDouble())) + "k$areaAsInt": areaAsInt, + "k42": 42 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k4": 4, "k42": 42}, map); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t11.dart b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t11.dart new file mode 100644 index 0000000000..441afdb4e6 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t11.dart @@ -0,0 +1,27 @@ +// 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 +/// Likewise, a collection element of the form: +/// +/// for (; ; ) +/// Is executed like a traditional for loop though is more likely to declare +/// multiple variables +/// +/// @description Check that a pattern-for element is executed like a traditional +/// for loop. Test variable and identifier subpatterns of a parenthesized +/// pattern. Test pattern-for element in a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns + +import "../../Utils/expect.dart"; + +main() { + var set1 = {0, for (var (int i) = 1; i <= 3; (i) = (i + 1)) i, 4}; + Expect.setEquals({0, 1, 2, 3, 4}, set1); + int j = -1; + var set2 = {0, for ((j) = 1; j < 4; (j) = (j + 1)) j, 4}; + Expect.setEquals({0, 1, 2, 3, 4}, set2); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t12.dart b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t12.dart new file mode 100644 index 0000000000..e8a18aafcc --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t12.dart @@ -0,0 +1,27 @@ +// 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 +/// Likewise, a collection element of the form: +/// +/// for (; ; ) +/// Is executed like a traditional for loop though is more likely to declare +/// multiple variables +/// +/// @description Check that a pattern-for element is executed like a traditional +/// for loop. Test a list pattern in a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns + +import "../../Utils/expect.dart"; + +main() { + var set = { + -1, + for (var [a, b] = [0, 1]; a <= 8; [a, b] = [b, a + b]) a, + 13 + }; + Expect.setEquals({-1, 0, 1, 2, 3, 5, 8, 13}, set); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t13.dart b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t13.dart new file mode 100644 index 0000000000..dcb4ae9e34 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t13.dart @@ -0,0 +1,30 @@ +// 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 +/// Likewise, a collection element of the form: +/// +/// for (; ; ) +/// Is executed like a traditional for loop though is more likely to declare +/// multiple variables +/// +/// @description Check that a pattern-for element is executed like a traditional +/// for loop. Test a map pattern in a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns + +import "../../Utils/expect.dart"; + +main() { + var set = { + -1, + for (var {"k1": a, "k2": b} = {"k1": 0, "k2": 1}; + a <= 8; + {"k1": a, "k2": b} = {"k1": b, "k2": a + b}) + a, + 13 + }; + Expect.setEquals({-1, 0, 1, 2, 3, 5, 8, 13}, set); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t14.dart b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t14.dart new file mode 100644 index 0000000000..e1ea60929a --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t14.dart @@ -0,0 +1,23 @@ +// 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 +/// Likewise, a collection element of the form: +/// +/// for (; ; ) +/// Is executed like a traditional for loop though is more likely to declare +/// multiple variables +/// +/// @description Check that a pattern-for element is executed like a traditional +/// for loop. Test a record pattern in a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; + +main() { + var set = {-1, for (var (a, b) = (0, 1); a <= 8; (a, b) = (b, a + b)) a, 13}; + Expect.setEquals({-1, 0, 1, 2, 3, 5, 8, 13}, set); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t15.dart b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t15.dart new file mode 100644 index 0000000000..c294cf2862 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_element_A01_t15.dart @@ -0,0 +1,31 @@ +// 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 +/// Likewise, a collection element of the form: +/// +/// for (; ; ) +/// Is executed like a traditional for loop though is more likely to declare +/// multiple variables +/// +/// @description Check that a pattern-for element is executed like a traditional +/// for loop. Test an object pattern in a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "patterns_lib.dart"; + +main() { + var set = { + 0, + for (var Square(:areaAsInt) = Square(1); + areaAsInt <= 9; + Square(:areaAsInt) = Square((++areaAsInt).toDouble())) + areaAsInt, + 42 + }; + Expect.setEquals({0, 1, 4, 42}, set); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_element_A02_t01.dart b/LanguageFeatures/Patterns/execution_pattern_for_element_A02_t01.dart index fa5f3960ed..151e107457 100644 --- a/LanguageFeatures/Patterns/execution_pattern_for_element_A02_t01.dart +++ b/LanguageFeatures/Patterns/execution_pattern_for_element_A02_t01.dart @@ -12,7 +12,7 @@ /// /// @description Check that a pattern-for element is executed like a traditional /// for loop. Test that it is a compile-time error if a final variable is -/// assigned in a pattern-for element +/// assigned in a pattern-for element in a list literal /// @author sgrekhov22@gmail.com // SharedOptions=--enable-experiment=patterns,records diff --git a/LanguageFeatures/Patterns/execution_pattern_for_element_A02_t02.dart b/LanguageFeatures/Patterns/execution_pattern_for_element_A02_t02.dart new file mode 100644 index 0000000000..b5f6b609f1 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_element_A02_t02.dart @@ -0,0 +1,54 @@ +// 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 +/// A statement of the form: +/// +/// for (; ; ) +/// +/// Is executed like a traditional for loop though is more likely to declare +/// multiple variables. +/// +/// @description Check that a pattern-for element is executed like a traditional +/// for loop. Test that it is a compile-time error if a final variable is +/// assigned in a pattern-for element in a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "patterns_lib.dart"; + +main() { + var m1 = { + for (final (int i) = 0; i < 3; i++) "k$i": i, +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + var m2 = { + for (final [a] = [0]; a < 3; [a] = [a + 1]) "k$a": a, +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + var m3 = { + for (final {"k1": a} = {"k1": 0}; a < 3; {"k1": a} = {"k1": a + 1}) "k$a": a, +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + var m4 = { + for (final (a,) = (0,); a < 3; (a,) = (a + 1,)) "k$a": a, +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + var m5 = { + for (final Square(:areaAsInt) = Square(1); areaAsInt < 2; + Square(:areaAsInt) = Square(areaAsInt + 1)) "k$areaAsInt": areaAsInt, +// ^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + }; +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_element_A02_t03.dart b/LanguageFeatures/Patterns/execution_pattern_for_element_A02_t03.dart new file mode 100644 index 0000000000..e3e9f66182 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_element_A02_t03.dart @@ -0,0 +1,54 @@ +// 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 +/// A statement of the form: +/// +/// for (; ; ) +/// +/// Is executed like a traditional for loop though is more likely to declare +/// multiple variables. +/// +/// @description Check that a pattern-for element is executed like a traditional +/// for loop. Test that it is a compile-time error if a final variable is +/// assigned in a pattern-for element in a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "patterns_lib.dart"; + +main() { + var s1 = { + for (final (int i) = 0; i < 3; i++) i, +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + var s2 = { + for (final [a] = [0]; a < 3; [a] = [a + 1]) a, +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + var s3 = { + for (final {"k1": a} = {"k1": 0}; a < 3; {"k1": a} = {"k1": a + 1}) a, +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + var s4 = { + for (final (a,) = (0,); a < 3; (a,) = (a + 1,)) a, +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + var s5 = { + for (final Square(:areaAsInt) = Square(1); areaAsInt < 2; + Square(:areaAsInt) = Square(areaAsInt + 1)) areaAsInt, +// ^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + }; +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t05.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t05.dart new file mode 100644 index 0000000000..7ecc5643cf --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t05.dart @@ -0,0 +1,128 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that if `I` implements `Iterable` for some `T` then +/// `E` is `T`. Test `T` inferred from `` to `` and from +/// `` to ``. Test a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "../../Utils/static_type_helper.dart"; +import "patterns_lib.dart"; + +extension Storing on Object? { + static dynamic stored; + void get store => stored = this; +} + +main() { + var m1 = { + "k0": 0, + for (var (num v1) in [1, 2, 3]..store) "k$v1": v1, + "k4": 4 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2, "k3": 3, "k4": 4}, m1); + Expect.isTrue(Storing.stored is List); + Storing.stored.add(42); + Storing.stored.add(3.14); + + var m2 = { + "k0": 0, + for (final (v2) in [1, 2, 3]) + "k$v2": v2.expectStaticType>(), + "k4": 4 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2, "k3": 3, "k4": 4}, m2); + + var m3 = { + "k0": 0, + for (var [v3] in [[1], [2], [3]]..store) "k$v3": v3, + "k4": 4 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2, "k3": 3, "k4": 4}, m3); + Expect.isTrue(Storing.stored is List>); + Storing.stored.first.add(42); + Storing.stored.first.add(3.14); + + var m4 = { + "k0": 0, + for (final [v4] in >[[1], [2], [3]]) + "k$v4": v4.expectStaticType>(), + "k4": 4 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2, "k3": 3, "k4": 4}, m4); + + var m5 = { + "k0": 0, + for (var {"k1": v5} in [{"k1": 1}]..store) "k$v5": v5, + "k2": 2 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2}, m5); + Expect.isTrue(Storing.stored is List>); + Expect.isTrue(Storing.stored.first is Map); + Storing.stored.first["answer"] = 42; + Storing.stored.first["pi"] = 3.14; + + var m6 = { + "k0": 0, + for (final {"k1": v6} in >[{"k1": 1}]) + "k$v6": v6.expectStaticType>(), + "k2": 2 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2}, m6); + + var m7 = { + "k0": 0, + for (var (num v7, n: num v8) in [(1, n: 2)]..store) "k$v7": v7, + "k2": 2 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2}, m7); + Expect.isTrue(Storing.stored is List<(num, {num n})>); + + var m8 = { + "k1": 1, + for (final (v9, n: v10) in <(num, {num n})>[(1, n: 2)]) + "k$v10": v10.expectStaticType>(), + "k3": 3 + }; + Expect.mapEquals({"k1": 1, "k2": 2, "k3": 3}, m8); + + var m9 = { + for (var Square(area: v11) in [Square(1)]..store) "k": v11, + }; + Expect.mapEquals({"k": Unit(1)}, m9); + Expect.isTrue(Storing.stored is List>); + + var m10 = { + for (final Square(area: v12) in >[Square(1)]) + "k": v12.expectStaticType>>() + }; + Expect.mapEquals({"k": Unit(1)}, m10); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t06.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t06.dart new file mode 100644 index 0000000000..e7216ad81c --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t06.dart @@ -0,0 +1,122 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that if `I` is `dynamic` and runtime type of `I` is +/// `Iterable` where `T` is assignable to `` required type then +/// for-in element works as expected. Test a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "../../Utils/static_type_helper.dart"; +import "patterns_lib.dart"; + +main() { + var m1 = { + "k0": 0, + for (var (num v1) in [1, 2, 3] as dynamic) "k$v1": v1, + "k4": 4 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2, "k3": 3, "k4": 4}, m1); + m1.expectStaticType>>(); + + var m2 = { + "k0": 0, + for (final (v2) in [1, 2, 3] as dynamic) + "k$v2": (1 > 2 ? v2.whatever : null, v2).$2, + "k4": 4 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2, "k3": 3, "k4": 4}, m2); + m2.expectStaticType>>(); + + var m3 ={ + "k0": 0, + for (var [v3] in [[1], [2], [3]] as dynamic) "k$v3": v3, + "k4": 4 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2, "k3": 3, "k4": 4}, m3); + m3.expectStaticType>>(); + + var m4 = { + "k0": 0, + for (final [v4] in >[[1], [2], [3]] as dynamic) + "k$v4": (1 > 2 ? v4.whatever : null, v4).$2, + "k4": 4 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2, "k3": 3, "k4": 4}, m4); + m4.expectStaticType>>(); + + var m5 = { + "k0": 0, + for (var {"k1": v5} in [{"k1": 1}] as dynamic) "k$v5": v5, + "k2": 2 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2}, m5); + m5.expectStaticType>>(); + + var m6 = { + "k0": 0, + for (final {"k1": v6} in >[{"k1": 1}] as dynamic) + "k$v6": (1 > 2 ? v6.whatever : null, v6).$2, + "k2": 2 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2}, m6); + m6.expectStaticType>>(); + + var m7 = { + "k0": 0, + for (var (num v7, n: num v8) in [(1, n: 2)] as dynamic) "k$v7": v7, + "k2": 2 + }; + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2}, m7); + m7.expectStaticType>>(); + + var m8 = { + "k1": 1, + for (final (v9, n: v10) in <(num, {num n})>[(1, n: 2)] as dynamic) + "k$v10": (1 > 2 ? v10.whatever : null, v10).$2, + "k3": 3 + }; + Expect.mapEquals({"k1": 1, "k2": 2, "k3": 3}, m8); + m8.expectStaticType>>(); + + var m9 = { + for (var Square(area: v11) in + [Square(1)] as dynamic) "k": v11, + }; + Expect.isTrue(m9["k"] == 1); + m9.expectStaticType>>>(); + + var m10 = { + for (final Square(area: v12) in + >[Square(1)] as dynamic) "k": v12 + }; + Expect.isTrue(m10["k"] == 1); + m10.expectStaticType>>>(); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t07.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t07.dart new file mode 100644 index 0000000000..2881bb9eca --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t07.dart @@ -0,0 +1,84 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that if `I` is `dynamic` and runtime type of `I` is +/// `Iterable` where `T` is not assignable to `` required type then +/// a run-time error occurs. Test a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "patterns_lib.dart"; + +main() { + Expect.throws(() { + { + for (var (int v1) in [1, 2, 3.14] as dynamic) "k$v1": v1 + }; + }); + + Expect.throws(() { + { + for (final [v2] in >[[1], [2], [3]] as dynamic) "k$v2": v2 + }; + }); + + Expect.throws(() { + { + for (var {"k1": v3} in + >[{"k1": 1}] as dynamic) "k$v3": v3 + }; + }); + + Expect.throws(() { + { + for (final (int v4,) in <(num,)>[(1.1,)] as dynamic) "k$v4": v4 + }; + }); + + Expect.throws(() { + { + for (var (n: int v5) in <({num n})>[(n: 2.1)] as dynamic) "k$v5": v5 + }; + }); + + Expect.throws(() { + { + for (var Square(area: v6) in [Circle(1)] as dynamic) + "k$v6": v6 + }; + }); + + Expect.throws(() { + { + for (final Square(area: v7) in + >[Square(1)] as dynamic) "k$v7": v7 + }; + }); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t08.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t08.dart new file mode 100644 index 0000000000..8a93836f72 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t08.dart @@ -0,0 +1,74 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that it is a compile-time error if `I` doesn't implement +/// `Iterable` and in not `dynamic`. Test a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "patterns_lib.dart"; + +main() { + { + for (var (int v1) in 42) "k$v1": v1 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final [v2] in "42") "k$v2": v2 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (var {"k1": v3} in 42) "k$v3": v3 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final (int v4,) in "42") "k$v4": v4 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (var (n: v5) in 42) "k$v5": v5 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final Square(area: v6) in "42") "k$v6": v6 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t09.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t09.dart new file mode 100644 index 0000000000..8d7080dff5 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t09.dart @@ -0,0 +1,128 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that if `I` implements `Iterable` for some `T` then +/// `E` is `T`. Test `T` inferred from `` to `` and from +/// `` to ``. Test a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "../../Utils/static_type_helper.dart"; +import "patterns_lib.dart"; + +extension Storing on Object? { + static dynamic stored; + void get store => stored = this; +} + +main() { + var s1 = { + 0, + for (var (num v1) in [1, 2, 3]..store) v1, + 4 + }; + Expect.setEquals({0, 1, 2, 3, 4}, s1); + Expect.isTrue(Storing.stored is List); + Storing.stored.add(42); + Storing.stored.add(3.14); + + var s2 = { + 0, + for (final (v2) in [1, 2, 3]) + v2.expectStaticType>(), + 4 + }; + Expect.setEquals({0, 1, 2, 3, 4}, s2); + + var s3 = { + 0, + for (var [v3] in [[1], [2], [3]]..store) v3, + 4 + }; + Expect.setEquals({0, 1, 2, 3, 4}, s3); + Expect.isTrue(Storing.stored is List>); + Storing.stored.first.add(42); + Storing.stored.first.add(3.14); + + var s4 = { + 0, + for (final [v4] in >[[1], [2], [3]]) + v4.expectStaticType>(), + 4 + }; + Expect.setEquals({0, 1, 2, 3, 4}, s4); + + var s5 = { + 0, + for (var {"k1": v5} in [{"k1": 1}]..store) v5, + 2 + }; + Expect.setEquals({0, 1, 2}, s5); + Expect.isTrue(Storing.stored is List>); + Expect.isTrue(Storing.stored.first is Map); + Storing.stored.first["answer"] = 42; + Storing.stored.first["pi"] = 3.14; + + var s6 = { + 0, + for (final {"k1": v6} in >[{"k1": 1}]) + v6.expectStaticType>(), + 2 + }; + Expect.setEquals({0, 1, 2}, s6); + + var s7 = { + 0, + for (var (num v7, n: num v8) in [(1, n: 2)]..store) v7, + 2 + }; + Expect.setEquals({0, 1, 2}, s7); + Expect.isTrue(Storing.stored is List<(num, {num n})>); + + var s8 = { + 1, + for (final (v9, n: v10) in <(num, {num n})>[(1, n: 2)]) + v10.expectStaticType>(), + 3 + }; + Expect.setEquals({1, 2, 3}, s8); + + var s9 = { + for (var Square(area: v11) in [Square(1)]..store) v11, + }; + Expect.setEquals({Unit(1)}, s9); + Expect.isTrue(Storing.stored is List>); + + var s10 = { + for (final Square(area: v12) in >[Square(1)]) + v12.expectStaticType>>() + }; + Expect.setEquals({Unit(1)}, s10); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t10.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t10.dart new file mode 100644 index 0000000000..333acd40ed --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t10.dart @@ -0,0 +1,122 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that if `I` is `dynamic` and runtime type of `I` is +/// `Iterable` where `T` is assignable to `` required type then +/// for-in element works as expected. Test a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "../../Utils/static_type_helper.dart"; +import "patterns_lib.dart"; + +main() { + var s1 = { + 0, + for (var (num v1) in [1, 2, 3] as dynamic) v1, + 4 + }; + Expect.setEquals({0, 1, 2, 3, 4}, s1); + s1.expectStaticType>>(); + + var s2 = { + 0, + for (final (v2) in [1, 2, 3] as dynamic) + (1 > 2 ? v2.whatever : null, v2).$2, + 4 + }; + Expect.setEquals({0, 1, 2, 3, 4}, s2); + s2.expectStaticType>>(); + + var s3 ={ + 0, + for (var [v3] in [[1], [2], [3]] as dynamic) v3, + 4 + }; + Expect.setEquals({0, 1, 2, 3, 4}, s3); + s3.expectStaticType>>(); + + var s4 = { + 0, + for (final [v4] in >[[1], [2], [3]] as dynamic) + (1 > 2 ? v4.whatever : null, v4).$2, + 4 + }; + Expect.setEquals({0, 1, 2, 3, 4}, s4); + s4.expectStaticType>>(); + + var s5 = { + 0, + for (var {"k1": v5} in [{"k1": 1}] as dynamic) v5, + 2 + }; + Expect.setEquals({0, 1, 2}, s5); + s5.expectStaticType>>(); + + var s6 = { + 0, + for (final {"k1": v6} in >[{"k1": 1}] as dynamic) + (1 > 2 ? v6.whatever : null, v6).$2, + 2 + }; + Expect.setEquals({0, 1, 2}, s6); + s6.expectStaticType>>(); + + var s7 = { + 0, + for (var (num v7, n: num v8) in [(1, n: 2)] as dynamic) v7, + 2 + }; + Expect.setEquals({0, 1, 2}, s7); + s7.expectStaticType>>(); + + var s8 = { + 1, + for (final (v9, n: v10) in <(num, {num n})>[(1, n: 2)] as dynamic) + (1 > 2 ? v10.whatever : null, v10).$2, + 3 + }; + Expect.setEquals({1, 2, 3}, s8); + s8.expectStaticType>>(); + + var s9 = { + for (var Square(area: v11) in + [Square(1)] as dynamic) v11, + }; + Expect.isTrue(s9.toList()[0] == 1); + s9.expectStaticType>>>(); + + var s10 = { + for (final Square(area: v12) in + >[Square(1)] as dynamic) v12 + }; + Expect.isTrue(s10.toList()[0] == 1); + s10.expectStaticType>>>(); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t11.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t11.dart new file mode 100644 index 0000000000..f528309b6c --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t11.dart @@ -0,0 +1,83 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that if `I` is `dynamic` and runtime type of `I` is +/// `Iterable` where `T` is not assignable to `` required type then +/// a run-time error occurs. Test a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "patterns_lib.dart"; + +main() { + Expect.throws(() { + { + for (var (int v1) in [1, 2, 3.14] as dynamic) v1 + }; + }); + + Expect.throws(() { + { + for (final [v2] in >[[1], [2], [3]] as dynamic) v2 + }; + }); + + Expect.throws(() { + { + for (var {"k1": v3} in + >[{"k1": 1}] as dynamic) v3 + }; + }); + + Expect.throws(() { + { + for (final (int v4,) in <(num,)>[(1.1,)] as dynamic) v4 + }; + }); + + Expect.throws(() { + { + for (var (n: int v5) in <({num n})>[(n: 2.1)] as dynamic) v5 + }; + }); + + Expect.throws(() { + { + for (var Square(area: v6) in [Circle(1)] as dynamic) v6 + }; + }); + + Expect.throws(() { + { + for (final Square(area: v7) in + >[Square(1)] as dynamic) v7 + }; + }); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t12.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t12.dart new file mode 100644 index 0000000000..57410d9172 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A01_t12.dart @@ -0,0 +1,74 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that it is a compile-time error if `I` doesn't implement +/// `Iterable` and in not `dynamic`. Test a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "patterns_lib.dart"; + +main() { + { + for (var (int v1) in 42) v1 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final [v2] in "42") v2 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (var {"k1": v3} in 42) v3 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final (int v4,) in "42") v4 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (var (n: v5) in 42) v5 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final Square(area: v6) in "42") v6 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A02_t01.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A02_t01.dart index 1a3521584e..fff706b406 100644 --- a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A02_t01.dart +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A02_t01.dart @@ -27,7 +27,7 @@ /// } /// ``` /// @description Checks that it is a compile-time error if type check ot the -/// `` with matched value `E` fails +/// `` with matched value `E` fails. Test a list literal /// @author sgrekhov22@gmail.com // SharedOptions=--enable-experiment=patterns,records diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A02_t02.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A02_t02.dart new file mode 100644 index 0000000000..6d91e4492a --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A02_t02.dart @@ -0,0 +1,74 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that it is a compile-time error if type check ot the +/// `` with matched value `E` fails. Test a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "patterns_lib.dart"; + +main() { + { + for (var (int v1) in [1, 2, 3]) "k$v1": v1 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final [v2] in >[[1], [2], [3]]) "k$v2": v2 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (var {"k1": v3} in >[{"k1": 1}]) "k$v3": v3 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final (int v4,) in <(num,)>[(1,)]) "k$v4": v4 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (var (n: int v5) in <({num n})>[(n: 2)]) "k$v5": v5 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (var Square(area: v) in >[Square(1)]) "k$v": v +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A02_t03.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A02_t03.dart new file mode 100644 index 0000000000..d3fa0aa344 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A02_t03.dart @@ -0,0 +1,74 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that it is a compile-time error if type check ot the +/// `` with matched value `E` fails. Test a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "patterns_lib.dart"; + +main() { + { + for (var (int v1) in [1, 2, 3]) v1 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final [v2] in >[[1], [2], [3]]) v2 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (var {"k1": v3} in >[{"k1": 1}]) v3 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final (int v4,) in <(num,)>[(1,)]) v4 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (var (n: int v5) in <({num n})>[(n: 2)]) v5 +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (var Square(area: v) in >[Square(1)]) v +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A03_t01.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A03_t01.dart index 74345cd2d0..c671d258c4 100644 --- a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A03_t01.dart +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A03_t01.dart @@ -27,7 +27,7 @@ /// } /// ``` /// @description Checks that it is a compile-time error if a final variable -/// declared by the pattern is assigned in a for-in element +/// declared by the pattern is assigned in a for-in element. Test a list literal /// @author sgrekhov22@gmail.com // SharedOptions=--enable-experiment=patterns,records diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A03_t02.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A03_t02.dart new file mode 100644 index 0000000000..64d65f2d15 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A03_t02.dart @@ -0,0 +1,74 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that it is a compile-time error if a final variable +/// declared by the pattern is assigned in a for-in element. Test a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "patterns_lib.dart"; + +main() { + { + for (final (int v1) in [1, 2, 3]) "k$v1": v1++ +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final [v2] in [[1], [2], [3]]) "k$v2": v2++ +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final {"k1": v3} in [{"k1": 1}]) "k$v3": v3++ +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final (int v4,) in [(1,)]) "k$v4": v4++ +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final (n: int v5) in [(n: 2)]) "k$v5": v5++ +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final Square(area: v6) in [Square(1)]) "k$v6": v6++ +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A03_t03.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A03_t03.dart new file mode 100644 index 0000000000..2176c21c48 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A03_t03.dart @@ -0,0 +1,74 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that it is a compile-time error if a final variable +/// declared by the pattern is assigned in a for-in element. Test a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "patterns_lib.dart"; + +main() { + { + for (final (int v1) in [1, 2, 3]) v1++ +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final [v2] in [[1], [2], [3]]) v2++ +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final {"k1": v3} in [{"k1": 1}]) v3++ +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final (int v4,) in [(1,)]) v4++ +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final (n: int v5) in [(n: 2)]) v5++ +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final Square(area: v6) in [Square(1)]) v6++ +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A04_t01.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A04_t01.dart index dc4902b1a6..1006b7d721 100644 --- a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A04_t01.dart +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A04_t01.dart @@ -27,7 +27,7 @@ /// } /// ``` /// @description Checks that it is a compile-time error if type of for-in -/// element is not assignable to the type of the collection +/// element is not assignable to the type of the collection. Test a list literal /// @author sgrekhov22@gmail.com // SharedOptions=--enable-experiment=patterns,records diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A04_t02.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A04_t02.dart new file mode 100644 index 0000000000..514cf4f071 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A04_t02.dart @@ -0,0 +1,74 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that it is a compile-time error if type of for-in +/// element is not assignable to the type of the collection. Test a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "patterns_lib.dart"; + +main() { + { + for (var (int v1) in [1, 2, 3]) "k$v1": v1 +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final [v2] in [[1], [2], [3]]) "k$v2": v2 +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final {"k1": v3} in [{"k1": 1}]) "k$v3": v3 +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final (int v4,) in [(1,)]) "k$v4": v4 +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final (n: int v5) in [(n: 2)]) "k$v5": v5 +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final Square(area: v6) in [Square(1)]) "k$v6": v6 +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; +} diff --git a/LanguageFeatures/Patterns/execution_pattern_for_in_element_A04_t03.dart b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A04_t03.dart new file mode 100644 index 0000000000..b6f1aef6a0 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_for_in_element_A04_t03.dart @@ -0,0 +1,74 @@ +// 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 +/// A collection element of the form: +/// +/// for ( in ) +/// +/// Where is var or final is treated like so: +/// 1. Let I be the static type of , inferred using context type +/// schema Iterable

where P is the context type schema of . +/// 2. Calculate the element type of I: +/// i. If I implements Iterable for some T then E is T. +/// ii. Else if I is dynamic then E is dynamic. +/// iii. Else it is a compile-time error. +/// 3. Type check with matched value type E. +/// 4. If there are no compile-time errors, then execution proceeds as the +/// following code, where id1 and id2 are fresh identifiers and append() is +/// an operation to add an element to the surrounding collection being built: +/// ```dart +/// I id1 = ; +/// Iterator id2 = id1.iterator; +/// while (id2.moveNext()) { +/// = id2.current; +/// append(); +/// } +/// ``` +/// @description Checks that it is a compile-time error if type of for-in +/// element is not assignable to the type of the collection. Test a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "patterns_lib.dart"; + +main() { + { + for (var (int v1) in [1, 2, 3]) v1 +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final [v2] in [[1], [2], [3]]) v2 +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final {"k1": v3} in [{"k1": 1}]) v3 +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final (int v4,) in [(1,)]) v4 +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final (n: int v5) in [(n: 2)]) v5 +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; + { + for (final Square(area: v6) in [Square(1)]) v6 +// ^^ +// [analyzer] unspecified +// [cfe] unspecified + }; +} diff --git a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t01.dart b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t01.dart index c3445815a7..24f579cf4e 100644 --- a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t01.dart +++ b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t01.dart @@ -52,8 +52,8 @@ main() { log = ""; Expect.throws(() { [ - if (Square(2, logger) case Square(area: unit4) when guard(null)) 1 - else 2 + if (Square(2, logger) case Square(area: unit4) when guard(null)) 1 + else 2 ]; }); Expect.equals("Square.area:(4==4);guard(null);", log); diff --git a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t04.dart b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t04.dart new file mode 100644 index 0000000000..1f09027a0c --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t04.dart @@ -0,0 +1,88 @@ +// 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 If-case element +/// 1. Evaluate the expression producing v. +/// 2. Match the pattern in the guardedPattern against v. +/// 3. If the match succeeds: +/// i. If there is a guard clause: +/// a. Evaluate it. If it does not evaluate to a bool, throw a runtime error +/// b. If the guard evaluates to true, evaluate the then element and yield +/// the result into the collection. +/// c. Else, evaluate the else element if there is one and yield the result +/// into the collection. +/// ii. Else there is no guard clause. Evaluate the then element and yield the +/// result into the collection. +/// 4. Else the match failed. Evaluate the else element if there is one and +/// yield the result into the collection. +/// +/// @description Check that it is a runtime error if a guard clause doesn't +/// evaluate to bool. If a guard clause does evaluate to bool then there is no +/// runtime error. Test a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "patterns_lib.dart"; + +String log = ""; + +dynamic guard(Object? v) { + log += "guard($v);"; + return v; +} + +void logger(String s) { + log += s; +} + +const unit4 = const Unit(4, logger); + +main() { + Expect.throws(() { + { + if (Square(2, logger) case Square(area: unit4) when guard(42)) "k": 1 + else "k": 2 + }; + }); + Expect.equals("Square.area:(4==4);guard(42);", log); + + log = ""; + Expect.throws(() { + { + if (Square(2, logger) case Square(area: unit4) when guard(null)) "k": 1 + else "k": 2 + }; + }); + Expect.equals("Square.area:(4==4);guard(null);", log); + + log = ""; + var m1 = { + "k0": 0, + if (Square(2, logger) case Square(area: unit4) when guard(false)) "k1": 1, + "k2": 2 + }; + Expect.equals("Square.area:(4==4);guard(false);", log); + Expect.mapEquals({"k0": 0, "k2": 2}, m1); + + log = ""; + var m2 = { + "k0": 0, + if (Square(2, logger) case Square(area: unit4) when guard(true)) "k1": 1, + "k2": 2 + }; + Expect.equals("Square.area:(4==4);guard(true);", log); + Expect.mapEquals({"k0": 0, "k2": 2}, m2); + + log = ""; + var m3 = { + "k0": 0, + if (Square(2, logger) case Square(area: unit4) when guard(false)) "k1": 1 + else "k1": 2, + "k2": 3 + }; + Expect.equals("Square.area:(4==4);guard(false);", log); + Expect.mapEquals({"k0": 0, "k1": 2, "k2": 3}, m3); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t05.dart b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t05.dart new file mode 100644 index 0000000000..9a66c6a4e6 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t05.dart @@ -0,0 +1,70 @@ +// 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 If-case element +/// 1. Evaluate the expression producing v. +/// 2. Match the pattern in the guardedPattern against v. +/// 3. If the match succeeds: +/// i. If there is a guard clause: +/// a. Evaluate it. If it does not evaluate to a bool, throw a runtime error +/// b. If the guard evaluates to true, evaluate the then element and yield +/// the result into the collection. +/// c. Else, evaluate the else element if there is one and yield the result +/// into the collection. +/// ii. Else there is no guard clause. Evaluate the then element and yield the +/// result into the collection. +/// 4. Else the match failed. Evaluate the else element if there is one and +/// yield the result into the collection. +/// +/// @description Check that if pattern matches and there is a guard clause it is +/// executed. If the guard is evaluated to `true` then the statement is +/// executed. If the guard is evaluated to `false` then the else statement is +/// executed, if any. Test a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "patterns_lib.dart"; + +String log = ""; + +bool guard(bool v) { + log += "guard($v);"; + return v; +} + +void logger(String s) { + log += s; +} + +const unit4 = const Unit(4, logger); + +main() { + var m1 = { + "k0": 0, + if (Square(2, logger) case Square(area: unit4) when guard(true)) "k1": 1, + "k2": 2 + }; + Expect.equals("Square.area:(4==4);guard(true);", log); + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2}, m1); + + log = ""; + var m2 = { + "k0": 0, + if (Square(2, logger) case Square(area: unit4) when guard(false)) "k1": 1, + "k2": 2 + }; + Expect.equals("Square.area:(4==4);guard(false);", log); + Expect.mapEquals({"k0": 0, "k2": 2}, m2); + + log = ""; + var m3 = { + "k0": 0, + if (Square(2, logger) case Square(area: unit4) when guard(false)) "k1": 1, + "k2": 2 + }; + Expect.equals("Square.area:(4==4);guard(false);", log); + Expect.mapEquals({"k0": 0, "k2": 2}, m3); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t06.dart b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t06.dart new file mode 100644 index 0000000000..dce3e6448c --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t06.dart @@ -0,0 +1,43 @@ +// 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 If-case element +/// 1. Evaluate the expression producing v. +/// 2. Match the pattern in the guardedPattern against v. +/// 3. If the match succeeds: +/// i. If there is a guard clause: +/// a. Evaluate it. If it does not evaluate to a bool, throw a runtime error +/// b. If the guard evaluates to true, evaluate the then element and yield +/// the result into the collection. +/// c. Else, evaluate the else element if there is one and yield the result +/// into the collection. +/// ii. Else there is no guard clause. Evaluate the then element and yield the +/// result into the collection. +/// 4. Else the match failed. Evaluate the else element if there is one and +/// yield the result into the collection. +/// +/// @description Check that if a pattern matches and there is no guard clause +/// then the statement is executed. Test a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "patterns_lib.dart"; + +String log = ""; + +void logger(String s) { + log += s; +} + +main() { + var m = { + "k0": 0, + if (Square(2, logger) case Square(area: const Unit(4, logger))) "k1": 1, + "k2": 2 + }; + Expect.equals("Square.area:(4==4);", log); + Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2}, m); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t07.dart b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t07.dart new file mode 100644 index 0000000000..06b4ee85a7 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t07.dart @@ -0,0 +1,87 @@ +// 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 If-case element +/// 1. Evaluate the expression producing v. +/// 2. Match the pattern in the guardedPattern against v. +/// 3. If the match succeeds: +/// i. If there is a guard clause: +/// a. Evaluate it. If it does not evaluate to a bool, throw a runtime error +/// b. If the guard evaluates to true, evaluate the then element and yield +/// the result into the collection. +/// c. Else, evaluate the else element if there is one and yield the result +/// into the collection. +/// ii. Else there is no guard clause. Evaluate the then element and yield the +/// result into the collection. +/// 4. Else the match failed. Evaluate the else element if there is one and +/// yield the result into the collection. +/// +/// @description Check that it is a runtime error if a guard clause doesn't +/// evaluate to bool. If a guard clause does evaluate to bool then there is no +/// runtime error. Test a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "patterns_lib.dart"; + +String log = ""; + +dynamic guard(Object? v) { + log += "guard($v);"; + return v; +} + +void logger(String s) { + log += s; +} + +const unit4 = const Unit(4, logger); + +main() { + Expect.throws(() { + { + if (Square(2, logger) case Square(area: unit4) when guard(42)) 1 + else 2 + }; + }); + Expect.equals("Square.area:(4==4);guard(42);", log); + + log = ""; + Expect.throws(() { + { + if (Square(2, logger) case Square(area: unit4) when guard(null)) 1 + else 2 + }; + }); + Expect.equals("Square.area:(4==4);guard(null);", log); + + log = ""; + var s1 = { + 0, + if (Square(2, logger) case Square(area: unit4) when guard(false)) 1, + 2 + }; + Expect.equals("Square.area:(4==4);guard(false);", log); + Expect.setEquals({0, 2}, s1); + + log = ""; + var s2 = { + 0, + if (Square(2, logger) case Square(area: unit4) when guard(true)) 1, + 2 + }; + Expect.equals("Square.area:(4==4);guard(true);", log); + Expect.setEquals({0, 1, 2}, s2); + + log = ""; + var s3 = { + 0, + if (Square(2, logger) case Square(area: unit4) when guard(false)) 1 else 2, + 3 + }; + Expect.equals("Square.area:(4==4);guard(false);", log); + Expect.setEquals({0, 2, 3}, s3); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t08.dart b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t08.dart new file mode 100644 index 0000000000..11e07e8e3d --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t08.dart @@ -0,0 +1,70 @@ +// 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 If-case element +/// 1. Evaluate the expression producing v. +/// 2. Match the pattern in the guardedPattern against v. +/// 3. If the match succeeds: +/// i. If there is a guard clause: +/// a. Evaluate it. If it does not evaluate to a bool, throw a runtime error +/// b. If the guard evaluates to true, evaluate the then element and yield +/// the result into the collection. +/// c. Else, evaluate the else element if there is one and yield the result +/// into the collection. +/// ii. Else there is no guard clause. Evaluate the then element and yield the +/// result into the collection. +/// 4. Else the match failed. Evaluate the else element if there is one and +/// yield the result into the collection. +/// +/// @description Check that if pattern matches and there is a guard clause it is +/// executed. If the guard is evaluated to `true` then the statement is +/// executed. If the guard is evaluated to `false` then the else statement is +/// executed, if any. Test a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "patterns_lib.dart"; + +String log = ""; + +bool guard(bool v) { + log += "guard($v);"; + return v; +} + +void logger(String s) { + log += s; +} + +const unit4 = const Unit(4, logger); + +main() { + var s1 = { + 0, + if (Square(2, logger) case Square(area: unit4) when guard(true)) 1, + 2 + }; + Expect.equals("Square.area:(4==4);guard(true);", log); + Expect.setEquals({0, 1, 2}, s1); + + log = ""; + var s2 = { + 0, + if (Square(2, logger) case Square(area: unit4) when guard(false)) 1, + 2 + }; + Expect.equals("Square.area:(4==4);guard(false);", log); + Expect.setEquals({0, 2}, s2); + + log = ""; + var s3 = { + 0, + if (Square(2, logger) case Square(area: unit4) when guard(false)) 1, + 2 + }; + Expect.equals("Square.area:(4==4);guard(false);", log); + Expect.setEquals({0, 2}, s3); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t09.dart b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t09.dart new file mode 100644 index 0000000000..1965726c1d --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A01_t09.dart @@ -0,0 +1,43 @@ +// 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 If-case element +/// 1. Evaluate the expression producing v. +/// 2. Match the pattern in the guardedPattern against v. +/// 3. If the match succeeds: +/// i. If there is a guard clause: +/// a. Evaluate it. If it does not evaluate to a bool, throw a runtime error +/// b. If the guard evaluates to true, evaluate the then element and yield +/// the result into the collection. +/// c. Else, evaluate the else element if there is one and yield the result +/// into the collection. +/// ii. Else there is no guard clause. Evaluate the then element and yield the +/// result into the collection. +/// 4. Else the match failed. Evaluate the else element if there is one and +/// yield the result into the collection. +/// +/// @description Check that if a pattern matches and there is no guard clause +/// then the statement is executed. Test a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "patterns_lib.dart"; + +String log = ""; + +void logger(String s) { + log += s; +} + +main() { + var s = { + 0, + if (Square(2, logger) case Square(area: const Unit(4, logger))) 1, + 2 + }; + Expect.equals("Square.area:(4==4);", log); + Expect.setEquals({0, 1, 2}, s); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A02_t02.dart b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A02_t02.dart new file mode 100644 index 0000000000..b0dfceb4b1 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A02_t02.dart @@ -0,0 +1,53 @@ +// 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 If-case element +/// 1. Evaluate the expression producing v. +/// 2. Match the pattern in the guardedPattern against v. +/// 3. If the match succeeds: +/// i. If there is a guard clause: +/// a. Evaluate it. If it does not evaluate to a bool, throw a runtime error +/// b. If the guard evaluates to true, evaluate the then element and yield +/// the result into the collection. +/// c. Else, evaluate the else element if there is one and yield the result +/// into the collection. +/// ii. Else there is no guard clause. Evaluate the then element and yield the +/// result into the collection. +/// 4. Else the match failed. Evaluate the else element if there is one and +/// yield the result into the collection. +/// +/// @description Check that if the match fails then the else statement is +/// evaluated if any. Test a map literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "patterns_lib.dart"; + +String log = ""; + +void logger(String s) { + log += s; +} + +main() { + var m1 = { + "k0": 0, + if (Square(2, logger) case Square(area: const Unit(2, logger))) "k1": 1, + "k2": 2 + }; + Expect.equals("Square.area:(2==4);", log); + Expect.mapEquals({"k0": 0, "k2": 2}, m1); + + log = ""; + var m2 = { + "k0": 0, + if (Square(2, logger) case Square(area: const Unit(2, logger))) "k1": 1 + else "k1": 2, + "k2": 3 + }; + Expect.equals("Square.area:(2==4);", log); + Expect.mapEquals({"k0": 0, "k1": 2, "k2": 3}, m2); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A02_t03.dart b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A02_t03.dart new file mode 100644 index 0000000000..449c6ced42 --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A02_t03.dart @@ -0,0 +1,52 @@ +// 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 If-case element +/// 1. Evaluate the expression producing v. +/// 2. Match the pattern in the guardedPattern against v. +/// 3. If the match succeeds: +/// i. If there is a guard clause: +/// a. Evaluate it. If it does not evaluate to a bool, throw a runtime error +/// b. If the guard evaluates to true, evaluate the then element and yield +/// the result into the collection. +/// c. Else, evaluate the else element if there is one and yield the result +/// into the collection. +/// ii. Else there is no guard clause. Evaluate the then element and yield the +/// result into the collection. +/// 4. Else the match failed. Evaluate the else element if there is one and +/// yield the result into the collection. +/// +/// @description Check that if the match fails then the else statement is +/// evaluated if any. Test a set literal +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "../../Utils/expect.dart"; +import "patterns_lib.dart"; + +String log = ""; + +void logger(String s) { + log += s; +} + +main() { + var s1 = { + 0, + if (Square(2, logger) case Square(area: const Unit(2, logger))) 1, + 2 + }; + Expect.equals("Square.area:(2==4);", log); + Expect.setEquals({0, 2}, s1); + + log = ""; + var s2 = { + 0, + if (Square(2, logger) case Square(area: const Unit(2, logger))) 1 else 2, + 3 + }; + Expect.equals("Square.area:(2==4);", log); + Expect.setEquals({0, 2, 3}, s2); +} diff --git a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A03_t01.dart b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A03_t01.dart index 1d6dd692b0..381d592bbf 100644 --- a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A03_t01.dart +++ b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A03_t01.dart @@ -19,10 +19,12 @@ /// /// @description Check that it is a compile-time error if the value yielded by /// pattern-if-case element is not assignable to the collection element type -/// @author sgrekhov22@gmail.com +/// @author sgrekhov22@gmail.com. Test a list literal // SharedOptions=--enable-experiment=patterns,records +import "patterns_lib.dart"; + main() { [ if (Square(2) case Square(area: const Unit(4))) 1, diff --git a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A03_t02.dart b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A03_t02.dart new file mode 100644 index 0000000000..6f61e3edca --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A03_t02.dart @@ -0,0 +1,42 @@ +// 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 If-case element +/// 1. Evaluate the expression producing v. +/// 2. Match the pattern in the guardedPattern against v. +/// 3. If the match succeeds: +/// i. If there is a guard clause: +/// a. Evaluate it. If it does not evaluate to a bool, throw a runtime error +/// b. If the guard evaluates to true, evaluate the then element and yield +/// the result into the collection. +/// c. Else, evaluate the else element if there is one and yield the result +/// into the collection. +/// ii. Else there is no guard clause. Evaluate the then element and yield the +/// result into the collection. +/// 4. Else the match failed. Evaluate the else element if there is one and +/// yield the result into the collection. +/// +/// @description Check that it is a compile-time error if the value yielded by +/// pattern-if-case element is not assignable to the collection element type +/// @author sgrekhov22@gmail.com. Test a map literal + +// SharedOptions=--enable-experiment=patterns,records + +import "patterns_lib.dart"; + +main() { + { + if (Square(2) case Square(area: const Unit(4))) "k": 1, +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + + { + if (Square(2) case Square(area: const Unit(4))) "k": 1 else "k2": 2 as num +// ^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + }; +} diff --git a/LanguageFeatures/Patterns/execution_pattern_if_case_element_A03_t03.dart b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A03_t03.dart new file mode 100644 index 0000000000..b2fe34650c --- /dev/null +++ b/LanguageFeatures/Patterns/execution_pattern_if_case_element_A03_t03.dart @@ -0,0 +1,42 @@ +// 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 If-case element +/// 1. Evaluate the expression producing v. +/// 2. Match the pattern in the guardedPattern against v. +/// 3. If the match succeeds: +/// i. If there is a guard clause: +/// a. Evaluate it. If it does not evaluate to a bool, throw a runtime error +/// b. If the guard evaluates to true, evaluate the then element and yield +/// the result into the collection. +/// c. Else, evaluate the else element if there is one and yield the result +/// into the collection. +/// ii. Else there is no guard clause. Evaluate the then element and yield the +/// result into the collection. +/// 4. Else the match failed. Evaluate the else element if there is one and +/// yield the result into the collection. +/// +/// @description Check that it is a compile-time error if the value yielded by +/// pattern-if-case element is not assignable to the collection element type +/// @author sgrekhov22@gmail.com. Test a set literal + +// SharedOptions=--enable-experiment=patterns,records + +import "patterns_lib.dart"; + +main() { + { + if (Square(2) case Square(area: const Unit(4))) "k": 1, +// ^ +// [analyzer] unspecified +// [cfe] unspecified + }; + + { + if (Square(2) case Square(area: const Unit(4))) "k": 1 else "k2": 2 as num +// ^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + }; +} diff --git a/LanguageFeatures/Patterns/invocation_keys_A07_t08.dart b/LanguageFeatures/Patterns/invocation_keys_A07_t08.dart index 562ad8d902..3b2c454808 100644 --- a/LanguageFeatures/Patterns/invocation_keys_A07_t08.dart +++ b/LanguageFeatures/Patterns/invocation_keys_A07_t08.dart @@ -47,7 +47,7 @@ String test1(Object o) { switch (o) { case [1]: // Expect call length return "match-1"; - case [1, ... var r, 4]: // Expect call [0], [2] + case [1, ... var r, 4]: // Expect call [0], sublist(1, 2), [2] return "match-2"; case [1, _, 3]: // Expect call [2] return "match-3";