rustc_infer/infer/
type_variable.rs

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/// Represents a single undo-able action that affects a type inference variable.
17#[derive(Clone)]
18pub(crate) enum UndoLog<'tcx> {
19    EqRelation(sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>),
20    SubRelation(sv::UndoLog<ut::Delegate<TyVidSubKey>>),
21}
22
23/// Convert from a specific kind of undo to the more general UndoLog
24impl<'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
30/// Convert from a specific kind of undo to the more general UndoLog
31impl<'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    /// The origins of each type variable.
61    values: IndexVec<TyVid, TypeVariableData>,
62    /// Two variables are unified in `eq_relations` when we have a
63    /// constraint `?X == ?Y`. This table also stores, for each key,
64    /// the known value.
65    eq_relations: ut::UnificationTableStorage<TyVidEqKey<'tcx>>,
66    /// Only used by `-Znext-solver` and for diagnostics. Tracks whether
67    /// type variables are related via subtyping at all, ignoring which of
68    /// the two is the subtype.
69    ///
70    /// When reporting ambiguity errors, we sometimes want to
71    /// treat all inference vars which are subtypes of each
72    /// others as if they are equal. For this case we compute
73    /// the transitive closure of our subtype obligations here.
74    ///
75    /// E.g. when encountering ambiguity errors, we want to suggest
76    /// specifying some method argument or to add a type annotation
77    /// to a local variable. Because subtyping cannot change the
78    /// shape of a type, it's fine if the cause of the ambiguity error
79    /// is only related to the suggested variable via subtyping.
80    ///
81    /// Even for something like `let x = returns_arg(); x.method();` the
82    /// type of `x` is only a supertype of the argument of `returns_arg`. We
83    /// still want to suggest specifying the type of the argument.
84    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    /// `DefId` of the type parameter this was instantiated for, if any.
97    ///
98    /// This should only be used for diagnostics.
99    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    /// If this value is known, returns the type it is known to be.
115    /// Otherwise, `None`.
116    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    /// Returns the origin that was given when `vid` was created.
153    ///
154    /// Note that this function does not return care whether
155    /// `vid` has been unified with something else or not.
156    pub(crate) fn var_origin(&self, vid: ty::TyVid) -> TypeVariableOrigin {
157        self.storage.values[vid].origin
158    }
159
160    /// Records that `a == b`.
161    ///
162    /// Precondition: neither `a` nor `b` are known.
163    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    /// Records that `a` and `b` are related via subtyping. We don't track
171    /// which of the two is the subtype.
172    ///
173    /// Precondition: neither `a` nor `b` are known.
174    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    /// Instantiates `vid` with the type `ty`.
181    ///
182    /// Precondition: `vid` must not have been previously instantiated.
183    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    /// Creates a new type variable.
196    ///
197    /// - `diverging`: indicates if this is a "diverging" type
198    ///   variable, e.g.,  one created as the type of a `return`
199    ///   expression. The code in this module doesn't care if a
200    ///   variable is diverging, but the main Rust type-checker will
201    ///   sometimes "unify" such variables with the `!` or `()` types.
202    /// - `origin`: indicates *why* the type variable was created.
203    ///   The code in this module doesn't care, but it can be useful
204    ///   for improving error messages.
205    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    /// Returns the number of type variables created thus far.
224    pub(crate) fn num_vars(&self) -> usize {
225        self.storage.values.len()
226    }
227
228    /// Returns the "root" variable of `vid` in the `eq_relations`
229    /// equivalence table. All type variables that have been equated
230    /// will yield the same root variable (per the union-find
231    /// algorithm), so `root_var(a) == root_var(b)` implies that `a ==
232    /// b` (transitively).
233    pub(crate) fn root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
234        self.eq_relations().find(vid).vid
235    }
236
237    /// Returns the "root" variable of `vid` in the `sub_unification_table`
238    /// equivalence table. All type variables that have been are related via
239    /// equality or subtyping will yield the same root variable (per the
240    /// union-find algorithm), so `sub_unification_table_root_var(a)
241    /// == sub_unification_table_root_var(b)` implies that:
242    /// ```text
243    /// exists X. (a <: X || X <: a) && (b <: X || X <: b)
244    /// ```
245    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    /// Retrieves the type to which `vid` has been instantiated, if
250    /// any.
251    pub(crate) fn probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
252        self.inlined_probe(vid)
253    }
254
255    /// An always-inlined variant of `probe`, for very hot call sites.
256    #[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    /// Returns a range of the type variables created during the snapshot.
272    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    /// Returns indices of all variables that are not yet
281    /// instantiated.
282    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///////////////////////////////////////////////////////////////////////////
296
297/// These structs (a newtyped TyVid) are used as the unification key
298/// for the `eq_relations`; they carry a `TypeVariableValue` along
299/// with them.
300#[derive(Copy, Clone, Debug, PartialEq, Eq)]
301pub(crate) struct TyVidEqKey<'tcx> {
302    vid: ty::TyVid,
303
304    // in the table, we map each ty-vid to one of these:
305    phantom: PhantomData<TypeVariableValue<'tcx>>,
306}
307
308impl<'tcx> From<ty::TyVid> for TyVidEqKey<'tcx> {
309    #[inline] // make this function eligible for inlining - it is quite hot.
310    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] // make this function eligible for inlining - it is quite hot.
340    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            // We never equate two type variables, both of which
366            // have known types. Instead, we recursively equate
367            // those types.
368            (&TypeVariableValue::Known { .. }, &TypeVariableValue::Known { .. }) => {
369                bug!("equating two type variables, both of which have known types")
370            }
371
372            // If one side is known, prefer that one.
373            (&TypeVariableValue::Known { .. }, &TypeVariableValue::Unknown { .. }) => Ok(*value1),
374            (&TypeVariableValue::Unknown { .. }, &TypeVariableValue::Known { .. }) => Ok(*value2),
375
376            // If both sides are *unknown*, it hardly matters, does it?
377            (
378                &TypeVariableValue::Unknown { universe: universe1 },
379                &TypeVariableValue::Unknown { universe: universe2 },
380            ) => {
381                // If we unify two unbound variables, ?T and ?U, then whatever
382                // value they wind up taking (which must be the same value) must
383                // be nameable by both universes. Therefore, the resulting
384                // universe is the minimum of the two universes, because that is
385                // the one which contains the fewest names in scope.
386                let universe = cmp::min(universe1, universe2);
387                Ok(TypeVariableValue::Unknown { universe })
388            }
389        }
390    }
391}