From 6d2a3c5c20b77475cae2c6c70ff62b1d2c55672e Mon Sep 17 00:00:00 2001 From: "Sergey G. Grekhov" Date: Thu, 19 Dec 2024 19:03:54 +0200 Subject: [PATCH] #2976. Add equality tests. Part 1. (#3021) Add equality tests. Part 1 --- .../equality_A01_t01.dart | 81 +++++++++++++++++ .../equality_A01_t02.dart | 65 ++++++++++++++ .../equality_A01_t03.dart | 42 +++++++++ .../equality_A01_t04.dart | 48 +++++++++++ .../equality_A01_t05.dart | 86 +++++++++++++++++++ .../equality_A01_t06.dart | 55 ++++++++++++ .../equality_A01_t07.dart | 50 +++++++++++ .../equality_A01_t08.dart | 76 ++++++++++++++++ .../equality_A02_t01.dart | 73 ++++++++++++++++ .../equality_A02_t02.dart | 67 +++++++++++++++ .../equality_A02_t03.dart | 54 ++++++++++++ .../equality_A02_t04.dart | 66 ++++++++++++++ 12 files changed, 763 insertions(+) create mode 100644 LanguageFeatures/Static-access-shorthand/equality_A01_t01.dart create mode 100644 LanguageFeatures/Static-access-shorthand/equality_A01_t02.dart create mode 100644 LanguageFeatures/Static-access-shorthand/equality_A01_t03.dart create mode 100644 LanguageFeatures/Static-access-shorthand/equality_A01_t04.dart create mode 100644 LanguageFeatures/Static-access-shorthand/equality_A01_t05.dart create mode 100644 LanguageFeatures/Static-access-shorthand/equality_A01_t06.dart create mode 100644 LanguageFeatures/Static-access-shorthand/equality_A01_t07.dart create mode 100644 LanguageFeatures/Static-access-shorthand/equality_A01_t08.dart create mode 100644 LanguageFeatures/Static-access-shorthand/equality_A02_t01.dart create mode 100644 LanguageFeatures/Static-access-shorthand/equality_A02_t02.dart create mode 100644 LanguageFeatures/Static-access-shorthand/equality_A02_t03.dart create mode 100644 LanguageFeatures/Static-access-shorthand/equality_A02_t04.dart diff --git a/LanguageFeatures/Static-access-shorthand/equality_A01_t01.dart b/LanguageFeatures/Static-access-shorthand/equality_A01_t01.dart new file mode 100644 index 0000000000..ae689e1c61 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/equality_A01_t01.dart @@ -0,0 +1,81 @@ +// 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 an expression has the form `e1 == e2` or `e1 != e2`, or a +/// pattern has the form `== e2` or `!= e2`, where the static type of `e1`, or +/// the matched value type of the pattern, is S1, and e2 is precisely a +/// `` expression, then assign the type S1 as the +/// shorthand context of the `` of e2 before +/// inferring its static type the same way as above. +/// +/// @description Checks that if an expression has the form `e1 == e2` or +/// `e1 != e2` and `e2` is a shorthand expression then it has shorthand +/// context from `e1`. Test a class. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + String log = ""; + T value; + C(this.value); + C.id(this.value); + factory C.f(T t) = C; + + static C get staticGetter => C(1); + static C staticMethod(X x) => C(x); + static List> instances = [C("one")]; + + @override + bool operator ==(Object other) { + log = "$this == $other"; + if (other is C) { + return value == other.value; + } + return false; + } + + @override + String toString() => "C<$T>($value)"; +} + +main() { + C c1 = C(1); + Expect.isTrue(c1 == .staticGetter); + Expect.equals("C(1) == C(1)", c1.log); + Expect.isFalse(c1 != .staticGetter); + Expect.equals("C(1) == C(1)", c1.log); + + C c2 = C(2); + Expect.isFalse(c2 == .staticMethod("two")); + Expect.equals("C(2) == C(two)", c2.log); + Expect.isTrue(c2 != .staticMethod("two")); + Expect.equals("C(2) == C(two)", c2.log); + + C c3 = C("one"); + Expect.isTrue(c3 == .instances[0]); + Expect.equals("C(one) == C(one)", c3.log); + Expect.isFalse(c3 != .instances[0]); + Expect.equals("C(one) == C(one)", c3.log); + + C c4 = C(4); + Expect.isTrue(c4 == .new(4)); + Expect.equals("C(4) == C(4)", c4.log); + Expect.isFalse(c4 != .new(4)); + Expect.equals("C(4) == C(4)", c4.log); + + C c5 = C(5); + Expect.isTrue(c5 == .id(5)); + Expect.equals("C(5) == C(5)", c5.log); + Expect.isFalse(c5 != .id(5)); + Expect.equals("C(5) == C(5)", c5.log); + + C c6 = C(6); + Expect.isTrue(c6 == .f(6)); + Expect.equals("C(6) == C(6)", c6.log); + Expect.isFalse(c6 != .f(6)); + Expect.equals("C(6) == C(6)", c6.log); +} diff --git a/LanguageFeatures/Static-access-shorthand/equality_A01_t02.dart b/LanguageFeatures/Static-access-shorthand/equality_A01_t02.dart new file mode 100644 index 0000000000..69e359e3b9 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/equality_A01_t02.dart @@ -0,0 +1,65 @@ +// 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 an expression has the form `e1 == e2` or `e1 != e2`, or a +/// pattern has the form `== e2` or `!= e2`, where the static type of `e1`, or +/// the matched value type of the pattern, is S1, and e2 is precisely a +/// `` expression, then assign the type S1 as the +/// shorthand context of the `` of e2 before +/// inferring its static type the same way as above. +/// +/// @description Checks that if an expression has the form `e1 == e2` or +/// `e1 != e2` and `e2` is a shorthand expression then it has shorthand +/// context from `e1`. Test a mixin. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + String log = ""; + T value; + C(this.value); + + @override + bool operator ==(Object other) { + log = "$this == $other"; + if (other is C) { + return value == other.value; + } + return false; + } + + @override + String toString() => "${this.runtimeType}($value)"; +} + +mixin M on C { + static M get staticGetter => MC(1); + static M staticMethod(X x) => MC(x); + static List> instances = [MC("one")]; +} + +class MC = C with M; + +main() { + M m1 = MC(1); + Expect.isTrue(m1 == .staticGetter); + Expect.equals("MC(1) == MC(1)", m1.log); + Expect.isFalse(m1 != .staticGetter); + Expect.equals("MC(1) == MC(1)", m1.log); + + M m2 = MC(2); + Expect.isFalse(m2 == .staticMethod("two")); + Expect.equals("MC(2) == MC(two)", m2.log); + Expect.isTrue(m2 != .staticMethod("two")); + Expect.equals("MC(2) == MC(two)", m2.log); + + MC m3 = MC("one"); + Expect.isTrue(m3 == .instances[0]); + Expect.equals("MC(one) == MC(one)", m3.log); + Expect.isFalse(m3 != .instances[0]); + Expect.equals("MC(one) == MC(one)", m3.log); +} diff --git a/LanguageFeatures/Static-access-shorthand/equality_A01_t03.dart b/LanguageFeatures/Static-access-shorthand/equality_A01_t03.dart new file mode 100644 index 0000000000..a6cafbc40f --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/equality_A01_t03.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 If an expression has the form `e1 == e2` or `e1 != e2`, or a +/// pattern has the form `== e2` or `!= e2`, where the static type of `e1`, or +/// the matched value type of the pattern, is S1, and e2 is precisely a +/// `` expression, then assign the type S1 as the +/// shorthand context of the `` of e2 before +/// inferring its static type the same way as above. +/// +/// @description Checks that if an expression has the form `e1 == e2` or +/// `e1 != e2` and `e2` is a shorthand expression then it has shorthand +/// context from `e1`. Test an enum. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +enum E { + e0(0), e1("1"); + final T value; + const E(this.value); + + static E get staticGetter => E.e0; + static E staticMethod(int index) => E.values[index]; +} + +main() { + Expect.isTrue(E.e0 == .e0); + Expect.isFalse(E.e0 != .e1); + + Expect.isTrue(E.e0 == .staticGetter); + Expect.isFalse(E.e0 != .staticGetter); + + Expect.isFalse(E.e1 == .staticMethod(0)); + Expect.isTrue(E.e1 != .staticMethod(0)); + + Expect.isTrue(E.e0 == .values[0]); + Expect.isFalse(E.e0 != .values[0]); +} diff --git a/LanguageFeatures/Static-access-shorthand/equality_A01_t04.dart b/LanguageFeatures/Static-access-shorthand/equality_A01_t04.dart new file mode 100644 index 0000000000..78ba75da6c --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/equality_A01_t04.dart @@ -0,0 +1,48 @@ +// 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 an expression has the form `e1 == e2` or `e1 != e2`, or a +/// pattern has the form `== e2` or `!= e2`, where the static type of `e1`, or +/// the matched value type of the pattern, is S1, and e2 is precisely a +/// `` expression, then assign the type S1 as the +/// shorthand context of the `` of e2 before +/// inferring its static type the same way as above. +/// +/// @description Checks that if an expression has the form `e1 == e2` or +/// `e1 != e2` and `e2` is a shorthand expression then it has shorthand +/// context from `e1`. Test an extension type. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +extension type ET(T value) { + ET.id(this.value); + factory ET.f(T t) = ET; + + static ET get staticGetter => ET(1); + static ET staticMethod(X x) => ET(x); + static List> instances = [ET("one")]; +} + +main() { + Expect.isTrue(ET(1) == .staticGetter); + Expect.isFalse(ET(1) != .staticGetter); + + Expect.isFalse(ET(2) == .staticMethod("two")); + Expect.isTrue(ET(2) != .staticMethod("two")); + + Expect.isTrue(ET("one") == .instances[0]); + Expect.isFalse(ET("one") != .instances[0]); + + Expect.isTrue(ET(4) == .new(4)); + Expect.isFalse(ET(4) != .new(4)); + + Expect.isTrue(ET(5) == .id(5)); + Expect.isFalse(ET(5) != .id(5)); + + Expect.isTrue(ET(6) == .f(6)); + Expect.isFalse(ET(6) != .f(6)); +} diff --git a/LanguageFeatures/Static-access-shorthand/equality_A01_t05.dart b/LanguageFeatures/Static-access-shorthand/equality_A01_t05.dart new file mode 100644 index 0000000000..6587979dc4 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/equality_A01_t05.dart @@ -0,0 +1,86 @@ +// 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 an expression has the form `e1 == e2` or `e1 != e2`, or a +/// pattern has the form `== e2` or `!= e2`, where the static type of `e1`, or +/// the matched value type of the pattern, is S1, and e2 is precisely a +/// `` expression, then assign the type S1 as the +/// shorthand context of the `` of e2 before +/// inferring its static type the same way as above. +/// +/// @description Checks that if a pattern has the form `== e2` or `!= e2` and +/// `e2` is a shorthand expression, then it has context type from the matched +/// value type of the pattern. Test a class. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final T value; + const C(this.value); + const C.id(this.value); + const factory C.f(T t) = C; + + static const C intOne = C(1); + + @override + bool operator ==(Object other) { + if (other is C) { + return value == other.value; + } + return false; + } +} + +main() { + bool success = false; + void checkSuccess() { + Expect.isTrue(success); + success = false; + } + + C c1 = C(1); + if (c1 case == const .new(1)) { + success = true; + } + checkSuccess(); + + if (c1 case == const .id(1)) { + success = true; + } + checkSuccess(); + + if (c1 case == const .f(1)) { + success = true; + } + checkSuccess(); + + if (c1 case == .intOne) { + success = true; + } + checkSuccess(); + + C c2 = C(2); + if (c2 case != const .new(1)) { + success = true; + } + checkSuccess(); + + if (c2 case != const .id(1)) { + success = true; + } + checkSuccess(); + + if (c2 case != const .f(1)) { + success = true; + } + checkSuccess(); + + if (c2 case != .intOne) { + success = true; + } + checkSuccess(); +} diff --git a/LanguageFeatures/Static-access-shorthand/equality_A01_t06.dart b/LanguageFeatures/Static-access-shorthand/equality_A01_t06.dart new file mode 100644 index 0000000000..115d6ac7bd --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/equality_A01_t06.dart @@ -0,0 +1,55 @@ +// 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 an expression has the form `e1 == e2` or `e1 != e2`, or a +/// pattern has the form `== e2` or `!= e2`, where the static type of `e1`, or +/// the matched value type of the pattern, is S1, and e2 is precisely a +/// `` expression, then assign the type S1 as the +/// shorthand context of the `` of e2 before +/// inferring its static type the same way as above. +/// +/// @description Checks that if a pattern has the form `== e2` or `!= e2` and +/// `e2` is a shorthand expression, then it has context type from the matched +/// value type of the pattern. Test a mixin. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final T value; + const C(this.value); + + @override + bool operator ==(Object other) { + if (other is C) { + return value == other.value; + } + return false; + } +} + +mixin M on C { + static const M intOne = MC(1); +} + +class MC extends C with M { + const MC(T t) : super(t); +} + +main() { + bool success = false; + M m = MC(1); + if (m case == .intOne) { + success = true; + } + Expect.isTrue(success); + + success = false; + if (m case != .intOne) { + success = true; + } + Expect.isFalse(success); +} diff --git a/LanguageFeatures/Static-access-shorthand/equality_A01_t07.dart b/LanguageFeatures/Static-access-shorthand/equality_A01_t07.dart new file mode 100644 index 0000000000..5d1af4bccb --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/equality_A01_t07.dart @@ -0,0 +1,50 @@ +// 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 an expression has the form `e1 == e2` or `e1 != e2`, or a +/// pattern has the form `== e2` or `!= e2`, where the static type of `e1`, or +/// the matched value type of the pattern, is S1, and e2 is precisely a +/// `` expression, then assign the type S1 as the +/// shorthand context of the `` of e2 before +/// inferring its static type the same way as above. +/// +/// @description Checks that if a pattern has the form `== e2` or `!= e2` and +/// `e2` is a shorthand expression, then it has context type from the matched +/// value type of the pattern. Test an enum. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +enum E { + e0(0), e1("one"); + final T value; + const E(this.value); + + static const E intOne = E.e0; +} + +main() { + bool success = false; + + if (E.e0 case == .e0) { + success = true; + } + Expect.isTrue(success); + + if (E.e1 case != .e0) { + success = true; + } + + if (E.e0 case == .intOne) { + success = true; + } + Expect.isTrue(success); + + if (E.e1 case != .intOne) { + success = true; + } + Expect.isTrue(success); +} diff --git a/LanguageFeatures/Static-access-shorthand/equality_A01_t08.dart b/LanguageFeatures/Static-access-shorthand/equality_A01_t08.dart new file mode 100644 index 0000000000..68b50116b2 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/equality_A01_t08.dart @@ -0,0 +1,76 @@ +// 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 an expression has the form `e1 == e2` or `e1 != e2`, or a +/// pattern has the form `== e2` or `!= e2`, where the static type of `e1`, or +/// the matched value type of the pattern, is S1, and e2 is precisely a +/// `` expression, then assign the type S1 as the +/// shorthand context of the `` of e2 before +/// inferring its static type the same way as above. +/// +/// @description Checks that if a pattern has the form `== e2` or `!= e2` and +/// `e2` is a shorthand expression, then it has context type from the matched +/// value type of the pattern. Test an extension type. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +extension type const ET(T value) { + const ET.id(this.value); + const factory ET.f(T t) = ET; + + static const ET intOne = ET(1); +} + +main() { + bool success = false; + void checkSuccess() { + Expect.isTrue(success); + success = false; + } + + ET et1 = ET(1); + if (et1 case == const .new(1)) { + success = true; + } + checkSuccess(); + + if (et1 case == const .id(1)) { + success = true; + } + checkSuccess(); + + if (et1 case == const .f(1)) { + success = true; + } + checkSuccess(); + + if (et1 case == .intOne) { + success = true; + } + checkSuccess(); + + ET et2 = ET(2); + if (et2 case != const .new(1)) { + success = true; + } + checkSuccess(); + + if (et2 case != const .id(1)) { + success = true; + } + checkSuccess(); + + if (et2 case != const .f(1)) { + success = true; + } + checkSuccess(); + + if (et2 case != .intOne) { + success = true; + } + checkSuccess(); +} diff --git a/LanguageFeatures/Static-access-shorthand/equality_A02_t01.dart b/LanguageFeatures/Static-access-shorthand/equality_A02_t01.dart new file mode 100644 index 0000000000..0e23b19712 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/equality_A02_t01.dart @@ -0,0 +1,73 @@ +// 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 For ==, we special-case when the right operand is (precisely!) a +/// static member shorthand. +/// ... +/// This special-casing is only against an immediate static member shorthand. It +/// does not change the context type of the second operand, so it would not work +/// with, for example, `Endian.host == wantBig ? .big : .little`. Here the +/// second operand is not a ``, so it won't have a +/// shorthand context set, and the context type of the second operand of `==` is +/// the empty context `_`. (It's neither the static type of the first operand, +/// nor the parameter type of the first operand's `operator==`.) +/// +/// @description Checks that it is a compile-time error if the right operand of +/// `==` operator is not a precisely static member shorthand but a ternary +/// operator. Test a class. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +class C { + T value; + C(this.value); + C.id(this.value); + factory C.f(T t) = C; + + static C get intOne => C(1); + static C intTwo() => C(2); + static List> values = [C(3)]; + + @override + bool operator ==(Object other) { + if (other is C) { + return value == other.value; + } + return false; + } +} + +main() { + bool condition = 2 > 1; + if (C(0) == condition ? .new(0) : C.id(1)) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (C(0) == condition ? C.new(0) : .id(1)) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (C(0) != condition ? .f(0) : C.intOne) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (C(0) != condition ? C.f(0) : .intOne) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (C(0) == condition ? .intTwo() : C.values[0]) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (C(0) == condition ? C.intTwo() : .values[0]) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} diff --git a/LanguageFeatures/Static-access-shorthand/equality_A02_t02.dart b/LanguageFeatures/Static-access-shorthand/equality_A02_t02.dart new file mode 100644 index 0000000000..5f21c1dce2 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/equality_A02_t02.dart @@ -0,0 +1,67 @@ +// 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 For ==, we special-case when the right operand is (precisely!) a +/// static member shorthand. +/// ... +/// This special-casing is only against an immediate static member shorthand. It +/// does not change the context type of the second operand, so it would not work +/// with, for example, `Endian.host == wantBig ? .big : .little`. Here the +/// second operand is not a ``, so it won't have a +/// shorthand context set, and the context type of the second operand of `==` is +/// the empty context `_`. (It's neither the static type of the first operand, +/// nor the parameter type of the first operand's `operator==`.) +/// +/// @description Checks that it is a compile-time error if the right operand of +/// `==` operator is not a precisely static member shorthand but a ternary +/// operator. Test a mixin. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +class C { + final T value; + const C(this.value); + + @override + bool operator ==(Object other) { + if (other is C) { + return value == other.value; + } + return false; + } +} + +mixin M on C { + static M get intOne => MC(1); + static M intTwo() => MC(2); + static List> values = [MC(3)]; +} + +class MC = C with M; + +main() { + bool condition = 2 > 1; + M m = MC(0); + + if (m == condition ? .intOne : M.intTwo()) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (m == condition ? M.intOne : .intTwo()) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (m != condition ? .intTwo() : M.values[0]) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (m != condition ? .values[0] : M.intTwo()) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} diff --git a/LanguageFeatures/Static-access-shorthand/equality_A02_t03.dart b/LanguageFeatures/Static-access-shorthand/equality_A02_t03.dart new file mode 100644 index 0000000000..57afbb6db9 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/equality_A02_t03.dart @@ -0,0 +1,54 @@ +// 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 For ==, we special-case when the right operand is (precisely!) a +/// static member shorthand. +/// ... +/// This special-casing is only against an immediate static member shorthand. It +/// does not change the context type of the second operand, so it would not work +/// with, for example, `Endian.host == wantBig ? .big : .little`. Here the +/// second operand is not a ``, so it won't have a +/// shorthand context set, and the context type of the second operand of `==` is +/// the empty context `_`. (It's neither the static type of the first operand, +/// nor the parameter type of the first operand's `operator==`.) +/// +/// @description Checks that it is a compile-time error if the right operand of +/// `==` operator is not a precisely static member shorthand but a ternary +/// operator. Test an enum. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +enum E { + e0(0), e1("1"); + final T value; + const E(this.value); + + static E get staticGetter => E.e0; + static E staticMethod(int index) => E.values[index]; +} + +main() { + bool condition = 2 > 1; + + if (E.e0 == condition ? .e0 : E.e1) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (E.e0 == condition ? E.e0 : .staticGetter) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (E.e0 != condition ? .staticMethod(0) : E.values[1]) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (E.e0 != condition ? .values[0] : E.e1) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} diff --git a/LanguageFeatures/Static-access-shorthand/equality_A02_t04.dart b/LanguageFeatures/Static-access-shorthand/equality_A02_t04.dart new file mode 100644 index 0000000000..af5e5081dc --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/equality_A02_t04.dart @@ -0,0 +1,66 @@ +// 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 For ==, we special-case when the right operand is (precisely!) a +/// static member shorthand. +/// ... +/// This special-casing is only against an immediate static member shorthand. It +/// does not change the context type of the second operand, so it would not work +/// with, for example, `Endian.host == wantBig ? .big : .little`. Here the +/// second operand is not a ``, so it won't have a +/// shorthand context set, and the context type of the second operand of `==` is +/// the empty context `_`. (It's neither the static type of the first operand, +/// nor the parameter type of the first operand's `operator==`.) +/// +/// @description Checks that it is a compile-time error if the right operand of +/// `==` operator is not a precisely static member shorthand but a ternary +/// operator. Test an enum. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +extension type ET(T value) { + ET.id(this.value); + factory ET.f(T t) = ET; + + static ET get staticGetter => ET(1); + static ET staticMethod(X x) => ET(x); + static List> instances = [ET("one")]; +} + + +main() { + bool condition = 2 > 1; + ET one = ET(1); + + if (one == condition ? .new(1) : ET.id(2)) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (one == condition ? ET(1) : .id(2)) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (one != condition ? .f(0) : ET.f(1)) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (one != condition ? ET(0) : .staticGetter) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (one != condition ? .staticMethod(1) : ET.staticGetter) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (one != condition ? ET.staticMethod(1) : .instances[0]) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +}