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
16impl<'tcx> Rollback<sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>> for TypeVariableStorage<'tcx> {
17 fn reverse(&mut self, undo: sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>) {
18 self.eq_relations.reverse(undo)
19 }
20}
21
22#[derive(Clone, Default)]
23pub(crate) struct TypeVariableStorage<'tcx> {
24 values: IndexVec<TyVid, TypeVariableData>,
26 eq_relations: ut::UnificationTableStorage<TyVidEqKey<'tcx>>,
30}
31
32pub(crate) struct TypeVariableTable<'a, 'tcx> {
33 storage: &'a mut TypeVariableStorage<'tcx>,
34
35 undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
36}
37
38#[derive(Copy, Clone, Debug)]
39pub struct TypeVariableOrigin {
40 pub span: Span,
41 pub param_def_id: Option<DefId>,
45}
46
47#[derive(Clone)]
48pub(crate) struct TypeVariableData {
49 origin: TypeVariableOrigin,
50}
51
52#[derive(Copy, Clone, Debug)]
53pub(crate) enum TypeVariableValue<'tcx> {
54 Known { value: Ty<'tcx> },
55 Unknown { universe: ty::UniverseIndex },
56}
57
58impl<'tcx> TypeVariableValue<'tcx> {
59 pub(crate) fn known(&self) -> Option<Ty<'tcx>> {
62 match *self {
63 TypeVariableValue::Unknown { .. } => None,
64 TypeVariableValue::Known { value } => Some(value),
65 }
66 }
67
68 pub(crate) fn is_unknown(&self) -> bool {
69 match *self {
70 TypeVariableValue::Unknown { .. } => true,
71 TypeVariableValue::Known { .. } => false,
72 }
73 }
74}
75
76impl<'tcx> TypeVariableStorage<'tcx> {
77 #[inline]
78 pub(crate) fn with_log<'a>(
79 &'a mut self,
80 undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
81 ) -> TypeVariableTable<'a, 'tcx> {
82 TypeVariableTable { storage: self, undo_log }
83 }
84
85 #[inline]
86 pub(crate) fn eq_relations_ref(&self) -> &ut::UnificationTableStorage<TyVidEqKey<'tcx>> {
87 &self.eq_relations
88 }
89
90 pub(super) fn finalize_rollback(&mut self) {
91 debug_assert!(self.values.len() >= self.eq_relations.len());
92 self.values.truncate(self.eq_relations.len());
93 }
94}
95
96impl<'tcx> TypeVariableTable<'_, 'tcx> {
97 pub(crate) fn var_origin(&self, vid: ty::TyVid) -> TypeVariableOrigin {
102 self.storage.values[vid].origin
103 }
104
105 pub(crate) fn equate(&mut self, a: ty::TyVid, b: ty::TyVid) {
109 debug_assert!(self.probe(a).is_unknown());
110 debug_assert!(self.probe(b).is_unknown());
111 self.eq_relations().union(a, b);
112 }
113
114 pub(crate) fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) {
118 let vid = self.root_var(vid);
119 debug_assert!(!ty.is_ty_var(), "instantiating ty var with var: {vid:?} {ty:?}");
120 debug_assert!(self.probe(vid).is_unknown());
121 debug_assert!(
122 self.eq_relations().probe_value(vid).is_unknown(),
123 "instantiating type variable `{vid:?}` twice: new-value = {ty:?}, old-value={:?}",
124 self.eq_relations().probe_value(vid)
125 );
126 self.eq_relations().union_value(vid, TypeVariableValue::Known { value: ty });
127 }
128
129 pub(crate) fn new_var(
140 &mut self,
141 universe: ty::UniverseIndex,
142 origin: TypeVariableOrigin,
143 ) -> ty::TyVid {
144 let eq_key = self.eq_relations().new_key(TypeVariableValue::Unknown { universe });
145 let index = self.storage.values.push(TypeVariableData { origin });
146 debug_assert_eq!(eq_key.vid, index);
147
148 debug!("new_var(index={:?}, universe={:?}, origin={:?})", eq_key.vid, universe, origin);
149
150 index
151 }
152
153 pub(crate) fn num_vars(&self) -> usize {
155 self.storage.values.len()
156 }
157
158 pub(crate) fn root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
164 self.eq_relations().find(vid).vid
165 }
166
167 pub(crate) fn probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
170 self.inlined_probe(vid)
171 }
172
173 #[inline(always)]
175 pub(crate) fn inlined_probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
176 self.eq_relations().inlined_probe_value(vid)
177 }
178
179 #[inline]
180 fn eq_relations(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidEqKey<'tcx>> {
181 self.storage.eq_relations.with_log(self.undo_log)
182 }
183
184 pub(crate) fn vars_since_snapshot(
186 &mut self,
187 value_count: usize,
188 ) -> (Range<TyVid>, Vec<TypeVariableOrigin>) {
189 let range = TyVid::from_usize(value_count)..TyVid::from_usize(self.num_vars());
190 (range.clone(), range.map(|index| self.var_origin(index)).collect())
191 }
192
193 pub(crate) fn unresolved_variables(&mut self) -> Vec<ty::TyVid> {
196 (0..self.num_vars())
197 .filter_map(|i| {
198 let vid = ty::TyVid::from_usize(i);
199 match self.probe(vid) {
200 TypeVariableValue::Unknown { .. } => Some(vid),
201 TypeVariableValue::Known { .. } => None,
202 }
203 })
204 .collect()
205 }
206}
207
208#[derive(Copy, Clone, Debug, PartialEq, Eq)]
214pub(crate) struct TyVidEqKey<'tcx> {
215 vid: ty::TyVid,
216
217 phantom: PhantomData<TypeVariableValue<'tcx>>,
219}
220
221impl<'tcx> From<ty::TyVid> for TyVidEqKey<'tcx> {
222 #[inline] fn from(vid: ty::TyVid) -> Self {
224 TyVidEqKey { vid, phantom: PhantomData }
225 }
226}
227
228impl<'tcx> ut::UnifyKey for TyVidEqKey<'tcx> {
229 type Value = TypeVariableValue<'tcx>;
230 #[inline(always)]
231 fn index(&self) -> u32 {
232 self.vid.as_u32()
233 }
234 #[inline]
235 fn from_index(i: u32) -> Self {
236 TyVidEqKey::from(ty::TyVid::from_u32(i))
237 }
238 fn tag() -> &'static str {
239 "TyVidEqKey"
240 }
241}
242
243impl<'tcx> ut::UnifyValue for TypeVariableValue<'tcx> {
244 type Error = ut::NoError;
245
246 fn unify_values(value1: &Self, value2: &Self) -> Result<Self, ut::NoError> {
247 match (value1, value2) {
248 (&TypeVariableValue::Known { .. }, &TypeVariableValue::Known { .. }) => {
252 bug!("equating two type variables, both of which have known types")
253 }
254
255 (&TypeVariableValue::Known { .. }, &TypeVariableValue::Unknown { .. }) => Ok(*value1),
257 (&TypeVariableValue::Unknown { .. }, &TypeVariableValue::Known { .. }) => Ok(*value2),
258
259 (
261 &TypeVariableValue::Unknown { universe: universe1 },
262 &TypeVariableValue::Unknown { universe: universe2 },
263 ) => {
264 let universe = cmp::min(universe1, universe2);
270 Ok(TypeVariableValue::Unknown { universe })
271 }
272 }
273 }
274}