1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//! `void ConstraintSolver::run()` (`Analysis/src/ConstraintSolver.cpp:506-765`,
//! the main solver loop, hand-ported faithfully). The C++ `runSolverPass` lambda
//! is lowered to the private `run_solver_pass` method below.
use alloc::string::String;
use core::ptr::NonNull;
use crate::functions::dump_bindings::dump_bindings;
use crate::functions::dump_constraint_solver::dump;
use crate::functions::follow_type::follow_type_id;
use crate::functions::to_string_to_string_alt_c::to_string_type_id;
use crate::functions::to_string_to_string_alt_q::to_string_constraint_to_string_options;
use crate::records::constraint::Constraint;
use crate::records::constraint_solver::ConstraintSolver;
use crate::records::constraint_solving_incomplete_error::ConstraintSolvingIncompleteError;
use crate::type_aliases::constraint_vertex::ConstraintVertex;
use crate::type_aliases::type_id::TypeId;
use luaur_ast::records::location::Location;
use luaur_common::records::dense_hash_set::DenseHashSet;
use luaur_common::{FFlag, FInt};
impl ConstraintSolver {
pub fn constraint_solver_run(&mut self) {
// LUAU_TIMETRACE_SCOPE("ConstraintSolver::run", "Typechecking");
if self.is_done() {
return;
}
if FFlag::DebugLuauLogSolver.get() {
let (human, name) = match &self.module {
Some(m) => (m.human_readable_name.clone(), m.name.clone()),
None => (String::new(), String::new()),
};
std::print!("Starting solver for module {} ({})\n", human, name);
let mut opts = self.opts.clone();
dump(self as *mut ConstraintSolver, &mut opts);
self.opts = opts;
std::print!("Bindings:\n");
dump_bindings(self.root_scope, &mut self.opts.clone());
}
if !self.logger.is_null() {
let unsolved = self.unsolved_constraints.clone();
unsafe { (*self.logger).capture_initial_solver_state(&*self.root_scope, &unsolved) };
}
// Free types that have no constraints at all can be generalized right away.
if FFlag::LuauConstraintGraph.get() {
luaur_common::macros::luau_assert::LUAU_ASSERT!(!self.cgraph.is_null());
// TODO CLI-206649: We can fold constraint set into constraint graph.
let free_types: alloc::vec::Vec<TypeId> = self.constraint_set.free_types.order.clone();
for ty in free_types {
if !unsafe { (*self.cgraph).has_unsolved_dependencies(ConstraintVertex::V0(ty)) } {
self.generalize_one_type(ty);
}
}
} else {
let free_types: alloc::vec::Vec<TypeId> = self.constraint_set.free_types.order.clone();
for ty in free_types {
let empty = match self.deprecated_type_to_constraint_set.get(&ty) {
Some(set) => set.is_empty(),
None => true,
};
if empty {
self.generalize_one_type(ty);
}
}
}
self.constraint_set.free_types.clear();
let mut progress;
loop {
progress = self.run_solver_pass(false);
if !progress {
progress |= self.run_solver_pass(true);
}
if !progress {
break;
}
}
if !self.unsolved_constraints.is_empty() {
self.report_error_type_error_data_location(
ConstraintSolvingIncompleteError::default().into(),
&Location::default(),
);
}
// After we have run all the constraints, type functions should be generalized
// At this point, we can try to perform one final simplification to suss out
// whether type functions are truly uninhabited or if they can reduce
self.constraint_solver_finalize_type_functions();
if FFlag::DebugLuauLogSolver.get() || FFlag::DebugLuauLogBindings.get() {
dump_bindings(self.root_scope, &mut self.opts.clone());
}
if !self.logger.is_null() {
let unsolved = self.unsolved_constraints.clone();
unsafe { (*self.logger).capture_final_solver_state(&*self.root_scope, &unsolved) };
}
}
/// C++ `auto runSolverPass = [&](bool force) { ... };`
fn run_solver_pass(&mut self, force: bool) -> bool {
let mut progress = false;
let mut i: usize = 0;
while i < self.unsolved_constraints.len() {
let c: *const Constraint = self.unsolved_constraints[i];
if FFlag::LuauConstraintGraph.get() {
if !force
&& unsafe { (*self.cgraph).has_unsolved_dependencies(ConstraintVertex::V2(c)) }
{
i += 1;
continue;
}
} else if !force && self.deprecate_d_is_blocked(c) {
i += 1;
continue;
}
if let Some(finish_time) = self.limits.finishTime() {
if luaur_common::functions::get_clock::get_clock() > finish_time {
self.constraint_solver_throw_time_limit_error();
}
}
if let Some(token) = self.limits.cancellationToken() {
if token.requested() {
self.constraint_solver_throw_user_cancel_error();
}
}
// If we were _given_ a limit, and the current limit has hit zero,
// then early exit from constraint solving.
if FInt::LuauSolverConstraintLimit.get() > 0 && self.solver_constraint_limit == 0 {
break;
}
let save_me: String = if FFlag::DebugLuauLogSolver.get() {
to_string_constraint_to_string_options(unsafe { &*c }, &mut self.opts.clone())
} else {
String::new()
};
let mut snapshot = None;
if !self.logger.is_null() {
let unsolved = self.unsolved_constraints.clone();
snapshot = Some(unsafe {
(*self.logger).prepare_step_snapshot(&*self.root_scope, c, force, &unsolved)
});
}
if FFlag::DebugLuauAssertOnForcedConstraint.get() {
luaur_common::macros::luau_assert::LUAU_ASSERT!(!force);
}
let success = self.try_dispatch_not_null_constraint_bool(c, force);
progress |= success;
if success {
if !self.logger.is_null() {
if let Some(snap) = snapshot.take() {
unsafe {
(*self.logger).commit_step_snapshot(
luaur_common::records::variant::Variant2::V0(snap),
)
};
}
}
if FFlag::LuauConstraintGraph.get() {
luaur_common::macros::luau_assert::LUAU_ASSERT!(!self.cgraph.is_null());
let unblock_result = unsafe {
(*self.cgraph)
.unblock_constraint(NonNull::new(c as *mut Constraint).unwrap())
};
// We need to handle the logger here.
if !self.logger.is_null() {
unsafe { (*self.logger).pop_block_not_null_constraint(c) };
}
self.unsolved_constraints.remove(i);
let unblocked_types: alloc::vec::Vec<TypeId> =
unblock_result.types.order.clone();
for ty in unblocked_types {
if !unsafe {
(*self.cgraph).has_unsolved_dependencies(ConstraintVertex::V0(ty))
} {
let mut snap = None;
if !self.logger.is_null() {
let unsolved = self.unsolved_constraints.clone();
snap = Some(unsafe {
(*self.logger).prepare_generalization_snapshot(
to_string_type_id(ty),
&*self.root_scope,
&unsolved,
)
});
}
self.generalize_one_type(ty);
if !self.logger.is_null() {
if let Some(mut s) = snap.take() {
s.after = to_string_type_id(ty);
unsafe {
(*self.logger).commit_step_snapshot(
luaur_common::records::variant::Variant2::V1(s),
)
};
}
}
self.unblock_type_id_location(ty, Location::default());
}
}
// TODO CLI-206534: We never eagerly generalize free type
// packs. Maybe we should.
} else {
self.constraint_solver_deprecate_d_unblock(c);
self.unsolved_constraints.remove(i);
if let Some(entry) = self.deprecated_constraint_to_mutated_types.find(&c) {
let mutated: alloc::vec::Vec<TypeId> = entry.order.clone();
let mut seen: DenseHashSet<TypeId> = DenseHashSet::new(core::ptr::null());
for ty in mutated {
// There is a high chance that this type has been rebound
// across blocked types, rebound free types, pending
// expansion types, etc, so we need to follow it.
let ty = unsafe { follow_type_id(ty) };
if seen.contains(&ty) {
continue;
}
seen.insert(ty);
let present = self.deprecated_type_to_constraint_set.contains_key(&ty);
if present {
let (became_small, became_empty) = {
let set = self
.deprecated_type_to_constraint_set
.get_mut(&ty)
.unwrap();
set.remove(&c);
(set.len() <= 1, set.is_empty())
};
if became_small {
self.unblock_type_id_location(ty, Location::default());
}
if became_empty {
let mut snap = None;
if !self.logger.is_null() {
let unsolved = self.unsolved_constraints.clone();
snap = Some(unsafe {
(*self.logger).prepare_generalization_snapshot(
to_string_type_id(ty),
&*self.root_scope,
&unsolved,
)
});
}
self.generalize_one_type(ty);
if !self.logger.is_null() {
if let Some(mut s) = snap.take() {
s.after = to_string_type_id(ty);
unsafe {
(*self.logger).commit_step_snapshot(
luaur_common::records::variant::Variant2::V1(s),
)
};
}
}
}
}
}
}
}
if FFlag::DebugLuauLogSolver.get() {
if force {
std::print!("Force ");
}
std::print!("Dispatched\n\t{}\n", save_me);
if force {
if FFlag::LuauConstraintGraph.get() {
let mut opts = self.opts.clone();
unsafe {
(*self.cgraph).dump_blocked(
NonNull::new(c as *mut Constraint).unwrap(),
&mut opts,
)
};
self.opts = opts;
}
}
let mut opts = self.opts.clone();
dump(self as *mut ConstraintSolver, &mut opts);
self.opts = opts;
}
} else {
i += 1;
}
if force && success {
return true;
}
}
progress
}
}