Skip to main content

luaur_analysis/methods/
unifier_can_cache_result.rs

1use crate::records::skip_cache_for_type::SkipCacheForType;
2use crate::records::unifier::Unifier;
3use crate::type_aliases::type_id::TypeId;
4
5impl Unifier {
6    pub fn unifier_can_cache_result(&mut self, sub_ty: TypeId, super_ty: TypeId) -> bool {
7        let shared_state = unsafe { &*self.shared_state };
8
9        if let Some(super_ty_info) = shared_state.skip_cache_for_type.find(&super_ty) {
10            if *super_ty_info {
11                return false;
12            }
13        }
14
15        if let Some(sub_ty_info) = shared_state.skip_cache_for_type.find(&sub_ty) {
16            if *sub_ty_info {
17                return false;
18            }
19        }
20
21        let mut skip_cache_for = |ty: TypeId| -> bool {
22            let mut visitor = SkipCacheForType::skip_cache_for_type_skip_cache_for_type(
23                &shared_state.skip_cache_for_type,
24                self.types,
25            );
26            // C++ `visitor.traverse(ty)` — dispatch to the per-variant visit
27            // overrides and recurse into composite types, so any nested mutable
28            // element (unsealed/free table, free/bound/generic/blocked pack,
29            // etc.) flips `result` and makes the unification uncacheable.
30            let mut seen_types = std::collections::HashSet::new();
31            let mut seen_packs = std::collections::HashSet::new();
32            visitor.traverse_skip_cache(ty, &mut seen_types, &mut seen_packs);
33
34            let mut_shared_state = unsafe { &mut *self.shared_state };
35            mut_shared_state
36                .skip_cache_for_type
37                .try_insert(ty, visitor.result);
38            visitor.result
39        };
40
41        if shared_state.skip_cache_for_type.find(&super_ty).is_none() && skip_cache_for(super_ty) {
42            return false;
43        }
44
45        if shared_state.skip_cache_for_type.find(&sub_ty).is_none() && skip_cache_for(sub_ty) {
46            return false;
47        }
48
49        true
50    }
51}