forked from dart-lang/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regress_37254_test.dart
258 lines (207 loc) · 6.45 KB
/
regress_37254_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
// This program tests interaction with generic Pointers.
//
// Notation used in following tables:
// * static_type//dynamic_type
// * P = Pointer
// * I = Int8
// * NT = NativeType
//
// Note #1: When NNBD is landed, implicit downcasts will be static errors.
//
// Note #2: When we switch to extension methods we will _only_ use the static
// type of the container.
//
// ===== a.value = b ======
// Does a.value = b, where a and b have specific static and dynamic types: run
// fine, fail at compile time, or fail at runtime?
// =======================
// b P<I>//P<I> P<NT>//P<I> P<NT>//P<NT>
// a
// P<P<I>>//P<P<I>> 1 ok 2 implicit downcast 3 implicit downcast
// of argument: of argument:
// static error static error
//
// P<P<NT>>//P<P<I>> 4 ok 5 ok 6 fail at runtime
//
// P<P<NT>>//P<P<NT>> 7 ok 8 ok 9 ok
//
// ====== final c = a.value ======
// What is the (inferred) static type and runtime type of `a.value`. Note that
// we assume extension method here: on Pointer<PointerT>> { Pointer<T> load(); }
// ================================
// a a.value
// inferred static type*//runtime type
// P<P<I>>//P<P<I>> P<I>//P<I>
//
// P<P<NT>>//P<P<I>> P<NT>//P<I>
//
// P<P<NT>>//P<P<NT>> P<NT>//P<NT>
//
// * The inferred static type when we get extension methods.
//
// ====== b = a.value ======
// What happens when we try to assign the result of a.value to variable b with
// a specific static type: runs fine, fails at compile time, or fails at runtime.
// ==========================
// b P<I> P<NT>
// a
// P<P<I>>//P<P<I>> 1 ok 2 ok
//
// P<P<NT>>//P<P<I>> 3 implicit downcast 4 ok
// of returnvalue: ok
//
// P<P<NT>>//P<P<NT>> 5 implicit downcast 6 ok
// of returnvalue: fail
// at runtime
//
// These are the normal Dart assignment rules.
import 'dart:ffi';
import "package:expect/expect.dart";
import "package:ffi/ffi.dart";
// ===== a.value = b ======
// The tests follow table cells left to right, top to bottom.
void store1() {
final Pointer<Pointer<Int8>> a = calloc<Pointer<Int8>>();
final Pointer<Int8> b = calloc<Int8>();
a.value = b;
calloc.free(a);
calloc.free(b);
}
void store2() {
final Pointer<Pointer<Int8>> a = calloc<Pointer<Int8>>();
final Pointer<NativeType> b =
calloc<Int8>(); // Reified Pointer<Int8> at runtime.
// Successful implicit downcast of argument at runtime.
// Should succeed now, should statically be rejected when NNBD lands.
a.value = b;
calloc.free(a);
calloc.free(b);
}
void store3() {
final Pointer<Pointer<Int8>> a = calloc<Pointer<Int8>>();
final Pointer<NativeType> b = calloc<Int8>().cast<Pointer<NativeType>>();
// Failing implicit downcast of argument at runtime.
// Should fail now at runtime, should statically be rejected when NNBD lands.
Expect.throws(() {
a.value = b;
});
calloc.free(a);
calloc.free(b);
}
void store4() {
// Reified as Pointer<Pointer<Int8>> at runtime.
final Pointer<Pointer<NativeType>> a = calloc<Pointer<Int8>>();
final Pointer<Int8> b = calloc<Int8>();
a.value = b;
calloc.free(a);
calloc.free(b);
}
void store5() {
// Reified as Pointer<Pointer<Int8>> at runtime.
final Pointer<Pointer<NativeType>> a = calloc<Pointer<Int8>>();
final Pointer<NativeType> b =
calloc<Int8>(); // Reified as Pointer<Int8> at runtime.
a.value = b;
calloc.free(a);
calloc.free(b);
}
void store6() {
// Reified as Pointer<Pointer<Int8>> at runtime.
final Pointer<Pointer<NativeType>> a = calloc<Pointer<Int8>>();
final Pointer<NativeType> b = calloc<Int8>().cast<Pointer<NativeType>>();
// Fails on type check of argument.
Expect.throws(() {
a.value = b;
});
calloc.free(a);
calloc.free(b);
}
void store7() {
final Pointer<Pointer<NativeType>> a = calloc<Pointer<NativeType>>();
final Pointer<Int8> b = calloc<Int8>();
a.value = b;
calloc.free(a);
calloc.free(b);
}
void store8() {
final Pointer<Pointer<NativeType>> a = calloc<Pointer<NativeType>>();
// Reified as Pointer<Int8> at runtime.
final Pointer<NativeType> b = calloc<Int8>();
a.value = b;
calloc.free(a);
calloc.free(b);
}
void store9() {
final Pointer<Pointer<NativeType>> a = calloc<Pointer<NativeType>>();
final Pointer<NativeType> b = calloc<Int8>().cast<Pointer<NativeType>>();
a.value = b;
calloc.free(a);
calloc.free(b);
}
// ====== b = a.value ======
// The tests follow table cells left to right, top to bottom.
void load1() {
final Pointer<Pointer<Int8>> a = calloc<Pointer<Int8>>();
Pointer<Int8> b = a.value;
Expect.type<Pointer<Int8>>(b);
calloc.free(a);
}
void load2() {
final Pointer<Pointer<Int8>> a = calloc<Pointer<Int8>>();
Pointer<NativeType> b = a.value;
Expect.type<Pointer<Int8>>(b);
calloc.free(a);
}
void load3() {
// Reified as Pointer<Pointer<Int8>> at runtime.
final Pointer<Pointer<NativeType>> a = calloc<Pointer<Int8>>();
Pointer<Int8> b = a.value;
Expect.type<Pointer<Int8>>(b);
calloc.free(a);
}
void load4() {
// Reified as Pointer<Pointer<Int8>> at runtime.
final Pointer<Pointer<NativeType>> a = calloc<Pointer<Int8>>();
// Return value runtime type is Pointer<Int8>.
Pointer<NativeType> b = a.value;
Expect.type<Pointer<Int8>>(b);
calloc.free(a);
}
void load5() {
final Pointer<Pointer<NativeType>> a = calloc<Pointer<NativeType>>();
// Failing implicit downcast of return value at runtime.
// Should fail now at runtime, should statically be rejected when NNBD lands.
Expect.throws(() {
Pointer<Int8> b = a.value;
});
calloc.free(a);
}
void load6() {
final Pointer<Pointer<NativeType>> a = calloc<Pointer<NativeType>>();
Pointer<NativeType> b = a.value;
Expect.type<Pointer<NativeType>>(b);
calloc.free(a);
}
void main() {
for (int i = 0; i < 100; i++) {
store1();
store2();
store3();
store4();
store5();
store6();
store7();
store8();
store9();
load1();
load2();
load3();
load4();
load5();
load6();
}
}