]> git.lizzy.rs Git - rust.git/blob - src/librustc_infer/infer/type_variable.rs
Rollup merge of #71731 - mark-i-m:guide-toolstate-off-for-now, r=kennytm
[rust.git] / src / librustc_infer / infer / type_variable.rs
1 use rustc_hir::def_id::DefId;
2 use rustc_middle::ty::{self, Ty, TyVid};
3 use rustc_span::symbol::Symbol;
4 use rustc_span::Span;
5
6 use crate::infer::InferCtxtUndoLogs;
7
8 use rustc_data_structures::snapshot_vec as sv;
9 use rustc_data_structures::unify as ut;
10 use std::cmp;
11 use std::marker::PhantomData;
12 use std::ops::Range;
13
14 use rustc_data_structures::undo_log::{Rollback, UndoLogs};
15
16 /// Represents a single undo-able action that affects a type inference variable.
17 pub(crate) enum UndoLog<'tcx> {
18     EqRelation(sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>),
19     SubRelation(sv::UndoLog<ut::Delegate<ty::TyVid>>),
20     Values(sv::UndoLog<Delegate>),
21 }
22
23 /// Convert from a specific kind of undo to the more general UndoLog
24 impl<'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
31 impl<'tcx> From<sv::UndoLog<ut::Delegate<ty::TyVid>>> for UndoLog<'tcx> {
32     fn from(l: sv::UndoLog<ut::Delegate<ty::TyVid>>) -> Self {
33         UndoLog::SubRelation(l)
34     }
35 }
36
37 /// Convert from a specific kind of undo to the more general UndoLog
38 impl<'tcx> From<sv::UndoLog<Delegate>> for UndoLog<'tcx> {
39     fn from(l: sv::UndoLog<Delegate>) -> Self {
40         UndoLog::Values(l)
41     }
42 }
43
44 /// Convert from a specific kind of undo to the more general UndoLog
45 impl<'tcx> From<Instantiate> for UndoLog<'tcx> {
46     fn from(l: Instantiate) -> Self {
47         UndoLog::Values(sv::UndoLog::Other(l))
48     }
49 }
50
51 impl<'tcx> Rollback<UndoLog<'tcx>> for TypeVariableStorage<'tcx> {
52     fn reverse(&mut self, undo: UndoLog<'tcx>) {
53         match undo {
54             UndoLog::EqRelation(undo) => self.eq_relations.reverse(undo),
55             UndoLog::SubRelation(undo) => self.sub_relations.reverse(undo),
56             UndoLog::Values(undo) => self.values.reverse(undo),
57         }
58     }
59 }
60
61 pub struct TypeVariableStorage<'tcx> {
62     values: sv::SnapshotVecStorage<Delegate>,
63
64     /// Two variables are unified in `eq_relations` when we have a
65     /// constraint `?X == ?Y`. This table also stores, for each key,
66     /// the known value.
67     eq_relations: ut::UnificationTableStorage<TyVidEqKey<'tcx>>,
68
69     /// Two variables are unified in `sub_relations` when we have a
70     /// constraint `?X <: ?Y` *or* a constraint `?Y <: ?X`. This second
71     /// table exists only to help with the occurs check. In particular,
72     /// we want to report constraints like these as an occurs check
73     /// violation:
74     ///
75     ///     ?1 <: ?3
76     ///     Box<?3> <: ?1
77     ///
78     /// This works because `?1` and `?3` are unified in the
79     /// `sub_relations` relation (not in `eq_relations`). Then when we
80     /// process the `Box<?3> <: ?1` constraint, we do an occurs check
81     /// on `Box<?3>` and find a potential cycle.
82     ///
83     /// This is reasonable because, in Rust, subtypes have the same
84     /// "skeleton" and hence there is no possible type such that
85     /// (e.g.)  `Box<?3> <: ?3` for any `?3`.
86     sub_relations: ut::UnificationTableStorage<ty::TyVid>,
87 }
88
89 pub struct TypeVariableTable<'a, 'tcx> {
90     values: &'a mut sv::SnapshotVecStorage<Delegate>,
91
92     eq_relations: &'a mut ut::UnificationTableStorage<TyVidEqKey<'tcx>>,
93
94     sub_relations: &'a mut ut::UnificationTableStorage<ty::TyVid>,
95
96     undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
97 }
98
99 #[derive(Copy, Clone, Debug)]
100 pub struct TypeVariableOrigin {
101     pub kind: TypeVariableOriginKind,
102     pub span: Span,
103 }
104
105 /// Reasons to create a type inference variable
106 #[derive(Copy, Clone, Debug)]
107 pub enum TypeVariableOriginKind {
108     MiscVariable,
109     NormalizeProjectionType,
110     TypeInference,
111     TypeParameterDefinition(Symbol, Option<DefId>),
112
113     /// One of the upvars or closure kind parameters in a `ClosureSubsts`
114     /// (before it has been determined).
115     // FIXME(eddyb) distinguish upvar inference variables from the rest.
116     ClosureSynthetic,
117     SubstitutionPlaceholder,
118     AutoDeref,
119     AdjustmentType,
120     DivergingFn,
121     LatticeVariable,
122 }
123
124 pub(crate) struct TypeVariableData {
125     origin: TypeVariableOrigin,
126     diverging: bool,
127 }
128
129 #[derive(Copy, Clone, Debug)]
130 pub enum TypeVariableValue<'tcx> {
131     Known { value: Ty<'tcx> },
132     Unknown { universe: ty::UniverseIndex },
133 }
134
135 impl<'tcx> TypeVariableValue<'tcx> {
136     /// If this value is known, returns the type it is known to be.
137     /// Otherwise, `None`.
138     pub fn known(&self) -> Option<Ty<'tcx>> {
139         match *self {
140             TypeVariableValue::Unknown { .. } => None,
141             TypeVariableValue::Known { value } => Some(value),
142         }
143     }
144
145     pub fn is_unknown(&self) -> bool {
146         match *self {
147             TypeVariableValue::Unknown { .. } => true,
148             TypeVariableValue::Known { .. } => false,
149         }
150     }
151 }
152
153 pub(crate) struct Instantiate {
154     vid: ty::TyVid,
155 }
156
157 pub(crate) struct Delegate;
158
159 impl<'tcx> TypeVariableStorage<'tcx> {
160     pub fn new() -> TypeVariableStorage<'tcx> {
161         TypeVariableStorage {
162             values: sv::SnapshotVecStorage::new(),
163             eq_relations: ut::UnificationTableStorage::new(),
164             sub_relations: ut::UnificationTableStorage::new(),
165         }
166     }
167
168     pub(crate) fn with_log<'a>(
169         &'a mut self,
170         undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
171     ) -> TypeVariableTable<'a, 'tcx> {
172         let TypeVariableStorage { values, eq_relations, sub_relations } = self;
173         TypeVariableTable { values, eq_relations, sub_relations, undo_log }
174     }
175 }
176
177 impl<'tcx> TypeVariableTable<'_, 'tcx> {
178     /// Returns the diverges flag given when `vid` was created.
179     ///
180     /// Note that this function does not return care whether
181     /// `vid` has been unified with something else or not.
182     pub fn var_diverges(&self, vid: ty::TyVid) -> bool {
183         self.values.get(vid.index as usize).diverging
184     }
185
186     /// Returns the origin that was given when `vid` was created.
187     ///
188     /// Note that this function does not return care whether
189     /// `vid` has been unified with something else or not.
190     pub fn var_origin(&self, vid: ty::TyVid) -> &TypeVariableOrigin {
191         &self.values.get(vid.index as usize).origin
192     }
193
194     /// Records that `a == b`, depending on `dir`.
195     ///
196     /// Precondition: neither `a` nor `b` are known.
197     pub fn equate(&mut self, a: ty::TyVid, b: ty::TyVid) {
198         debug_assert!(self.probe(a).is_unknown());
199         debug_assert!(self.probe(b).is_unknown());
200         self.eq_relations().union(a, b);
201         self.sub_relations().union(a, b);
202     }
203
204     /// Records that `a <: b`, depending on `dir`.
205     ///
206     /// Precondition: neither `a` nor `b` are known.
207     pub fn sub(&mut self, a: ty::TyVid, b: ty::TyVid) {
208         debug_assert!(self.probe(a).is_unknown());
209         debug_assert!(self.probe(b).is_unknown());
210         self.sub_relations().union(a, b);
211     }
212
213     /// Instantiates `vid` with the type `ty`.
214     ///
215     /// Precondition: `vid` must not have been previously instantiated.
216     pub fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) {
217         let vid = self.root_var(vid);
218         debug_assert!(self.probe(vid).is_unknown());
219         debug_assert!(
220             self.eq_relations().probe_value(vid).is_unknown(),
221             "instantiating type variable `{:?}` twice: new-value = {:?}, old-value={:?}",
222             vid,
223             ty,
224             self.eq_relations().probe_value(vid)
225         );
226         self.eq_relations().union_value(vid, TypeVariableValue::Known { value: ty });
227
228         // Hack: we only need this so that `types_escaping_snapshot`
229         // can see what has been unified; see the Delegate impl for
230         // more details.
231         self.undo_log.push(Instantiate { vid });
232     }
233
234     /// Creates a new type variable.
235     ///
236     /// - `diverging`: indicates if this is a "diverging" type
237     ///   variable, e.g.,  one created as the type of a `return`
238     ///   expression. The code in this module doesn't care if a
239     ///   variable is diverging, but the main Rust type-checker will
240     ///   sometimes "unify" such variables with the `!` or `()` types.
241     /// - `origin`: indicates *why* the type variable was created.
242     ///   The code in this module doesn't care, but it can be useful
243     ///   for improving error messages.
244     pub fn new_var(
245         &mut self,
246         universe: ty::UniverseIndex,
247         diverging: bool,
248         origin: TypeVariableOrigin,
249     ) -> ty::TyVid {
250         let eq_key = self.eq_relations().new_key(TypeVariableValue::Unknown { universe });
251
252         let sub_key = self.sub_relations().new_key(());
253         assert_eq!(eq_key.vid, sub_key);
254
255         let index = self.values().push(TypeVariableData { origin, diverging });
256         assert_eq!(eq_key.vid.index, index as u32);
257
258         debug!(
259             "new_var(index={:?}, universe={:?}, diverging={:?}, origin={:?}",
260             eq_key.vid, universe, diverging, origin,
261         );
262
263         eq_key.vid
264     }
265
266     /// Returns the number of type variables created thus far.
267     pub fn num_vars(&self) -> usize {
268         self.values.len()
269     }
270
271     /// Returns the "root" variable of `vid` in the `eq_relations`
272     /// equivalence table. All type variables that have been equated
273     /// will yield the same root variable (per the union-find
274     /// algorithm), so `root_var(a) == root_var(b)` implies that `a ==
275     /// b` (transitively).
276     pub fn root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
277         self.eq_relations().find(vid).vid
278     }
279
280     /// Returns the "root" variable of `vid` in the `sub_relations`
281     /// equivalence table. All type variables that have been are
282     /// related via equality or subtyping will yield the same root
283     /// variable (per the union-find algorithm), so `sub_root_var(a)
284     /// == sub_root_var(b)` implies that:
285     ///
286     ///     exists X. (a <: X || X <: a) && (b <: X || X <: b)
287     pub fn sub_root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
288         self.sub_relations().find(vid)
289     }
290
291     /// Returns `true` if `a` and `b` have same "sub-root" (i.e., exists some
292     /// type X such that `forall i in {a, b}. (i <: X || X <: i)`.
293     pub fn sub_unified(&mut self, a: ty::TyVid, b: ty::TyVid) -> bool {
294         self.sub_root_var(a) == self.sub_root_var(b)
295     }
296
297     /// Retrieves the type to which `vid` has been instantiated, if
298     /// any.
299     pub fn probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
300         self.inlined_probe(vid)
301     }
302
303     /// An always-inlined variant of `probe`, for very hot call sites.
304     #[inline(always)]
305     pub fn inlined_probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
306         self.eq_relations().inlined_probe_value(vid)
307     }
308
309     /// If `t` is a type-inference variable, and it has been
310     /// instantiated, then return the with which it was
311     /// instantiated. Otherwise, returns `t`.
312     pub fn replace_if_possible(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
313         match t.kind {
314             ty::Infer(ty::TyVar(v)) => match self.probe(v) {
315                 TypeVariableValue::Unknown { .. } => t,
316                 TypeVariableValue::Known { value } => value,
317             },
318             _ => t,
319         }
320     }
321
322     fn values(
323         &mut self,
324     ) -> sv::SnapshotVec<Delegate, &mut Vec<TypeVariableData>, &mut InferCtxtUndoLogs<'tcx>> {
325         self.values.with_log(self.undo_log)
326     }
327
328     fn eq_relations(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidEqKey<'tcx>> {
329         self.eq_relations.with_log(self.undo_log)
330     }
331
332     fn sub_relations(&mut self) -> super::UnificationTable<'_, 'tcx, ty::TyVid> {
333         self.sub_relations.with_log(self.undo_log)
334     }
335
336     /// Returns a range of the type variables created during the snapshot.
337     pub fn vars_since_snapshot(
338         &mut self,
339         value_count: usize,
340     ) -> (Range<TyVid>, Vec<TypeVariableOrigin>) {
341         let range = TyVid { index: value_count as u32 }..TyVid { index: self.num_vars() as u32 };
342         (
343             range.start..range.end,
344             (range.start.index..range.end.index)
345                 .map(|index| self.values.get(index as usize).origin)
346                 .collect(),
347         )
348     }
349
350     /// Finds the set of type variables that existed *before* `s`
351     /// but which have only been unified since `s` started, and
352     /// return the types with which they were unified. So if we had
353     /// a type variable `V0`, then we started the snapshot, then we
354     /// created a type variable `V1`, unified `V0` with `T0`, and
355     /// unified `V1` with `T1`, this function would return `{T0}`.
356     pub fn types_escaping_snapshot(&mut self, s: &super::Snapshot<'tcx>) -> Vec<Ty<'tcx>> {
357         let mut new_elem_threshold = u32::MAX;
358         let mut escaping_types = Vec::new();
359         let actions_since_snapshot = self.undo_log.actions_since_snapshot(s);
360         debug!("actions_since_snapshot.len() = {}", actions_since_snapshot.len());
361         for i in 0..actions_since_snapshot.len() {
362             let actions_since_snapshot = self.undo_log.actions_since_snapshot(s);
363             match actions_since_snapshot[i] {
364                 super::UndoLog::TypeVariables(UndoLog::Values(sv::UndoLog::NewElem(index))) => {
365                     // if any new variables were created during the
366                     // snapshot, remember the lower index (which will
367                     // always be the first one we see). Note that this
368                     // action must precede those variables being
369                     // specified.
370                     new_elem_threshold = cmp::min(new_elem_threshold, index as u32);
371                     debug!("NewElem({}) new_elem_threshold={}", index, new_elem_threshold);
372                 }
373
374                 super::UndoLog::TypeVariables(UndoLog::Values(sv::UndoLog::Other(
375                     Instantiate { vid, .. },
376                 ))) => {
377                     if vid.index < new_elem_threshold {
378                         // quick check to see if this variable was
379                         // created since the snapshot started or not.
380                         let mut eq_relations = ut::UnificationTable::with_log(
381                             &mut *self.eq_relations,
382                             &mut *self.undo_log,
383                         );
384                         let escaping_type = match eq_relations.probe_value(vid) {
385                             TypeVariableValue::Unknown { .. } => bug!(),
386                             TypeVariableValue::Known { value } => value,
387                         };
388                         escaping_types.push(escaping_type);
389                     }
390                     debug!("SpecifyVar({:?}) new_elem_threshold={}", vid, new_elem_threshold);
391                 }
392
393                 _ => {}
394             }
395         }
396
397         escaping_types
398     }
399
400     /// Returns indices of all variables that are not yet
401     /// instantiated.
402     pub fn unsolved_variables(&mut self) -> Vec<ty::TyVid> {
403         (0..self.values.len())
404             .filter_map(|i| {
405                 let vid = ty::TyVid { index: i as u32 };
406                 match self.probe(vid) {
407                     TypeVariableValue::Unknown { .. } => Some(vid),
408                     TypeVariableValue::Known { .. } => None,
409                 }
410             })
411             .collect()
412     }
413 }
414
415 impl sv::SnapshotVecDelegate for Delegate {
416     type Value = TypeVariableData;
417     type Undo = Instantiate;
418
419     fn reverse(_values: &mut Vec<TypeVariableData>, _action: Instantiate) {
420         // We don't actually have to *do* anything to reverse an
421         // instantiation; the value for a variable is stored in the
422         // `eq_relations` and hence its rollback code will handle
423         // it. In fact, we could *almost* just remove the
424         // `SnapshotVec` entirely, except that we would have to
425         // reproduce *some* of its logic, since we want to know which
426         // type variables have been instantiated since the snapshot
427         // was started, so we can implement `types_escaping_snapshot`.
428         //
429         // (If we extended the `UnificationTable` to let us see which
430         // values have been unified and so forth, that might also
431         // suffice.)
432     }
433 }
434
435 ///////////////////////////////////////////////////////////////////////////
436
437 /// These structs (a newtyped TyVid) are used as the unification key
438 /// for the `eq_relations`; they carry a `TypeVariableValue` along
439 /// with them.
440 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
441 pub(crate) struct TyVidEqKey<'tcx> {
442     vid: ty::TyVid,
443
444     // in the table, we map each ty-vid to one of these:
445     phantom: PhantomData<TypeVariableValue<'tcx>>,
446 }
447
448 impl<'tcx> From<ty::TyVid> for TyVidEqKey<'tcx> {
449     fn from(vid: ty::TyVid) -> Self {
450         TyVidEqKey { vid, phantom: PhantomData }
451     }
452 }
453
454 impl<'tcx> ut::UnifyKey for TyVidEqKey<'tcx> {
455     type Value = TypeVariableValue<'tcx>;
456     fn index(&self) -> u32 {
457         self.vid.index
458     }
459     fn from_index(i: u32) -> Self {
460         TyVidEqKey::from(ty::TyVid { index: i })
461     }
462     fn tag() -> &'static str {
463         "TyVidEqKey"
464     }
465 }
466
467 impl<'tcx> ut::UnifyValue for TypeVariableValue<'tcx> {
468     type Error = ut::NoError;
469
470     fn unify_values(value1: &Self, value2: &Self) -> Result<Self, ut::NoError> {
471         match (value1, value2) {
472             // We never equate two type variables, both of which
473             // have known types.  Instead, we recursively equate
474             // those types.
475             (&TypeVariableValue::Known { .. }, &TypeVariableValue::Known { .. }) => {
476                 bug!("equating two type variables, both of which have known types")
477             }
478
479             // If one side is known, prefer that one.
480             (&TypeVariableValue::Known { .. }, &TypeVariableValue::Unknown { .. }) => Ok(*value1),
481             (&TypeVariableValue::Unknown { .. }, &TypeVariableValue::Known { .. }) => Ok(*value2),
482
483             // If both sides are *unknown*, it hardly matters, does it?
484             (
485                 &TypeVariableValue::Unknown { universe: universe1 },
486                 &TypeVariableValue::Unknown { universe: universe2 },
487             ) => {
488                 // If we unify two unbound variables, ?T and ?U, then whatever
489                 // value they wind up taking (which must be the same value) must
490                 // be nameable by both universes. Therefore, the resulting
491                 // universe is the minimum of the two universes, because that is
492                 // the one which contains the fewest names in scope.
493                 let universe = cmp::min(universe1, universe2);
494                 Ok(TypeVariableValue::Unknown { universe })
495             }
496         }
497     }
498 }