1use std::cmp;
2use std::marker::PhantomData;
3use std::ops::Range;
45use rustc_data_structures::undo_log::Rollback;
6use rustc_data_structures::{snapshot_vecas sv, unifyas 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;
1415use crate::infer::InferCtxtUndoLogs;
1617/// 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}
2324/// Convert from a specific kind of undo to the more general UndoLog
25impl<'tcx> From<sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>> for UndoLog<'tcx> {
26fn from(l: sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>) -> Self {
27 UndoLog::EqRelation(l)
28 }
29}
3031/// Convert from a specific kind of undo to the more general UndoLog
32impl<'tcx> From<sv::UndoLog<ut::Delegate<TyVidSubKey>>> for UndoLog<'tcx> {
33fn from(l: sv::UndoLog<ut::Delegate<TyVidSubKey>>) -> Self {
34 UndoLog::SubRelation(l)
35 }
36}
3738impl<'tcx> Rollback<sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>> for TypeVariableStorage<'tcx> {
39fn reverse(&mut self, undo: sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>) {
40self.eq_relations.reverse(undo)
41 }
42}
4344impl<'tcx> Rollback<sv::UndoLog<ut::Delegate<TyVidSubKey>>> for TypeVariableStorage<'tcx> {
45fn reverse(&mut self, undo: sv::UndoLog<ut::Delegate<TyVidSubKey>>) {
46self.sub_unification_table.reverse(undo)
47 }
48}
4950impl<'tcx> Rollback<UndoLog<'tcx>> for TypeVariableStorage<'tcx> {
51fn reverse(&mut self, undo: UndoLog<'tcx>) {
52match undo {
53 UndoLog::EqRelation(undo) => self.eq_relations.reverse(undo),
54 UndoLog::SubRelation(undo) => self.sub_unification_table.reverse(undo),
55 }
56 }
57}
5859#[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.
62values: 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.
66eq_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.
85sub_unification_table: ut::UnificationTableStorage<TyVidSubKey>,
86}
8788pub(crate) struct TypeVariableTable<'a, 'tcx> {
89 storage: &'a mut TypeVariableStorage<'tcx>,
9091 undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
92}
9394#[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 {
96pub span: Span,
97/// `DefId` of the type parameter this was instantiated for, if any.
98 ///
99 /// This should only be used for diagnostics.
100pub param_def_id: Option<DefId>,
101}
102103#[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 {
105pub span: Span,
106107/// `HirId` to lint at for this float variable, if any.
108 ///
109 /// This should only be used for diagnostics.
110pub lint_id: Option<HirId>,
111}
112113#[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}
117118#[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}
123124impl<'tcx> TypeVariableValue<'tcx> {
125/// If this value is known, returns the type it is known to be.
126 /// Otherwise, `None`.
127pub(crate) fn known(&self) -> Option<Ty<'tcx>> {
128match *self {
129 TypeVariableValue::Unknown { .. } => None,
130 TypeVariableValue::Known { value } => Some(value),
131 }
132 }
133134pub(crate) fn is_unknown(&self) -> bool {
135match *self {
136 TypeVariableValue::Unknown { .. } => true,
137 TypeVariableValue::Known { .. } => false,
138 }
139 }
140}
141142impl<'tcx> TypeVariableStorage<'tcx> {
143#[inline]
144pub(crate) fn with_log<'a>(
145&'a mut self,
146 undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
147 ) -> TypeVariableTable<'a, 'tcx> {
148TypeVariableTable { storage: self, undo_log }
149 }
150151#[inline]
152pub(crate) fn eq_relations_ref(&self) -> &ut::UnificationTableStorage<TyVidEqKey<'tcx>> {
153&self.eq_relations
154 }
155156pub(super) fn finalize_rollback(&mut self) {
157if 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());
158self.values.truncate(self.eq_relations.len());
159 }
160}
161162impl<'tcx> TypeVariableTable<'_, 'tcx> {
163/// Returns the origin that was given when `vid` was created.
164 ///
165 /// Note that this function does not return care whether
166 /// `vid` has been unified with something else or not.
167pub(crate) fn var_origin(&self, vid: ty::TyVid) -> TypeVariableOrigin {
168self.storage.values[vid].origin
169 }
170171/// Records that `a == b`.
172 ///
173 /// Precondition: neither `a` nor `b` are known.
174pub(crate) fn equate(&mut self, a: ty::TyVid, b: ty::TyVid) {
175if true {
if !self.probe(a).is_unknown() {
::core::panicking::panic("assertion failed: self.probe(a).is_unknown()")
};
};debug_assert!(self.probe(a).is_unknown());
176if true {
if !self.probe(b).is_unknown() {
::core::panicking::panic("assertion failed: self.probe(b).is_unknown()")
};
};debug_assert!(self.probe(b).is_unknown());
177self.eq_relations().union(a, b);
178self.sub_unification_table().union(a, b);
179 }
180181/// Records that `a` and `b` are related via subtyping. We don't track
182 /// which of the two is the subtype.
183 ///
184 /// Precondition: neither `a` nor `b` are known.
185pub(crate) fn sub_unify(&mut self, a: ty::TyVid, b: ty::TyVid) {
186if true {
if !self.probe(a).is_unknown() {
::core::panicking::panic("assertion failed: self.probe(a).is_unknown()")
};
};debug_assert!(self.probe(a).is_unknown());
187if true {
if !self.probe(b).is_unknown() {
::core::panicking::panic("assertion failed: self.probe(b).is_unknown()")
};
};debug_assert!(self.probe(b).is_unknown());
188self.sub_unification_table().union(a, b);
189 }
190191/// Instantiates `vid` with the type `ty`.
192 ///
193 /// Precondition: `vid` must not have been previously instantiated.
194pub(crate) fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) {
195let vid = self.root_var(vid);
196if 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:?}");
197if true {
if !self.probe(vid).is_unknown() {
::core::panicking::panic("assertion failed: self.probe(vid).is_unknown()")
};
};debug_assert!(self.probe(vid).is_unknown());
198if 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!(
199self.eq_relations().probe_value(vid).is_unknown(),
200"instantiating type variable `{vid:?}` twice: new-value = {ty:?}, old-value={:?}",
201self.eq_relations().probe_value(vid)
202 );
203self.eq_relations().union_value(vid, TypeVariableValue::Known { value: ty });
204 }
205206/// Creates a new type variable.
207 ///
208 /// - `diverging`: indicates if this is a "diverging" type
209 /// variable, e.g., one created as the type of a `return`
210 /// expression. The code in this module doesn't care if a
211 /// variable is diverging, but the main Rust type-checker will
212 /// sometimes "unify" such variables with the `!` or `()` types.
213 /// - `origin`: indicates *why* the type variable was created.
214 /// The code in this module doesn't care, but it can be useful
215 /// for improving error messages.
216pub(crate) fn new_var(
217&mut self,
218 universe: ty::UniverseIndex,
219 origin: TypeVariableOrigin,
220 ) -> ty::TyVid {
221let eq_key = self.eq_relations().new_key(TypeVariableValue::Unknown { universe });
222223let sub_key = self.sub_unification_table().new_key(());
224if 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);
225226let index = self.storage.values.push(TypeVariableData { origin });
227if 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);
228229{
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:229",
"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(229u32),
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("new_var(index={0:?}, universe={1:?}, origin={2:?})",
eq_key.vid, universe, origin) as &dyn Value))])
});
} else { ; }
};debug!("new_var(index={:?}, universe={:?}, origin={:?})", eq_key.vid, universe, origin);
230231index232 }
233234/// Returns the number of type variables created thus far.
235pub(crate) fn num_vars(&self) -> usize {
236self.storage.values.len()
237 }
238239/// Returns the "root" variable of `vid` in the `eq_relations`
240 /// equivalence table. All type variables that have been equated
241 /// will yield the same root variable (per the union-find
242 /// algorithm), so `root_var(a) == root_var(b)` implies that `a ==
243 /// b` (transitively).
244pub(crate) fn root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
245self.eq_relations().find(vid).vid
246 }
247248/// Returns the "root" variable of `vid` in the `sub_unification_table`
249 /// equivalence table. All type variables that have been are related via
250 /// equality or subtyping will yield the same root variable (per the
251 /// union-find algorithm), so `sub_unification_table_root_var(a)
252 /// == sub_unification_table_root_var(b)` implies that:
253 /// ```text
254 /// exists X. (a <: X || X <: a) && (b <: X || X <: b)
255 /// ```
256pub(crate) fn sub_unification_table_root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
257self.sub_unification_table().find(vid).vid
258 }
259260/// Retrieves the type to which `vid` has been instantiated, if
261 /// any.
262pub(crate) fn probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
263self.inlined_probe(vid)
264 }
265266/// An always-inlined variant of `probe`, for very hot call sites.
267#[inline(always)]
268pub(crate) fn inlined_probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
269self.eq_relations().inlined_probe_value(vid)
270 }
271272#[inline]
273fn eq_relations(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidEqKey<'tcx>> {
274self.storage.eq_relations.with_log(self.undo_log)
275 }
276277#[inline]
278fn sub_unification_table(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidSubKey> {
279self.storage.sub_unification_table.with_log(self.undo_log)
280 }
281282/// Returns a range of the type variables created during the snapshot.
283pub(crate) fn vars_since_snapshot(
284&mut self,
285 value_count: usize,
286 ) -> (Range<TyVid>, Vec<TypeVariableOrigin>) {
287let range = TyVid::from_usize(value_count)..TyVid::from_usize(self.num_vars());
288 (range.clone(), range.map(|index| self.var_origin(index)).collect())
289 }
290291/// Returns indices of all variables that are not yet
292 /// instantiated.
293pub(crate) fn unresolved_variables(&mut self) -> Vec<ty::TyVid> {
294 (0..self.num_vars())
295 .filter_map(|i| {
296let vid = ty::TyVid::from_usize(i);
297match self.probe(vid) {
298 TypeVariableValue::Unknown { .. } => Some(vid),
299 TypeVariableValue::Known { .. } => None,
300 }
301 })
302 .collect()
303 }
304}
305306///////////////////////////////////////////////////////////////////////////
307308/// These structs (a newtyped TyVid) are used as the unification key
309/// for the `eq_relations`; they carry a `TypeVariableValue` along
310/// with them.
311#[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)]
312pub(crate) struct TyVidEqKey<'tcx> {
313 vid: ty::TyVid,
314315// in the table, we map each ty-vid to one of these:
316phantom: PhantomData<TypeVariableValue<'tcx>>,
317}
318319impl<'tcx> From<ty::TyVid> for TyVidEqKey<'tcx> {
320#[inline] // make this function eligible for inlining - it is quite hot.
321fn from(vid: ty::TyVid) -> Self {
322TyVidEqKey { vid, phantom: PhantomData }
323 }
324}
325326impl<'tcx> ut::UnifyKeyfor TyVidEqKey<'tcx> {
327type Value = TypeVariableValue<'tcx>;
328#[inline(always)]
329fn index(&self) -> u32 {
330self.vid.as_u32()
331 }
332#[inline]
333fn from_index(i: u32) -> Self {
334TyVidEqKey::from(ty::TyVid::from_u32(i))
335 }
336fn tag() -> &'static str {
337"TyVidEqKey"
338}
339fn order_roots(a: Self, _: &Self::Value, b: Self, _: &Self::Value) -> Option<(Self, Self)> {
340if a.vid.as_u32() < b.vid.as_u32() { Some((a, b)) } else { Some((b, a)) }
341 }
342}
343344#[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)]
345pub(crate) struct TyVidSubKey {
346 vid: ty::TyVid,
347}
348349impl From<ty::TyVid> for TyVidSubKey {
350#[inline] // make this function eligible for inlining - it is quite hot.
351fn from(vid: ty::TyVid) -> Self {
352TyVidSubKey { vid }
353 }
354}
355356impl ut::UnifyKeyfor TyVidSubKey {
357type Value = ();
358#[inline]
359fn index(&self) -> u32 {
360self.vid.as_u32()
361 }
362#[inline]
363fn from_index(i: u32) -> TyVidSubKey {
364TyVidSubKey { vid: ty::TyVid::from_u32(i) }
365 }
366fn tag() -> &'static str {
367"TyVidSubKey"
368}
369}
370371impl<'tcx> ut::UnifyValuefor TypeVariableValue<'tcx> {
372type Error = ut::NoError;
373374fn unify_values(value1: &Self, value2: &Self) -> Result<Self, ut::NoError> {
375match (value1, value2) {
376// We never equate two type variables, both of which
377 // have known types. Instead, we recursively equate
378 // those types.
379(&TypeVariableValue::Known { .. }, &TypeVariableValue::Known { .. }) => {
380::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")381 }
382383// If one side is known, prefer that one.
384(&TypeVariableValue::Known { .. }, &TypeVariableValue::Unknown { .. }) => Ok(*value1),
385 (&TypeVariableValue::Unknown { .. }, &TypeVariableValue::Known { .. }) => Ok(*value2),
386387// If both sides are *unknown*, it hardly matters, does it?
388(
389&TypeVariableValue::Unknown { universe: universe1 },
390&TypeVariableValue::Unknown { universe: universe2 },
391 ) => {
392// If we unify two unbound variables, ?T and ?U, then whatever
393 // value they wind up taking (which must be the same value) must
394 // be nameable by both universes. Therefore, the resulting
395 // universe is the minimum of the two universes, because that is
396 // the one which contains the fewest names in scope.
397let universe = cmp::min(universe1, universe2);
398Ok(TypeVariableValue::Unknown { universe })
399 }
400 }
401 }
402}