From 4941d31d1ef81a56b1bb24133032756f854c140e Mon Sep 17 00:00:00 2001 From: "Sergey G. Grekhov" Date: Wed, 15 Jan 2025 17:26:59 +0200 Subject: [PATCH] #2976. Add more tests for records, factories and parenthesized expressions (#3047) Add more tests for records, factories and parenthesized expressions --- .../grammar_A01_t01.dart | 32 +++++++++ .../grammar_A01_t02.dart | 9 +++ .../grammar_A01_t03.dart | 12 ++++ .../grammar_A01_t04.dart | 40 +++++++++-- .../grammar_A01_t07.dart | 64 +++++++++++++++++ .../grammar_A01_t08.dart | 70 +++++++++++++++++++ 6 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 LanguageFeatures/Static-access-shorthand/grammar_A01_t07.dart create mode 100644 LanguageFeatures/Static-access-shorthand/grammar_A01_t08.dart diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A01_t01.dart b/LanguageFeatures/Static-access-shorthand/grammar_A01_t01.dart index fc7af547ad..f4498d4e80 100644 --- a/LanguageFeatures/Static-access-shorthand/grammar_A01_t01.dart +++ b/LanguageFeatures/Static-access-shorthand/grammar_A01_t01.dart @@ -33,6 +33,8 @@ class C { final String value; C(this.value); const C.foo(this.value); + factory C.bar(String v) = C; + const factory C.baz(String v) = C.foo; static C get staticGetter => C("Static getter"); static C staticMethod() => C("Static method"); @@ -57,4 +59,34 @@ main() { C c6 = const .foo("const foo"); Expect.equals("const foo", c6.value); + + C c7 = .bar("bar"); + Expect.equals("bar", c7.value); + + C c8 = .baz("baz"); + Expect.equals("baz", c8.value); + + C c9 = const .baz("const baz"); + Expect.equals("const baz", c9.value); + + C c10 = (.staticGetter); + Expect.equals("Static getter", c10.value); + + C c11 = (.staticMethod()); + Expect.equals("Static method", c11.value); + + C c12 = (.instances[0]); + Expect.equals("one", c12.value); + + C c13 = (.new("new")); + Expect.equals("new", c13.value); + + C c14 = (.foo("foo")); + Expect.equals("foo", c14.value); + + C c15 = (.bar("bar")); + Expect.equals("bar", c15.value); + + C c16 = (.baz("baz")); + Expect.equals("baz", c16.value); } diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A01_t02.dart b/LanguageFeatures/Static-access-shorthand/grammar_A01_t02.dart index 7c67f632d4..d22382cc82 100644 --- a/LanguageFeatures/Static-access-shorthand/grammar_A01_t02.dart +++ b/LanguageFeatures/Static-access-shorthand/grammar_A01_t02.dart @@ -56,4 +56,13 @@ main() { M m3 = .instances[0]; Expect.equals("M: one", m3.value); + + M m4 = (.staticGetter); + Expect.equals("M: static getter", m1.value); + + M m5 = (.staticMethod()); + Expect.equals("M: static method", m2.value); + + M m6 = (.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 index d844f446b3..b1e74f8e10 100644 --- a/LanguageFeatures/Static-access-shorthand/grammar_A01_t03.dart +++ b/LanguageFeatures/Static-access-shorthand/grammar_A01_t03.dart @@ -51,4 +51,16 @@ main() { E e3 = .values[1]; Expect.equals(E.v2, e3); + + E e4 = (.v1); + Expect.equals(E.v1, e4); + + E e5 = (.staticGetter); + Expect.equals("v1", e5.value); + + E e6 = (.staticMethod()); + Expect.equals("v2", e6.value); + + E e7 = (.values[1]); + Expect.equals(E.v2, e7); } diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A01_t04.dart b/LanguageFeatures/Static-access-shorthand/grammar_A01_t04.dart index b92d6293e5..20016938e6 100644 --- a/LanguageFeatures/Static-access-shorthand/grammar_A01_t04.dart +++ b/LanguageFeatures/Static-access-shorthand/grammar_A01_t04.dart @@ -32,6 +32,7 @@ import '../../Utils/expect.dart'; extension type ET1(int v) { const ET1.foo(this.v); ET1.bar(this.v); + const factory ET1.baz(int v) = ET1.foo; static ET1 get staticGetter => ET1(1); static ET1 staticMethod() => ET1(2); @@ -65,15 +66,46 @@ main() { ET1 et5 = .bar(5); Expect.equals(5, et5.v); - ET2 et6 = .baz(6); + ET1 et6 = .baz(6); Expect.equals(6, et6.v); - ET2 et7 = const .new(7); + ET1 et7 = const .baz(7); Expect.equals(7, et7.v); - ET3 et8 = const .qux(8); + ET2 et8 = .baz(8); Expect.equals(8, et8.v); - ET3 et9 = .new(9); + ET2 et9 = const .new(9); Expect.equals(9, et9.v); + + ET3 et10 = const .qux(10); + Expect.equals(10, et10.v); + + ET3 et11 = .new(11); + Expect.equals(11, et11.v); + + + ET1 et20 = (.instances[0]); + Expect.equals(0, et20.v); + + ET1 et21 = (.staticGetter); + Expect.equals(1, et21.v); + + ET1 et22 = (.staticMethod()); + Expect.equals(2, et22.v); + + ET1 et23 = (.new(23)); + Expect.equals(23, et23.v); + + ET1 et24 = (.bar(24)); + Expect.equals(24, et24.v); + + ET1 et25 = (.baz(25)); + Expect.equals(25, et25.v); + + ET2 et26 = (.baz(26)); + Expect.equals(26, et26.v); + + ET3 et27 = (.new(27)); + Expect.equals(27, et27.v); } diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A01_t07.dart b/LanguageFeatures/Static-access-shorthand/grammar_A01_t07.dart new file mode 100644 index 0000000000..5e7f406d55 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/grammar_A01_t07.dart @@ -0,0 +1,64 @@ +// 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 expressions of the form +/// `const '(' '.' id(arguments) ',' ')'` and +/// `const '(' '.' new(arguments) ',' ')'` are records containing constant +/// shorthand expressions. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + final String value; + const C(this.value); + const C.id(this.value); + const factory C.f(String value) = C; +} + +extension type const ET(String value) { + const ET.id(this.value); + const factory ET.f(String value) = ET; +} + +main() { + const (C,) c1 = const (.new("one"),); + Expect.equals("one", c1.$1.value); + + const (C,) c2 = const (.id("two"),); + Expect.equals("two", c2.$1.value); + + const (C,) c3 = const (.f("three"),); + Expect.equals("three", c3.$1.value); + + const (ET,) et1 = const (.new("new"),); + Expect.equals("new", et1.$1.value); + + const (ET,) et2 = const (.id("id"),); + Expect.equals("id", et2.$1.value); + + const (ET,) et3 = const (.f("f"),); + Expect.equals("f", et3.$1.value); +} diff --git a/LanguageFeatures/Static-access-shorthand/grammar_A01_t08.dart b/LanguageFeatures/Static-access-shorthand/grammar_A01_t08.dart new file mode 100644 index 0000000000..dda5b843d4 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/grammar_A01_t08.dart @@ -0,0 +1,70 @@ +// 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 expressions of the form +/// `const '(' '.' id(arguments) ')'` and +/// `const '(' '.' new(arguments) ')'` are still compile-time errors. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +class C { + final String value; + const C(this.value); + const C.id(this.value); + const factory C.f(String value) = C; +} + +extension type const ET(String value) { + const ET.id(this.value); + const factory ET.f(String value) = ET; +} + +main() { + const C c1 = const (.new("one")); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + const C c2 = const (.id("two")); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + const C c3 = const (.f("three")); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + const ET et1 = const (.new("new")); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + const ET et2 = const (.id("id")); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + + const ET et3 = const (.f("f")); +// ^ +// [analyzer] unspecified +// [cfe] unspecified +}