-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathbase_adaptive_full.nit
360 lines (331 loc) · 5.75 KB
/
base_adaptive_full.nit
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# This file is part of NIT ( http://www.nitlanguage.org ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# A comprehensive test to check most cast of adaptive typing with merge, union and intersections.
#
# alt1 is for necessary static errors
# alt2 is for errors due to the lack of union&intersection types
import core::kernel
# Base diamond hierarchy to have types
class A
end
class B
super A
end
class C
super A
end
class D
super B
super C
end
class N
end
# Base methods to statically check the adapted types.
fun test_a(x:A) do end
fun test_b(x:B) do end
fun test_nb(x:nullable B) do end
fun test_c(x:C) do end
fun test_nn(x:nullable N) do end
fun test_o(x:Object) do end
## Intersections and substractions
# Intersection B and C (from B)
fun inter1(x: B) do
if x isa C then
test_a(x)
#alt2#test_b(x)
test_c(x)
else
test_a(x)
test_b(x)
#alt1#test_c(x)
end
test_a(x)
test_b(x)
#alt1#test_c(x)
end
# Intersection B and C (from Object)
fun inter2(x: nullable Object) do
if x isa B and x isa C then
test_a(x)
#alt2#test_b(x)
test_c(x)
end
end
# Intersection B and then C (from Object)
fun inter3(x: nullable Object) do
if x isa B then
test_a(x)
test_b(x)
#alt1#test_c(x)
if x isa C then
test_a(x)
#alt2#test_b(x)
test_c(x)
else
test_a(x)
test_b(x)
#alt1#test_c(x)
end
#alt2#test_a(x)
# The previous one need an explanation: merge(inter(B,C),sub(B,C)) = merge(C,B) = null (because conflict).
# Unfortunately the fallback is on Object (instead of B) because the B information in the then branch
# is lost and for the typer, `x isa C` is indistinguishable with `x=new C`.
#alt2#test_b(x)
#alt1#test_c(x)
test_o(x)
else
#alt1#test_a(x)
#alt1#test_b(x)
#alt1#test_c(x)
if x isa C then
test_a(x)
#alt1#test_b(x)
test_c(x)
else
#alt1#test_a(x)
#alt1#test_b(x)
#alt1#test_c(x)
end
#alt1#test_a(x)
#alt1#test_b(x)
#alt1#test_c(x)
end
#alt1#test_a(x)
#alt1#test_b(x)
#alt1#test_c(x)
end
# Intersection nB and notnull (from nb)
fun null_inter1n(x: nullable B) do
if x != null then
test_b(x)
else
test_nn(x)
end
test_nb(x)
#alt1#test_b(x)
#alt1#test_nn(x)
end
# Intersection nB and notnull (from nObject)
fun null_inter2n(b: nullable B) do
var x
x = b
if x != null then
test_b(x)
else
test_nb(x)
test_nn(x)
end
test_nb(x)
#alt1#test_b(x)
#alt1#test_nn(x)
end
# Intersection nB and Object (from nb)
fun null_inter1o(x: nullable B) do
if x isa A then
test_b(x)
else
test_nb(x)
test_nn(x)
end
test_nb(x)
#alt1#test_b(x)
#alt1#test_nn(x)
end
# Intersection nB and Object (from nObject)
fun null_inter2o(b: nullable B) do
var x
x = b
if x isa A then
test_b(x)
else
test_nb(x)
test_nn(x)
end
test_nb(x)
#alt1#test_b(x)
#alt1#test_nn(x)
end
# Intersection Object and nB
fun null_inter3(x: A) do
if x isa nullable B then
test_b(x)
else
test_a(x)
end
test_a(x)
end
## Unions
# Union between B and C (with assigment)
fun union1(b:B, c:C, m: Bool) do
var x
x = b
if m then x = c
#alt2#test_a(x)
#alt1#test_b(x)
#alt1#test_c(x)
test_o(x)
end
# Union between B and C (with isa or isa)
fun union2(x: nullable Object) do
if x isa B or x isa C then
#alt2#test_a(x)
#alt1#test_b(x)
#alt1#test_c(x)
test_o(x)
end
end
# Union between B and null (from nullable Object)
fun null_union(b: B) do
var x
x = b
test_nb(x)
test_b(x)
if true then x = null
test_nb(x)
#alt1#test_b(x)
end
## Loops
# Type adaptation in a loop
fun loop1(b:B, c:C, m: Bool) do
var x
x = b
while m do
#alt2#test_a(x)
#alt1#test_b(x)
#alt1#test_c(x)
test_o(x)
x = c
test_a(x)
#alt1#test_b(x)
test_c(x)
end
#alt2#test_a(x)
#alt1#test_b(x)
#alt1#test_c(x)
test_o(x)
end
# Union in a loop
fun loop_union(b:B, c:C, m: Bool) do
var x
x = b
while m do
test_o(x)
#alt2#test_a(x)
#alt1#test_b(x)
#alt1#test_c(x)
if m then x = c
#alt2#test_a(x)
#alt1#test_b(x)
#alt1#test_c(x)
test_o(x)
end
#alt2#test_a(x)
#alt1#test_b(x)
#alt1#test_c(x)
test_o(x)
end
# Loop while not null
fun null_loop_1(b: nullable B) do
var x
x = b
while x != null do
test_b(x)
test_nb(x)
if true then x = null
#alt1#test_b(x)
test_nb(x)
end
#alt1#test_b(x)
test_nb(x)
end
# Loop while null
fun null_loop_2(b: nullable B) do
var x
x = b
while x == null do
#alt1#test_b(x)
test_nb(x)
if true then x = b
#alt1#test_b(x)
test_nb(x)
end
test_b(x)
test_nb(x)
end
# Loop while isa (from B)
fun loop_inter1a(b: B) do
var x = b
while x isa C do
test_a(x)
#alt2#test_b(x)
test_c(x)
x = b
test_a(x)
test_b(x)
#alt1#test_c(x)
end
test_a(x)
test_b(x)
#alt1#test_c(x)
end
# Loop while not isa (from B)
fun loop_inter2a(b: B) do
var x = b
while not x isa C do
test_a(x)
test_b(x)
#alt1#test_c(x)
x = b
test_a(x)
test_b(x)
#alt1#test_c(x)
end
test_a(x)
#alt2#test_b(x)
test_c(x)
end
# loop while isa (from Object)
fun loop_inter1b(b: B) do
var x
x = b
while x isa C do
test_a(x)
#alt2#test_b(x)
test_c(x)
x = b
test_a(x)
test_b(x)
#alt1#test_c(x)
end
test_a(x)
test_b(x)
#alt1#test_c(x)
end
# loop while not isa (from Object)
fun loop_inter2b(b: B) do
var x
x = b
while not x isa C do
test_a(x)
test_b(x)
#alt1#test_c(x)
x = b
test_a(x)
test_b(x)
#alt1#test_c(x)
end
test_a(x)
#alt2#test_b(x)
test_c(x)
end