Skip to main content

luaur_analysis/methods/
constraint_solver_constraint_solver_constraint_solver_alt_b.rs

1use crate::functions::borrow_constraints::borrow_constraints;
2use crate::records::constraint::Constraint;
3use crate::records::constraint_graph::ConstraintGraph;
4use crate::records::constraint_set::ConstraintSet;
5use crate::records::constraint_solver::ConstraintSolver;
6use crate::records::data_flow_graph::DataFlowGraph;
7use crate::records::dcr_logger::DcrLogger;
8use crate::records::module_resolver::ModuleResolver;
9use crate::records::normalizer::Normalizer;
10use crate::records::require_cycle::RequireCycle;
11use crate::records::scope::Scope;
12use crate::records::subtyping::Subtyping;
13use crate::records::type_check_limits::TypeCheckLimits;
14use crate::records::type_function_runtime::TypeFunctionRuntime;
15use crate::type_aliases::module_ptr_module::ModulePtr;
16use alloc::vec::Vec;
17use core::ptr::NonNull;
18use luaur_common::records::dense_hash_map::DenseHashMap;
19use luaur_common::FInt;
20
21impl ConstraintSolver {
22    pub fn constraint_solver_not_null_normalizer_not_null_type_function_runtime_not_null_scope_vector_not_null_constraint_not_null_dense_hash_map_scope_type_id_module_ptr_not_null_module_resolver_vector_require_cycle_dcr_logger_not_null_data_flow_graph_type_check_limits_constraint_graph_not_null_subtyping(
23        normalizer: *const Normalizer,
24        type_function_runtime: *const TypeFunctionRuntime,
25        root_scope: NonNull<Scope>,
26        constraints: alloc::vec::Vec<NonNull<Constraint>>,
27        scope_to_function: NonNull<DenseHashMap<*mut Scope, crate::type_aliases::type_id::TypeId>>,
28        module: ModulePtr,
29        module_resolver: *const ModuleResolver,
30        require_cycles: alloc::vec::Vec<RequireCycle>,
31        logger: *mut DcrLogger,
32        dfg: *const DataFlowGraph,
33        limits: TypeCheckLimits,
34        cgraph: *mut ConstraintGraph,
35        subtyping: *const Subtyping,
36    ) -> Self {
37        // C++ `constraintSet{rootScope}` aggregate-initializes only `rootScope`;
38        // every other field is default-constructed.
39        let mut constraint_set = ConstraintSet {
40            root_scope: root_scope.as_ptr(),
41            constraints: Vec::new(),
42            free_types: crate::records::type_ids::TypeIds::type_ids(),
43            scope_to_function: DenseHashMap::new(core::ptr::null_mut()),
44            errors: Vec::new(),
45        };
46        constraint_set.constraints = constraints
47            .iter()
48            .map(|c| unsafe { c.as_ptr() })
49            .collect::<Vec<_>>();
50        constraint_set.root_scope = root_scope.as_ptr();
51
52        let mut result = ConstraintSolver::constraint_solver_not_null_normalizer_not_null_type_function_runtime_module_ptr_not_null_module_resolver_vector_require_cycle_dcr_logger_not_null_data_flow_graph_type_check_limits_constraint_graph_not_null_subtyping(
53            normalizer,
54            type_function_runtime,
55            module,
56            module_resolver,
57            require_cycles,
58            logger,
59            dfg,
60            limits.clone(),
61            constraint_set,
62            cgraph,
63            subtyping,
64        );
65
66        // The delegated base constructor already populated `result.constraints`
67        // (from `constraint_set.constraints`, which holds the same pointers as the
68        // `constraints` argument) and already ran `init_free_type_tracking()`.
69        // Re-running it here would double-insert every constraint and trip the
70        // `LUAU_ASSERT(fresh1)` in `deprecated_constraint_to_mutated_types`. We only
71        // need to point the solver at the fragment generator's `scopeToFunction` and
72        // `rootScope` (C++ `NotNull{&cg.scopeToFunction}`, `NotNull(cg.rootScope)`).
73        result.scope_to_function = scope_to_function.as_ptr();
74        result.root_scope = root_scope.as_ptr();
75        result.solver_constraint_limit = FInt::LuauSolverConstraintLimit.get() as usize;
76        result.module_resolver = module_resolver as *mut ModuleResolver;
77        result.logger = logger;
78        result.limits = limits;
79        result.cgraph = cgraph;
80        result.subtyping = subtyping as *mut Subtyping;
81
82        result
83    }
84}