Skip to content

Commit

Permalink
dart-lang#2485. Update map and list constant literals tests. Add pare…
Browse files Browse the repository at this point in the history
…nthesized for records
  • Loading branch information
sgrekhov committed Jan 30, 2024
1 parent a38a448 commit ecd6ff2
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 44 deletions.
28 changes: 21 additions & 7 deletions Language/Expressions/Constants/constant_list_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@
/// change, and that can be evaluated entirely at compile time.
/// A constant expression is one of the following:
/// . . .
/// • A constant list literal.
/// @description Checks that a constant list literal can be an element of
/// a constant list literal and is, therefore, a constant expression.
/// • A constant list 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 list literal evaluates to an
/// object.
///
/// @description Checks that a constant list literal of the form
/// `const <T>[e1, ..., en]`, or `<T>[e1, ..., en]` that occurs in a constant
/// context are constants
/// @author iefremov
import '../../../Utils/expect.dart';

final constList = const [const ["hello", "world"]];

main() {
Expect.isTrue(constList is List);
Expect.runtimeIsType<List>(constList);
const c1 = <int>[1, 2];
var c2 = const [1, 2];

var c3 = const <num>[];
const List<num> c4 = [];

const c5 = <Object>[[], {}, ()];
var c6 = const [[], {}, ()];

Expect.identical(c1, c2);
Expect.identical(c3, c4);
Expect.identical(c5, c6);
}
29 changes: 18 additions & 11 deletions Language/Expressions/Constants/constant_list_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@
/// change, and that can be evaluated entirely at compile time.
/// A constant expression is one of the following:
/// . . .
/// • A constant list literal.
/// @description Checks that a non-constant list literal cannot be assigned to
/// a constant variable.
/// • A constant list 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 list literal evaluates to an
/// object.
///
/// @description Checks that it is a compile-time error if in a constant list
/// literal of the form const `<T>[e1, ..., en]`, or `<T>[e1, ..., en]`, `T` is
/// not a constant type expression
/// @author iefremov
/// @reviewer rodionov

var nonconstant = [1, 2, 3];
const a = nonconstant;
// ^
test<T extends num>() {
const c1 = <T>[];
// ^
// [analyzer] unspecified
// [cfe] unspecified
print(const <T>[]);
// ^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
try {
print(a);
} catch (x) {}
print(test);
}
7 changes: 6 additions & 1 deletion Language/Expressions/Constants/constant_list_t03.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
/// change, and that can be evaluated entirely at compile time.
/// A constant expression is one of the following:
/// . . .
/// • A constant list literal.
/// • A constant list 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 list literal evaluates to an
/// object.
///
/// @description Checks that a non-constant variable cannot be used in a
/// constant list literal.
/// @author sgrekhov22@gmail.com
Expand Down
29 changes: 21 additions & 8 deletions Language/Expressions/Constants/constant_map_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,30 @@
/// @assertion A constant expression is an expression whose value can never
/// change, and that can be evaluated entirely at compile time.
/// A constant expression is one of the following:
/// . . .
/// • A constant map literal.
/// @description Checks that a constant map literal can be an element of
/// a constant list literal and is, therefore, a constant expression.
/// ...
/// A constant map literal, const <K, V >{k1: v1, ..., kn: vn}, or
/// <K, V >{k1: v1, ..., kn: vn} that occurs in a constant context, is a
/// potentially constant expression. It is further a constant expression if the
/// map literal evaluates to an object.
///
/// @description Checks that a constant map literal of the form
/// `const <K, V >{k1: v1, ..., kn: vn}`, or `<K, V >{k1: v1, ..., kn: vn}` that
/// occurs in a constant context are constants
/// @author iefremov
import '../../../Utils/expect.dart';

final constMap = const {"a" : 1, "b" : 2};

main() {
Expect.isTrue(constMap is Map);
Expect.runtimeIsType<Map>(constMap);
const c1 = <String, int>{"one": 1, "two": 2};
var c2 = const {"one": 1, "two": 2};

var c3 = const <num, Object>{};
const Map<num, Object> c4 = {};

const c5 = <Object, Object>{[]: [], {}: {}, (): ()};
var c6 = const {[]: [], {}: {}, (): ()};

Expect.identical(c1, c2);
Expect.identical(c3, c4);
Expect.identical(c5, c6);
}
30 changes: 19 additions & 11 deletions Language/Expressions/Constants/constant_map_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@
/// @assertion A constant expression is an expression whose value can never
/// change, and that can be evaluated entirely at compile time.
/// A constant expression is one of the following:
/// . . .
/// • A constant map literal.
/// @description Checks that a non-constant map literal cannot be assigned to
/// a constant variable.
/// ...
/// A constant map literal, const <K, V >{k1: v1, ..., kn: vn}, or
/// <K, V >{k1: v1, ..., kn: vn} that occurs in a constant context, is a
/// potentially constant expression. It is further a constant expression if the
/// map literal evaluates to an object.
///
/// @description Checks that it is a compile-time error if in a constant map
/// literal of the form `const <K, V >{k1: v1, ..., kn: vn`, or
/// `<K, V >{k1: v1, ..., kn: vn}`, `K` or `V` are not a constant type
/// expressions
/// @author iefremov

var b = {"a" : "b"};
const a = b;
// ^
test<T extends num>() {
const c1 = <T, String>{};
// ^
// [analyzer] unspecified
// [cfe] unspecified
print(const <String, T>{});
// ^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
try {
print(a);
} catch (x) {}
print(test);
}
8 changes: 6 additions & 2 deletions Language/Expressions/Constants/constant_map_t03.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
/// @assertion A constant expression is an expression whose value can never
/// change, and that can be evaluated entirely at compile time.
/// A constant expression is one of the following:
/// . . .
/// • A constant map literal.
/// ...
/// A constant map literal, const <K, V >{k1: v1, ..., kn: vn}, or
/// <K, V >{k1: v1, ..., kn: vn} that occurs in a constant context, is a
/// potentially constant expression. It is further a constant expression if the
/// map literal evaluates to an object.
///
/// @description Checks that a non-constant map literal cannot be assigned to
/// a constant variable.
/// @author sgrekhov22@gmail.com
Expand Down
2 changes: 1 addition & 1 deletion Language/Expressions/Constants/constant_set_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/// 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
/// literal of the form `const <T>{e1, ..., en}`, or `<T>{e1, ..., en}`, `T` is
/// not a constant type expression
/// @author sgrekhov22@gmail.com
Expand Down
4 changes: 2 additions & 2 deletions Language/Expressions/Constants/constant_set_t03.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/// 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
/// literal of the form `const <T>{e1, ..., en}`, or `<T>{e1, ..., en}`, any of
/// `ei` is not a constant expression
/// @author sgrekhov22@gmail.com
Expand All @@ -29,7 +29,7 @@ const a = {nonconstant1};
// [cfe] unspecified

Set<int> get test => const {1, nonconstant1};
// ^^^^^^^^^^^^
// ^^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ const constList = const [
(const {}),
(1 == 1),
(identical('', null)),
(identical((), ())),
(true != false),
(!true),
(false && false),
(1 ^ 2),
(~0),
(2 + 0.5),
(1 * 1)
(1 * 1),
(()),
((1,)),
((x: 2)),
((1 < 2), (2 > 3)),
(((0)))
];

main() {
Expand Down

0 comments on commit ecd6ff2

Please sign in to comment.