Skip to content

Commit 2e0bec3

Browse files
eernstgcommit-bot@chromium.org
authored andcommitted
Add tests static_extension_constant_...
Testing that it is a compile-time error to invoke an extension method during constant expression evaluation. Change-Id: I52404d87c7da977451411f6cfc10c7059864fba4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/123580 Commit-Queue: Erik Ernst <[email protected]> Reviewed-by: Leaf Petersen <[email protected]>
1 parent 28eb884 commit 2e0bec3

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright (c) 2019, 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+
// SharedOptions=--enable-experiment=extension-methods
6+
7+
import 'static_extension_constant_lib.dart';
8+
9+
// Tests that it is an error to invoke an extension method during constant
10+
// expression evaluation. The expressions should be the same as those in
11+
// `runtimeExtensionCalls`, so that it is verified that each of them will
12+
// invoke an extension method.
13+
14+
void main() {
15+
// The initializing expressions should be identical to the elements in
16+
// `runtimeExtensionCalls`.
17+
18+
const c01 = ~i;
19+
// ^^
20+
// [analyzer] unspecified
21+
// [cfe] unspecified
22+
const c02 = b & b;
23+
// ^^^^^
24+
// [analyzer] unspecified
25+
// [cfe] unspecified
26+
const c03 = b | b;
27+
// ^^^^^
28+
// [analyzer] unspecified
29+
// [cfe] unspecified
30+
const c04 = b ^ b;
31+
// ^^^^^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
const c05 = i ~/ i;
35+
// ^^^^^^
36+
// [analyzer] unspecified
37+
// [cfe] unspecified
38+
const c06 = i >> i;
39+
// ^^^^^^
40+
// [analyzer] unspecified
41+
// [cfe] unspecified
42+
const c08 = i << i;
43+
// ^^^^^^
44+
// [analyzer] unspecified
45+
// [cfe] unspecified
46+
const c09 = i + i;
47+
// ^^^^^
48+
// [analyzer] unspecified
49+
// [cfe] unspecified
50+
const c10 = -i;
51+
// ^^
52+
// [analyzer] unspecified
53+
// [cfe] unspecified
54+
const c11 = d - d;
55+
// ^^^^^
56+
// [analyzer] unspecified
57+
// [cfe] unspecified
58+
const c12 = d * d;
59+
// ^^^^^
60+
// [analyzer] unspecified
61+
// [cfe] unspecified
62+
const c13 = d / d;
63+
// ^^^^^
64+
// [analyzer] unspecified
65+
// [cfe] unspecified
66+
const c14 = d % d;
67+
// ^^^^^
68+
// [analyzer] unspecified
69+
// [cfe] unspecified
70+
const c15 = d < i;
71+
// ^^^^^
72+
// [analyzer] unspecified
73+
// [cfe] unspecified
74+
const c16 = i <= d;
75+
// ^^^^^^
76+
// [analyzer] unspecified
77+
// [cfe] unspecified
78+
const c17 = d > i;
79+
// ^^^^^
80+
// [analyzer] unspecified
81+
// [cfe] unspecified
82+
const c18 = i >= i;
83+
// ^^^^^^
84+
// [analyzer] unspecified
85+
// [cfe] unspecified
86+
const c19 = s.length;
87+
// ^^^^^^^^
88+
// [analyzer] unspecified
89+
// [cfe] unspecified
90+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2019, 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+
extension ExtendObject on Object {
6+
int operator ~() => 1;
7+
bool operator &(Object other) => false;
8+
bool operator |(Object other) => false;
9+
bool operator ^(Object other) => true;
10+
int operator ~/(Object other) => 0;
11+
int operator >>(Object other) => 1;
12+
// int operator >>>(Object other) => 2; // Requires triple-shift.
13+
int operator <<(Object other) => 0;
14+
int operator +(Object other) => 0;
15+
double operator -() => 1.0;
16+
double operator -(Object other) => 1.0;
17+
double operator *(Object other) => 1.0;
18+
double operator /(Object other) => 2.0;
19+
double operator %(Object other) => 1.0;
20+
bool operator <(Object other) => false;
21+
bool operator <=(Object other) => true;
22+
bool operator >(Object other) => true;
23+
bool operator >=(Object other) => false;
24+
int get length => 1;
25+
}
26+
27+
const Object b = true;
28+
const Object i = 3;
29+
const Object d = 2.4;
30+
const Object s = 'Hello!';
31+
32+
// These expressions should be identical to the ones in
33+
// static_extension_constant_{,error}_test.dart, to ensure that
34+
// they invoke an extension method, and that this is an error.
35+
var runtimeExtensionCalls = <Object>[
36+
~i,
37+
b & b,
38+
b | b,
39+
b ^ b,
40+
i ~/ i,
41+
i >> i,
42+
// i >>> i, // Requries triple-shift.
43+
i << i,
44+
i + i,
45+
-i,
46+
d - d,
47+
d * d,
48+
d / d,
49+
d % d,
50+
d < i,
51+
i <= d,
52+
d > i,
53+
i >= i,
54+
s.length,
55+
];
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2019, 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+
// SharedOptions=--enable-experiment=extension-methods
6+
7+
import 'package:expect/expect.dart';
8+
import 'static_extension_constant_lib.dart' hide b, i, d, s;
9+
import 'static_extension_constant_lib.dart' as lib show b, i, d, s;
10+
11+
// Ensure that all expressions in runtimeExtensionCalls invoke
12+
// an extension method rather than an instance method, such that
13+
// static_extension_constant_error_test gets an error for them all.
14+
15+
const dynamic b = lib.b;
16+
const dynamic i = lib.i;
17+
const dynamic d = lib.d;
18+
const dynamic s = lib.s;
19+
20+
// These expressions should be identical to those in
21+
// `lib.runtimeExtensionCalls`.
22+
var dynamicInstanceCalls = <Object>[
23+
~i,
24+
b & b,
25+
b | b,
26+
b ^ b,
27+
i ~/ i,
28+
i >> i,
29+
// i >>> i, // Requries triple-shift.
30+
i << i,
31+
i + i,
32+
-i,
33+
d - d,
34+
d * d,
35+
d / d,
36+
d % d,
37+
d < i,
38+
i <= d,
39+
d > i,
40+
i >= i,
41+
s.length,
42+
];
43+
44+
void main() {
45+
for (int i = 0; i < dynamicInstanceCalls.length; ++i) {
46+
Expect.notEquals(dynamicInstanceCalls[i], runtimeExtensionCalls[i]);
47+
}
48+
}

0 commit comments

Comments
 (0)