From 9dc93e90355548c29969debfff9354d5d3d5e0f4 Mon Sep 17 00:00:00 2001 From: sgrekhov Date: Thu, 5 Jan 2023 11:09:20 +0400 Subject: [PATCH 1/2] #1401. [Patterns] Object pattern test added --- LanguageFeatures/Patterns/object_A07_t05.dart | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 LanguageFeatures/Patterns/object_A07_t05.dart diff --git a/LanguageFeatures/Patterns/object_A07_t05.dart b/LanguageFeatures/Patterns/object_A07_t05.dart new file mode 100644 index 0000000000..5f3837a780 --- /dev/null +++ b/LanguageFeatures/Patterns/object_A07_t05.dart @@ -0,0 +1,58 @@ +// Copyright (c) 2022, 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 objectPattern ::= typeName typeArguments? '(' patternFields? ')' +/// ... +/// It is a compile-time error if: +/// +/// `typeNam`e does not refer to a type. +/// +/// A type argument list is present and does not match the arity of the type of +/// `typeName`. +/// +/// A `patternField` is of the form `pattern`. Positional fields aren't allowed. +/// +/// Any two named fields have the same name. This applies to both explicit and +/// inferred field names. +/// +/// It is a compile-time error if a name cannot be inferred for a named getter +/// pattern with the getter name omitted (see name inference below). +/// +/// @description Checks that it is a compile-time error if a name cannot be +/// inferred for a named getter pattern with the getter name omitted. Test the +/// case when class has a getter named `_` +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +class C { + int get _ => 42; +} + +main() { + C c = C(); + switch (c) { + case C(:var _): +// ^ +// [analyzer] unspecified +// [cfe] unspecified + print("matched"); + break; + default: + print("not matched"); + } + + if (c case C(:var _)) { +// ^ +// [analyzer] unspecified +// [cfe] unspecified + } + var x = switch(c) { + C(:final _) => "match", +// ^ +// [analyzer] unspecified +// [cfe] unspecified + _ => "no match" + }; +} From 5d555b6d73cfe6530548033a5c2338c2793fc4a1 Mon Sep 17 00:00:00 2001 From: sgrekhov Date: Wed, 11 Jan 2023 13:31:43 +0200 Subject: [PATCH 2/2] Test for Object pattern with ommitted getter name in a declaration contecst --- LanguageFeatures/Patterns/object_A07_t06.dart | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 LanguageFeatures/Patterns/object_A07_t06.dart diff --git a/LanguageFeatures/Patterns/object_A07_t06.dart b/LanguageFeatures/Patterns/object_A07_t06.dart new file mode 100644 index 0000000000..0d1bb9c3a7 --- /dev/null +++ b/LanguageFeatures/Patterns/object_A07_t06.dart @@ -0,0 +1,46 @@ +// 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 objectPattern ::= typeName typeArguments? '(' patternFields? ')' +/// ... +/// It is a compile-time error if: +/// +/// `typeNam`e does not refer to a type. +/// +/// A type argument list is present and does not match the arity of the type of +/// `typeName`. +/// +/// A `patternField` is of the form `pattern`. Positional fields aren't allowed. +/// +/// Any two named fields have the same name. This applies to both explicit and +/// inferred field names. +/// +/// It is a compile-time error if a name cannot be inferred for a named getter +/// pattern with the getter name omitted (see name inference below). +/// +/// @description Check that wildcard `_` can be used in Object pattern in a +/// declaration context. Getter is called, but its value is ignored +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns,records + +import "patterns_lib.dart"; +import "../../Utils/expect.dart"; + +String log = ""; + +void logger(String s) { + log += s; +} + +main() { + var Square(area: _) = Square(2, logger); + Expect.equals("Square.area", log); + + var Square(areaAsInt: int _) = Square(1); + + Expect.throws(() { + var Square(areaAsInt: _ as String) = Square(1); + }); +}