1use std::cmp;
2use std::marker::PhantomData;
3use std::ops::Range;
4
5use rustc_data_structures::undo_log::Rollback;
6use rustc_data_structures::{snapshot_vec as sv, unify as ut};
7use rustc_hir::def_id::DefId;
8use rustc_index::IndexVec;
9use rustc_middle::bug;
10use rustc_middle::ty::{self, Ty, TyVid};
11use rustc_span::Span;
12use tracing::debug;
13
14use crate::infer::InferCtxtUndoLogs;
15
16#[derive(Clone)]
18pub(crate) enum UndoLog<'tcx> {
19 EqRelation(sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>),
20 SubRelation(sv::UndoLog<ut::Delegate<TyVidSubKey>>),
21}
22
23impl<'tcx> From<sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>> for UndoLog<'tcx> {
25 fn from(l: sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>) -> Self {
26 UndoLog::EqRelation(l)
27 }
28}
29
30impl<'tcx> From<sv::UndoLog<ut::Delegate<TyVidSubKey>>> for UndoLog<'tcx> {
32 fn from(l: sv::UndoLog<ut::Delegate<TyVidSubKey>>) -> Self {
33 UndoLog::SubRelation(l)
34 }
35}
36
37impl<'tcx> Rollback<sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>> for TypeVariableStorage<'tcx> {
38 fn reverse(&mut self, undo: sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>) {
39 self.eq_relations.reverse(undo)
40 }
41}
42
43impl<'tcx> Rollback<sv::UndoLog<ut::Delegate<TyVidSubKey>>> for TypeVariableStorage<'tcx> {
44 fn reverse(&mut self, undo: sv::UndoLog<ut::Delegate<TyVidSubKey>>) {
45 self.sub_unification_table.reverse(undo)
46 }
47}
48
49impl<'tcx> Rollback<UndoLog<'tcx>> for TypeVariableStorage<'tcx> {
50 fn reverse(&mut self, undo: UndoLog<'tcx>) {
51 match undo {
52 UndoLog::EqRelation(undo) => self.eq_relations.reverse(undo),
53 UndoLog::SubRelation(undo) => self.sub_unification_table.reverse(undo),
54 }
55 }
56}
57
58#[derive(Clone, Default)]
59pub(crate) struct TypeVariableStorage<'tcx> {
60 values: IndexVec<TyVid, TypeVariableData>,
62 eq_relations: ut::UnificationTableStorage<TyVidEqKey<'tcx>>,
66 sub_unification_table: ut::UnificationTableStorage<TyVidSubKey>,
85}
86
87pub(crate) struct TypeVariableTable<'a, 'tcx> {
88 storage: &'a mut TypeVariableStorage<'tcx>,
89
90 undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
91}
92
93#[derive(Copy, Clone, Debug)]
94pub struct TypeVariableOrigin {
95 pub span: Span,
96 pub param_def_id: Option<DefId>,
100}
101
102#[derive(Clone)]
103pub(crate) struct TypeVariableData {
104 origin: TypeVariableOrigin,
105}
106
107#[derive(Copy, Clone, Debug)]
108pub(crate) enum TypeVariableValue<'tcx> {
109 Known { value: Ty<'tcx> },
110 Unknown { universe: ty::UniverseIndex },
111}
112
113impl<'tcx> TypeVariableValue<'tcx> {
114 pub(crate) fn known(&self) -> Option<Ty<'tcx>> {
117 match *self {
118 TypeVariableValue::Unknown { .. } => None,
119 TypeVariableValue::Known { value } => Some(value),
120 }
121 }
122
123 pub(crate) fn is_unknown(&self) -> bool {
124 match *self {
125 TypeVariableValue::Unknown { .. } => true,
126 TypeVariableValue::Known { .. } => false,
127 }
128 }
129}
130
131impl<'tcx> TypeVariableStorage<'tcx> {
132 #[inline]
133 pub(crate) fn with_log<'a>(
134 &'a mut self,
135 undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
136 ) -> TypeVariableTable<'a, 'tcx> {
137 TypeVariableTable { storage: self, undo_log }
138 }
139
140 #[inline]
141 pub(crate) fn eq_relations_ref(&self) -> &ut::UnificationTableStorage<TyVidEqKey<'tcx>> {
142 &self.eq_relations
143 }
144
145 pub(super) fn finalize_rollback(&mut self) {
146 debug_assert!(self.values.len() >= self.eq_relations.len());
147 self.values.truncate(self.eq_relations.len());
148 }
149}
150
151impl<'tcx> TypeVariableTable<'_, 'tcx> {
152 pub(crate) fn var_origin(&self, vid: ty::TyVid) -> TypeVariableOrigin {
157 self.storage.values[vid].origin
158 }
159
160 pub(crate) fn equate(&mut self, a: ty::TyVid, b: ty::TyVid) {
164 debug_assert!(self.probe(a).is_unknown());
165 debug_assert!(self.probe(b).is_unknown());
166 self.eq_relations().union(a, b);
167 self.sub_unification_table().union(a, b);
168 }
169
170 pub(crate) fn sub_unify(&mut self, a: ty::TyVid, b: ty::TyVid) {
175 debug_assert!(self.probe(a).is_unknown());
176 debug_assert!(self.probe(b).is_unknown());
177 self.sub_unification_table().union(a, b);
178 }
179
180 pub(crate) fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) {
184 let vid = self.root_var(vid);
185 debug_assert!(!ty.is_ty_var(), "instantiating ty var with var: {vid:?} {ty:?}");
186 debug_assert!(self.probe(vid).is_unknown());
187 debug_assert!(
188 self.eq_relations().probe_value(vid).is_unknown(),
189 "instantiating type variable `{vid:?}` twice: new-value = {ty:?}, old-value={:?}",
190 self.eq_relations().probe_value(vid)
191 );
192 self.eq_relations().union_value(vid, TypeVariableValue::Known { value: ty });
193 }
194
195 pub(crate) fn new_var(
206 &mut self,
207 universe: ty::UniverseIndex,
208 origin: TypeVariableOrigin,
209 ) -> ty::TyVid {
210 let eq_key = self.eq_relations().new_key(TypeVariableValue::Unknown { universe });
211
212 let sub_key = self.sub_unification_table().new_key(());
213 debug_assert_eq!(eq_key.vid, sub_key.vid);
214
215 let index = self.storage.values.push(TypeVariableData { origin });
216 debug_assert_eq!(eq_key.vid, index);
217
218 debug!("new_var(index={:?}, universe={:?}, origin={:?})", eq_key.vid, universe, origin);
219
220 index
221 }
222
223 pub(crate) fn num_vars(&self) -> usize {
225 self.storage.values.len()
226 }
227
228 pub(crate) fn root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
234 self.eq_relations().find(vid).vid
235 }
236
237 pub(crate) fn sub_unification_table_root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
246 self.sub_unification_table().find(vid).vid
247 }
248
249 pub(crate) fn probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
252 self.inlined_probe(vid)
253 }
254
255 #[inline(always)]
257 pub(crate) fn inlined_probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
258 self.eq_relations().inlined_probe_value(vid)
259 }
260
261 #[inline]
262 fn eq_relations(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidEqKey<'tcx>> {
263 self.storage.eq_relations.with_log(self.undo_log)
264 }
265
266 #[inline]
267 fn sub_unification_table(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidSubKey> {
268 self.storage.sub_unification_table.with_log(self.undo_log)
269 }
270
271 pub(crate) fn vars_since_snapshot(
273 &mut self,
274 value_count: usize,
275 ) -> (Range<TyVid>, Vec<TypeVariableOrigin>) {
276 let range = TyVid::from_usize(value_count)..TyVid::from_usize(self.num_vars());
277 (range.clone(), range.map(|index| self.var_origin(index)).collect())
278 }
279
280 pub(crate) fn unresolved_variables(&mut self) -> Vec<ty::TyVid> {
283 (0..self.num_vars())
284 .filter_map(|i| {
285 let vid = ty::TyVid::from_usize(i);
286 match self.probe(vid) {
287 TypeVariableValue::Unknown { .. } => Some(vid),
288 TypeVariableValue::Known { .. } => None,
289 }
290 })
291 .collect()
292 }
293}
294
295#[derive(Copy, Clone, Debug, PartialEq, Eq)]
301pub(crate) struct TyVidEqKey<'tcx> {
302 vid: ty::TyVid,
303
304 phantom: PhantomData<TypeVariableValue<'tcx>>,
306}
307
308impl<'tcx> From<ty::TyVid> for TyVidEqKey<'tcx> {
309 #[inline] fn from(vid: ty::TyVid) -> Self {
311 TyVidEqKey { vid, phantom: PhantomData }
312 }
313}
314
315impl<'tcx> ut::UnifyKey for TyVidEqKey<'tcx> {
316 type Value = TypeVariableValue<'tcx>;
317 #[inline(always)]
318 fn index(&self) -> u32 {
319 self.vid.as_u32()
320 }
321 #[inline]
322 fn from_index(i: u32) -> Self {
323 TyVidEqKey::from(ty::TyVid::from_u32(i))
324 }
325 fn tag() -> &'static str {
326 "TyVidEqKey"
327 }
328 fn order_roots(a: Self, _: &Self::Value, b: Self, _: &Self::Value) -> Option<(Self, Self)> {
329 if a.vid.as_u32() < b.vid.as_u32() { Some((a, b)) } else { Some((b, a)) }
330 }
331}
332
333#[derive(Copy, Clone, Debug, PartialEq, Eq)]
334pub(crate) struct TyVidSubKey {
335 vid: ty::TyVid,
336}
337
338impl From<ty::TyVid> for TyVidSubKey {
339 #[inline] fn from(vid: ty::TyVid) -> Self {
341 TyVidSubKey { vid }
342 }
343}
344
345impl ut::UnifyKey for TyVidSubKey {
346 type Value = ();
347 #[inline]
348 fn index(&self) -> u32 {
349 self.vid.as_u32()
350 }
351 #[inline]
352 fn from_index(i: u32) -> TyVidSubKey {
353 TyVidSubKey { vid: ty::TyVid::from_u32(i) }
354 }
355 fn tag() -> &'static str {
356 "TyVidSubKey"
357 }
358}
359
360impl<'tcx> ut::UnifyValue for TypeVariableValue<'tcx> {
361 type Error = ut::NoError;
362
363 fn unify_values(value1: &Self, value2: &Self) -> Result<Self, ut::NoError> {
364 match (value1, value2) {
365 (&TypeVariableValue::Known { .. }, &TypeVariableValue::Known { .. }) => {
369 bug!("equating two type variables, both of which have known types")
370 }
371
372 (&TypeVariableValue::Known { .. }, &TypeVariableValue::Unknown { .. }) => Ok(*value1),
374 (&TypeVariableValue::Unknown { .. }, &TypeVariableValue::Known { .. }) => Ok(*value2),
375
376 (
378 &TypeVariableValue::Unknown { universe: universe1 },
379 &TypeVariableValue::Unknown { universe: universe2 },
380 ) => {
381 let universe = cmp::min(universe1, universe2);
387 Ok(TypeVariableValue::Unknown { universe })
388 }
389 }
390 }
391}