From 9e8fd44f7c185482e8a9204caeefe2f3d7e1a822 Mon Sep 17 00:00:00 2001 From: sgrekhov Date: Wed, 25 Dec 2024 16:25:48 +0200 Subject: [PATCH] #2976. Add constant expressions tests.Part 2. --- .../constant_expression_A10_t01.dart | 32 +++++++++++ .../constant_expression_A10_t02.dart | 32 +++++++++++ .../constant_expression_A10_t03.dart | 35 ++++++++++++ .../constant_expression_A10_t04.dart | 46 +++++++++++++++ .../constant_expression_A10_t05.dart | 56 +++++++++++++++++++ .../constant_expression_A10_t06.dart | 46 +++++++++++++++ .../constant_expression_A10_t07.dart | 56 +++++++++++++++++++ .../constant_expression_A10_t08.dart | 53 ++++++++++++++++++ .../constant_expression_A10_t09.dart | 56 +++++++++++++++++++ .../constant_expression_A10_t10.dart | 50 +++++++++++++++++ .../constant_expression_A10_t11.dart | 56 +++++++++++++++++++ .../semantics_A01_t05.dart | 14 ++--- .../semantics_A01_t06.dart | 12 ++-- .../semantics_lib.dart | 2 +- .../shorthand_lib.dart | 20 ++++--- 15 files changed, 545 insertions(+), 21 deletions(-) create mode 100644 LanguageFeatures/Static-access-shorthand/constant_expression_A10_t01.dart create mode 100644 LanguageFeatures/Static-access-shorthand/constant_expression_A10_t02.dart create mode 100644 LanguageFeatures/Static-access-shorthand/constant_expression_A10_t03.dart create mode 100644 LanguageFeatures/Static-access-shorthand/constant_expression_A10_t04.dart create mode 100644 LanguageFeatures/Static-access-shorthand/constant_expression_A10_t05.dart create mode 100644 LanguageFeatures/Static-access-shorthand/constant_expression_A10_t06.dart create mode 100644 LanguageFeatures/Static-access-shorthand/constant_expression_A10_t07.dart create mode 100644 LanguageFeatures/Static-access-shorthand/constant_expression_A10_t08.dart create mode 100644 LanguageFeatures/Static-access-shorthand/constant_expression_A10_t09.dart create mode 100644 LanguageFeatures/Static-access-shorthand/constant_expression_A10_t10.dart create mode 100644 LanguageFeatures/Static-access-shorthand/constant_expression_A10_t11.dart diff --git a/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t01.dart b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t01.dart new file mode 100644 index 0000000000..63ba0fc2bb --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t01.dart @@ -0,0 +1,32 @@ +// 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 static member shorthand expression should be a potentially +/// constant expression if the corresponding explicit static member plus +/// selectors expression would be, which currently means that it's a potentially +/// constant expression if and only if it's a constant expression. +/// +/// @description Checks that a qualified reference to a static constant variable +/// that is not qualified by a deferred prefix, is a potentially constant and +/// constant expression +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; +import 'shorthand_lib.dart'as p; + +main() { + const p.C c = .answer; + Expect.identical(42, c.t); + + const p.M m = .answer; + Expect.identical(42, m.t); + + const p.E e = .answer; + Expect.identical(42, e.t); + + const p.ET et = .answer; + Expect.identical(42, et.t); +} diff --git a/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t02.dart b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t02.dart new file mode 100644 index 0000000000..3d8f1c37e9 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t02.dart @@ -0,0 +1,32 @@ +// 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 static member shorthand expression should be a potentially +/// constant expression if the corresponding explicit static member plus +/// selectors expression would be, which currently means that it's a potentially +/// constant expression if and only if it's a constant expression. +/// +/// @description Checks that a static member shorthand expression can be used in +/// the initializer list of a constant constructor. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class A { + final int id; + const A(this.id); + static const A answer = const A(42); +} + +class C { + final A a; + const C(this.a); + const C.fromAnswer() : a = .answer; +} + +main() { + Expect.identical(42, C.fromAnswer().a.id); +} diff --git a/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t03.dart b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t03.dart new file mode 100644 index 0000000000..6c2407cf22 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t03.dart @@ -0,0 +1,35 @@ +// 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 static member shorthand expression should be a potentially +/// constant expression if the corresponding explicit static member plus +/// selectors expression would be, which currently means that it's a potentially +/// constant expression if and only if it's a constant expression. +/// +/// @description Checks that it is a compile-time error if a non-constant static +/// member shorthand expression is used in the initializer list of the constant +/// constructor. +/// @author sgrekhov22@gmail.com +/// @issue 59804 + +// SharedOptions=--enable-experiment=enum-shorthands + +class A { + int id; + A(this.id); + static A answer = A(42); +} + +class C { + final A a; + const C(this.a); + const C.fromAnswer() : a = .answer; +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +main() { + print(C); +} diff --git a/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t04.dart b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t04.dart new file mode 100644 index 0000000000..aebb0dfe0e --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t04.dart @@ -0,0 +1,46 @@ +// 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 static member shorthand expression should be a potentially +/// constant expression if the corresponding explicit static member plus +/// selectors expression would be, which currently means that it's a potentially +/// constant expression if and only if it's a constant expression. +/// +/// @description Checks that a static member shorthand expression can be used in +/// a constant list literal. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + static const C answer = const C(42); + final int v; + const C(this.v); +} + +mixin M on C { + static const M answer = const MC(42); +} + +class MC extends C with M { + const MC(super.v); +} + +enum E { + e0; + static const E answer = E.e0; +} + +extension type const ET(int v) { + static const ET answer = const ET(42); +} + +main() { + Expect.identical(const [C(42)], const [.answer]); + Expect.identical(const [MC(42)], const [.answer]); + Expect.identical(const [E.e0], const [.answer]); + Expect.identical(const [ET(42)], const [.answer]); +} diff --git a/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t05.dart b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t05.dart new file mode 100644 index 0000000000..1469e0e39c --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t05.dart @@ -0,0 +1,56 @@ +// 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 static member shorthand expression should be a potentially +/// constant expression if the corresponding explicit static member plus +/// selectors expression would be, which currently means that it's a potentially +/// constant expression if and only if it's a constant expression. +/// +/// @description Checks that it is a compile-time error to use a non-constant +/// static member shorthand expression in a constant list literal. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +class C { + static C answer = const C(42); + final int v; + const C(this.v); +} + +mixin M on C { + static M answer = const MC(42); +} + +class MC extends C with M { + const MC(super.v); +} + +enum E { + e0; + static E answer = E.e0; +} + +extension type const ET(int v) { + static ET answer = const ET(42); +} + +main() { + print(const [.answer]); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + print(const [.answer]); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + print(const [.answer]); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + print(const [.answer]); +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} diff --git a/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t06.dart b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t06.dart new file mode 100644 index 0000000000..30a7610527 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t06.dart @@ -0,0 +1,46 @@ +// 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 static member shorthand expression should be a potentially +/// constant expression if the corresponding explicit static member plus +/// selectors expression would be, which currently means that it's a potentially +/// constant expression if and only if it's a constant expression. +/// +/// @description Checks that a static member shorthand expression can be used in +/// a constant set literal. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + static const C answer = const C(42); + final int v; + const C(this.v); +} + +mixin M on C { + static const M answer = const MC(42); +} + +class MC extends C with M { + const MC(super.v); +} + +enum E { + e0; + static const E answer = E.e0; +} + +extension type const ET(int v) { + static const ET answer = const ET(42); +} + +main() { + Expect.identical(const {C(42)}, const {.answer}); + Expect.identical(const {MC(42)}, const {.answer}); + Expect.identical(const {E.e0}, const {.answer}); + Expect.identical(const {ET(42)}, const {.answer}); +} diff --git a/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t07.dart b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t07.dart new file mode 100644 index 0000000000..9b8d8161cb --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t07.dart @@ -0,0 +1,56 @@ +// 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 static member shorthand expression should be a potentially +/// constant expression if the corresponding explicit static member plus +/// selectors expression would be, which currently means that it's a potentially +/// constant expression if and only if it's a constant expression. +/// +/// @description Checks that it is a compile-time error to use a non-constant +/// static member shorthand expression in a constant set literal. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +class C { + static C answer = const C(42); + final int v; + const C(this.v); +} + +mixin M on C { + static M answer = const MC(42); +} + +class MC extends C with M { + const MC(super.v); +} + +enum E { + e0; + static E answer = E.e0; +} + +extension type const ET(int v) { + static ET answer = const ET(42); +} + +main() { + print(const {.answer}); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + print(const {.answer}); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + print(const {.answer}); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + print(const {.answer}); +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} diff --git a/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t08.dart b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t08.dart new file mode 100644 index 0000000000..6abf47cdb8 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t08.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 A static member shorthand expression should be a potentially +/// constant expression if the corresponding explicit static member plus +/// selectors expression would be, which currently means that it's a potentially +/// constant expression if and only if it's a constant expression. +/// +/// @description Checks that a static member shorthand expression can be used in +/// a constant map literal. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + static const C answer = const C(42); + final int v; + const C(this.v); +} + +mixin M on C { + static const M answer = const MC(42); +} + +class MC extends C with M { + const MC(super.v); +} + +enum E { + e0; + + static const E answer = E.e0; +} + +extension type const ET(int v) { + static const ET answer = const ET(42); +} + +main() { + Expect.identical(const {"key": C(42)}, const {"key": .answer}); + Expect.identical( + const {MC(42): "value"}, + const {.answer: "value"}, + ); + Expect.identical(const {"key": E.e0}, const {"key": .answer}); + Expect.identical( + const {ET(42): "value"}, + const {.answer: "value"}, + ); +} diff --git a/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t09.dart b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t09.dart new file mode 100644 index 0000000000..0a3dec4eee --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t09.dart @@ -0,0 +1,56 @@ +// 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 static member shorthand expression should be a potentially +/// constant expression if the corresponding explicit static member plus +/// selectors expression would be, which currently means that it's a potentially +/// constant expression if and only if it's a constant expression. +/// +/// @description Checks that it is a compile-time error to use a non-constant +/// static member shorthand expression in a constant map literal. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +class C { + static C answer = const C(42); + final int v; + const C(this.v); +} + +mixin M on C { + static M answer = const MC(42); +} + +class MC extends C with M { + const MC(super.v); +} + +enum E { + e0; + static E answer = E.e0; +} + +extension type const ET(int v) { + static ET answer = const ET(42); +} + +main() { + print(const {"key": .answer}); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + print(const {.answer: "value"}); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + print(const {"key": .answer}); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + print(const {.answer: "value"}); +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} diff --git a/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t10.dart b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t10.dart new file mode 100644 index 0000000000..a057ec15f6 --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t10.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 A static member shorthand expression should be a potentially +/// constant expression if the corresponding explicit static member plus +/// selectors expression would be, which currently means that it's a potentially +/// constant expression if and only if it's a constant expression. +/// +/// @description Checks that a static member shorthand expression can be used in +/// a constant parenthesized expression. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../../Utils/expect.dart'; + +class C { + static const C answer = const C(42); + final int v; + const C(this.v); +} + +mixin M on C { + static const M answer = const MC(42); +} + +class MC extends C with M { + const MC(super.v); +} + +enum E { + e0; + static const E answer = E.e0; +} + +extension type const ET(int v) { + static const ET answer = const ET(42); +} + +main() { + const C c = (.answer); + Expect.identical(const C(42), c); + const M m = (.answer); + Expect.identical(const MC(42), m); + const E e = (.answer); + Expect.identical(E.e0, e); + const ET et = (.answer); + Expect.identical(const ET(42), et); +} diff --git a/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t11.dart b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t11.dart new file mode 100644 index 0000000000..79ed57d19d --- /dev/null +++ b/LanguageFeatures/Static-access-shorthand/constant_expression_A10_t11.dart @@ -0,0 +1,56 @@ +// 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 static member shorthand expression should be a potentially +/// constant expression if the corresponding explicit static member plus +/// selectors expression would be, which currently means that it's a potentially +/// constant expression if and only if it's a constant expression. +/// +/// @description Checks that it is a compile-time error to use a non-constant +/// static member shorthand expression in a constant parenthesized expression. +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=enum-shorthands + +class C { + static C answer = const C(42); + final int v; + const C(this.v); +} + +mixin M on C { + static M answer = const MC(42); +} + +class MC extends C with M { + const MC(super.v); +} + +enum E { + e0; + static E answer = E.e0; +} + +extension type const ET(int v) { + static ET answer = const ET(42); +} + +main() { + const C c = (.answer); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + const M m = (.answer); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + const E e = (.answer); +// ^ +// [analyzer] unspecified +// [cfe] unspecified + const ET et = (.answer); +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} diff --git a/LanguageFeatures/Static-access-shorthand/semantics_A01_t05.dart b/LanguageFeatures/Static-access-shorthand/semantics_A01_t05.dart index d717e1e233..2c70243e9e 100644 --- a/LanguageFeatures/Static-access-shorthand/semantics_A01_t05.dart +++ b/LanguageFeatures/Static-access-shorthand/semantics_A01_t05.dart @@ -31,14 +31,14 @@ import '../../Utils/expect.dart'; import 'shorthand_lib.dart'; main() { - Object o = C(); + Object o = C(0); if (o is C) { o = .id3; if (o is Function) { Expect.equals(42, o(42)); } } - o = C(); + o = C(0); if (o is CAlias) { o = .id3; if (o is Function) { @@ -46,14 +46,14 @@ main() { } } - o = MA(); + o = MC(0); if (o is M) { o = .id3; if (o is Function) { Expect.equals(42, o(42)); } } - o = MA(); + o = MC(0); if (o is MAlias) { o = .id3; if (o is Function) { @@ -61,14 +61,14 @@ main() { } } - o = E.e0; + o = E.e1; if (o is E) { o = .id3; if (o is Function) { Expect.equals(42, o(42)); } } - o = E.e0; + o = E.e1; if (o is EAlias) { o = .id3; if (o is Function) { @@ -76,7 +76,7 @@ main() { } } - o = ET(0); + o = ET(0); if (o is ET) { o = .id3; if (o is Function) { diff --git a/LanguageFeatures/Static-access-shorthand/semantics_A01_t06.dart b/LanguageFeatures/Static-access-shorthand/semantics_A01_t06.dart index 114f47cc24..b97fa6d3ab 100644 --- a/LanguageFeatures/Static-access-shorthand/semantics_A01_t06.dart +++ b/LanguageFeatures/Static-access-shorthand/semantics_A01_t06.dart @@ -31,14 +31,14 @@ import '../../Utils/expect.dart'; import 'shorthand_lib.dart' as p; main() { - Object o = p.C(); + Object o = p.C(0); if (o is p.C) { o = .id3; if (o is Function) { Expect.equals(42, o(42)); } } - o = p.C(); + o = p.C(0); if (o is p.CAlias) { o = .id3; if (o is Function) { @@ -46,14 +46,14 @@ main() { } } - o = p.MA(); + o = p.MC(0); if (o is p.M) { o = .id3; if (o is Function) { Expect.equals(42, o(42)); } } - o = p.MA(); + o = p.MC(0); if (o is p.MAlias) { o = .id3; if (o is Function) { @@ -61,14 +61,14 @@ main() { } } - o = p.E.e0; + o = p.E.e1; if (o is p.E) { o = .id3; if (o is Function) { Expect.equals(42, o(42)); } } - o = p.E.e0; + o = p.E.e1; if (o is p.EAlias) { o = .id3; if (o is Function) { diff --git a/LanguageFeatures/Static-access-shorthand/semantics_lib.dart b/LanguageFeatures/Static-access-shorthand/semantics_lib.dart index b435349809..66adb81589 100644 --- a/LanguageFeatures/Static-access-shorthand/semantics_lib.dart +++ b/LanguageFeatures/Static-access-shorthand/semantics_lib.dart @@ -13,4 +13,4 @@ import 'shorthand_lib.dart'; C testClass(C c) => c; M testMixin(M m) => m; E testEnum(E e) => e; -ET testExtensionType(ET et) => et; +ET testExtensionType(ET et) => et; diff --git a/LanguageFeatures/Static-access-shorthand/shorthand_lib.dart b/LanguageFeatures/Static-access-shorthand/shorthand_lib.dart index a132641774..0d10f30f70 100644 --- a/LanguageFeatures/Static-access-shorthand/shorthand_lib.dart +++ b/LanguageFeatures/Static-access-shorthand/shorthand_lib.dart @@ -9,12 +9,13 @@ // SharedOptions=--enable-experiment=enum-shorthands class C { - T t; - C(this.t); + final T t; + const C(this.t); static C get id1 => C(1); static C id2(X x) => C(x); static X id3(X x) => x; + static const C answer = const C(42); } typedef CAlias = C; @@ -24,33 +25,38 @@ mixin M on C { static M get id1 => MC(2); static M id2(X x) => MC(x); static X id3(X x) => x; + static const M answer = const MC(42); } -class MC = C with M; +class MC extends C with M { + const MC(super.t); +} typedef MAlias = M; typedef MInt = M; enum E { - e1(3), e2("3"); + e1(3), e2("3"), e3(42); final T t; const E(this.t); static E get id1 => E.e1; static E id2() => E.e2; static X id3(X x) => x; + static const E answer = E.e3; } typedef EAlias = E; typedef EInt = E; -extension type ET(T t) { +extension type const ET(T t) implements num { static ET get id1 => ET(4); - static ET id2(X x) => ET(x); + static ET id2(X x) => ET(x); static X id3(X x) => x; + static const ET answer = const ET(42); } -typedef ETAlias = ET; +typedef ETAlias = ET; typedef ETInt = ET; List foo(List list) => list;