1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # A comprehensive test to check most cast of adaptive typing with merge, union and intersections.
17 # alt1 is for necessary static errors
18 # alt2 is for errors due to the lack of union&intersection types
22 # Base diamond hierarchy to have types
40 # Base methods to statically check the adapted types.
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
49 ## Intersections and substractions
51 # Intersection B and C (from B)
67 # Intersection B and C (from Object)
68 fun inter2
(x
: nullable Object) do
69 if x
isa B
and x
isa C
then
76 # Intersection B and then C (from Object)
77 fun inter3
(x
: nullable Object) do
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`.
120 # Intersection nB and notnull (from nb)
121 fun null_inter1n
(x
: nullable B
) do
132 # Intersection nB and notnull (from nObject)
133 fun null_inter2n
(b
: nullable B
) do
147 # Intersection nB and Object (from nb)
148 fun null_inter1o
(x
: nullable B
) do
160 # Intersection nB and Object (from nObject)
161 fun null_inter2o
(b
: nullable B
) do
175 # Intersection Object and nB
176 fun null_inter3
(x
: A
) do
177 if x
isa nullable B
then
187 # Union between B and C (with assigment)
188 fun union1
(b
:B
, c
:C
, m
: Bool) do
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
208 # Union between B and null (from nullable Object)
209 fun null_union
(b
: B
) do
214 if true then x
= null
221 # Type adaptation in a loop
222 fun loop1
(b
:B
, c
:C
, m
: Bool) do
242 fun loop_union
(b
:B
, c
:C
, m
: Bool) do
262 # Loop while not null
263 fun null_loop_1
(b
: nullable B
) do
269 if true then x
= null
278 fun null_loop_2
(b
: nullable B
) do
292 # Loop while isa (from B)
293 fun loop_inter1a
(b
: B
) do
309 # Loop while not isa (from B)
310 fun loop_inter2a
(b
: B
) do
326 # loop while isa (from Object)
327 fun loop_inter1b
(b
: B
) do
344 # loop while not isa (from Object)
345 fun loop_inter2b
(b
: B
) do