Skip to content

#2485. Add constant evaluation tests for Set #2509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [email protected]

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 [email protected]

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 [email protected]

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);
}