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 }
160161pub(crate) fn sub_unification_table_ref(&self) -> &ut::UnificationTableStorage<TyVidSubKey> {
162&self.sub_unification_table
163 }
164}
165166impl<'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.
171pub(crate) fn var_origin(&self, vid: ty::TyVid) -> TypeVariableOrigin {
172self.storage.values[vid].origin
173 }
174175/// Records that `a == b`.
176 ///
177 /// Precondition: neither `a` nor `b` are known.
178pub(crate) fn equate(&mut self, a: ty::TyVid, b: ty::TyVid) {
179if true {
if !self.probe(a).is_unknown() {
::core::panicking::panic("assertion failed: self.probe(a).is_unknown()")
};
};debug_assert!(self.probe(a).is_unknown());
180if true {
if !self.probe(b).is_unknown() {
::core::panicking::panic("assertion failed: self.probe(b).is_unknown()")
};
};debug_assert!(self.probe(b).is_unknown());
181self.eq_relations().union(a, b);
182self.sub_unification_table().union(a, b);
183 }
184185/// 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.
189pub(crate) fn sub_unify(&mut self, a: ty::TyVid, b: ty::TyVid) {
190if true {
if !self.probe(a).is_unknown() {
::core::panicking::panic("assertion failed: self.probe(a).is_unknown()")
};
};debug_assert!(self.probe(a).is_unknown());
191if true {
if !self.probe(b).is_unknown() {
::core::panicking::panic("assertion failed: self.probe(b).is_unknown()")
};
};debug_assert!(self.probe(b).is_unknown());
192self.sub_unification_table().union(a, b);
193 }
194195/// Instantiates `vid` with the type `ty`.
196 ///
197 /// Precondition: `vid` must not have been previously instantiated.
198pub(crate) fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) {
199let vid = self.root_var(vid);
200if 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:?}");
201if true {
if !self.probe(vid).is_unknown() {
::core::panicking::panic("assertion failed: self.probe(vid).is_unknown()")
};
};debug_assert!(self.probe(vid).is_unknown());
202if 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!(
203self.eq_relations().probe_value(vid).is_unknown(),
204"instantiating type variable `{vid:?}` twice: new-value = {ty:?}, old-value={:?}",
205self.eq_relations().probe_value(vid)
206 );
207self.eq_relations().union_value(vid, TypeVariableValue::Known { value: ty });
208 }
209210/// 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.
220pub(crate) fn new_var(
221&mut self,
222 universe: ty::UniverseIndex,
223 origin: TypeVariableOrigin,
224 ) -> ty::TyVid {
225let eq_key = self.eq_relations().new_key(TypeVariableValue::Unknown { universe });
226227let sub_key = self.sub_unification_table().new_key(());
228if 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);
229230let index = self.storage.values.push(TypeVariableData { origin });
231if 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);
232233{
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);
234235index236 }
237238/// Returns the number of type variables created thus far.
239pub(crate) fn num_vars(&self) -> usize {
240self.storage.values.len()
241 }
242243/// 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).
248pub(crate) fn root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
249self.eq_relations().find(vid).vid
250 }
251252/// 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.
258pub(crate) fn sub_unification_table_root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
259self.sub_unification_table().find(vid).vid
260 }
261262/// Retrieves the type to which `vid` has been instantiated, if
263 /// any.
264pub(crate) fn probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
265self.inlined_probe(vid)
266 }
267268/// An always-inlined variant of `probe`, for very hot call sites.
269#[inline(always)]
270pub(crate) fn inlined_probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
271self.eq_relations().inlined_probe_value(vid)
272 }
273274/// Retrieves the type to which `vid` has been instantiated, if
275 /// any, along with the root `vid`.
276pub(crate) fn probe_with_root_vid(
277&mut self,
278 vid: ty::TyVid,
279 ) -> (ty::TyVid, TypeVariableValue<'tcx>) {
280self.inlined_probe_with_vid(vid)
281 }
282283/// An always-inlined variant of `probe_with_root_vid`, for very hot call sites.
284#[inline(always)]
285pub(crate) fn inlined_probe_with_vid(
286&mut self,
287 vid: ty::TyVid,
288 ) -> (ty::TyVid, TypeVariableValue<'tcx>) {
289let (id, value) = self.eq_relations().inlined_probe_key_value(vid);
290 (id.vid, value)
291 }
292293#[inline]
294fn eq_relations(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidEqKey<'tcx>> {
295self.storage.eq_relations.with_log(self.undo_log)
296 }
297298#[inline]
299fn sub_unification_table(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidSubKey> {
300self.storage.sub_unification_table.with_log(self.undo_log)
301 }
302303/// Returns a range of the type variables created during the snapshot.
304pub(crate) fn vars_since_snapshot(
305&mut self,
306 value_count: usize,
307 ) -> (Range<TyVid>, Vec<TypeVariableOrigin>) {
308let range = TyVid::from_usize(value_count)..TyVid::from_usize(self.num_vars());
309 (range.clone(), range.map(|index| self.var_origin(index)).collect())
310 }
311312/// Returns indices of all root variables that are not yet instantiated.
313pub(crate) fn unresolved_root_variables(&mut self) -> Vec<ty::TyVid> {
314 (0..self.num_vars())
315 .map(ty::TyVid::from_usize)
316 .filter(|&vid| {
317let (root, value) = self.probe_with_root_vid(vid);
318root == vid && value.is_unknown()
319 })
320 .collect()
321 }
322}
323324///////////////////////////////////////////////////////////////////////////
325326/// 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,
332333// in the table, we map each ty-vid to one of these:
334phantom: PhantomData<TypeVariableValue<'tcx>>,
335}
336337impl<'tcx> From<ty::TyVid> for TyVidEqKey<'tcx> {
338#[inline] // make this function eligible for inlining - it is quite hot.
339fn from(vid: ty::TyVid) -> Self {
340TyVidEqKey { vid, phantom: PhantomData }
341 }
342}
343344impl<'tcx> ut::UnifyKeyfor TyVidEqKey<'tcx> {
345type Value = TypeVariableValue<'tcx>;
346#[inline(always)]
347fn index(&self) -> u32 {
348self.vid.as_u32()
349 }
350#[inline]
351fn from_index(i: u32) -> Self {
352TyVidEqKey::from(ty::TyVid::from_u32(i))
353 }
354fn tag() -> &'static str {
355"TyVidEqKey"
356}
357fn order_roots(a: Self, _: &Self::Value, b: Self, _: &Self::Value) -> Option<(Self, Self)> {
358if a.vid.as_u32() < b.vid.as_u32() { Some((a, b)) } else { Some((b, a)) }
359 }
360}
361362#[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}
366367impl From<ty::TyVid> for TyVidSubKey {
368#[inline] // make this function eligible for inlining - it is quite hot.
369fn from(vid: ty::TyVid) -> Self {
370TyVidSubKey { vid }
371 }
372}
373374impl ut::UnifyKeyfor TyVidSubKey {
375type Value = ();
376#[inline]
377fn index(&self) -> u32 {
378self.vid.as_u32()
379 }
380#[inline]
381fn from_index(i: u32) -> TyVidSubKey {
382TyVidSubKey { vid: ty::TyVid::from_u32(i) }
383 }
384fn tag() -> &'static str {
385"TyVidSubKey"
386}
387}
388389impl<'tcx> ut::UnifyValuefor TypeVariableValue<'tcx> {
390type Error = ut::NoError;
391392fn unify_values(value1: &Self, value2: &Self) -> Result<Self, ut::NoError> {
393match (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 }
400401// If one side is known, prefer that one.
402(&TypeVariableValue::Known { .. }, &TypeVariableValue::Unknown { .. }) => Ok(*value1),
403 (&TypeVariableValue::Unknown { .. }, &TypeVariableValue::Known { .. }) => Ok(*value2),
404405// 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.
415let universe = cmp::min(universe1, universe2);
416Ok(TypeVariableValue::Unknown { universe })
417 }
418 }
419 }
420}