Skip to content

Commit ae38f3e

Browse files
authored
#2485. Add is T constant evaluation tests (#2501)
Add `is T` constant evaluation tests
1 parent fee3d8a commit ae38f3e

10 files changed

+450
-7
lines changed

Language/Expressions/Constants/as_type_A01_t02.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ typedef IntET1Alias = IntET1;
5252
typedef IntET2Alias = IntET2;
5353

5454
void foo() {}
55-
55+
void as() {}
5656
int bar<T>(T t) => 42;
5757

5858
main() {
@@ -73,7 +73,8 @@ main() {
7373
const fo7 = IntET2(1) as FutureOr<IntET1Alias>;
7474

7575
const f1 = foo as void Function();
76-
const f2 = bar<int> as int Function<int>(int);
76+
const f2 = as as void Function();
77+
const f3 = bar<int> as int Function(int);
7778

7879
const d = 2 as dynamic;
7980

@@ -86,6 +87,7 @@ main() {
8687
Expect.identical(fo4, fo5);
8788
Expect.identical(fo6, fo7);
8889
Expect.identical(foo, f1);
89-
Expect.identical(bar<int>, f2);
90+
Expect.identical(as, f2);
91+
Expect.identical(bar<int>, f3);
9092
Expect.identical(2, d);
9193
}

Language/Expressions/Constants/as_type_A02_t01.dart

+4
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,9 @@ main() {
106106
const c19 = c as Object;
107107
// ^
108108
// [analyzer] unspecified
109+
// [cfe] unspecified
110+
const c20 = () {} as Function;
111+
// ^^^^^
112+
// [analyzer] unspecified
109113
// [cfe] unspecified
110114
}

Language/Expressions/Constants/as_type_A02_t02.dart

-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ main() {
2828
const cIntExtension = IntExtension as Type;
2929
// ^^^^^^^^^^^^
3030
// [analyzer] unspecified
31-
// [cfe] unspecified
32-
const F = () {} as Function;
33-
// ^^^^^
34-
// [analyzer] unspecified
3531
// [cfe] unspecified
3632
const R1 = (void,) as Type;
3733
// ^^^^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion A constant expression is an expression whose value can never
6+
/// change, and that can be evaluated entirely at compile time.
7+
/// A constant expression is one of the following:
8+
/// ...
9+
/// • An expression of the form e is T is potentially constant if e is a
10+
/// potentially constant expression and T is a constant type expression, and
11+
/// it is further constant if e is constant.
12+
///
13+
/// @description Checks that an expression of the form `e is T` is a constant
14+
/// expression if `e` is a constant expression and `T` is a constant type
15+
/// expression
16+
/// @author [email protected]
17+
/// @issue 54620
18+
19+
// SharedOptions=--enable-experiment=inline-class
20+
21+
import "../../../Utils/expect.dart";
22+
23+
class C {
24+
const C();
25+
}
26+
27+
extension type const IntET1(int _) {}
28+
extension type const IntET2(int _) implements int {}
29+
30+
main() {
31+
const c1 = 1 is num;
32+
const c2 = (3.14 as num) is double;
33+
const c3 = IntET1(1) is int;
34+
const c4 = IntET2(1) is int;
35+
const c5 = (IntET1(1),) is (int,);
36+
const c6 = (IntET2(1),) is (int,);
37+
const c7 = [IntET1(1)] is List<int>;
38+
const c8 = [IntET2(1)] is List<num>;
39+
const c9 = {IntET1(1)} is Set<num>;
40+
const c10 = {IntET2(1)} is Set<num>;
41+
const c11 = {IntET1(1): IntET1(2)} is Map<num, int>;
42+
const c12 = {IntET1(1): IntET2(2)} is Map<num, int>;
43+
const c13 = {IntET2(1): IntET1(2)} is Map<num, int>;
44+
const c14 = {IntET2(1): IntET2(2)} is Map<num, int>;
45+
const c15 = 1 is IntET1;
46+
const c16 = 1 is IntET2;
47+
const c17 = IntET1(1) is IntET2;
48+
const c18 = IntET2(1) is IntET1;
49+
const c19 = C() is Object;
50+
const c20 = const C() is Object;
51+
const c21 = () is ();
52+
const c22 = (i: 1) is ({num i});
53+
54+
Expect.isTrue(c1);
55+
Expect.isTrue(c2);
56+
Expect.isTrue(c3);
57+
Expect.isTrue(c4);
58+
Expect.isTrue(c5);
59+
Expect.isTrue(c6);
60+
Expect.isTrue(c7);
61+
Expect.isTrue(c8);
62+
Expect.isTrue(c9);
63+
Expect.isTrue(c10);
64+
Expect.isTrue(c11);
65+
Expect.isTrue(c12);
66+
Expect.isTrue(c13);
67+
Expect.isTrue(c14);
68+
Expect.isTrue(c15);
69+
Expect.isTrue(c16);
70+
Expect.isTrue(c17);
71+
Expect.isTrue(c18);
72+
Expect.isTrue(c19);
73+
Expect.isTrue(c20);
74+
Expect.isTrue(c21);
75+
Expect.isTrue(c22);
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion A constant expression is an expression whose value can never
6+
/// change, and that can be evaluated entirely at compile time.
7+
/// A constant expression is one of the following:
8+
/// ...
9+
/// • An expression of the form e is T is potentially constant if e is a
10+
/// potentially constant expression and T is a constant type expression, and
11+
/// it is further constant if e is constant.
12+
/// ...
13+
/// A constant type expression is one of:
14+
/// • An simple or qualified identifier denoting a type declaration (a type
15+
/// alias, class or mixin declaration) that is not qualified by a deferred
16+
/// prefix, optionally followed by type arguments of the form <T1, ..., Tn>
17+
/// where T1, ..., Tn are constant type expressions.
18+
/// • A type of the form FutureOr<T> where T is a constant type expression.
19+
/// • A function type R Function<typeParameters>(argumentTypes) (where
20+
/// R and <typeParameters> may be omitted) and where R, typeParameters
21+
/// and argumentTypes (if present) contain only constant type expressions.
22+
/// • The type void.
23+
/// • The type dynamic
24+
///
25+
/// @description Checks that an expression of the form `e is T` is a constant
26+
/// expression if `e` is a constant expression and `T` is a constant type
27+
/// expression. Test different constant type expressions
28+
/// @author [email protected]
29+
/// @issue 54636
30+
31+
// SharedOptions=--enable-experiment=inline-class
32+
33+
import "dart:async";
34+
import "../../../Utils/expect.dart";
35+
36+
mixin M {}
37+
38+
class C<T> with M {
39+
const C();
40+
}
41+
42+
typedef CNumAlias = C<num>;
43+
44+
enum E {
45+
e1, e2;
46+
}
47+
48+
extension type const IntET1(int _) {}
49+
extension type const IntET2(int _) implements int {}
50+
51+
typedef IntET1Alias = IntET1;
52+
typedef IntET2Alias = IntET2;
53+
54+
void foo() {}
55+
void as() {}
56+
int bar<T>(T t) => 42;
57+
58+
main() {
59+
const c1 = C() is C;
60+
const c2 = C() is M;
61+
const c3 = E.e1 is E;
62+
const c4 = C<int>() is C<num>;
63+
const c5 = C<int>() is CNumAlias;
64+
const c6 = IntET1(1) is IntET2Alias;
65+
const c7 = IntET2(1) is IntET1Alias;
66+
67+
const fo1 = C() is FutureOr<C>;
68+
const fo2 = C() is FutureOr<M>;
69+
const fo3 = E.e1 is FutureOr<E>;
70+
const fo4 = C<int>() is FutureOr<C<num>>;
71+
const fo5 = C<int>() is FutureOr<CNumAlias>;
72+
const fo6 = IntET1(1) is FutureOr<IntET2Alias>;
73+
const fo7 = IntET2(1) is FutureOr<IntET1Alias>;
74+
75+
const f1 = foo is void Function();
76+
const f2 = bar<int> is int Function<int>(int);
77+
78+
const d = 2 is dynamic;
79+
80+
Expect.isTrue(c1);
81+
Expect.isTrue(c2);
82+
Expect.isTrue(c3);
83+
Expect.isTrue(c4);
84+
Expect.isTrue(c5);
85+
Expect.isTrue(c6);
86+
Expect.isTrue(c7);
87+
Expect.isTrue(fo1);
88+
Expect.isTrue(fo2);
89+
Expect.isTrue(fo3);
90+
Expect.isTrue(fo4);
91+
Expect.isTrue(fo5);
92+
Expect.isTrue(fo6);
93+
Expect.isTrue(fo7);
94+
Expect.isTrue(f1);
95+
Expect.isTrue(f2);
96+
Expect.isTrue(d);
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion A constant expression is an expression whose value can never
6+
/// change, and that can be evaluated entirely at compile time.
7+
/// A constant expression is one of the following:
8+
/// ...
9+
/// • An expression of the form e is T is potentially constant if e is a
10+
/// potentially constant expression and T is a constant type expression, and
11+
/// it is further constant if e is constant.
12+
/// ...
13+
/// A constant type expression is one of:
14+
/// • An simple or qualified identifier denoting a type declaration (a type
15+
/// alias, class or mixin declaration) that is not qualified by a deferred
16+
/// prefix, optionally followed by type arguments of the form <T1, ..., Tn>
17+
/// where T1, ..., Tn are constant type expressions.
18+
/// • A type of the form FutureOr<T> where T is a constant type expression.
19+
/// • A function type R Function<typeParameters>(argumentTypes) (where
20+
/// R and <typeParameters> may be omitted) and where R, typeParameters
21+
/// and argumentTypes (if present) contain only constant type expressions.
22+
/// • The type void.
23+
/// • The type dynamic
24+
///
25+
/// @description Checks that an expression of the form `e is T` is a constant
26+
/// expression if `T` is a constant type expression of type `FutureOr<void>` or
27+
/// an extension type with a representation type `void`
28+
/// @author [email protected]
29+
/// @issue 54620
30+
31+
// SharedOptions=--enable-experiment=inline-class
32+
33+
import "dart:async";
34+
import "../../../Utils/expect.dart";
35+
36+
extension type const VoidET(void _) {}
37+
38+
main() {
39+
const c1 = 1 is VoidET;
40+
const c2 = 2 is FutureOr<void>;
41+
const c3 = (3,) is (VoidET,);
42+
const c4 = (4,) is (FutureOr<void>,);
43+
const c5 = (i: 5) is ({VoidET i});
44+
const c6 = (i: 6) is ({FutureOr<void> i});
45+
46+
Expect.isTrue(c1);
47+
Expect.isTrue(c2);
48+
Expect.isTrue(c3);
49+
Expect.isTrue(c4);
50+
Expect.isTrue(c5);
51+
Expect.isTrue(c6);
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion A constant expression is an expression whose value can never
6+
/// change, and that can be evaluated entirely at compile time.
7+
/// A constant expression is one of the following:
8+
/// ...
9+
/// • An expression of the form e is T is potentially constant if e is a
10+
/// potentially constant expression and T is a constant type expression, and
11+
/// it is further constant if e is constant.
12+
///
13+
/// @description Checks that an expression of the form `e is T` is not a
14+
/// constant expression if `e` is not a constant expression
15+
/// @author [email protected]
16+
17+
// SharedOptions=--enable-experiment=inline-class
18+
19+
class C {
20+
C();
21+
}
22+
23+
extension type IntET1(int _) {}
24+
extension type IntET2(int _) implements int {}
25+
26+
test<T>() {
27+
const c = T is Type;
28+
// ^
29+
// [analyzer] unspecified
30+
// [cfe] unspecified
31+
}
32+
33+
main() {
34+
int i = 1;
35+
var d = 3.14;
36+
var c = C();
37+
38+
const c1 = i is num;
39+
// ^
40+
// [analyzer] unspecified
41+
// [cfe] unspecified
42+
const c2 = (d as num) is double;
43+
// ^
44+
// [analyzer] unspecified
45+
// [cfe] unspecified
46+
const c3 = IntET1(1) is int;
47+
// ^^^^^^^^^
48+
// [analyzer] unspecified
49+
// [cfe] unspecified
50+
const c4 = IntET2(1) is int;
51+
// ^^^^^^^^^
52+
// [analyzer] unspecified
53+
// [cfe] unspecified
54+
const c5 = (i,) is (int,);
55+
// ^
56+
// [analyzer] unspecified
57+
// [cfe] unspecified
58+
const c6 = (IntET1(1),) is (int,);
59+
// ^^^^^^^^^
60+
// [analyzer] unspecified
61+
// [cfe] unspecified
62+
const c7 = (IntET2(1),) is (int,);
63+
// ^^^^^^^^^
64+
// [analyzer] unspecified
65+
// [cfe] unspecified
66+
const c8 = [i] is List<int>;
67+
// ^
68+
// [analyzer] unspecified
69+
// [cfe] unspecified
70+
const c9 = [IntET1(1)] is List<int>;
71+
// ^^^^^^^^^
72+
// [analyzer] unspecified
73+
// [cfe] unspecified
74+
const c10 = [IntET2(1)] is List<num>;
75+
// ^^^^^^^^^
76+
// [analyzer] unspecified
77+
// [cfe] unspecified
78+
const c11 = {d} is Set<num>;
79+
// ^
80+
// [analyzer] unspecified
81+
// [cfe] unspecified
82+
const c12 = {IntET1(1)} is Set<num>;
83+
// ^^^^^^^^^
84+
// [analyzer] unspecified
85+
// [cfe] unspecified
86+
const c13 = {IntET2(1)} is Set<num>;
87+
// ^^^^^^^^^
88+
// [analyzer] unspecified
89+
// [cfe] unspecified
90+
const c14 = {IntET1(1): IntET1(2)} is Map<num, int>;
91+
// ^^^^^^^^^
92+
// [analyzer] unspecified
93+
// [cfe] unspecified
94+
const c15 = {IntET2(1): IntET2(2)} is Map<num, int>;
95+
// ^^^^^^^^^
96+
// [analyzer] unspecified
97+
// [cfe] unspecified
98+
const c16 = IntET1(1) is IntET2;
99+
// ^^^^^^^^^
100+
// [analyzer] unspecified
101+
// [cfe] unspecified
102+
const c17 = IntET2(1) is IntET1;
103+
// ^^^^^^^^^
104+
// [analyzer] unspecified
105+
// [cfe] unspecified
106+
const c19 = c is Object;
107+
// ^
108+
// [analyzer] unspecified
109+
// [cfe] unspecified
110+
}

0 commit comments

Comments
 (0)