diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A01_t01.dart b/LanguageFeatures/Static-access-shorthand/grammar_A01_t01.dart new file mode 100644 index 0000000000..1ff0d97405 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/grammar_A01_t01.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 We introduce grammar productions of the form: +/// ``` +/// ::= ... -- all current productions +/// | +/// +/// ::= ... -- all current productions +/// | +/// +/// ::= +/// '.' ( | 'new') -- shorthand qualified name +/// | 'const' '.' ( | 'new') -- shorthand object creation +/// ``` +/// +/// @description Checks that static members and constructors of a class can be +/// accessed using the static access short syntax. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final String value; + C(this.value); + const C.foo(this.value); + + static C get staticGetter => C("Static getter"); + static C staticMethod() => C("Static method"); + static List instances = [C("one"), C("two")]; +} + +main() { + C c1 = .staticGetter; + Expect.equals("Static getter", c1.value); + + C c2 = .staticMethod(); + Expect.equals("Static method", c2.value); + + C c3 = .instances[0]; + Expect.equals("one", c3.value); + + C c4 = .new("new"); + Expect.equals("new", c4.value); + + C c5 = .foo("foo"); + Expect.equals("foo", c5.value); + + C c6 = const .foo("const foo"); + Expect.equals("const foo", c6.value); +} diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A01_t02.dart b/LanguageFeatures/Static-access-shorthand/grammar_A01_t02.dart new file mode 100644 index 0000000000..3ed9a01003 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/grammar_A01_t02.dart @@ -0,0 +1,53 @@ +// 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 We introduce grammar productions of the form: +/// ``` +/// ::= ... -- all current productions +/// | +/// +/// ::= ... -- all current productions +/// | +/// +/// ::= +/// '.' ( | 'new') -- shorthand qualified name +/// | 'const' '.' ( | 'new') -- shorthand object creation +/// ``` +/// +/// @description Checks that static members of a mixin can be accessed using the +/// static access short syntax. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final String value; + C(this.value); + const C.foo(this.value); + + static C get staticGetter => C("C: static getter"); + static C staticMethod() => C("C: static method"); + static List instances = [C("one"), C("two")]; +} + +mixin M on C { + static M get staticGetter => CM("M: static getter"); + static M staticMethod() => CM("M: static method"); + static List instances = [CM("M: one"), CM("M: two")]; +} + +class CM = C with M; + +main() { + M m1 = .staticGetter; + Expect.equals("M: static getter", m1.value); + + M m2 = .staticMethod(); + Expect.equals("M: static method", m2.value); + + M m3 = .instances[0]; + Expect.equals("M: one", m3.value); +} diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A01_t03.dart b/LanguageFeatures/Static-access-shorthand/grammar_A01_t03.dart new file mode 100644 index 0000000000..19ac762638 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/grammar_A01_t03.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 We introduce grammar productions of the form: +/// ``` +/// ::= ... -- all current productions +/// | +/// +/// ::= ... -- all current productions +/// | +/// +/// ::= +/// '.' ( | 'new') -- shorthand qualified name +/// | 'const' '.' ( | 'new') -- shorthand object creation +/// ``` +/// +/// @description Checks that static members and values of an enum can be +/// accessed using the static access short syntax. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +enum E { + v1("v1"), v2("v2"); + + final String value; + const E(this.value); + + static E get staticGetter => v1; + static E staticMethod() => v2; +} + +main() { + E e0 = .v1; + Expect.equals(E.v1, e0); + + E e1 = .staticGetter; + Expect.equals("v1", e1.value); + + E e2 = .staticMethod(); + Expect.equals("v2", e2.value); + + E e3 = E.values[1]; + Expect.equals(E.v2, e3); +} diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A01_t04.dart b/LanguageFeatures/Static-access-shorthand/grammar_A01_t04.dart new file mode 100644 index 0000000000..33ca092d10 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/grammar_A01_t04.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 We introduce grammar productions of the form: +/// ``` +/// ::= ... -- all current productions +/// | +/// +/// ::= ... -- all current productions +/// | +/// +/// ::= +/// '.' ( | 'new') -- shorthand qualified name +/// | 'const' '.' ( | 'new') -- shorthand object creation +/// ``` +/// +/// @description Checks that static members and constructors of an extension +/// type can be accessed using the static access short syntax. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +extension type ET1(int v) { + const ET1.foo(this.v); + ET1.bar(this.v); + + static ET1 get staticGetter => ET1(1); + static ET1 staticMethod() => ET1(2); + static ET1 instances = [ET1(0), ET1(1)]; +} + +extension type ET2.baz(int v) { + const ET2(this.v); +} + +extension type const ET3.qux(int v) { + ET3.new(this.v); +} + +main() { + ET1 et0 = .instances[0]; + Expect.equals(0, et0.v); + + ET1 et1 = .staticGetter; + Expect.equals(1, et1.v); + + ET1 et2 = .staticMethod(); + Expect.equals(2, et2.v); + + ET1 et3 = .new(3); + Expect.equals(3, et3.v); + + ET1 et4 = const .foo(4); + Expect.equals(4, et4.v); + + ET1 et5 = .bar(5); + Expect.equals(5, et5.v); + + ET2 et6 = .baz(6); + Expect.equals(6, et6.v); + + ET2 et7 = const .new(7); + Expect.equals(7, et7.v); + + ET3 et8 = const .qux(8); + Expect.equals(8, et8.v); + + ET3 et9 = .new(9); + Expect.equals(9, et9.v); +} diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A02_t01.dart b/LanguageFeatures/Static-access-shorthand/grammar_A02_t01.dart new file mode 100644 index 0000000000..be9655ca16 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/grammar_A02_t01.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 We introduce grammar productions of the form: +/// ``` +/// ::= ... -- all current productions +/// | +/// +/// ::= ... -- all current productions +/// | +/// +/// ::= +/// '.' ( | 'new') -- shorthand qualified name +/// | 'const' '.' ( | 'new') -- shorthand object creation +/// ``` +/// +/// @description Checks that it is a syntax error to use a `new` keyword in the +/// static access short syntax. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +class C { + final String value; + C(this.value); + const C.foo(this.value); +} + +extension type ET(int v) { + const ET.foo(this.v); + ET.bar(this.v); +} + +main() { + C c1 = new .new("new"); +// ^^^ +// [analyzer] unspecified +// [cfe] unspecified + C c2 = new .foo("foo"); +// ^^^ +// [analyzer] unspecified +// [cfe] unspecified + + ET et1 = new .new(1); +// ^^^ +// [analyzer] unspecified +// [cfe] unspecified + ET et2 = new .foo(2); +// ^^^ +// [analyzer] unspecified +// [cfe] unspecified + ET et3 = new .bar(3); +// ^^^ +// [analyzer] unspecified +// [cfe] unspecified +}