Skip to main content

luaur_analysis/methods/
unifier_try_unify_singletons.rs

1use crate::enums::variance::Variance;
2use crate::functions::get_singleton_type::get_singleton_type;
3use crate::functions::get_type_alt_j::get_type_id;
4use crate::records::boolean_singleton::BooleanSingleton;
5use crate::records::primitive_type::PrimitiveType;
6use crate::records::singleton_type::SingletonType;
7use crate::records::string_singleton::StringSingleton;
8use crate::records::type_mismatch::TypeMismatch;
9use crate::records::unifier::Unifier;
10use crate::type_aliases::type_error_data::TypeErrorData;
11use crate::type_aliases::type_id::TypeId;
12use alloc::string::String;
13
14impl Unifier {
15    pub fn unifier_try_unify_singletons(&mut self, sub_ty: TypeId, super_ty: TypeId) {
16        let super_prim = unsafe { get_type_id::<PrimitiveType>(super_ty) };
17        let super_singleton = unsafe { get_type_id::<SingletonType>(super_ty) };
18        let sub_singleton = unsafe { get_type_id::<SingletonType>(sub_ty) };
19
20        if (super_prim.is_null() && super_singleton.is_null()) || sub_singleton.is_null() {
21            self.ice_string("passed non singleton/primitive types to unifySingletons");
22            return;
23        }
24
25        if !super_singleton.is_null() && unsafe { *super_singleton == *sub_singleton } {
26            return;
27        }
28
29        if !super_prim.is_null()
30            && unsafe { (*super_prim).r#type == PrimitiveType::Boolean }
31            && !get_singleton_type::<BooleanSingleton>(sub_singleton).is_null()
32            && self.variance == Variance::Covariant
33        {
34            return;
35        }
36
37        if !super_prim.is_null()
38            && unsafe { (*super_prim).r#type == PrimitiveType::String }
39            && !get_singleton_type::<StringSingleton>(sub_singleton).is_null()
40            && self.variance == Variance::Covariant
41        {
42            return;
43        }
44
45        let context = self.unifier_mismatch_context();
46        self.report_error_location_type_error_data(
47            self.location,
48            TypeErrorData::TypeMismatch(TypeMismatch {
49                wanted_type: super_ty,
50                given_type: sub_ty,
51                reason: String::new(),
52                error: None,
53                context,
54            }),
55        );
56    }
57}