Skip to main content

luaur_analysis/methods/
normalizer_normalize_intersections.rs

1use crate::enums::normalization_result::NormalizationResult;
2use crate::records::normalized_extern_type::NormalizedExternType;
3use crate::records::normalized_function_type::NormalizedFunctionType;
4use crate::records::normalized_string_type::NormalizedStringType;
5use crate::records::normalized_type::NormalizedType;
6use crate::records::normalizer::Normalizer;
7use crate::records::type_ids::TypeIds;
8use crate::type_aliases::seen_table_prop_pairs::SeenTablePropPairs;
9use crate::type_aliases::type_id::TypeId;
10use alloc::collections::BTreeMap;
11use luaur_common::records::dense_hash_set::DenseHashSet;
12
13impl Normalizer {
14    pub fn normalize_intersections(
15        &mut self,
16        intersections: &alloc::vec::Vec<TypeId>,
17        out_type: &mut NormalizedType,
18        seen_table_prop_pairs: &mut SeenTablePropPairs,
19        seen_set: &mut DenseHashSet<TypeId>,
20    ) -> NormalizationResult {
21        if self.arena.is_null() {
22            panic!("Normalizing types outside a module");
23        }
24
25        self.consume_fuel();
26
27        let never_type = unsafe { (*self.builtin_types).neverType };
28        let mut norm = NormalizedType {
29            builtin_types: self.builtin_types,
30            tops: unsafe { (*self.builtin_types).unknownType },
31            booleans: never_type,
32            extern_types: NormalizedExternType {
33                extern_types: BTreeMap::new(),
34                shape_extensions: TypeIds::type_ids(),
35                ordering: Vec::new(),
36            },
37            errors: never_type,
38            nils: never_type,
39            numbers: never_type,
40            integers: never_type,
41            strings: NormalizedStringType::never,
42            threads: never_type,
43            buffers: never_type,
44            tables: TypeIds::type_ids(),
45            functions: NormalizedFunctionType {
46                is_top: false,
47                parts: TypeIds::type_ids(),
48            },
49            tyvars: BTreeMap::new(),
50            is_cacheable: true,
51        };
52
53        for &ty in intersections {
54            let res = self.intersect_normal_with_ty(&mut norm, ty, seen_table_prop_pairs, seen_set);
55            if res != NormalizationResult::True {
56                return res;
57            }
58        }
59
60        let res = self.union_normals(out_type, &norm, -1);
61        if res != NormalizationResult::True {
62            return res;
63        }
64
65        NormalizationResult::True
66    }
67}