From 947af2a61703eaa1f211238b177d6b4c78164eca Mon Sep 17 00:00:00 2001 From: "Sergey G. Grekhov" Date: Wed, 27 Mar 2024 17:00:49 +0700 Subject: [PATCH] Fixes #2577. Add more chained patterns assignment tests (#2580) --- .../Patterns/pattern_assignment_A02_t02.dart | 42 +++++++++++++++++++ .../Patterns/pattern_assignment_A02_t03.dart | 40 ++++++++++++++++++ .../Patterns/pattern_assignment_A02_t04.dart | 32 ++++++++++++++ .../Patterns/pattern_assignment_A02_t05.dart | 29 +++++++++++++ .../Patterns/pattern_assignment_A02_t06.dart | 29 +++++++++++++ .../Patterns/pattern_assignment_A02_t07.dart | 30 +++++++++++++ 6 files changed, 202 insertions(+) create mode 100644 LanguageFeatures/Patterns/pattern_assignment_A02_t02.dart create mode 100644 LanguageFeatures/Patterns/pattern_assignment_A02_t03.dart create mode 100644 LanguageFeatures/Patterns/pattern_assignment_A02_t04.dart create mode 100644 LanguageFeatures/Patterns/pattern_assignment_A02_t05.dart create mode 100644 LanguageFeatures/Patterns/pattern_assignment_A02_t06.dart create mode 100644 LanguageFeatures/Patterns/pattern_assignment_A02_t07.dart diff --git a/LanguageFeatures/Patterns/pattern_assignment_A02_t02.dart b/LanguageFeatures/Patterns/pattern_assignment_A02_t02.dart new file mode 100644 index 0000000000..7ad9082eac --- /dev/null +++ b/LanguageFeatures/Patterns/pattern_assignment_A02_t02.dart @@ -0,0 +1,42 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion A pattern on the left side of an assignment expression is used to +/// destructure the assigned value. We extend expression: +/// +/// expression ::= patternAssignment +/// | // Existing productions... +/// +/// patternAssignment ::= outerPattern '=' expression +/// +/// This syntax allows chaining pattern assignments and mixing them with other +/// assignments, but does not allow patterns to the left of a compound +/// assignment operator. +/// +/// @description Check that pattern assignments may be chained. Test +/// parenthesized pattern +/// @author sgrekhov22@gmail.com +/// @issue 55232 + +import '../../Utils/expect.dart'; + +void main() { + int v1; + int v2; + (v2) = (v1) = 0; + Expect.equals(0, v1); + Expect.equals(0, v2); + + (v2) = v1 = 1; + Expect.equals(1, v1); + Expect.equals(1, v2); + + v2 = (v1) = 2; + Expect.equals(2, v1); + Expect.equals(2, v2); + + (v2) = ((v1)) = (3); + Expect.equals(3, v1); + Expect.equals(3, v2); +} diff --git a/LanguageFeatures/Patterns/pattern_assignment_A02_t03.dart b/LanguageFeatures/Patterns/pattern_assignment_A02_t03.dart new file mode 100644 index 0000000000..089479a669 --- /dev/null +++ b/LanguageFeatures/Patterns/pattern_assignment_A02_t03.dart @@ -0,0 +1,40 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion A pattern on the left side of an assignment expression is used to +/// destructure the assigned value. We extend expression: +/// +/// expression ::= patternAssignment +/// | // Existing productions... +/// +/// patternAssignment ::= outerPattern '=' expression +/// +/// This syntax allows chaining pattern assignments and mixing them with other +/// assignments, but does not allow patterns to the left of a compound +/// assignment operator. +/// +/// @description Check that pattern assignments may be chained. Test that +/// chaining an object pattern doesn't create an excessive instances +/// @author sgrekhov22@gmail.com + +import '../../Utils/expect.dart'; + +var cCounter = 0; +String log = ""; + +class C { + int _x; + final int counter = cCounter++; + C({required int x}) : _x = x + 1; + int get x => _x++; +} + +void main() { + var y = 0; + var z = C(x: y) = C(x: y) = C(x: y) = C(x: y); + Expect.equals(1, cCounter); + Expect.equals(3, y); + Expect.equals(0, z.counter); + Expect.equals(4, z.x); +} diff --git a/LanguageFeatures/Patterns/pattern_assignment_A02_t04.dart b/LanguageFeatures/Patterns/pattern_assignment_A02_t04.dart new file mode 100644 index 0000000000..4490f6d19b --- /dev/null +++ b/LanguageFeatures/Patterns/pattern_assignment_A02_t04.dart @@ -0,0 +1,32 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion A pattern on the left side of an assignment expression is used to +/// destructure the assigned value. We extend expression: +/// +/// expression ::= patternAssignment +/// | // Existing productions... +/// +/// patternAssignment ::= outerPattern '=' expression +/// +/// This syntax allows chaining pattern assignments and mixing them with other +/// assignments, but does not allow patterns to the left of a compound +/// assignment operator. +/// +/// @description Check that pattern assignments may be chained. Test record +/// pattern +/// @author sgrekhov22@gmail.com + +import '../../Utils/expect.dart'; + +void main() { + var y = 0; + var z1 = (y,) = (y,) = (y,) = (1,); + Expect.equals(1, y); + Expect.equals((1,), z1); + + var z2 = (y: y) = (y: y) = (y: y); + Expect.equals(1, y); + Expect.equals((y: 1), z2); +} diff --git a/LanguageFeatures/Patterns/pattern_assignment_A02_t05.dart b/LanguageFeatures/Patterns/pattern_assignment_A02_t05.dart new file mode 100644 index 0000000000..9f7e65c7ca --- /dev/null +++ b/LanguageFeatures/Patterns/pattern_assignment_A02_t05.dart @@ -0,0 +1,29 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion A pattern on the left side of an assignment expression is used to +/// destructure the assigned value. We extend expression: +/// +/// expression ::= patternAssignment +/// | // Existing productions... +/// +/// patternAssignment ::= outerPattern '=' expression +/// +/// This syntax allows chaining pattern assignments and mixing them with other +/// assignments, but does not allow patterns to the left of a compound +/// assignment operator. +/// +/// @description Check that pattern assignments may be chained. Test list +/// pattern +/// @author sgrekhov22@gmail.com + +import '../../Utils/expect.dart'; + +void main() { + var x, y; + var z = [x, y] = [y, x] = [x, y,] = [1, 2]; + Expect.equals(1, x); + Expect.equals(2, y); + Expect.listEquals([1, 2], z); +} diff --git a/LanguageFeatures/Patterns/pattern_assignment_A02_t06.dart b/LanguageFeatures/Patterns/pattern_assignment_A02_t06.dart new file mode 100644 index 0000000000..fa27f3e429 --- /dev/null +++ b/LanguageFeatures/Patterns/pattern_assignment_A02_t06.dart @@ -0,0 +1,29 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion A pattern on the left side of an assignment expression is used to +/// destructure the assigned value. We extend expression: +/// +/// expression ::= patternAssignment +/// | // Existing productions... +/// +/// patternAssignment ::= outerPattern '=' expression +/// +/// This syntax allows chaining pattern assignments and mixing them with other +/// assignments, but does not allow patterns to the left of a compound +/// assignment operator. +/// +/// @description Check that pattern assignments may be chained. Test map pattern +/// @author sgrekhov22@gmail.com + +import '../../Utils/expect.dart'; + +void main() { + const one = "1"; + int y, x; + var z = {"1": x} = {"1": y} = {one: y} = {"1": 2}; + Expect.equals(2, x); + Expect.equals(2, y); + Expect.mapEquals({"1": 2}, z); +} diff --git a/LanguageFeatures/Patterns/pattern_assignment_A02_t07.dart b/LanguageFeatures/Patterns/pattern_assignment_A02_t07.dart new file mode 100644 index 0000000000..142780d339 --- /dev/null +++ b/LanguageFeatures/Patterns/pattern_assignment_A02_t07.dart @@ -0,0 +1,30 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion A pattern on the left side of an assignment expression is used to +/// destructure the assigned value. We extend expression: +/// +/// expression ::= patternAssignment +/// | // Existing productions... +/// +/// patternAssignment ::= outerPattern '=' expression +/// +/// This syntax allows chaining pattern assignments and mixing them with other +/// assignments, but does not allow patterns to the left of a compound +/// assignment operator. +/// +/// @description Check that it is a run-time error if chained patterns don't +/// match +/// @author sgrekhov22@gmail.com + +import '../../Utils/expect.dart'; + +void main() { + const one = "1"; + int y = 0; + Expect.throws(() { + var z = {"1": y} = {"2": y} = {one: y} = {"1": 2}; + }); + Expect.equals(2, y); +}