-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add constant evaluation tests for `Set`
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <T>{e1, ..., en}, or <T>{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 <T>{e1, ..., en}`, or `<T>{e1, ..., en}` that occurs in a constant | ||
/// context are constants | ||
/// @author sgrekhov22@gmail.com | ||
import '../../../Utils/expect.dart'; | ||
|
||
main() { | ||
const c1 = <int>{1, 2}; | ||
var c2 = const {1, 2}; | ||
|
||
var c3 = const <num>{}; | ||
const Set<num> c4 = {}; | ||
|
||
const c5 = <Object>{[], {}, ()}; | ||
var c6 = const {[], {}, ()}; | ||
|
||
Expect.identical(c1, c2); | ||
Expect.identical(c3, c4); | ||
Expect.identical(c5, c6); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <T>{e1, ..., en}, or <T>{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 `<T>{e1, ..., en}`, or `<T>{e1, ..., en}`, `T` is | ||
/// not a constant type expression | ||
/// @author sgrekhov22@gmail.com | ||
test<T extends num>() { | ||
const c1 = <T>{}; | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
print(const <T>{}); | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
|
||
main() { | ||
print(test); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <T>{e1, ..., en}, or <T>{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 `<T>{e1, ..., en}`, or `<T>{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<int> get test => const {1, nonconstant1}; | ||
// ^^^^^^^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
class C { | ||
final int nonconstant2 = 1; | ||
|
||
const C(); | ||
|
||
Set<int> get test1 => const {nonconstant2, 2}; | ||
// ^^^^^^^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
Set<int> test2() => const {0, nonconstant2, 2}; | ||
// ^^^^^^^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
|
||
main() { | ||
print(a); | ||
print(test); | ||
print(C); | ||
} |