Skip to main content

luaur_analysis/methods/
unifier_2_unify_unifier_2_alt_f.rs

1use crate::enums::unify_result::UnifyResult;
2use crate::functions::follow_type::follow_type_id;
3use crate::records::unifier_2::Unifier2;
4use crate::records::union_type::UnionType;
5use crate::type_aliases::type_id::TypeId;
6
7impl Unifier2 {
8    pub fn unify_type_id_union_type(
9        &mut self,
10        sub_ty: TypeId,
11        super_union: &UnionType,
12    ) -> UnifyResult {
13        let sub_ty = unsafe { follow_type_id(sub_ty) };
14
15        // T <: T | U1 | U2 | ... | Un is trivially true, so we don't gain any information by unifying
16        for super_option in super_union.options.iter() {
17            let followed_super_option = unsafe { follow_type_id(*super_option) };
18            if sub_ty == followed_super_option {
19                return UnifyResult::Ok;
20            }
21        }
22
23        let mut result = UnifyResult::Ok;
24
25        // if the occurs check fails for any option, it fails overall
26        for super_option in super_union.options.iter() {
27            if unsafe { crate::functions::are_compatible::are_compatible(sub_ty, *super_option) } {
28                result &= self.unify_type_id_type_id(sub_ty, *super_option);
29            }
30        }
31
32        result
33    }
34}