diff --git a/Language/Expressions/Constants/constant_set_t01.dart b/Language/Expressions/Constants/constant_set_t01.dart new file mode 100644 index 0000000000..c79f9b6b5c --- /dev/null +++ b/Language/Expressions/Constants/constant_set_t01.dart @@ -0,0 +1,39 @@ +// 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 potentially constant expression is an expression that will +/// generally yield a constant value when the values of certain parameters are +/// given. The constant expressions is a subset of the potentially constant +/// expressions that can be evaluated at compile time. +/// ... +/// The potentially constant expressions and constant expressions are the +/// following: +/// ... +/// • A constant set literal, const {e1, ..., en}, or {e1, ..., en} that +/// occurs in a constant context, is a potentially constant expression if T is +/// a constant type expression, and e1, . . . , en are constant expressions. +/// It is further a constant expression if the set literal evaluates to an +/// object. +/// +/// @description Checks that a constant set literal of the form +/// `const {e1, ..., en}`, or `{e1, ..., en}` that occurs in a constant +/// context are constants +/// @author sgrekhov22@gmail.com + +import '../../../Utils/expect.dart'; + +main() { + const c1 = {1, 2}; + var c2 = const {1, 2}; + + var c3 = const {}; + const Set c4 = {}; + + const c5 = {[], {}, ()}; + var c6 = const {[], {}, ()}; + + Expect.identical(c1, c2); + Expect.identical(c3, c4); + Expect.identical(c5, c6); +} diff --git a/Language/Expressions/Constants/constant_set_t02.dart b/Language/Expressions/Constants/constant_set_t02.dart new file mode 100644 index 0000000000..648585ed0f --- /dev/null +++ b/Language/Expressions/Constants/constant_set_t02.dart @@ -0,0 +1,37 @@ +// 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 potentially constant expression is an expression that will +/// generally yield a constant value when the values of certain parameters are +/// given. The constant expressions is a subset of the potentially constant +/// expressions that can be evaluated at compile time. +/// ... +/// The potentially constant expressions and constant expressions are the +/// following: +/// ... +/// • A constant set literal, const {e1, ..., en}, or {e1, ..., en} that +/// occurs in a constant context, is a potentially constant expression if T is +/// a constant type expression, and e1, . . . , en are constant expressions. +/// It is further a constant expression if the set literal evaluates to an +/// object. +/// +/// @description Checks that it is a compile-time error if in a constant set +/// literal of the form const `{e1, ..., en}`, or `{e1, ..., en}`, `T` is +/// not a constant type expression +/// @author sgrekhov22@gmail.com + +test() { + const c1 = {}; +// ^ +// [analyzer] unspecified +// [cfe] unspecified + print(const {}); +// ^ +// [analyzer] unspecified +// [cfe] unspecified +} + +main() { + print(test); +} diff --git a/Language/Expressions/Constants/constant_set_t03.dart b/Language/Expressions/Constants/constant_set_t03.dart new file mode 100644 index 0000000000..59b2e1ada2 --- /dev/null +++ b/Language/Expressions/Constants/constant_set_t03.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 potentially constant expression is an expression that will +/// generally yield a constant value when the values of certain parameters are +/// given. The constant expressions is a subset of the potentially constant +/// expressions that can be evaluated at compile time. +/// ... +/// The potentially constant expressions and constant expressions are the +/// following: +/// ... +/// • A constant set literal, const {e1, ..., en}, or {e1, ..., en} that +/// occurs in a constant context, is a potentially constant expression if T is +/// a constant type expression, and e1, . . . , en are constant expressions. +/// It is further a constant expression if the set literal evaluates to an +/// object. +/// +/// @description Checks that it is a compile-time error if in a constant set +/// literal of the form const `{e1, ..., en}`, or `{e1, ..., en}`, any of +/// `ei` is not a constant expression +/// @author sgrekhov22@gmail.com + +final nonconstant1 = 1; + +const a = {nonconstant1}; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + +Set get test => const {1, nonconstant1}; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + +class C { + final int nonconstant2 = 1; + + const C(); + + Set get test1 => const {nonconstant2, 2}; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified + + Set test2() => const {0, nonconstant2, 2}; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +main() { + print(a); + print(test); + print(C); +}