1pub mod arena;
25pub mod autoderef;
26pub mod body_ctx;
27pub mod completion_cands;
28pub mod db;
29pub mod dirty_fn;
30pub mod dirty_fn_scan;
31pub mod expectation;
32mod infer_expr;
33mod infer_expr_call;
34mod infer_expr_cf;
35mod infer_expr_closure;
36mod infer_expr_ops;
37mod infer_macro;
38mod infer_macro_std;
39pub mod infer_pat;
40pub mod lang_ops;
41pub mod local_defs;
42mod local_defs_adt;
43mod local_defs_impl;
44mod local_defs_macro_lookup;
45mod local_defs_macros;
46mod local_defs_proj;
47pub mod lower_ty;
48mod lower_ty_tok;
49pub mod mem_ty_db;
50mod mem_ty_db_methods;
51mod mem_ty_db_traits;
52mod method_res;
53pub mod partial_code;
54mod tok_ty_scan;
55pub mod trait_solve;
56mod trait_solve_cst;
57mod trait_solve_ufcs;
58pub mod ty;
59pub mod uf_table;
60pub mod unify;
61
62pub use arena::InferTyArena;
63pub use body_ctx::{BodyInferCtx, FnInferResult, MacroExprTy, ResolvedDotRef};
64pub use completion_cands::{CompletionMethodHit, CompletionTraitHints};
65pub use db::{clone_ty_into, CtorShapeKind, EmptyTyDefDb, MethodCand, StdMacroTy, TraitAssocFnMeta, TyDefDb, TyLangItem, WhereObligation};
66pub use dirty_fn::{DirtyFileTracker, DirtyFnUpdateStats};
67pub use dirty_fn_scan::{extract_file_fn_summaries, CachedDirtyFn, FnBodySummary, FrozenTyArena};
68pub use expectation::{BranchCoercer, Expectation};
69pub use infer_pat::BindingMode;
70pub use local_defs::{effective_local_def_id, LocalCstDefs};
71pub use lower_ty::LowerTyEnv;
72pub use mem_ty_db::MemTyDefDb;
73pub use ty::{is_float_prim, is_int_prim, InferTyId, InferTyKind, TyFlags, TySliceRange, TyVarId, TyVarKind};
74pub use uf_table::{InferSnapshot, UnionFindTable};
75pub use unify::{coerce, equate, lub, TypeError, UnifyResult};
76
77use uf_table::InlineTyBuf;
78
79#[derive(Debug, Default)]
81pub struct InferenceTable {
82 pub arena: InferTyArena,
83 pub vars: UnionFindTable,
84 pub(crate) gid_scratch: Vec<crate::name_res::GlobalDefId>,
85}
86
87impl InferenceTable {
88 pub fn new() -> Self {
89 Self::with_capacity(256, 128)
90 }
91
92 pub fn with_capacity(arena_cap: usize, var_cap: usize) -> Self {
93 Self { arena: InferTyArena::with_capacity(arena_cap), vars: UnionFindTable::with_capacity(var_cap), gid_scratch: Vec::with_capacity(48) }
94 }
95
96 #[inline]
98 pub fn clear(&mut self) {
99 self.arena.clear();
100 self.vars.clear();
101 self.gid_scratch.clear();
102 }
103
104 #[inline]
105 pub fn new_ty_var(&mut self) -> InferTyId {
106 self.vars.new_var(&mut self.arena, TyVarKind::General).1
107 }
108
109 #[inline]
110 pub fn new_int_var(&mut self) -> InferTyId {
111 self.vars.new_var(&mut self.arena, TyVarKind::IntVar).1
112 }
113
114 #[inline]
115 pub fn new_float_var(&mut self) -> InferTyId {
116 self.vars.new_var(&mut self.arena, TyVarKind::FloatVar).1
117 }
118
119 #[inline]
120 pub fn new_var_with_id(&mut self, kind: TyVarKind) -> (TyVarId, InferTyId) {
121 self.vars.new_var(&mut self.arena, kind)
122 }
123
124 #[inline]
125 pub fn start_snapshot(&mut self) -> InferSnapshot {
126 self.vars.start_snapshot()
127 }
128
129 #[inline]
130 pub fn commit(&mut self, snap: InferSnapshot) {
131 self.vars.commit(snap);
132 }
133
134 #[inline]
135 pub fn rollback_to(&mut self, snap: InferSnapshot) {
136 self.vars.rollback_to(snap);
137 }
138
139 #[inline]
140 pub fn shallow_resolve(&mut self, ty: InferTyId) -> InferTyId {
141 self.vars.shallow_resolve(&self.arena, ty)
142 }
143
144 #[inline]
146 pub fn deep_resolve(&mut self, ty: InferTyId) -> InferTyId {
147 self.vars.deep_resolve(&mut self.arena, ty, false)
148 }
149
150 #[inline]
152 pub fn resolve_with_fallback(&mut self, ty: InferTyId) -> InferTyId {
153 self.vars.deep_resolve(&mut self.arena, ty, true)
154 }
155
156 #[inline]
157 pub fn equate(&mut self, a: InferTyId, b: InferTyId) -> UnifyResult {
158 unify::equate(&mut self.arena, &mut self.vars, a, b)
159 }
160
161 #[inline]
163 pub fn equate_or_recover(&mut self, a: InferTyId, b: InferTyId) -> InferTyId {
164 if self.equate(a, b).is_ok() {
165 self.shallow_resolve(a)
166 } else {
167 InferTyId::UNKNOWN
168 }
169 }
170
171 #[inline]
172 pub fn coerce(&mut self, actual: InferTyId, expected: InferTyId) -> UnifyResult {
173 unify::coerce(&mut self.arena, &mut self.vars, actual, expected)
174 }
175
176 #[inline]
177 pub fn lub(&mut self, a: InferTyId, b: InferTyId) -> Result<InferTyId, TypeError> {
178 unify::lub(&mut self.arena, &mut self.vars, a, b)
179 }
180
181 pub fn fresh_subst(&mut self, count: u16, out: &mut Vec<InferTyId>) {
183 out.clear();
184 out.reserve(count as usize);
185 for _ in 0..count {
186 out.push(self.new_ty_var());
187 }
188 }
189
190 pub fn instantiate(&mut self, ty: InferTyId, subst: &[InferTyId]) -> InferTyId {
192 self.instantiate_rec(ty, subst, 0)
193 }
194
195 fn instantiate_rec(&mut self, ty: InferTyId, subst: &[InferTyId], depth: u8) -> InferTyId {
196 if depth > 64 || !self.arena.flags(ty).has_param() {
197 return ty;
198 }
199 match self.arena.kind(ty) {
200 InferTyKind::Param { idx, .. } => subst.get(idx as usize).copied().unwrap_or(InferTyId::UNKNOWN),
201 InferTyKind::Ref { is_mut, inner } => {
202 let s = self.instantiate_rec(inner, subst, depth + 1);
203 self.arena.mk_ref(is_mut, s)
204 }
205 InferTyKind::Ptr { is_mut, inner } => {
206 let s = self.instantiate_rec(inner, subst, depth + 1);
207 self.arena.mk_ptr(is_mut, s)
208 }
209 InferTyKind::Slice(elem) => {
210 let s = self.instantiate_rec(elem, subst, depth + 1);
211 self.arena.mk_slice(s)
212 }
213 InferTyKind::Array { elem, len } => {
214 let s = self.instantiate_rec(elem, subst, depth + 1);
215 let l = self.arena.resolve_array_len(len, subst);
216 self.arena.mk_array(s, l)
217 }
218 InferTyKind::Tuple(r) => {
219 let sub = self.instantiate_slice(r, subst, depth + 1);
220 self.arena.mk_tuple(sub.as_slice())
221 }
222 InferTyKind::Adt { def, args } => {
223 let sub = self.instantiate_slice(args, subst, depth + 1);
224 self.arena.mk_adt(def, sub.as_slice())
225 }
226 InferTyKind::FnPtr { sig } => {
227 let sub = self.instantiate_slice(sig, subst, depth + 1);
228 let s = sub.as_slice();
229 self.arena.mk_fn_ptr(&s[..s.len() - 1], s[s.len() - 1])
230 }
231 InferTyKind::FnDef { def, args } => {
232 let sub = self.instantiate_slice(args, subst, depth + 1);
233 self.arena.mk_fn_def(def, sub.as_slice())
234 }
235 InferTyKind::Closure { def_start, def_end, sig } => {
236 let sub = self.instantiate_slice(sig, subst, depth + 1);
237 let s = sub.as_slice();
238 self.arena.mk_closure(def_start, def_end, &s[..s.len() - 1], s[s.len() - 1])
239 }
240 InferTyKind::Alias { trait_def, assoc_def, self_ty, args } => {
241 let ns = self.instantiate_rec(self_ty, subst, depth + 1);
242 let sub = self.instantiate_slice(args, subst, depth + 1);
243 self.arena.mk_alias(trait_def, assoc_def, ns, sub.as_slice())
244 }
245 InferTyKind::Dyn { principal, args } => {
246 let sub = self.instantiate_slice(args, subst, depth + 1);
247 self.arena.mk_dyn(principal, sub.as_slice())
248 }
249 InferTyKind::Opaque { def, args } => {
250 let sub = self.instantiate_slice(args, subst, depth + 1);
251 self.arena.mk_opaque(def, sub.as_slice())
252 }
253 InferTyKind::Unknown | InferTyKind::Never | InferTyKind::Prim(_) | InferTyKind::Infer(_) => ty,
254 }
255 }
256
257 fn instantiate_slice(&mut self, r: TySliceRange, subst: &[InferTyId], depth: u8) -> InlineTyBuf {
258 let mut buf = InlineTyBuf::new();
259 for idx in 0..r.len as usize {
260 let orig = self.arena.slice(r)[idx];
261 let next = self.instantiate_rec(orig, subst, depth);
262 buf.push(next);
263 }
264 buf
265 }
266
267 #[inline]
269 pub fn has_unresolved_infer(&mut self, ty: InferTyId) -> bool {
270 vars_has_unresolved_infer(&self.arena, &mut self.vars, ty)
271 }
272}
273
274#[inline]
276pub fn vars_has_unresolved_infer(arena: &InferTyArena, vars: &mut UnionFindTable, ty: InferTyId) -> bool {
277 vars_has_unresolved_infer_rec(arena, vars, ty, 0)
278}
279
280fn vars_has_unresolved_infer_rec(arena: &InferTyArena, vars: &mut UnionFindTable, ty: InferTyId, depth: u8) -> bool {
281 if depth > 16 || !ty.is_some() || !arena.flags(ty).has_infer() {
282 return false;
283 }
284 let cur = vars.shallow_resolve(arena, ty);
285 if !arena.flags(cur).has_infer() {
286 return false;
287 }
288 match arena.kind(cur) {
289 InferTyKind::Infer(_) => true,
290 InferTyKind::Ref { inner, .. } | InferTyKind::Ptr { inner, .. } | InferTyKind::Slice(inner) | InferTyKind::Array { elem: inner, .. } => {
291 vars_has_unresolved_infer_rec(arena, vars, inner, depth + 1)
292 }
293 InferTyKind::Tuple(r)
294 | InferTyKind::Adt { args: r, .. }
295 | InferTyKind::FnPtr { sig: r }
296 | InferTyKind::FnDef { args: r, .. }
297 | InferTyKind::Closure { sig: r, .. }
298 | InferTyKind::Dyn { args: r, .. }
299 | InferTyKind::Opaque { args: r, .. } => {
300 for idx in 0..r.len as usize {
301 let elem = arena.slice(r)[idx];
302 if vars_has_unresolved_infer_rec(arena, vars, elem, depth + 1) {
303 return true;
304 }
305 }
306 false
307 }
308 InferTyKind::Alias { self_ty, args, .. } => {
309 if vars_has_unresolved_infer_rec(arena, vars, self_ty, depth + 1) {
310 return true;
311 }
312 for idx in 0..args.len as usize {
313 let elem = arena.slice(args)[idx];
314 if vars_has_unresolved_infer_rec(arena, vars, elem, depth + 1) {
315 return true;
316 }
317 }
318 false
319 }
320 _ => false,
321 }
322}