Skip to main content

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::HirId;
8use rustc_hir::def_id::DefId;
9use rustc_index::IndexVec;
10use rustc_middle::bug;
11use rustc_middle::ty::{self, Ty, TyVid};
12use rustc_span::Span;
13use tracing::debug;
14
15use crate::infer::InferCtxtUndoLogs;
16
17/// Represents a single undo-able action that affects a type inference variable.
18#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for UndoLog<'tcx> {
    #[inline]
    fn clone(&self) -> UndoLog<'tcx> {
        match self {
            UndoLog::EqRelation(__self_0) =>
                UndoLog::EqRelation(::core::clone::Clone::clone(__self_0)),
            UndoLog::SubRelation(__self_0) =>
                UndoLog::SubRelation(::core::clone::Clone::clone(__self_0)),
        }
    }
}Clone)]
19pub(crate) enum UndoLog<'tcx> {
20    EqRelation(sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>),
21    SubRelation(sv::UndoLog<ut::Delegate<TyVidSubKey>>),
22}
23
24/// Convert from a specific kind of undo to the more general UndoLog
25impl<'tcx> From<sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>> for UndoLog<'tcx> {
26    fn from(l: sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>) -> Self {
27        UndoLog::EqRelation(l)
28    }
29}
30
31/// Convert from a specific kind of undo to the more general UndoLog
32impl<'tcx> From<sv::UndoLog<ut::Delegate<TyVidSubKey>>> for UndoLog<'tcx> {
33    fn from(l: sv::UndoLog<ut::Delegate<TyVidSubKey>>) -> Self {
34        UndoLog::SubRelation(l)
35    }
36}
37
38impl<'tcx> Rollback<sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>> for TypeVariableStorage<'tcx> {
39    fn reverse(&mut self, undo: sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>) {
40        self.eq_relations.reverse(undo)
41    }
42}
43
44impl<'tcx> Rollback<sv::UndoLog<ut::Delegate<TyVidSubKey>>> for TypeVariableStorage<'tcx> {
45    fn reverse(&mut self, undo: sv::UndoLog<ut::Delegate<TyVidSubKey>>) {
46        self.sub_unification_table.reverse(undo)
47    }
48}
49
50impl<'tcx> Rollback<UndoLog<'tcx>> for TypeVariableStorage<'tcx> {
51    fn reverse(&mut self, undo: UndoLog<'tcx>) {
52        match undo {
53            UndoLog::EqRelation(undo) => self.eq_relations.reverse(undo),
54            UndoLog::SubRelation(undo) => self.sub_unification_table.reverse(undo),
55        }
56    }
57}
58
59#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for TypeVariableStorage<'tcx> {
    #[inline]
    fn clone(&self) -> TypeVariableStorage<'tcx> {
        TypeVariableStorage {
            values: ::core::clone::Clone::clone(&self.values),
            eq_relations: ::core::clone::Clone::clone(&self.eq_relations),
            sub_unification_table: ::core::clone::Clone::clone(&self.sub_unification_table),
        }
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::default::Default for TypeVariableStorage<'tcx> {
    #[inline]
    fn default() -> TypeVariableStorage<'tcx> {
        TypeVariableStorage {
            values: ::core::default::Default::default(),
            eq_relations: ::core::default::Default::default(),
            sub_unification_table: ::core::default::Default::default(),
        }
    }
}Default)]
60pub(crate) struct TypeVariableStorage<'tcx> {
61    /// The origins of each type variable.
62    values: IndexVec<TyVid, TypeVariableData>,
63    /// Two variables are unified in `eq_relations` when we have a
64    /// constraint `?X == ?Y`. This table also stores, for each key,
65    /// the known value.
66    eq_relations: ut::UnificationTableStorage<TyVidEqKey<'tcx>>,
67    /// Only used by `-Znext-solver` and for diagnostics. Tracks whether
68    /// type variables are related via subtyping at all, ignoring which of
69    /// the two is the subtype.
70    ///
71    /// When reporting ambiguity errors, we sometimes want to
72    /// treat all inference vars which are subtypes of each
73    /// others as if they are equal. For this case we compute
74    /// the transitive closure of our subtype obligations here.
75    ///
76    /// E.g. when encountering ambiguity errors, we want to suggest
77    /// specifying some method argument or to add a type annotation
78    /// to a local variable. Because subtyping cannot change the
79    /// shape of a type, it's fine if the cause of the ambiguity error
80    /// is only related to the suggested variable via subtyping.
81    ///
82    /// Even for something like `let x = returns_arg(); x.method();` the
83    /// type of `x` is only a supertype of the argument of `returns_arg`. We
84    /// still want to suggest specifying the type of the argument.
85    sub_unification_table: ut::UnificationTableStorage<TyVidSubKey>,
86}
87
88pub(crate) struct TypeVariableTable<'a, 'tcx> {
89    storage: &'a mut TypeVariableStorage<'tcx>,
90
91    undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
92}
93
94#[derive(#[automatically_derived]
impl ::core::marker::Copy for TypeVariableOrigin { }Copy, #[automatically_derived]
impl ::core::clone::Clone for TypeVariableOrigin {
    #[inline]
    fn clone(&self) -> TypeVariableOrigin {
        let _: ::core::clone::AssertParamIsClone<Span>;
        let _: ::core::clone::AssertParamIsClone<Option<DefId>>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for TypeVariableOrigin {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "TypeVariableOrigin", "span", &self.span, "param_def_id",
            &&self.param_def_id)
    }
}Debug)]
95pub struct TypeVariableOrigin {
96    pub span: Span,
97    /// `DefId` of the type parameter this was instantiated for, if any.
98    ///
99    /// This should only be used for diagnostics.
100    pub param_def_id: Option<DefId>,
101}
102
103#[derive(#[automatically_derived]
impl ::core::marker::Copy for FloatVariableOrigin { }Copy, #[automatically_derived]
impl ::core::clone::Clone for FloatVariableOrigin {
    #[inline]
    fn clone(&self) -> FloatVariableOrigin {
        let _: ::core::clone::AssertParamIsClone<Span>;
        let _: ::core::clone::AssertParamIsClone<Option<HirId>>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for FloatVariableOrigin {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "FloatVariableOrigin", "span", &self.span, "lint_id",
            &&self.lint_id)
    }
}Debug)]
104pub struct FloatVariableOrigin {
105    pub span: Span,
106
107    /// `HirId` to lint at for this float variable, if any.
108    ///
109    /// This should only be used for diagnostics.
110    pub lint_id: Option<HirId>,
111}
112
113#[derive(#[automatically_derived]
impl ::core::clone::Clone for TypeVariableData {
    #[inline]
    fn clone(&self) -> TypeVariableData {
        TypeVariableData { origin: ::core::clone::Clone::clone(&self.origin) }
    }
}Clone)]
114pub(crate) struct TypeVariableData {
115    origin: TypeVariableOrigin,
116}
117
118#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for TypeVariableValue<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for TypeVariableValue<'tcx> {
    #[inline]
    fn clone(&self) -> TypeVariableValue<'tcx> {
        let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<ty::UniverseIndex>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for TypeVariableValue<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            TypeVariableValue::Known { value: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f, "Known",
                    "value", &__self_0),
            TypeVariableValue::Unknown { universe: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "Unknown", "universe", &__self_0),
        }
    }
}Debug)]
119pub(crate) enum TypeVariableValue<'tcx> {
120    Known { value: Ty<'tcx> },
121    Unknown { universe: ty::UniverseIndex },
122}
123
124impl<'tcx> TypeVariableValue<'tcx> {
125    /// If this value is known, returns the type it is known to be.
126    /// Otherwise, `None`.
127    pub(crate) fn known(&self) -> Option<Ty<'tcx>> {
128        match *self {
129            TypeVariableValue::Unknown { .. } => None,
130            TypeVariableValue::Known { value } => Some(value),
131        }
132    }
133
134    pub(crate) fn is_unknown(&self) -> bool {
135        match *self {
136            TypeVariableValue::Unknown { .. } => true,
137            TypeVariableValue::Known { .. } => false,
138        }
139    }
140}
141
142impl<'tcx> TypeVariableStorage<'tcx> {
143    #[inline]
144    pub(crate) fn with_log<'a>(
145        &'a mut self,
146        undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
147    ) -> TypeVariableTable<'a, 'tcx> {
148        TypeVariableTable { storage: self, undo_log }
149    }
150
151    #[inline]
152    pub(crate) fn eq_relations_ref(&self) -> &ut::UnificationTableStorage<TyVidEqKey<'tcx>> {
153        &self.eq_relations
154    }
155
156    pub(super) fn finalize_rollback(&mut self) {
157        if true {
    if !(self.values.len() >= self.eq_relations.len()) {
        ::core::panicking::panic("assertion failed: self.values.len() >= self.eq_relations.len()")
    };
};debug_assert!(self.values.len() >= self.eq_relations.len());
158        self.values.truncate(self.eq_relations.len());
159    }
160
161    pub(crate) fn sub_unification_table_ref(&self) -> &ut::UnificationTableStorage<TyVidSubKey> {
162        &self.sub_unification_table
163    }
164}
165
166impl<'tcx> TypeVariableTable<'_, 'tcx> {
167    /// Returns the origin that was given when `vid` was created.
168    ///
169    /// Note that this function does not return care whether
170    /// `vid` has been unified with something else or not.
171    pub(crate) fn var_origin(&self, vid: ty::TyVid) -> TypeVariableOrigin {
172        self.storage.values[vid].origin
173    }
174
175    /// Records that `a == b`.
176    ///
177    /// Precondition: neither `a` nor `b` are known.
178    pub(crate) fn equate(&mut self, a: ty::TyVid, b: ty::TyVid) {
179        if true {
    if !self.probe(a).is_unknown() {
        ::core::panicking::panic("assertion failed: self.probe(a).is_unknown()")
    };
};debug_assert!(self.probe(a).is_unknown());
180        if true {
    if !self.probe(b).is_unknown() {
        ::core::panicking::panic("assertion failed: self.probe(b).is_unknown()")
    };
};debug_assert!(self.probe(b).is_unknown());
181        self.eq_relations().union(a, b);
182        self.sub_unification_table().union(a, b);
183    }
184
185    /// Records that `a` and `b` are related via subtyping. We don't track
186    /// which of the two is the subtype.
187    ///
188    /// Precondition: neither `a` nor `b` are known.
189    pub(crate) fn sub_unify(&mut self, a: ty::TyVid, b: ty::TyVid) {
190        if true {
    if !self.probe(a).is_unknown() {
        ::core::panicking::panic("assertion failed: self.probe(a).is_unknown()")
    };
};debug_assert!(self.probe(a).is_unknown());
191        if true {
    if !self.probe(b).is_unknown() {
        ::core::panicking::panic("assertion failed: self.probe(b).is_unknown()")
    };
};debug_assert!(self.probe(b).is_unknown());
192        self.sub_unification_table().union(a, b);
193    }
194
195    /// Instantiates `vid` with the type `ty`.
196    ///
197    /// Precondition: `vid` must not have been previously instantiated.
198    pub(crate) fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) {
199        let vid = self.root_var(vid);
200        if true {
    if !!ty.is_ty_var() {
        {
            ::core::panicking::panic_fmt(format_args!("instantiating ty var with var: {0:?} {1:?}",
                    vid, ty));
        }
    };
};debug_assert!(!ty.is_ty_var(), "instantiating ty var with var: {vid:?} {ty:?}");
201        if true {
    if !self.probe(vid).is_unknown() {
        ::core::panicking::panic("assertion failed: self.probe(vid).is_unknown()")
    };
};debug_assert!(self.probe(vid).is_unknown());
202        if true {
    if !self.eq_relations().probe_value(vid).is_unknown() {
        {
            ::core::panicking::panic_fmt(format_args!("instantiating type variable `{1:?}` twice: new-value = {2:?}, old-value={0:?}",
                    self.eq_relations().probe_value(vid), vid, ty));
        }
    };
};debug_assert!(
203            self.eq_relations().probe_value(vid).is_unknown(),
204            "instantiating type variable `{vid:?}` twice: new-value = {ty:?}, old-value={:?}",
205            self.eq_relations().probe_value(vid)
206        );
207        self.eq_relations().union_value(vid, TypeVariableValue::Known { value: ty });
208    }
209
210    /// Creates a new type variable.
211    ///
212    /// - `diverging`: indicates if this is a "diverging" type
213    ///   variable, e.g.,  one created as the type of a `return`
214    ///   expression. The code in this module doesn't care if a
215    ///   variable is diverging, but the main Rust type-checker will
216    ///   sometimes "unify" such variables with the `!` or `()` types.
217    /// - `origin`: indicates *why* the type variable was created.
218    ///   The code in this module doesn't care, but it can be useful
219    ///   for improving error messages.
220    pub(crate) fn new_var(
221        &mut self,
222        universe: ty::UniverseIndex,
223        origin: TypeVariableOrigin,
224    ) -> ty::TyVid {
225        let eq_key = self.eq_relations().new_key(TypeVariableValue::Unknown { universe });
226
227        let sub_key = self.sub_unification_table().new_key(());
228        if true {
    {
        match (&eq_key.vid, &sub_key.vid) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(eq_key.vid, sub_key.vid);
229
230        let index = self.storage.values.push(TypeVariableData { origin });
231        if true {
    {
        match (&eq_key.vid, &index) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(eq_key.vid, index);
232
233        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/type_variable.rs:233",
                        "rustc_infer::infer::type_variable",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/type_variable.rs"),
                        ::tracing_core::__macro_support::Option::Some(233u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::type_variable"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("new_var(index={0:?}, universe={1:?}, origin={2:?})",
                                                    eq_key.vid, universe, origin) as
                                            &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("new_var(index={:?}, universe={:?}, origin={:?})", eq_key.vid, universe, origin);
234
235        index
236    }
237
238    /// Returns the number of type variables created thus far.
239    pub(crate) fn num_vars(&self) -> usize {
240        self.storage.values.len()
241    }
242
243    /// Returns the "root" variable of `vid` in the `eq_relations`
244    /// equivalence table. All type variables that have been equated
245    /// will yield the same root variable (per the union-find
246    /// algorithm), so `root_var(a) == root_var(b)` implies that `a ==
247    /// b` (transitively).
248    pub(crate) fn root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
249        self.eq_relations().find(vid).vid
250    }
251
252    /// Returns the "root" variable of `vid` in the `sub_unification_table`
253    /// equivalence table. All type variables that have been related via
254    /// equality or subtyping will yield the same root variable (per the
255    /// union-find algorithm), so `sub_unification_table_root_var(a)
256    /// == sub_unification_table_root_var(b)` implies that `a` and `b` are
257    /// transitively related via subtyping.
258    pub(crate) fn sub_unification_table_root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
259        self.sub_unification_table().find(vid).vid
260    }
261
262    /// Retrieves the type to which `vid` has been instantiated, if
263    /// any.
264    pub(crate) fn probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
265        self.inlined_probe(vid)
266    }
267
268    /// An always-inlined variant of `probe`, for very hot call sites.
269    #[inline(always)]
270    pub(crate) fn inlined_probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
271        self.eq_relations().inlined_probe_value(vid)
272    }
273
274    /// Retrieves the type to which `vid` has been instantiated, if
275    /// any, along with the root `vid`.
276    pub(crate) fn probe_with_root_vid(
277        &mut self,
278        vid: ty::TyVid,
279    ) -> (ty::TyVid, TypeVariableValue<'tcx>) {
280        self.inlined_probe_with_vid(vid)
281    }
282
283    /// An always-inlined variant of `probe_with_root_vid`, for very hot call sites.
284    #[inline(always)]
285    pub(crate) fn inlined_probe_with_vid(
286        &mut self,
287        vid: ty::TyVid,
288    ) -> (ty::TyVid, TypeVariableValue<'tcx>) {
289        let (id, value) = self.eq_relations().inlined_probe_key_value(vid);
290        (id.vid, value)
291    }
292
293    #[inline]
294    fn eq_relations(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidEqKey<'tcx>> {
295        self.storage.eq_relations.with_log(self.undo_log)
296    }
297
298    #[inline]
299    fn sub_unification_table(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidSubKey> {
300        self.storage.sub_unification_table.with_log(self.undo_log)
301    }
302
303    /// Returns a range of the type variables created during the snapshot.
304    pub(crate) fn vars_since_snapshot(
305        &mut self,
306        value_count: usize,
307    ) -> (Range<TyVid>, Vec<TypeVariableOrigin>) {
308        let range = TyVid::from_usize(value_count)..TyVid::from_usize(self.num_vars());
309        (range.clone(), range.map(|index| self.var_origin(index)).collect())
310    }
311
312    /// Returns indices of all root variables that are not yet instantiated.
313    pub(crate) fn unresolved_root_variables(&mut self) -> Vec<ty::TyVid> {
314        (0..self.num_vars())
315            .map(ty::TyVid::from_usize)
316            .filter(|&vid| {
317                let (root, value) = self.probe_with_root_vid(vid);
318                root == vid && value.is_unknown()
319            })
320            .collect()
321    }
322}
323
324///////////////////////////////////////////////////////////////////////////
325
326/// These structs (a newtyped TyVid) are used as the unification key
327/// for the `eq_relations`; they carry a `TypeVariableValue` along
328/// with them.
329#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for TyVidEqKey<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for TyVidEqKey<'tcx> {
    #[inline]
    fn clone(&self) -> TyVidEqKey<'tcx> {
        let _: ::core::clone::AssertParamIsClone<ty::TyVid>;
        let _:
                ::core::clone::AssertParamIsClone<PhantomData<TypeVariableValue<'tcx>>>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for TyVidEqKey<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "TyVidEqKey",
            "vid", &self.vid, "phantom", &&self.phantom)
    }
}Debug, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for TyVidEqKey<'tcx> {
    #[inline]
    fn eq(&self, other: &TyVidEqKey<'tcx>) -> bool {
        self.vid == other.vid && self.phantom == other.phantom
    }
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for TyVidEqKey<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<ty::TyVid>;
        let _:
                ::core::cmp::AssertParamIsEq<PhantomData<TypeVariableValue<'tcx>>>;
    }
}Eq)]
330pub(crate) struct TyVidEqKey<'tcx> {
331    vid: ty::TyVid,
332
333    // in the table, we map each ty-vid to one of these:
334    phantom: PhantomData<TypeVariableValue<'tcx>>,
335}
336
337impl<'tcx> From<ty::TyVid> for TyVidEqKey<'tcx> {
338    #[inline] // make this function eligible for inlining - it is quite hot.
339    fn from(vid: ty::TyVid) -> Self {
340        TyVidEqKey { vid, phantom: PhantomData }
341    }
342}
343
344impl<'tcx> ut::UnifyKey for TyVidEqKey<'tcx> {
345    type Value = TypeVariableValue<'tcx>;
346    #[inline(always)]
347    fn index(&self) -> u32 {
348        self.vid.as_u32()
349    }
350    #[inline]
351    fn from_index(i: u32) -> Self {
352        TyVidEqKey::from(ty::TyVid::from_u32(i))
353    }
354    fn tag() -> &'static str {
355        "TyVidEqKey"
356    }
357    fn order_roots(a: Self, _: &Self::Value, b: Self, _: &Self::Value) -> Option<(Self, Self)> {
358        if a.vid.as_u32() < b.vid.as_u32() { Some((a, b)) } else { Some((b, a)) }
359    }
360}
361
362#[derive(#[automatically_derived]
impl ::core::marker::Copy for TyVidSubKey { }Copy, #[automatically_derived]
impl ::core::clone::Clone for TyVidSubKey {
    #[inline]
    fn clone(&self) -> TyVidSubKey {
        let _: ::core::clone::AssertParamIsClone<ty::TyVid>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for TyVidSubKey {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f, "TyVidSubKey",
            "vid", &&self.vid)
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for TyVidSubKey {
    #[inline]
    fn eq(&self, other: &TyVidSubKey) -> bool { self.vid == other.vid }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for TyVidSubKey {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<ty::TyVid>;
    }
}Eq)]
363pub(crate) struct TyVidSubKey {
364    vid: ty::TyVid,
365}
366
367impl From<ty::TyVid> for TyVidSubKey {
368    #[inline] // make this function eligible for inlining - it is quite hot.
369    fn from(vid: ty::TyVid) -> Self {
370        TyVidSubKey { vid }
371    }
372}
373
374impl ut::UnifyKey for TyVidSubKey {
375    type Value = ();
376    #[inline]
377    fn index(&self) -> u32 {
378        self.vid.as_u32()
379    }
380    #[inline]
381    fn from_index(i: u32) -> TyVidSubKey {
382        TyVidSubKey { vid: ty::TyVid::from_u32(i) }
383    }
384    fn tag() -> &'static str {
385        "TyVidSubKey"
386    }
387}
388
389impl<'tcx> ut::UnifyValue for TypeVariableValue<'tcx> {
390    type Error = ut::NoError;
391
392    fn unify_values(value1: &Self, value2: &Self) -> Result<Self, ut::NoError> {
393        match (value1, value2) {
394            // We never equate two type variables, both of which
395            // have known types. Instead, we recursively equate
396            // those types.
397            (&TypeVariableValue::Known { .. }, &TypeVariableValue::Known { .. }) => {
398                ::rustc_middle::util::bug::bug_fmt(format_args!("equating two type variables, both of which have known types"))bug!("equating two type variables, both of which have known types")
399            }
400
401            // If one side is known, prefer that one.
402            (&TypeVariableValue::Known { .. }, &TypeVariableValue::Unknown { .. }) => Ok(*value1),
403            (&TypeVariableValue::Unknown { .. }, &TypeVariableValue::Known { .. }) => Ok(*value2),
404
405            // If both sides are *unknown*, it hardly matters, does it?
406            (
407                &TypeVariableValue::Unknown { universe: universe1 },
408                &TypeVariableValue::Unknown { universe: universe2 },
409            ) => {
410                // If we unify two unbound variables, ?T and ?U, then whatever
411                // value they wind up taking (which must be the same value) must
412                // be nameable by both universes. Therefore, the resulting
413                // universe is the minimum of the two universes, because that is
414                // the one which contains the fewest names in scope.
415                let universe = cmp::min(universe1, universe2);
416                Ok(TypeVariableValue::Unknown { universe })
417            }
418        }
419    }
420}