Skip to content

Commit

Permalink
#2485. Add constant evaluation tests for Set (#2509)
Browse files Browse the repository at this point in the history
Add constant evaluation tests for `Set`
  • Loading branch information
sgrekhov authored Jan 26, 2024
1 parent c941786 commit af2ac96
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Language/Expressions/Constants/constant_set_t01.dart
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);
}
37 changes: 37 additions & 0 deletions Language/Expressions/Constants/constant_set_t02.dart
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);
}
56 changes: 56 additions & 0 deletions Language/Expressions/Constants/constant_set_t03.dart
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);
}

0 comments on commit af2ac96

Please sign in to comment.