Skip to content

Commit c426664

Browse files
authored
#1400. Dynamic semantics inline class tests (#2081)
Dynamic semantics inline class tests
1 parent 7b27534 commit c426664

5 files changed

+249
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2023, 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 At run time, for a given instance o typed as an inline type V,
6+
/// there is no reification of V associated with o.
7+
///
8+
/// @description Check that the run-time type of an instance of an inline class
9+
/// is its representation type
10+
/// @author [email protected]
11+
12+
// SharedOptions=--enable-experiment=inline-class
13+
14+
import "../../Utils/expect.dart";
15+
16+
inline class V1 {
17+
final int id;
18+
V1(this.id);
19+
}
20+
21+
inline class V2<T> {
22+
final T id;
23+
V2(this.id);
24+
}
25+
26+
main() {
27+
var v1 = V1(42);
28+
Expect.equals(int, v1.runtimeType);
29+
30+
int i = v1 as int;
31+
Expect.equals(42, i);
32+
Expect.isTrue(i.isEven);
33+
34+
var v2 = V2<String>("");
35+
Expect.equals(String, v2.runtimeType);
36+
37+
String s = v2 as String;
38+
Expect.equals("", s);
39+
Expect.isTrue(s.isEmpty);
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2023, 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 The run-time representation of a type argument which is an inline
6+
/// type V is the corresponding instantiated representation type.
7+
///
8+
/// @description Check that the run-time representation of a type argument which
9+
/// is an inline type V is the corresponding instantiated representation type.
10+
/// @author [email protected]
11+
12+
// SharedOptions=--enable-experiment=inline-class
13+
14+
import "../../Utils/expect.dart";
15+
16+
inline class V1 {
17+
final int id;
18+
V1(this.id);
19+
}
20+
21+
inline class V2<T> {
22+
final T id;
23+
V2(this.id);
24+
}
25+
26+
main() {
27+
List<V1> l1 = [];
28+
Expect.equals(List<int>, l1.runtimeType);
29+
(l1 as List<int>).add(42);
30+
Expect.throws(() { (l1 as dynamic).add(3.14);});
31+
32+
List<V2<String>> l2 = [];
33+
Expect.equals(List<String>, l2.runtimeType);
34+
(l2 as List<String>).add("42");
35+
Expect.throws(() { (l2 as dynamic).add(42);});
36+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2023, 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 type test, o is U or o is! U, and a type cast, o as U, where U
6+
/// is or contains an inline type, is performed at run time as a type test and
7+
/// type cast on the run-time representation of the inline type
8+
///
9+
/// @description Check that at run time a type test, `o is U` or `o is! U` is
10+
/// performed as a type test on the run-time representation of the inline type
11+
/// @author [email protected]
12+
13+
// SharedOptions=--enable-experiment=inline-class
14+
15+
import "../../Utils/expect.dart";
16+
17+
inline class V1 {
18+
final int id;
19+
V1(this.id);
20+
}
21+
22+
inline class V2<T> {
23+
final T id;
24+
V2(this.id);
25+
}
26+
27+
void main() {
28+
var objects = [
29+
true,
30+
1,
31+
3.14,
32+
"String",
33+
#foo,
34+
<String>[],
35+
{'a': () {}},
36+
{42,},
37+
(),
38+
(42,),
39+
StringBuffer('Hello')
40+
];
41+
42+
for (var o in objects) {
43+
Expect.equals(o is V1, o is int);
44+
Expect.equals(o is! V1, o is! int);
45+
Expect.equals(o is V2<String>, o is String);
46+
Expect.equals(o is! V2<String>, o is! String);
47+
}
48+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) 2023, 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 type test, o is U or o is! U, and a type cast, o as U, where U
6+
/// is or contains an inline type, is performed at run time as a type test and
7+
/// type cast on the run-time representation of the inline type
8+
///
9+
/// @description Check that at run time a type cast, `o as U` is performed as a
10+
/// type cast on the run-time representation of the inline type
11+
/// @author [email protected]
12+
13+
// SharedOptions=--enable-experiment=inline-class
14+
15+
import "../../Utils/expect.dart";
16+
17+
inline class V1 {
18+
final int id;
19+
V1(this.id);
20+
}
21+
22+
inline class V2<T> {
23+
final T id;
24+
V2(this.id);
25+
}
26+
27+
bool testAsInt(Object instance) {
28+
try {
29+
instance as int;
30+
return true;
31+
} catch (_) {
32+
return false;
33+
}
34+
}
35+
36+
bool testAsV1(Object instance) {
37+
try {
38+
instance as V1;
39+
return true;
40+
} catch (_) {
41+
return false;
42+
}
43+
}
44+
45+
bool testAsT<T>(Object instance) {
46+
try {
47+
instance as T;
48+
return true;
49+
} catch (_) {
50+
return false;
51+
}
52+
}
53+
54+
bool testAsV2<T>(Object instance) {
55+
try {
56+
instance as V2<T>;
57+
return true;
58+
} catch (_) {
59+
return false;
60+
}
61+
}
62+
63+
void main() {
64+
int i = 42;
65+
var v1 = V1(i);
66+
var v2 = V2<num>(i);
67+
68+
List<int> l = [];
69+
var v3 = V2<List<int>>(l);
70+
71+
var objects = [
72+
true,
73+
3.14,
74+
i,
75+
v1,
76+
v2,
77+
l,
78+
v3,
79+
"String",
80+
#foo,
81+
<String>[],
82+
{'a': () {}},
83+
{42,},
84+
(),
85+
(42,),
86+
StringBuffer('Hello')
87+
];
88+
89+
for (var o in objects) {
90+
Expect.equals(testAsInt(o), testAsV1(o));
91+
Expect.equals(testAsT<int>(o), testAsV2<int>(o));
92+
Expect.equals(testAsT<List<int>>(o), testAsV2<List<int>>(o));
93+
}
94+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2023, 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 An inline type V used as an expression (a type literal) evaluates
6+
/// to the value of the corresponding instantiated representation type used as
7+
/// an expression.
8+
///
9+
/// @description Check that if an inline type `V` is used as an expression then
10+
/// it evaluates to the value of the corresponding instantiated representation
11+
/// type
12+
/// @author [email protected]
13+
14+
// SharedOptions=--enable-experiment=inline-class
15+
16+
import "../../Utils/expect.dart";
17+
18+
inline class V1 {
19+
final int id;
20+
V1(this.id);
21+
}
22+
23+
inline class V2<T> {
24+
final T Function(int) f;
25+
V2(this.f);
26+
}
27+
28+
main() {
29+
Expect.equals(int, V1);
30+
Expect.equals(V2<bool>, typeOf<bool Function(int)>());
31+
}

0 commit comments

Comments
 (0)