var s2 = ctx2.stype(v)
if s1 == s and s2 == s then
# NOP
- else if s1 == s2 then
- stype(v) = s1
- else if s2 == null or s1 < s2 then
- stype(v) = s2
- else if s1 == null or s2 < s1 then
- stype(v) = s1
+ else if s1 == null or s2 == null then
+ stype(v) = null
else
- stype(v) = basectx.stype(v)
+ var sm = merge_types(s1, s2)
+ if sm == null then
+ stype(v) = basectx.stype(v)
+ else
+ stype(v) = sm
+ end
end
end
end
+ # Combine and get the most specific comon supertype
+ # return null if no comon supertype is found
+ private fun merge_types(t1, t2: MMType): nullable MMType
+ do
+ if t1 == t2 then return t1
+ if t1 isa MMTypeNone then return t2.as_nullable
+ if t2 isa MMTypeNone then return t1.as_nullable
+ var is_nullable = false
+ if t1.is_nullable then
+ is_nullable = true
+ t1 = t1.as_notnull
+ end
+ if t2.is_nullable then
+ is_nullable = true
+ t2 = t2.as_notnull
+ end
+ var t: MMType
+ if t1 < t2 then
+ t = t2
+ else if t2 < t1 then
+ t = t1
+ else
+ return null
+ end
+ if is_nullable then t = t.as_nullable
+ return t
+ end
+
redef fun to_s
do
var s = new Buffer
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2010 Jean Privat <jean@pryen.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.
+
+import kernel
+
+class A
+ fun a do 'A'.output
+end
+
+class B
+special A
+ fun b do 'B'.output
+end
+
+class C
+special A
+ fun c do 'C'.output
+end
+
+fun rand: Bool do return true
+
+var a = new A
+if rand then
+ a = new B
+end
+a.a
+#alt1#a.b
+'\n'.output
+
+a = new B
+if rand then
+ a = new B
+ #alt2#a = new C
+ #alt3#a = new A
+end
+a.a
+a.b
+'\n'.output
+
+a = new A
+if rand then
+ a = new B
+ #alt4#a = new C
+else
+ a = new B
+ #alt5#a = new C
+end
+a.a
+a.b
+'\n'.output
+
+a = new A
+if rand then
+ a = new B
+else
+ a = new C
+ abort#!alt6#
+end
+a.a
+a.b
+#alt7#a.c
+'\n'.output
+
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2010 Jean Privat <jean@pryen.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.
+
+import kernel
+
+class A
+end
+
+class B
+special A
+end
+
+fun rand: Bool do return true
+
+fun eat_na(a: nullable A) do 'a'.output
+fun eat_a(a: A) do 'A'.output
+fun eat_nb(b: nullable B) do 'b'.output
+fun eat_b(b: B) do 'B'.output
+
+var a: nullable Object = null
+a = new B
+if rand then
+ a = null #!alt1#
+ #alt2#a = new A
+ #alt3#a = new B
+end
+eat_na(a)
+#alt2#eat_a(a)
+eat_nb(a)
+#alt1#eat_b(a)
+#alt3#eat_b(a)
+#alt4#eat_b(a)
+'\n'.output
+
+# a is 'nullable B' here
+if rand then
+ a = new A
+end
+eat_na(a)
+#alt5#eat_a(a)
+#alt6#eat_nb(a)
+'\n'.output
+# a is 'nullable A' here
+
+if rand then
+ a = new B
+else
+ a = new B
+ #alt7#a = new A
+end
+eat_b(a)
+'\n'.output
+
--- /dev/null
+A
+AB
+AB
+AB
--- /dev/null
+alt/base_var_type_evolution_alt1.nit:40,1--3: Error: Method 'b' doesn't exists in A.
--- /dev/null
+alt/base_var_type_evolution_alt2.nit:50,1--3: Error: Method 'b' doesn't exists in A.
--- /dev/null
+alt/base_var_type_evolution_alt3.nit:50,1--3: Error: Method 'b' doesn't exists in A.
--- /dev/null
+alt/base_var_type_evolution_alt4.nit:62,1--3: Error: Method 'b' doesn't exists in A.
--- /dev/null
+alt/base_var_type_evolution_alt5.nit:62,1--3: Error: Method 'b' doesn't exists in A.
--- /dev/null
+alt/base_var_type_evolution_alt6.nit:72,1--3: Error: Method 'b' doesn't exists in A.
--- /dev/null
+alt/base_var_type_evolution_alt7.nit:74,1--3: Error: Method 'c' doesn't exists in B.
--- /dev/null
+alt/base_var_type_evolution_null_alt2.nit:42,8: Type error: expected nullable B, got A
--- /dev/null
+alt/base_var_type_evolution_null_alt4.nit:45,7: Type error: expected B, got nullable B
--- /dev/null
+alt/base_var_type_evolution_null_alt5.nit:53,7: Type error: expected A, got nullable A
--- /dev/null
+alt/base_var_type_evolution_null_alt6.nit:54,8: Type error: expected nullable B, got nullable A
--- /dev/null
+alt/base_var_type_evolution_null_alt7.nit:64,7: Type error: expected B, got A