Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / base_adaptive_full.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # A comprehensive test to check most cast of adaptive typing with merge, union and intersections.
16 #
17 # alt1 is for necessary static errors
18 # alt2 is for errors due to the lack of union&intersection types
19
20 import core::kernel
21
22 # Base diamond hierarchy to have types
23
24 class A
25 end
26 class B
27 super A
28 end
29 class C
30 super A
31 end
32 class D
33 super B
34 super C
35 end
36
37 class N
38 end
39
40 # Base methods to statically check the adapted types.
41
42 fun test_a(x:A) do end
43 fun test_b(x:B) do end
44 fun test_nb(x:nullable B) do end
45 fun test_c(x:C) do end
46 fun test_nn(x:nullable N) do end
47 fun test_o(x:Object) do end
48
49 ## Intersections and substractions
50
51 # Intersection B and C (from B)
52 fun inter1(x: B) do
53 if x isa C then
54 test_a(x)
55 #alt2#test_b(x)
56 test_c(x)
57 else
58 test_a(x)
59 test_b(x)
60 #alt1#test_c(x)
61 end
62 test_a(x)
63 test_b(x)
64 #alt1#test_c(x)
65 end
66
67 # Intersection B and C (from Object)
68 fun inter2(x: nullable Object) do
69 if x isa B and x isa C then
70 test_a(x)
71 #alt2#test_b(x)
72 test_c(x)
73 end
74 end
75
76 # Intersection B and then C (from Object)
77 fun inter3(x: nullable Object) do
78 if x isa B then
79 test_a(x)
80 test_b(x)
81 #alt1#test_c(x)
82 if x isa C then
83 test_a(x)
84 #alt2#test_b(x)
85 test_c(x)
86 else
87 test_a(x)
88 test_b(x)
89 #alt1#test_c(x)
90 end
91 #alt2#test_a(x)
92 # The previous one need an explanation: merge(inter(B,C),sub(B,C)) = merge(C,B) = null (because conflict).
93 # Unfortunately the fallback is on Object (instead of B) because the B information in the then branch
94 # is lost and for the typer, `x isa C` is indistinguishable with `x=new C`.
95 #alt2#test_b(x)
96 #alt1#test_c(x)
97 test_o(x)
98 else
99 #alt1#test_a(x)
100 #alt1#test_b(x)
101 #alt1#test_c(x)
102 if x isa C then
103 test_a(x)
104 #alt1#test_b(x)
105 test_c(x)
106 else
107 #alt1#test_a(x)
108 #alt1#test_b(x)
109 #alt1#test_c(x)
110 end
111 #alt1#test_a(x)
112 #alt1#test_b(x)
113 #alt1#test_c(x)
114 end
115 #alt1#test_a(x)
116 #alt1#test_b(x)
117 #alt1#test_c(x)
118 end
119
120 # Intersection nB and notnull (from nb)
121 fun null_inter1n(x: nullable B) do
122 if x != null then
123 test_b(x)
124 else
125 test_nn(x)
126 end
127 test_nb(x)
128 #alt1#test_b(x)
129 #alt1#test_nn(x)
130 end
131
132 # Intersection nB and notnull (from nObject)
133 fun null_inter2n(b: nullable B) do
134 var x
135 x = b
136 if x != null then
137 test_b(x)
138 else
139 test_nb(x)
140 test_nn(x)
141 end
142 test_nb(x)
143 #alt1#test_b(x)
144 #alt1#test_nn(x)
145 end
146
147 # Intersection nB and Object (from nb)
148 fun null_inter1o(x: nullable B) do
149 if x isa A then
150 test_b(x)
151 else
152 test_nb(x)
153 test_nn(x)
154 end
155 test_nb(x)
156 #alt1#test_b(x)
157 #alt1#test_nn(x)
158 end
159
160 # Intersection nB and Object (from nObject)
161 fun null_inter2o(b: nullable B) do
162 var x
163 x = b
164 if x isa A then
165 test_b(x)
166 else
167 test_nb(x)
168 test_nn(x)
169 end
170 test_nb(x)
171 #alt1#test_b(x)
172 #alt1#test_nn(x)
173 end
174
175 # Intersection Object and nB
176 fun null_inter3(x: A) do
177 if x isa nullable B then
178 test_b(x)
179 else
180 test_a(x)
181 end
182 test_a(x)
183 end
184
185 ## Unions
186
187 # Union between B and C (with assigment)
188 fun union1(b:B, c:C, m: Bool) do
189 var x
190 x = b
191 if m then x = c
192 #alt2#test_a(x)
193 #alt1#test_b(x)
194 #alt1#test_c(x)
195 test_o(x)
196 end
197
198 # Union between B and C (with isa or isa)
199 fun union2(x: nullable Object) do
200 if x isa B or x isa C then
201 #alt2#test_a(x)
202 #alt1#test_b(x)
203 #alt1#test_c(x)
204 test_o(x)
205 end
206 end
207
208 # Union between B and null (from nullable Object)
209 fun null_union(b: B) do
210 var x
211 x = b
212 test_nb(x)
213 test_b(x)
214 if true then x = null
215 test_nb(x)
216 #alt1#test_b(x)
217 end
218
219 ## Loops
220
221 # Type adaptation in a loop
222 fun loop1(b:B, c:C, m: Bool) do
223 var x
224 x = b
225 while m do
226 #alt2#test_a(x)
227 #alt1#test_b(x)
228 #alt1#test_c(x)
229 test_o(x)
230 x = c
231 test_a(x)
232 #alt1#test_b(x)
233 test_c(x)
234 end
235 #alt2#test_a(x)
236 #alt1#test_b(x)
237 #alt1#test_c(x)
238 test_o(x)
239 end
240
241 # Union in a loop
242 fun loop_union(b:B, c:C, m: Bool) do
243 var x
244 x = b
245 while m do
246 test_o(x)
247 #alt2#test_a(x)
248 #alt1#test_b(x)
249 #alt1#test_c(x)
250 if m then x = c
251 #alt2#test_a(x)
252 #alt1#test_b(x)
253 #alt1#test_c(x)
254 test_o(x)
255 end
256 #alt2#test_a(x)
257 #alt1#test_b(x)
258 #alt1#test_c(x)
259 test_o(x)
260 end
261
262 # Loop while not null
263 fun null_loop_1(b: nullable B) do
264 var x
265 x = b
266 while x != null do
267 test_b(x)
268 test_nb(x)
269 if true then x = null
270 #alt1#test_b(x)
271 test_nb(x)
272 end
273 #alt1#test_b(x)
274 test_nb(x)
275 end
276
277 # Loop while null
278 fun null_loop_2(b: nullable B) do
279 var x
280 x = b
281 while x == null do
282 #alt1#test_b(x)
283 test_nb(x)
284 if true then x = b
285 #alt1#test_b(x)
286 test_nb(x)
287 end
288 test_b(x)
289 test_nb(x)
290 end
291
292 # Loop while isa (from B)
293 fun loop_inter1a(b: B) do
294 var x = b
295 while x isa C do
296 test_a(x)
297 #alt2#test_b(x)
298 test_c(x)
299 x = b
300 test_a(x)
301 test_b(x)
302 #alt1#test_c(x)
303 end
304 test_a(x)
305 test_b(x)
306 #alt1#test_c(x)
307 end
308
309 # Loop while not isa (from B)
310 fun loop_inter2a(b: B) do
311 var x = b
312 while not x isa C do
313 test_a(x)
314 test_b(x)
315 #alt1#test_c(x)
316 x = b
317 test_a(x)
318 test_b(x)
319 #alt1#test_c(x)
320 end
321 test_a(x)
322 #alt2#test_b(x)
323 test_c(x)
324 end
325
326 # loop while isa (from Object)
327 fun loop_inter1b(b: B) do
328 var x
329 x = b
330 while x isa C do
331 test_a(x)
332 #alt2#test_b(x)
333 test_c(x)
334 x = b
335 test_a(x)
336 test_b(x)
337 #alt1#test_c(x)
338 end
339 test_a(x)
340 test_b(x)
341 #alt1#test_c(x)
342 end
343
344 # loop while not isa (from Object)
345 fun loop_inter2b(b: B) do
346 var x
347 x = b
348 while not x isa C do
349 test_a(x)
350 test_b(x)
351 #alt1#test_c(x)
352 x = b
353 test_a(x)
354 test_b(x)
355 #alt1#test_c(x)
356 end
357 test_a(x)
358 #alt2#test_b(x)
359 test_c(x)
360 end