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
+ }
0 commit comments