Skip to main content

luaur_analysis/functions/
maybe_singleton.rs

1use crate::functions::follow_type::follow_type_id;
2use crate::functions::get_type_alt_j::get_type_id;
3use crate::records::intersection_type::IntersectionType;
4use crate::records::singleton_type::SingletonType;
5use crate::records::type_function_instance_type::TypeFunctionInstanceType;
6use crate::records::union_type::UnionType;
7use crate::type_aliases::type_id::TypeId;
8
9pub fn maybe_singleton(ty: TypeId) -> bool {
10    unsafe {
11        let ty = follow_type_id(ty);
12        if !get_type_id::<SingletonType>(ty).is_null() {
13            return true;
14        }
15        let utv = get_type_id::<UnionType>(ty);
16        if !utv.is_null() {
17            for &option in &(*utv).options {
18                if !get_type_id::<SingletonType>(follow_type_id(option)).is_null() {
19                    return true;
20                }
21            }
22        }
23        let itv = get_type_id::<IntersectionType>(ty);
24        if !itv.is_null() {
25            for &part in &(*itv).parts {
26                if maybe_singleton(part) {
27                    // will i regret this?
28                    return true;
29                }
30            }
31        }
32        let tfit = get_type_id::<TypeFunctionInstanceType>(ty);
33        if !tfit.is_null() {
34            let name = &(*(*tfit).function.as_ptr()).name;
35            if name == "keyof" || name == "rawkeyof" {
36                return true;
37            }
38        }
39        false
40    }
41}