diff --git a/LanguageFeatures/Static-access-shorthand/patterns_A01_t01.dart b/LanguageFeatures/Static-access-shorthand/patterns_A01_t01.dart new file mode 100644 index 0000000000..13f9414672 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/patterns_A01_t01.dart @@ -0,0 +1,133 @@ +// 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 constant pattern `` is treated the +/// same as that static member shorthand as an expression that has no following +/// selectors, except with the matched value type is set as the shorthand +/// context of the ``. +/// +/// The restriction to `` is intended to match the +/// existing allowed constant patterns, `` and +/// ``, and nothing more, which is why it omits the +/// `.new` which is guaranteed to be a constructor tear-off. The shorthand +/// constant pattern `'.' ` must satisfy the same restrictions as +/// the `` constant pattern, mainly that it must denote a +/// constant getter. +/// +/// @description Checks that for a constant pattern +/// `` the matched value type is set as the +/// shorthand context of the `` +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final int value; + const C(this.value); + + static const C one = C(1); + + @override + bool operator ==(Object other) { + if (other is C) { + return other.value == value; + } + return false; + } +} + +mixin M on C { + static const M one = MC(1); +} + +class MC extends C with M { + const MC(super.value); +} + +enum E { + e1, + e2; + + static const E one = E.e1; +} + +extension type const ET(int v) implements int { + static const ET one = ET(1); +} + +main() { + String res = ""; + switch (C(1)) { + case .one: + res = "C one"; + default: + res = "default"; + } + Expect.equals("C one", res); + + res = switch (C(1)) { + .one => "C one", + _ => "default" + }; + Expect.equals("C one", res); + + M m = MC(1); + switch (m) { + case .one: + res = "M one"; + default: + res = "default"; + } + Expect.equals("M one", res); + + res = switch (m) { + .one => "M one", + _ => "default" + }; + Expect.equals("M one", res); + + switch (E.e1) { + case .one: + res = "E one"; + default: + res = "default"; + } + Expect.equals("E one", res); + + res = switch (E.e1) { + .one => "E one", + _ => "default" + }; + Expect.equals("E one", res); + + switch (E.e1) { + case .e1: + res = "E one"; + default: + res = "default"; + } + Expect.equals("E one", res); + + res = switch (E.e1) { + .e1 => "E one", + _ => "default" + }; + Expect.equals("E one", res); + + switch (ET(1)) { + case .one: + res = "ET one"; + default: + res = "default"; + } + Expect.equals("ET one", res); + + res = switch (ET(1)) { + .one => "ET one", + _ => "default" + }; + Expect.equals("ET one", res); +} diff --git a/LanguageFeatures/Static-access-shorthand/patterns_A01_t03.dart b/LanguageFeatures/Static-access-shorthand/patterns_A01_t03.dart new file mode 100644 index 0000000000..8e09c9a9eb --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/patterns_A01_t03.dart @@ -0,0 +1,57 @@ +// 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 constant pattern `` is treated the +/// same as that static member shorthand as an expression that has no following +/// selectors, except with the matched value type is set as the shorthand +/// context of the ``. +/// +/// The restriction to `` is intended to match the +/// existing allowed constant patterns, `` and +/// ``, and nothing more, which is why it omits the +/// `.new` which is guaranteed to be a constructor tear-off. The shorthand +/// constant pattern `'.' ` must satisfy the same restrictions as +/// the `` constant pattern, mainly that it must denote a +/// constant getter. +/// +/// @description Checks that it is a compile-time error if for a constant +/// pattern the matched value type has no appropriate member. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +class C { + final int value; + const C(this.value); + + static const C one = C(1); +} + +mixin M on C { + static const M one = MC(1); +} + +class MC extends C with M { + const MC(super.value); +} + +main() { + String res = ""; + + switch (MC(1)) { + case .one: +// ^ +// [analyzer] unspecified +// [cfe] unspecified + default: + } + + String res = switch (MC(1)) { + .one => "M one", +// ^ +// [analyzer] unspecified +// [cfe] unspecified + _ => "default" + }; +} diff --git a/LanguageFeatures/Static-access-shorthand/patterns_A02_t01.dart b/LanguageFeatures/Static-access-shorthand/patterns_A02_t01.dart new file mode 100644 index 0000000000..facd940401 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/patterns_A02_t01.dart @@ -0,0 +1,88 @@ +// 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 If a static member shorthand expression occurs elsewhere in a +/// pattern where a constant expression is generally allowed, like +/// `const (big ? .big : .little)` or `< .one`, except for the relational +/// pattern `== e`, it's treated as a normal constant expression, using the +/// context type it's given. The expression of `const (...)` will have the +/// matched value type as context type. The relational pattern expressions, +/// other than for `==` and `!=`, will have the parameter type of the +/// corresponding operator of the matched value type as context type. +/// +/// @description Checks that if a static member shorthand expression occurs in a +/// logical-or pattern it's treated as a normal constant expression, using the +/// context type it's given. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final int value; + const C(this.value); + + static const C one = C(1); + static const C two = C(2); + + @override + bool operator ==(Object other) { + if (other is C) { + return other.value == value; + } + return false; + } +} + +mixin M on C { + static const M one = MC(1); + static const M two = MC(2); +} + +class MC extends C with M { + const MC(super.value); +} + +enum E { + e1, + e2; + + static const E one = E.e1; + static const E two = E.e2; +} + +extension type const ET(int v) implements int { + static const ET one = ET(1); + static const ET two = ET(2); +} + +main() { + String res = ""; + if (C(1) case .one || .two) { + res = "C one or two"; + } + Expect.equals("C one or two", res); + + M m = MC(2); + if (m case .one || .two) { + res = "M one or two"; + } + Expect.equals("M one or two", res); + + if (E.e1 case .one || .two) { + res = "E one or two"; + } + Expect.equals("E one or two", res); + + if (E.e2 case .e1 || .e2) { + res = "E one or two again"; + } + Expect.equals("E one or two again", res); + + if (ET(1) case .one || .two) { + res = "ET one or two"; + } + Expect.equals("ET one or two", res); +} diff --git a/LanguageFeatures/Static-access-shorthand/patterns_A02_t02.dart b/LanguageFeatures/Static-access-shorthand/patterns_A02_t02.dart new file mode 100644 index 0000000000..079f8a8f4b --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/patterns_A02_t02.dart @@ -0,0 +1,132 @@ +// 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 If a static member shorthand expression occurs elsewhere in a +/// pattern where a constant expression is generally allowed, like +/// `const (big ? .big : .little)` or `< .one`, except for the relational +/// pattern `== e`, it's treated as a normal constant expression, using the +/// context type it's given. The expression of `const (...)` will have the +/// matched value type as context type. The relational pattern expressions, +/// other than for `==` and `!=`, will have the parameter type of the +/// corresponding operator of the matched value type as context type. +/// +/// @description Checks that if a static member shorthand expression occurs in a +/// logical-and pattern it's treated as a normal constant expression, using the +/// context type it's given. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final int value; + const C(this.value); + + static const C one = C(1); + static const C two = C(2); + + @override + bool operator ==(Object other) { + if (other is C) { + return other.value == value; + } + return false; + } + + bool operator >(Object other) { + if (other is C) { + return value > other.value; + } + return false; + } + + bool operator <=(Object other) { + if (other is C) { + return value <= other.value; + } + return false; + } +} + +mixin M on C { + static const M one = MC(1); + static const M two = MC(2); +} + +class MC extends C with M { + const MC(super.value); +} + +enum E { + e1(1), + e2(2); + final int value; + const E(this.value); + + static const E one = E.e1; + static const E two = E.e2; + + bool operator >(Object other) { + if (other is E) { + return value > other.value; + } + return false; + } + + bool operator <=(Object other) { + if (other is E) { + return value <= other.value; + } + return false; + } +} + +extension type const ET(int value) implements int { + static const ET one = ET(1); + static const ET two = ET(2); + + bool operator >(Object other) { + if (other is ET) { + return value > other.value; + } + return false; + } + + bool operator <=(Object other) { + if (other is ET) { + return value <= other.value; + } + return false; + } +} + +main() { + String res = ""; + if (C(2) case > .one && <= .two) { + res = "C Ok"; + } + Expect.equals("C Ok", res); + + M m = MC(2); + if (m case > .one && <= .two) { + res = "M Ok"; + } + Expect.equals("M Ok", res); + + if (E.e2 case > .one && <= .two) { + res = "E Ok"; + } + Expect.equals("E Ok", res); + + if (E.e2 case > .e1 && <= .e2) { + res = "E Ok again"; + } + Expect.equals("E Ok again", res); + + if (ET(2) case > .one && <= .two) { + res = "ET Ok"; + } + Expect.equals("ET Ok", res); +} diff --git a/LanguageFeatures/Static-access-shorthand/patterns_A02_t03.dart b/LanguageFeatures/Static-access-shorthand/patterns_A02_t03.dart new file mode 100644 index 0000000000..a8434046b3 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/patterns_A02_t03.dart @@ -0,0 +1,118 @@ +// 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 If a static member shorthand expression occurs elsewhere in a +/// pattern where a constant expression is generally allowed, like +/// `const (big ? .big : .little)` or `< .one`, except for the relational +/// pattern `== e`, it's treated as a normal constant expression, using the +/// context type it's given. The expression of `const (...)` will have the +/// matched value type as context type. The relational pattern expressions, +/// other than for `==` and `!=`, will have the parameter type of the +/// corresponding operator of the matched value type as context type. +/// +/// @description Checks that if a static member shorthand expression occurs in a +/// relational pattern it's treated as a normal constant expression, using the +/// context type it's given. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final int value; + const C(this.value); + + static const C one = C(1); + static const C two = C(2); + + @override + bool operator ==(Object other) { + if (other is C) { + return other.value == value; + } + return false; + } + + bool operator >(Object other) { + if (other is C) { + return value > other.value; + } + return false; + } + + bool operator <=(Object other) { + if (other is C) { + return value <= other.value; + } + return false; + } +} + +mixin M on C { + static const M one = MC(1); + static const M two = MC(2); +} + +class MC extends C with M { + const MC(super.value); +} + +enum E { + e1(1), + e2(2); + final int value; + const E(this.value); + + static const E one = E.e1; + static const E two = E.e2; + + bool operator <(Object other) { + if (other is E) { + return value < other.value; + } + return false; + } +} + +extension type const ET(int value) implements int { + static const ET one = ET(1); + static const ET two = ET(2); + + bool operator >=(Object other) { + if (other is ET) { + return value >= other.value; + } + return false; + } +} + +main() { + String res = ""; + if (C(2) case > .one) { + res = "C Ok"; + } + Expect.equals("C Ok", res); + + M m = MC(2); + if (m case <= .two) { + res = "M Ok"; + } + Expect.equals("M Ok", res); + + if (E.e1 case < .two) { + res = "E Ok"; + } + Expect.equals("E Ok", res); + + if (E.e1 case < .e2) { + res = "E Ok again"; + } + Expect.equals("E Ok again", res); + + if (ET(2) case >= .one) { + res = "ET Ok"; + } + Expect.equals("ET Ok", res); +} diff --git a/LanguageFeatures/Static-access-shorthand/patterns_A02_t05.dart b/LanguageFeatures/Static-access-shorthand/patterns_A02_t05.dart new file mode 100644 index 0000000000..cbb7acb7d6 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/patterns_A02_t05.dart @@ -0,0 +1,83 @@ +// 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 If a static member shorthand expression occurs elsewhere in a +/// pattern where a constant expression is generally allowed, like +/// `const (big ? .big : .little)` or `< .one`, except for the relational +/// pattern `== e`, it's treated as a normal constant expression, using the +/// context type it's given. The expression of `const (...)` will have the +/// matched value type as context type. The relational pattern expressions, +/// other than for `==` and `!=`, will have the parameter type of the +/// corresponding operator of the matched value type as context type. +/// +/// @description Checks that if a static member shorthand expression occurs in a +/// null-check pattern it's treated as a normal constant expression, using the +/// context type it's given. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final int value; + const C(this.value); + + static const C? one = C(1); + + @override + bool operator ==(Object other) { + if (other is C) { + return other.value == value; + } + return false; + } +} + +mixin M on C { + static const M? one = MC(1); +} + +class MC extends C with M { + const MC(super.value); +} + +enum E { + e1, e2; + + static const E? one = E.e1; +} + +extension type const ET(int value) implements int { + static const ET? one = ET(1); +} + +main() { + String res = ""; + if (C(1) case .one?) { // ignore: unnecessary_null_check_pattern + res = "C Ok"; + } + Expect.equals("C Ok", res); + + M m = MC(1); + if (m case .one?) { // ignore: unnecessary_null_check_pattern + res = "M Ok"; + } + Expect.equals("M Ok", res); + + if (E.e1 case .one?) { // ignore: unnecessary_null_check_pattern + res = "E Ok"; + } + Expect.equals("E Ok", res); + + if (E.e1 case .e1?) { // ignore: unnecessary_null_check_pattern + res = "E Ok again"; + } + Expect.equals("E Ok again", res); + + if (ET(1) case .one?) { // ignore: unnecessary_null_check_pattern + res = "ET Ok"; + } + Expect.equals("ET Ok", res); +} diff --git a/LanguageFeatures/Static-access-shorthand/patterns_A02_t06.dart b/LanguageFeatures/Static-access-shorthand/patterns_A02_t06.dart new file mode 100644 index 0000000000..51e1d90e03 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/patterns_A02_t06.dart @@ -0,0 +1,83 @@ +// 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 If a static member shorthand expression occurs elsewhere in a +/// pattern where a constant expression is generally allowed, like +/// `const (big ? .big : .little)` or `< .one`, except for the relational +/// pattern `== e`, it's treated as a normal constant expression, using the +/// context type it's given. The expression of `const (...)` will have the +/// matched value type as context type. The relational pattern expressions, +/// other than for `==` and `!=`, will have the parameter type of the +/// corresponding operator of the matched value type as context type. +/// +/// @description Checks that if a static member shorthand expression occurs in a +/// null-assert pattern it's treated as a normal constant expression, using the +/// context type it's given. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final int value; + const C(this.value); + + static const C? one = C(1); + + @override + bool operator ==(Object other) { + if (other is C) { + return other.value == value; + } + return false; + } +} + +mixin M on C { + static const M? one = MC(1); +} + +class MC extends C with M { + const MC(super.value); +} + +enum E { + e1, e2; + + static const E? one = E.e1; +} + +extension type const ET(int value) implements int { + static const ET? one = ET(1); +} + +main() { + String res = ""; + if (C(1) case C.one!) { // ignore: unnecessary_null_assert_pattern + res = "C Ok"; + } + Expect.equals("C Ok", res); + + M m = MC(1); + if (m case M.one!) { // ignore: unnecessary_null_assert_pattern + res = "M Ok"; + } + Expect.equals("M Ok", res); + + if (E.e1 case E.one!) { // ignore: unnecessary_null_assert_pattern + res = "E Ok"; + } + Expect.equals("E Ok", res); + + if (E.e1 case E.e1!) { // ignore: unnecessary_null_assert_pattern + res = "E Ok again"; + } + Expect.equals("E Ok again", res); + + if (ET(1) case ET.one!) { // ignore: unnecessary_null_assert_pattern + res = "ET Ok"; + } + Expect.equals("ET Ok", res); +} diff --git a/LanguageFeatures/Static-access-shorthand/patterns_A02_t07.dart b/LanguageFeatures/Static-access-shorthand/patterns_A02_t07.dart new file mode 100644 index 0000000000..edf4b42afa --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/patterns_A02_t07.dart @@ -0,0 +1,83 @@ +// 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 If a static member shorthand expression occurs elsewhere in a +/// pattern where a constant expression is generally allowed, like +/// `const (big ? .big : .little)` or `< .one`, except for the relational +/// pattern `== e`, it's treated as a normal constant expression, using the +/// context type it's given. The expression of `const (...)` will have the +/// matched value type as context type. The relational pattern expressions, +/// other than for `==` and `!=`, will have the parameter type of the +/// corresponding operator of the matched value type as context type. +/// +/// @description Checks that if a static member shorthand expression occurs in a +/// constant pattern it's treated as a normal constant expression, using the +/// context type it's given. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final int value; + const C(this.value); + + static const C one = C(1); + + @override + bool operator ==(Object other) { + if (other is C) { + return other.value == value; + } + return false; + } +} + +mixin M on C { + static const M one = MC(1); +} + +class MC extends C with M { + const MC(super.value); +} + +enum E { + e1, e2; + + static const E one = E.e1; +} + +extension type const ET(int value) implements int { + static const ET one = ET(1); +} + +main() { + String res = ""; + if (C(1) case const (.one)) { + res = "C Ok"; + } + Expect.equals("C Ok", res); + + M m = MC(1); + if (m case const (.one)) { + res = "M Ok"; + } + Expect.equals("M Ok", res); + + if (E.e1 case const (.one)) { + res = "E Ok"; + } + Expect.equals("E Ok", res); + + if (E.e1 case const (.e1)) { + res = "E Ok again"; + } + Expect.equals("E Ok again", res); + + if (ET(1) case const (.one)) { + res = "ET Ok"; + } + Expect.equals("ET Ok", res); +} diff --git a/LanguageFeatures/Static-access-shorthand/patterns_A02_t08.dart b/LanguageFeatures/Static-access-shorthand/patterns_A02_t08.dart new file mode 100644 index 0000000000..a16d15ec0c --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/patterns_A02_t08.dart @@ -0,0 +1,83 @@ +// 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 If a static member shorthand expression occurs elsewhere in a +/// pattern where a constant expression is generally allowed, like +/// `const (big ? .big : .little)` or `< .one`, except for the relational +/// pattern `== e`, it's treated as a normal constant expression, using the +/// context type it's given. The expression of `const (...)` will have the +/// matched value type as context type. The relational pattern expressions, +/// other than for `==` and `!=`, will have the parameter type of the +/// corresponding operator of the matched value type as context type. +/// +/// @description Checks that if a static member shorthand expression occurs in a +/// parenthesized pattern it's treated as a normal constant expression, using +/// the context type it's given. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final int value; + const C(this.value); + + static const C one = C(1); + + @override + bool operator ==(Object other) { + if (other is C) { + return other.value == value; + } + return false; + } +} + +mixin M on C { + static const M one = MC(1); +} + +class MC extends C with M { + const MC(super.value); +} + +enum E { + e1, e2; + + static const E one = E.e1; +} + +extension type const ET(int value) implements int { + static const ET one = ET(1); +} + +main() { + String res = ""; + if (C(1) case (.one)) { + res = "C Ok"; + } + Expect.equals("C Ok", res); + + M m = MC(1); + if (m case (.one)) { + res = "M Ok"; + } + Expect.equals("M Ok", res); + + if (E.e1 case (.one)) { + res = "E Ok"; + } + Expect.equals("E Ok", res); + + if (E.e1 case (.e1)) { + res = "E Ok again"; + } + Expect.equals("E Ok again", res); + + if (ET(1) case (.one)) { + res = "ET Ok"; + } + Expect.equals("ET Ok", res); +} diff --git a/LanguageFeatures/Static-access-shorthand/patterns_A02_t09.dart b/LanguageFeatures/Static-access-shorthand/patterns_A02_t09.dart new file mode 100644 index 0000000000..ee5d9f7e3a --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/patterns_A02_t09.dart @@ -0,0 +1,83 @@ +// 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 If a static member shorthand expression occurs elsewhere in a +/// pattern where a constant expression is generally allowed, like +/// `const (big ? .big : .little)` or `< .one`, except for the relational +/// pattern `== e`, it's treated as a normal constant expression, using the +/// context type it's given. The expression of `const (...)` will have the +/// matched value type as context type. The relational pattern expressions, +/// other than for `==` and `!=`, will have the parameter type of the +/// corresponding operator of the matched value type as context type. +/// +/// @description Checks that if a static member shorthand expression occurs in a +/// list pattern it's treated as a normal constant expression, using the context +/// type it's given. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final int value; + const C(this.value); + + static const C one = C(1); + + @override + bool operator ==(Object other) { + if (other is C) { + return other.value == value; + } + return false; + } +} + +mixin M on C { + static const M one = MC(1); +} + +class MC extends C with M { + const MC(super.value); +} + +enum E { + e1, e2; + + static const E one = E.e1; +} + +extension type const ET(int value) implements int { + static const ET one = ET(1); +} + +main() { + String res = ""; + if ([C(1)] case [.one]) { + res = "C Ok"; + } + Expect.equals("C Ok", res); + + M m = MC(1); + if ([m] case [.one]) { + res = "M Ok"; + } + Expect.equals("M Ok", res); + + if ([E.e1] case [.one]) { + res = "E Ok"; + } + Expect.equals("E Ok", res); + + if ([E.e1] case [.e1]) { + res = "E Ok again"; + } + Expect.equals("E Ok again", res); + + if ([ET(1)] case [.one]) { + res = "ET Ok"; + } + Expect.equals("ET Ok", res); +} diff --git a/LanguageFeatures/Static-access-shorthand/patterns_A02_t10.dart b/LanguageFeatures/Static-access-shorthand/patterns_A02_t10.dart new file mode 100644 index 0000000000..378e5de760 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/patterns_A02_t10.dart @@ -0,0 +1,83 @@ +// 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 If a static member shorthand expression occurs elsewhere in a +/// pattern where a constant expression is generally allowed, like +/// `const (big ? .big : .little)` or `< .one`, except for the relational +/// pattern `== e`, it's treated as a normal constant expression, using the +/// context type it's given. The expression of `const (...)` will have the +/// matched value type as context type. The relational pattern expressions, +/// other than for `==` and `!=`, will have the parameter type of the +/// corresponding operator of the matched value type as context type. +/// +/// @description Checks that if a static member shorthand expression occurs in a +/// map pattern it's treated as a normal constant expression, using the context +/// type it's given. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final int value; + const C(this.value); + + static const C one = C(1); + + @override + bool operator ==(Object other) { + if (other is C) { + return other.value == value; + } + return false; + } +} + +mixin M on C { + static const M one = MC(1); +} + +class MC extends C with M { + const MC(super.value); +} + +enum E { + e1, e2; + + static const E one = E.e1; +} + +extension type const ET(int value) implements int { + static const ET one = ET(1); +} + +main() { + String res = ""; + if ({"key": C(1)} case {"key": .one}) { + res = "C Ok"; + } + Expect.equals("C Ok", res); + + M m = MC(1); + if ({"key": m} case {"key": .one}) { + res = "M Ok"; + } + Expect.equals("M Ok", res); + + if ({"key": E.e1} case {"key": .one}) { + res = "E Ok"; + } + Expect.equals("E Ok", res); + + if ({"key": E.e1} case {"key": .e1}) { + res = "E Ok again"; + } + Expect.equals("E Ok again", res); + + if ({"key": ET(1)} case {"key": .one}) { + res = "ET Ok"; + } + Expect.equals("ET Ok", res); +} diff --git a/LanguageFeatures/Static-access-shorthand/patterns_A02_t11.dart b/LanguageFeatures/Static-access-shorthand/patterns_A02_t11.dart new file mode 100644 index 0000000000..cbc707986c --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/patterns_A02_t11.dart @@ -0,0 +1,108 @@ +// 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 If a static member shorthand expression occurs elsewhere in a +/// pattern where a constant expression is generally allowed, like +/// `const (big ? .big : .little)` or `< .one`, except for the relational +/// pattern `== e`, it's treated as a normal constant expression, using the +/// context type it's given. The expression of `const (...)` will have the +/// matched value type as context type. The relational pattern expressions, +/// other than for `==` and `!=`, will have the parameter type of the +/// corresponding operator of the matched value type as context type. +/// +/// @description Checks that if a static member shorthand expression occurs in a +/// record pattern it's treated as a normal constant expression, using the +/// context type it's given. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final int value; + const C(this.value); + + static const C one = C(1); + + @override + bool operator ==(Object other) { + if (other is C) { + return other.value == value; + } + return false; + } +} + +mixin M on C { + static const M one = MC(1); +} + +class MC extends C with M { + const MC(super.value); +} + +enum E { + e1, e2; + + static const E one = E.e1; +} + +extension type const ET(int value) implements int { + static const ET one = ET(1); +} + +main() { + String res = ""; + if ((C(1),) case (.one,)) { + res = "C Ok"; + } + Expect.equals("C Ok", res); + + if ((x: C(1)) case (x: .one)) { + res = "C Ok again"; + } + Expect.equals("C Ok again", res); + + M m = MC(1); + if ((m,) case (.one,)) { + res = "M Ok"; + } + Expect.equals("M Ok", res); + + if ((x: m) case (x: .one)) { + res = "M Ok again"; + } + Expect.equals("M Ok again", res); + + if ((E.e1,) case (.one,)) { + res = "E Ok"; + } + Expect.equals("E Ok", res); + + if ((x: E.e1) case (x: .one)) { + res = "E Ok again"; + } + Expect.equals("E Ok again", res); + + if ((E.e1,) case (.e1,)) { + res = "E Ok"; + } + Expect.equals("E Ok", res); + + if ((x: E.e1) case (x: .e1)) { + res = "E Ok again"; + } + Expect.equals("E Ok again", res); + + if ((ET(1),) case (.one,)) { + res = "ET Ok"; + } + Expect.equals("ET Ok", res); + + if ((x: ET(1)) case (x: .one)) { + res = "ET Ok again"; + } + Expect.equals("ET Ok again", res); +} diff --git a/LanguageFeatures/Static-access-shorthand/patterns_A02_t12.dart b/LanguageFeatures/Static-access-shorthand/patterns_A02_t12.dart new file mode 100644 index 0000000000..0d137e100d --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/patterns_A02_t12.dart @@ -0,0 +1,90 @@ +// 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 If a static member shorthand expression occurs elsewhere in a +/// pattern where a constant expression is generally allowed, like +/// `const (big ? .big : .little)` or `< .one`, except for the relational +/// pattern `== e`, it's treated as a normal constant expression, using the +/// context type it's given. The expression of `const (...)` will have the +/// matched value type as context type. The relational pattern expressions, +/// other than for `==` and `!=`, will have the parameter type of the +/// corresponding operator of the matched value type as context type. +/// +/// @description Checks that if a static member shorthand expression occurs in +/// an object pattern it's treated as a normal constant expression, using the +/// context type it's given. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final int value; + const C(this.value); + + static const C one = C(1); + + @override + bool operator ==(Object other) { + if (other is C) { + return other.value == value; + } + return false; + } +} + +mixin M on C { + static const M one = MC(1); +} + +class MC extends C with M { + const MC(super.value); +} + +enum E { + e1, e2; + + static const E one = E.e1; +} + +extension type const ET(int value) implements int { + static const ET one = ET(1); +} + +class Container { + C c = C(1); + M m = MC(1); + E e = E.e1; + ET et = ET(1); +} + +main() { + String res = ""; + Container c = Container(); + if (c case Container(c: .one)) { + res = "C Ok"; + } + Expect.equals("C Ok", res); + + if (c case Container(m: .one)) { + res = "M Ok"; + } + Expect.equals("M Ok", res); + + if (c case Container(e: .one)) { + res = "E Ok"; + } + Expect.equals("E Ok", res); + + if (c case Container(e: .e1)) { + res = "E Ok again"; + } + Expect.equals("E Ok again", res); + + if (c case Container(et: .one)) { + res = "ET Ok"; + } + Expect.equals("ET Ok", res); +}