Skip to content

Commit 7b46191

Browse files
leafpetersencommit-bot@chromium.org
authored andcommitted
Remove incorrect super bounded test, and add tests for bounds
violations in extension method applications. Fix #37765 Change-Id: I26d286251b0e832577dc170b9a41594c52e25377 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112149 Commit-Queue: Leaf Petersen <leafp@google.com> Reviewed-by: Erik Ernst <eernst@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent c565fbf commit 7b46191

File tree

2 files changed

+135
-3
lines changed

2 files changed

+135
-3
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
// Tests bounds checking for extension methods
8+
9+
extension E1<T extends num> on T {
10+
int get e1 => 1;
11+
}
12+
13+
extension E2<T extends S, S extends num> on T {
14+
int get e2 => 2;
15+
}
16+
17+
extension E3<T> on T {
18+
S f3<S extends T>(S x) => x;
19+
}
20+
21+
extension E4<T extends Rec<T>> on T {
22+
int get e4 => 4;
23+
}
24+
25+
class Rec<T extends Rec<T>> {}
26+
27+
class RecSolution extends Rec<RecSolution> {}
28+
29+
30+
void main() {
31+
String s = "s";
32+
int i = 0;
33+
double d = 1.0;
34+
35+
// Inferred type of String does not satisfy the bound.
36+
s.e1;
37+
// ^^
38+
// [analyzer] unspecified
39+
// [cfe] unspecified
40+
E1(s).e1;
41+
// ^^
42+
// [analyzer] unspecified
43+
// [cfe] unspecified
44+
E1<String>(s).e1;
45+
// ^^
46+
// [analyzer] unspecified
47+
// [cfe] unspecified
48+
49+
// Inferred types of int and double are ok
50+
i.e1;
51+
E1(i).e1;
52+
E1<int>(i).e1;
53+
d.e1;
54+
E1(d).e1;
55+
E1<double>(d).e1;
56+
57+
// Inferred type of String does not satisfy the bound.
58+
s.e2;
59+
// ^^
60+
// [analyzer] unspecified
61+
// [cfe] unspecified
62+
E2(s).e2;
63+
// ^^
64+
// [analyzer] unspecified
65+
// [cfe] unspecified
66+
E2<String, num>(s).e2;
67+
// ^^
68+
// [analyzer] unspecified
69+
// [cfe] unspecified
70+
71+
// Inferred types of int and double are ok
72+
i.e2;
73+
E2(i).e2;
74+
E2<int, num>(i).e2;
75+
d.e2;
76+
E2(d).e2;
77+
E2<double, num>(d).e2;
78+
79+
// Inferred type int for method type parameter doesn't match the inferred
80+
// bound of String
81+
s.f3(3);
82+
// ^^
83+
// [analyzer] unspecified
84+
// [cfe] unspecified
85+
E3(s).f3(3);
86+
// ^^
87+
// [analyzer] unspecified
88+
// [cfe] unspecified
89+
E3<String>(s).f3(3);
90+
// ^^
91+
// [analyzer] unspecified
92+
// [cfe] unspecified
93+
94+
// Inferred type int for method type parameter is ok
95+
i.f3(3);
96+
E3(i).f3(3);
97+
E3<int>(i).f3(3);
98+
99+
// Inferred type int for method type parameter doesn't match the inferred
100+
// bound of double
101+
d.f3(3);
102+
// ^^
103+
// [analyzer] unspecified
104+
// [cfe] unspecified
105+
E3(d).f3(3);
106+
// ^^
107+
// [analyzer] unspecified
108+
// [cfe] unspecified
109+
E3<double>(d).f3(3);
110+
// ^^
111+
// [analyzer] unspecified
112+
// [cfe] unspecified
113+
114+
RecSolution recs = RecSolution();
115+
Rec<dynamic> superRec = RecSolution(); // Super-bounded type.
116+
117+
// Inferred type of RecSolution is ok
118+
recs.e4;
119+
E4(recs).e4;
120+
E4<RecSolution>(recs).e4;
121+
122+
// Inferred super-bounded type is invalid as type argument
123+
superRec.e4;
124+
// ^^
125+
// [analyzer] unspecified
126+
// [cfe] unspecified
127+
E4(superRec).e4;
128+
// ^^
129+
// [analyzer] unspecified
130+
// [cfe] unspecified
131+
E4<Rec<dynamic>>(superRec).e4;
132+
// ^^
133+
// [analyzer] unspecified
134+
// [cfe] unspecified
135+
}

tests/language_2/extension_methods/static_extension_syntax_test.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ main() {
1818
List<num> numList = <int>[];
1919
Pair<int, double> numPair = Pair(1, 2.5);
2020
RecSolution recs = RecSolution();
21-
Rec<Object> superRec = RecSolution(); // Super-bounded type.
2221

2322
Expect.equals(0, object.e0);
2423
Expect.equals(0, list.e0);
@@ -71,13 +70,11 @@ main() {
7170
checkStaticType<List<num>>(numList.list17);
7271

7372
Expect.equals(0, object.e19);
74-
Expect.equals(0, superRec.e19);
7573
Expect.equals(19, recs.e19);
7674
Expect.type<RecSolution>(recs.list19);
7775
checkStaticType<RecSolution>(recs.list19);
7876

7977
Expect.equals(0, object.e20);
80-
Expect.equals(0, superRec.e20);
8178
Expect.equals(20, recs.e20);
8279
Expect.type<RecSolution>(recs.list20);
8380
checkStaticType<RecSolution>(recs.list20);

0 commit comments

Comments
 (0)