diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A04_t17.dart b/LanguageFeatures/Static-access-shorthand/grammar_A04_t17.dart new file mode 100644 index 0000000000..95f99556c6 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/grammar_A04_t17.dart @@ -0,0 +1,75 @@ +// Copyright (c) 2025, 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 We introduce grammar productions of the form: +/// ``` +/// ::= ... -- all current productions +/// | -- added production +/// +/// ::= ... -- all current productions +/// | -- No selectors, no `.new`. +/// +/// ::= * +/// +/// ::= +/// +/// | '.' 'new' -- shorthand unnamed constructor +/// +/// ::= -- something that can potentially create a value. +/// | '.' -- shorthand for qualified name +/// | 'const' '.' ( | 'new') -- shorthand for constant object creation +/// ``` +/// +/// @description Checks that the static access shorthand syntax can be used with +/// the `[]` operator. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + static C zero = C(0); + int index; + List values = [1, 2, 3]; + + C(this.index); + int operator [](C ind) => values[ind.index]; +} + +mixin M { + static M two = MO(2); + List values = [1, 2, 3]; + + int operator [](M ind) => values[(ind as MO).index]; +} +class MO with M { + int index; + MO(this.index); +} + +enum E { + e0(0), e1(1), e2(2); + final int value; + static E two = E.e2; + const E(this.value); + + int operator [](E ind) => values[ind.value].value; +} + +extension type ET(int v) implements int { + static ET two = ET(2); + static List values = [1, 2, 3]; + + int operator [](ET index) => ET.values[index.v]; +} + +main() { + Expect.equals(1, C(0)[.zero]); + Expect.equals(3, (MO(0) as M)[.two]); + Expect.equals(2, E.e1[.two]); + Expect.equals(1, E.e1[.e1]); + Expect.equals(3, ET(0)[.two]); + +} diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A04_t18.dart b/LanguageFeatures/Static-access-shorthand/grammar_A04_t18.dart new file mode 100644 index 0000000000..5037b4ad92 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/grammar_A04_t18.dart @@ -0,0 +1,94 @@ +// Copyright (c) 2025, 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 We introduce grammar productions of the form: +/// ``` +/// ::= ... -- all current productions +/// | -- added production +/// +/// ::= ... -- all current productions +/// | -- No selectors, no `.new`. +/// +/// ::= * +/// +/// ::= +/// +/// | '.' 'new' -- shorthand unnamed constructor +/// +/// ::= -- something that can potentially create a value. +/// | '.' -- shorthand for qualified name +/// | 'const' '.' ( | 'new') -- shorthand for constant object creation +/// ``` +/// +/// @description Checks that the static access shorthand syntax can be used with +/// the `[]=` operator. Test the first operand. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + static C zero = C(0); + int index; + List values = [1, 2, 3]; + + C(this.index); + void operator []=(C ind, int value) { + values[ind.index] = value; + } +} + +mixin M { + static M two = MO(2); + List values = [1, 2, 3]; + + void operator []=(M ind, int value) { + values[(ind as MO).index] = value; + } +} +class MO with M { + int index; + MO(this.index); +} + +enum E { + e0(0), e1(1), e2(2); + final int value; + static List vals = [1, 2, 3]; + static E two = E.e2; + const E(this.value); + + void operator []=(E ind, int value) { + E.vals[ind.value] = value; + } +} + +extension type ET(int v) implements int { + static ET two = ET(2); + static List values = [1, 2, 3]; + + void operator []=(ET index, int value) { + ET.values[index.v] = value; + } +} + +main() { + C c = C(0); + c[.zero] = 42; + Expect.equals(42, c.values[0]); + + M m = MO(0); + m[.two] = 42; + Expect.equals(42, m.values[2]); + + E.e0[.two] = 42; + Expect.equals(42, E.vals[2]); + E.e0[.e1] = 42; + Expect.equals(42, E.vals[1]); + + ET et = ET(0); + et[.two] = 42; + Expect.equals(42, ET.values[2]); +} diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A04_t19.dart b/LanguageFeatures/Static-access-shorthand/grammar_A04_t19.dart new file mode 100644 index 0000000000..1a05c5b00b --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/grammar_A04_t19.dart @@ -0,0 +1,91 @@ +// Copyright (c) 2025, 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 We introduce grammar productions of the form: +/// ``` +/// ::= ... -- all current productions +/// | -- added production +/// +/// ::= ... -- all current productions +/// | -- No selectors, no `.new`. +/// +/// ::= * +/// +/// ::= +/// +/// | '.' 'new' -- shorthand unnamed constructor +/// +/// ::= -- something that can potentially create a value. +/// | '.' -- shorthand for qualified name +/// | 'const' '.' ( | 'new') -- shorthand for constant object creation +/// ``` +/// +/// @description Checks that the static access shorthand syntax can be used with +/// the `[]=` operator. Test the second operand. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + static C zero = C(0); + int v; + List values = [1, 2, 3]; + + C(this.v); + void operator []=(int index, C value) { + values[index] = value.v; + } +} + +mixin M { + static M two = MO(2); + List values = [1, 2, 3]; + + void operator []=(int index, M value) { + values[index] = (value as MO).v; + } +} +class MO with M { + int v; + MO(this.v); +} + +enum E { + e0(0), e1(1), e2(2); + final int v; + static List vals = [1, 2, 3]; + static E two = E.e2; + const E(this.v); + + void operator []=(int index, E value) { + E.vals[index] = value.v; + } +} + +extension type ET(int v) implements int { + static ET two = ET(2); + static List values = [1, 2, 3]; + + void operator []=(int index, ET value) { + ET.values[index] = value.v; + } +} + +main() { + C c = C(0); + c[1] = .zero; + Expect.equals(0, c.values[1]); + M m = MO(0); + m[0] = .two; + Expect.equals(2, m.values[0]); + E.e0[0] = .two; + Expect.equals(2, E.vals[0]); + E.e0[0] = .e1; + Expect.equals(1, E.vals[0]); + ET et = ET(0); + et[0] = .two; + Expect.equals(2, ET.values[0]); +} diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A06_t03.dart b/LanguageFeatures/Static-access-shorthand/grammar_A06_t03.dart new file mode 100644 index 0000000000..4185ff7de3 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/grammar_A06_t03.dart @@ -0,0 +1,76 @@ +// Copyright (c) 2025, 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 We introduce grammar productions of the form: +/// ``` +/// ::= ... -- all current productions +/// | -- added production +/// +/// ::= ... -- all current productions +/// | -- No selectors, no `.new`. +/// +/// ::= * +/// +/// ::= +/// +/// | '.' 'new' -- shorthand unnamed constructor +/// +/// ::= -- something that can potentially create a value. +/// | '.' -- shorthand for qualified name +/// | 'const' '.' ( | 'new') -- shorthand for constant object creation +/// ``` +/// +/// @description Checks that it is a compile-time error to use the static access +/// shorthand syntax with the `as` operator. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +class C { + static C one = C(1); + int v; + C(this.v); +} + +mixin M on C { + static M two = MC(2); +} +class MC = C with M; + +enum E { + e1; + + static E one = E.e1; +} + +extension type ET(int v) implements int { + static ET one = ET(1); +} + +main() { + C c = .one as C; // ignore: unnecessary_cast +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + M m = .two as M; // ignore: unnecessary_cast +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + E e1 = .one as E; // ignore: unnecessary_cast +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + E e2 = .e1 as E; // ignore: unnecessary_cast +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + ET et = .one as ET; // ignore: unnecessary_cast +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A06_t04.dart b/LanguageFeatures/Static-access-shorthand/grammar_A06_t04.dart new file mode 100644 index 0000000000..9a84c84a85 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/grammar_A06_t04.dart @@ -0,0 +1,76 @@ +// Copyright (c) 2025, 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 We introduce grammar productions of the form: +/// ``` +/// ::= ... -- all current productions +/// | -- added production +/// +/// ::= ... -- all current productions +/// | -- No selectors, no `.new`. +/// +/// ::= * +/// +/// ::= +/// +/// | '.' 'new' -- shorthand unnamed constructor +/// +/// ::= -- something that can potentially create a value. +/// | '.' -- shorthand for qualified name +/// | 'const' '.' ( | 'new') -- shorthand for constant object creation +/// ``` +/// +/// @description Checks that it is a compile-time error to use the static access +/// shorthand syntax with the `is` operator. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +class C { + static C one = C(1); + int v; + C(this.v); +} + +mixin M on C { + static M two = MC(2); +} +class MC = C with M; + +enum E { + e1; + + static E one = E.e1; +} + +extension type ET(int v) implements int { + static ET one = ET(1); +} + +main() { + if (.one is C) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (.two is M) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (.one is E) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (.e1 is E) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (.one is ET) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A06_t05.dart b/LanguageFeatures/Static-access-shorthand/grammar_A06_t05.dart new file mode 100644 index 0000000000..ea20a46acc --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/grammar_A06_t05.dart @@ -0,0 +1,76 @@ +// Copyright (c) 2025, 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 We introduce grammar productions of the form: +/// ``` +/// ::= ... -- all current productions +/// | -- added production +/// +/// ::= ... -- all current productions +/// | -- No selectors, no `.new`. +/// +/// ::= * +/// +/// ::= +/// +/// | '.' 'new' -- shorthand unnamed constructor +/// +/// ::= -- something that can potentially create a value. +/// | '.' -- shorthand for qualified name +/// | 'const' '.' ( | 'new') -- shorthand for constant object creation +/// ``` +/// +/// @description Checks that it is a compile-time error to use the static access +/// shorthand syntax with the `is!` operator. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +class C { + static C one = C(1); + int v; + C(this.v); +} + +mixin M on C { + static M two = MC(2); +} +class MC = C with M; + +enum E { + e1; + + static E one = E.e1; +} + +extension type ET(int v) implements int { + static ET one = ET(1); +} + +main() { + if (.one is! C) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (.two is! M) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (.one is! E) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (.e1 is! E) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + if (.one is! ET) {} +// ^ +// [analyzer] unspecified +// [cfe] unspecified +}