fix: union/intersection types bugs

This commit is contained in:
Shunsuke Shibayama 2023-03-20 14:48:34 +09:00
parent 98a1a0292e
commit 40762bf8cf
5 changed files with 36 additions and 2 deletions

View file

@ -589,6 +589,12 @@ impl Context {
}
// Int or Str :> Str or Int == (Int :> Str && Str :> Int) || (Int :> Int && Str :> Str) == true
(Or(l_1, l_2), Or(r_1, r_2)) => {
if l_1.is_union_type() && self.supertype_of(l_1, rhs) {
return true;
}
if l_2.is_union_type() && self.supertype_of(l_2, rhs) {
return true;
}
(self.supertype_of(l_1, r_1) && self.supertype_of(l_2, r_2))
|| (self.supertype_of(l_1, r_2) && self.supertype_of(l_2, r_1))
}
@ -600,6 +606,12 @@ impl Context {
// Int :> (Nat or Str) == Int :> Nat && Int :> Str == false
(lhs, Or(l_or, r_or)) => self.supertype_of(lhs, l_or) && self.supertype_of(lhs, r_or),
(And(l_1, l_2), And(r_1, r_2)) => {
if l_1.is_intersection_type() && self.supertype_of(l_1, rhs) {
return true;
}
if l_2.is_intersection_type() && self.supertype_of(l_2, rhs) {
return true;
}
(self.supertype_of(l_1, r_1) && self.supertype_of(l_2, r_2))
|| (self.supertype_of(l_1, r_2) && self.supertype_of(l_2, r_1))
}

View file

@ -95,4 +95,10 @@ impl Context {
assert!(self.subtype_of(&Nat, &poly("Sub", vec![ty_tp(Nat)])));
Ok(())
}
pub fn test_intersection(&self) -> Result<(), ()> {
assert!(self.subtype_of(&Code, &(Int | Str | Code | NoneType)));
assert!(self.subtype_of(&(Int | Str), &(Int | Str | Code | NoneType)));
Ok(())
}
}