]> git.lizzy.rs Git - rust.git/blob - src/librustc_infer/infer/region_constraints/mod.rs
remove `pop_placeholders`
[rust.git] / src / librustc_infer / infer / region_constraints / mod.rs
1 //! See `README.md`.
2
3 use self::CombineMapType::*;
4 use self::UndoLog::*;
5
6 use super::unify_key;
7 use super::{
8     InferCtxtUndoLogs, MiscVariable, RegionVariableOrigin, Rollback, Snapshot, SubregionOrigin,
9 };
10
11 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
12 use rustc_data_structures::sync::Lrc;
13 use rustc_data_structures::undo_log::UndoLogs;
14 use rustc_data_structures::unify as ut;
15 use rustc_data_structures::unify::UnifyKey;
16 use rustc_hir::def_id::DefId;
17 use rustc_index::vec::IndexVec;
18 use rustc_middle::ty::ReStatic;
19 use rustc_middle::ty::{self, Ty, TyCtxt};
20 use rustc_middle::ty::{ReLateBound, ReVar};
21 use rustc_middle::ty::{Region, RegionVid};
22 use rustc_span::Span;
23
24 use std::collections::BTreeMap;
25 use std::ops::Range;
26 use std::{cmp, fmt, mem};
27
28 mod leak_check;
29
30 pub use rustc_middle::infer::MemberConstraint;
31
32 #[derive(Default)]
33 pub struct RegionConstraintStorage<'tcx> {
34     /// For each `RegionVid`, the corresponding `RegionVariableOrigin`.
35     var_infos: IndexVec<RegionVid, RegionVariableInfo>,
36
37     data: RegionConstraintData<'tcx>,
38
39     /// For a given pair of regions (R1, R2), maps to a region R3 that
40     /// is designated as their LUB (edges R1 <= R3 and R2 <= R3
41     /// exist). This prevents us from making many such regions.
42     lubs: CombineMap<'tcx>,
43
44     /// For a given pair of regions (R1, R2), maps to a region R3 that
45     /// is designated as their GLB (edges R3 <= R1 and R3 <= R2
46     /// exist). This prevents us from making many such regions.
47     glbs: CombineMap<'tcx>,
48
49     /// When we add a R1 == R2 constriant, we currently add (a) edges
50     /// R1 <= R2 and R2 <= R1 and (b) we unify the two regions in this
51     /// table. You can then call `opportunistic_resolve_var` early
52     /// which will map R1 and R2 to some common region (i.e., either
53     /// R1 or R2). This is important when fulfillment, dropck and other such
54     /// code is iterating to a fixed point, because otherwise we sometimes
55     /// would wind up with a fresh stream of region variables that have been
56     /// equated but appear distinct.
57     pub(super) unification_table: ut::UnificationTableStorage<ty::RegionVid>,
58
59     /// a flag set to true when we perform any unifications; this is used
60     /// to micro-optimize `take_and_reset_data`
61     any_unifications: bool,
62 }
63
64 pub struct RegionConstraintCollector<'a, 'tcx> {
65     storage: &'a mut RegionConstraintStorage<'tcx>,
66     undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
67 }
68
69 impl std::ops::Deref for RegionConstraintCollector<'_, 'tcx> {
70     type Target = RegionConstraintStorage<'tcx>;
71     #[inline]
72     fn deref(&self) -> &RegionConstraintStorage<'tcx> {
73         self.storage
74     }
75 }
76
77 impl std::ops::DerefMut for RegionConstraintCollector<'_, 'tcx> {
78     #[inline]
79     fn deref_mut(&mut self) -> &mut RegionConstraintStorage<'tcx> {
80         self.storage
81     }
82 }
83
84 pub type VarInfos = IndexVec<RegionVid, RegionVariableInfo>;
85
86 /// The full set of region constraints gathered up by the collector.
87 /// Describes constraints between the region variables and other
88 /// regions, as well as other conditions that must be verified, or
89 /// assumptions that can be made.
90 #[derive(Debug, Default, Clone)]
91 pub struct RegionConstraintData<'tcx> {
92     /// Constraints of the form `A <= B`, where either `A` or `B` can
93     /// be a region variable (or neither, as it happens).
94     pub constraints: BTreeMap<Constraint<'tcx>, SubregionOrigin<'tcx>>,
95
96     /// Constraints of the form `R0 member of [R1, ..., Rn]`, meaning that
97     /// `R0` must be equal to one of the regions `R1..Rn`. These occur
98     /// with `impl Trait` quite frequently.
99     pub member_constraints: Vec<MemberConstraint<'tcx>>,
100
101     /// A "verify" is something that we need to verify after inference
102     /// is done, but which does not directly affect inference in any
103     /// way.
104     ///
105     /// An example is a `A <= B` where neither `A` nor `B` are
106     /// inference variables.
107     pub verifys: Vec<Verify<'tcx>>,
108
109     /// A "given" is a relationship that is known to hold. In
110     /// particular, we often know from closure fn signatures that a
111     /// particular free region must be a subregion of a region
112     /// variable:
113     ///
114     ///    foo.iter().filter(<'a> |x: &'a &'b T| ...)
115     ///
116     /// In situations like this, `'b` is in fact a region variable
117     /// introduced by the call to `iter()`, and `'a` is a bound region
118     /// on the closure (as indicated by the `<'a>` prefix). If we are
119     /// naive, we wind up inferring that `'b` must be `'static`,
120     /// because we require that it be greater than `'a` and we do not
121     /// know what `'a` is precisely.
122     ///
123     /// This hashmap is used to avoid that naive scenario. Basically
124     /// we record the fact that `'a <= 'b` is implied by the fn
125     /// signature, and then ignore the constraint when solving
126     /// equations. This is a bit of a hack but seems to work.
127     pub givens: FxHashSet<(Region<'tcx>, ty::RegionVid)>,
128 }
129
130 /// Represents a constraint that influences the inference process.
131 #[derive(Clone, Copy, PartialEq, Eq, Debug, PartialOrd, Ord)]
132 pub enum Constraint<'tcx> {
133     /// A region variable is a subregion of another.
134     VarSubVar(RegionVid, RegionVid),
135
136     /// A concrete region is a subregion of region variable.
137     RegSubVar(Region<'tcx>, RegionVid),
138
139     /// A region variable is a subregion of a concrete region. This does not
140     /// directly affect inference, but instead is checked after
141     /// inference is complete.
142     VarSubReg(RegionVid, Region<'tcx>),
143
144     /// A constraint where neither side is a variable. This does not
145     /// directly affect inference, but instead is checked after
146     /// inference is complete.
147     RegSubReg(Region<'tcx>, Region<'tcx>),
148 }
149
150 impl Constraint<'_> {
151     pub fn involves_placeholders(&self) -> bool {
152         match self {
153             Constraint::VarSubVar(_, _) => false,
154             Constraint::VarSubReg(_, r) | Constraint::RegSubVar(r, _) => r.is_placeholder(),
155             Constraint::RegSubReg(r, s) => r.is_placeholder() || s.is_placeholder(),
156         }
157     }
158 }
159
160 #[derive(Debug, Clone)]
161 pub struct Verify<'tcx> {
162     pub kind: GenericKind<'tcx>,
163     pub origin: SubregionOrigin<'tcx>,
164     pub region: Region<'tcx>,
165     pub bound: VerifyBound<'tcx>,
166 }
167
168 #[derive(Copy, Clone, PartialEq, Eq, Hash, TypeFoldable)]
169 pub enum GenericKind<'tcx> {
170     Param(ty::ParamTy),
171     Projection(ty::ProjectionTy<'tcx>),
172 }
173
174 /// Describes the things that some `GenericKind` value `G` is known to
175 /// outlive. Each variant of `VerifyBound` can be thought of as a
176 /// function:
177 ///
178 ///     fn(min: Region) -> bool { .. }
179 ///
180 /// where `true` means that the region `min` meets that `G: min`.
181 /// (False means nothing.)
182 ///
183 /// So, for example, if we have the type `T` and we have in scope that
184 /// `T: 'a` and `T: 'b`, then the verify bound might be:
185 ///
186 ///     fn(min: Region) -> bool {
187 ///        ('a: min) || ('b: min)
188 ///     }
189 ///
190 /// This is described with a `AnyRegion('a, 'b)` node.
191 #[derive(Debug, Clone)]
192 pub enum VerifyBound<'tcx> {
193     /// Given a kind K and a bound B, expands to a function like the
194     /// following, where `G` is the generic for which this verify
195     /// bound was created:
196     ///
197     /// ```rust
198     /// fn(min) -> bool {
199     ///     if G == K {
200     ///         B(min)
201     ///     } else {
202     ///         false
203     ///     }
204     /// }
205     /// ```
206     ///
207     /// In other words, if the generic `G` that we are checking is
208     /// equal to `K`, then check the associated verify bound
209     /// (otherwise, false).
210     ///
211     /// This is used when we have something in the environment that
212     /// may or may not be relevant, depending on the region inference
213     /// results. For example, we may have `where <T as
214     /// Trait<'a>>::Item: 'b` in our where-clauses. If we are
215     /// generating the verify-bound for `<T as Trait<'0>>::Item`, then
216     /// this where-clause is only relevant if `'0` winds up inferred
217     /// to `'a`.
218     ///
219     /// So we would compile to a verify-bound like
220     ///
221     /// ```
222     /// IfEq(<T as Trait<'a>>::Item, AnyRegion('a))
223     /// ```
224     ///
225     /// meaning, if the subject G is equal to `<T as Trait<'a>>::Item`
226     /// (after inference), and `'a: min`, then `G: min`.
227     IfEq(Ty<'tcx>, Box<VerifyBound<'tcx>>),
228
229     /// Given a region `R`, expands to the function:
230     ///
231     /// ```
232     /// fn(min) -> bool {
233     ///     R: min
234     /// }
235     /// ```
236     ///
237     /// This is used when we can establish that `G: R` -- therefore,
238     /// if `R: min`, then by transitivity `G: min`.
239     OutlivedBy(Region<'tcx>),
240
241     /// Given a region `R`, true if it is `'empty`.
242     IsEmpty,
243
244     /// Given a set of bounds `B`, expands to the function:
245     ///
246     /// ```rust
247     /// fn(min) -> bool {
248     ///     exists (b in B) { b(min) }
249     /// }
250     /// ```
251     ///
252     /// In other words, if we meet some bound in `B`, that suffices.
253     /// This is used when all the bounds in `B` are known to apply to `G`.
254     AnyBound(Vec<VerifyBound<'tcx>>),
255
256     /// Given a set of bounds `B`, expands to the function:
257     ///
258     /// ```rust
259     /// fn(min) -> bool {
260     ///     forall (b in B) { b(min) }
261     /// }
262     /// ```
263     ///
264     /// In other words, if we meet *all* bounds in `B`, that suffices.
265     /// This is used when *some* bound in `B` is known to suffice, but
266     /// we don't know which.
267     AllBounds(Vec<VerifyBound<'tcx>>),
268 }
269
270 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
271 pub(crate) struct TwoRegions<'tcx> {
272     a: Region<'tcx>,
273     b: Region<'tcx>,
274 }
275
276 #[derive(Copy, Clone, PartialEq)]
277 pub(crate) enum UndoLog<'tcx> {
278     /// We added `RegionVid`.
279     AddVar(RegionVid),
280
281     /// We added the given `constraint`.
282     AddConstraint(Constraint<'tcx>),
283
284     /// We added the given `verify`.
285     AddVerify(usize),
286
287     /// We added the given `given`.
288     AddGiven(Region<'tcx>, ty::RegionVid),
289
290     /// We added a GLB/LUB "combination variable".
291     AddCombination(CombineMapType, TwoRegions<'tcx>),
292 }
293
294 #[derive(Copy, Clone, PartialEq)]
295 pub(crate) enum CombineMapType {
296     Lub,
297     Glb,
298 }
299
300 type CombineMap<'tcx> = FxHashMap<TwoRegions<'tcx>, RegionVid>;
301
302 #[derive(Debug, Clone, Copy)]
303 pub struct RegionVariableInfo {
304     pub origin: RegionVariableOrigin,
305     pub universe: ty::UniverseIndex,
306 }
307
308 pub struct RegionSnapshot {
309     any_unifications: bool,
310 }
311
312 /// When working with placeholder regions, we often wish to find all of
313 /// the regions that are either reachable from a placeholder region, or
314 /// which can reach a placeholder region, or both. We call such regions
315 /// *tainted* regions. This struct allows you to decide what set of
316 /// tainted regions you want.
317 #[derive(Debug)]
318 pub struct TaintDirections {
319     incoming: bool,
320     outgoing: bool,
321 }
322
323 impl TaintDirections {
324     pub fn incoming() -> Self {
325         TaintDirections { incoming: true, outgoing: false }
326     }
327
328     pub fn outgoing() -> Self {
329         TaintDirections { incoming: false, outgoing: true }
330     }
331
332     pub fn both() -> Self {
333         TaintDirections { incoming: true, outgoing: true }
334     }
335 }
336
337 impl<'tcx> RegionConstraintStorage<'tcx> {
338     pub fn new() -> Self {
339         Self::default()
340     }
341
342     #[inline]
343     pub(crate) fn with_log<'a>(
344         &'a mut self,
345         undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
346     ) -> RegionConstraintCollector<'a, 'tcx> {
347         RegionConstraintCollector { storage: self, undo_log }
348     }
349
350     fn rollback_undo_entry(&mut self, undo_entry: UndoLog<'tcx>) {
351         match undo_entry {
352             AddVar(vid) => {
353                 self.var_infos.pop().unwrap();
354                 assert_eq!(self.var_infos.len(), vid.index() as usize);
355             }
356             AddConstraint(ref constraint) => {
357                 self.data.constraints.remove(constraint);
358             }
359             AddVerify(index) => {
360                 self.data.verifys.pop();
361                 assert_eq!(self.data.verifys.len(), index);
362             }
363             AddGiven(sub, sup) => {
364                 self.data.givens.remove(&(sub, sup));
365             }
366             AddCombination(Glb, ref regions) => {
367                 self.glbs.remove(regions);
368             }
369             AddCombination(Lub, ref regions) => {
370                 self.lubs.remove(regions);
371             }
372         }
373     }
374 }
375
376 impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
377     pub fn num_region_vars(&self) -> usize {
378         self.var_infos.len()
379     }
380
381     pub fn region_constraint_data(&self) -> &RegionConstraintData<'tcx> {
382         &self.data
383     }
384
385     /// Once all the constraints have been gathered, extract out the final data.
386     ///
387     /// Not legal during a snapshot.
388     pub fn into_infos_and_data(self) -> (VarInfos, RegionConstraintData<'tcx>) {
389         assert!(!UndoLogs::<super::UndoLog<'_>>::in_snapshot(&self.undo_log));
390         (mem::take(&mut self.storage.var_infos), mem::take(&mut self.storage.data))
391     }
392
393     /// Takes (and clears) the current set of constraints. Note that
394     /// the set of variables remains intact, but all relationships
395     /// between them are reset. This is used during NLL checking to
396     /// grab the set of constraints that arose from a particular
397     /// operation.
398     ///
399     /// We don't want to leak relationships between variables between
400     /// points because just because (say) `r1 == r2` was true at some
401     /// point P in the graph doesn't imply that it will be true at
402     /// some other point Q, in NLL.
403     ///
404     /// Not legal during a snapshot.
405     pub fn take_and_reset_data(&mut self) -> RegionConstraintData<'tcx> {
406         assert!(!UndoLogs::<super::UndoLog<'_>>::in_snapshot(&self.undo_log));
407
408         // If you add a new field to `RegionConstraintCollector`, you
409         // should think carefully about whether it needs to be cleared
410         // or updated in some way.
411         let RegionConstraintStorage {
412             var_infos: _,
413             data,
414             lubs,
415             glbs,
416             unification_table: _,
417             any_unifications,
418         } = self.storage;
419
420         // Clear the tables of (lubs, glbs), so that we will create
421         // fresh regions if we do a LUB operation. As it happens,
422         // LUB/GLB are not performed by the MIR type-checker, which is
423         // the one that uses this method, but it's good to be correct.
424         lubs.clear();
425         glbs.clear();
426
427         let data = mem::take(data);
428
429         // Clear all unifications and recreate the variables a "now
430         // un-unified" state. Note that when we unify `a` and `b`, we
431         // also insert `a <= b` and a `b <= a` edges, so the
432         // `RegionConstraintData` contains the relationship here.
433         if *any_unifications {
434             *any_unifications = false;
435             self.unification_table()
436                 .reset_unifications(|vid| unify_key::RegionVidKey { min_vid: vid });
437         }
438
439         data
440     }
441
442     pub fn data(&self) -> &RegionConstraintData<'tcx> {
443         &self.data
444     }
445
446     pub fn start_snapshot(&mut self) -> RegionSnapshot {
447         debug!("RegionConstraintCollector: start_snapshot");
448         RegionSnapshot { any_unifications: self.any_unifications }
449     }
450
451     pub fn rollback_to(&mut self, snapshot: RegionSnapshot) {
452         debug!("RegionConstraintCollector: rollback_to({:?})", snapshot);
453         self.any_unifications = snapshot.any_unifications;
454     }
455
456     pub fn new_region_var(
457         &mut self,
458         universe: ty::UniverseIndex,
459         origin: RegionVariableOrigin,
460     ) -> RegionVid {
461         let vid = self.var_infos.push(RegionVariableInfo { origin, universe });
462
463         let u_vid = self.unification_table().new_key(unify_key::RegionVidKey { min_vid: vid });
464         assert_eq!(vid, u_vid);
465         self.undo_log.push(AddVar(vid));
466         debug!("created new region variable {:?} in {:?} with origin {:?}", vid, universe, origin);
467         vid
468     }
469
470     /// Returns the universe for the given variable.
471     pub fn var_universe(&self, vid: RegionVid) -> ty::UniverseIndex {
472         self.var_infos[vid].universe
473     }
474
475     /// Returns the origin for the given variable.
476     pub fn var_origin(&self, vid: RegionVid) -> RegionVariableOrigin {
477         self.var_infos[vid].origin
478     }
479
480     fn add_constraint(&mut self, constraint: Constraint<'tcx>, origin: SubregionOrigin<'tcx>) {
481         // cannot add constraints once regions are resolved
482         debug!("RegionConstraintCollector: add_constraint({:?})", constraint);
483
484         // never overwrite an existing (constraint, origin) - only insert one if it isn't
485         // present in the map yet. This prevents origins from outside the snapshot being
486         // replaced with "less informative" origins e.g., during calls to `can_eq`
487         let undo_log = &mut self.undo_log;
488         self.storage.data.constraints.entry(constraint).or_insert_with(|| {
489             undo_log.push(AddConstraint(constraint));
490             origin
491         });
492     }
493
494     fn add_verify(&mut self, verify: Verify<'tcx>) {
495         // cannot add verifys once regions are resolved
496         debug!("RegionConstraintCollector: add_verify({:?})", verify);
497
498         // skip no-op cases known to be satisfied
499         if let VerifyBound::AllBounds(ref bs) = verify.bound {
500             if bs.is_empty() {
501                 return;
502             }
503         }
504
505         let index = self.data.verifys.len();
506         self.data.verifys.push(verify);
507         self.undo_log.push(AddVerify(index));
508     }
509
510     pub fn add_given(&mut self, sub: Region<'tcx>, sup: ty::RegionVid) {
511         // cannot add givens once regions are resolved
512         if self.data.givens.insert((sub, sup)) {
513             debug!("add_given({:?} <= {:?})", sub, sup);
514
515             self.undo_log.push(AddGiven(sub, sup));
516         }
517     }
518
519     pub fn make_eqregion(
520         &mut self,
521         origin: SubregionOrigin<'tcx>,
522         sub: Region<'tcx>,
523         sup: Region<'tcx>,
524     ) {
525         if sub != sup {
526             // Eventually, it would be nice to add direct support for
527             // equating regions.
528             self.make_subregion(origin.clone(), sub, sup);
529             self.make_subregion(origin, sup, sub);
530
531             if let (ty::ReVar(sub), ty::ReVar(sup)) = (*sub, *sup) {
532                 debug!("make_eqregion: uniying {:?} with {:?}", sub, sup);
533                 self.unification_table().union(sub, sup);
534                 self.any_unifications = true;
535             }
536         }
537     }
538
539     pub fn member_constraint(
540         &mut self,
541         opaque_type_def_id: DefId,
542         definition_span: Span,
543         hidden_ty: Ty<'tcx>,
544         member_region: ty::Region<'tcx>,
545         choice_regions: &Lrc<Vec<ty::Region<'tcx>>>,
546     ) {
547         debug!("member_constraint({:?} in {:#?})", member_region, choice_regions);
548
549         if choice_regions.iter().any(|&r| r == member_region) {
550             return;
551         }
552
553         self.data.member_constraints.push(MemberConstraint {
554             opaque_type_def_id,
555             definition_span,
556             hidden_ty,
557             member_region,
558             choice_regions: choice_regions.clone(),
559         });
560     }
561
562     pub fn make_subregion(
563         &mut self,
564         origin: SubregionOrigin<'tcx>,
565         sub: Region<'tcx>,
566         sup: Region<'tcx>,
567     ) {
568         // cannot add constraints once regions are resolved
569         debug!(
570             "RegionConstraintCollector: make_subregion({:?}, {:?}) due to {:?}",
571             sub, sup, origin
572         );
573
574         match (sub, sup) {
575             (&ReLateBound(..), _) | (_, &ReLateBound(..)) => {
576                 span_bug!(origin.span(), "cannot relate bound region: {:?} <= {:?}", sub, sup);
577             }
578             (_, &ReStatic) => {
579                 // all regions are subregions of static, so we can ignore this
580             }
581             (&ReVar(sub_id), &ReVar(sup_id)) => {
582                 self.add_constraint(Constraint::VarSubVar(sub_id, sup_id), origin);
583             }
584             (_, &ReVar(sup_id)) => {
585                 self.add_constraint(Constraint::RegSubVar(sub, sup_id), origin);
586             }
587             (&ReVar(sub_id), _) => {
588                 self.add_constraint(Constraint::VarSubReg(sub_id, sup), origin);
589             }
590             _ => {
591                 self.add_constraint(Constraint::RegSubReg(sub, sup), origin);
592             }
593         }
594     }
595
596     pub fn verify_generic_bound(
597         &mut self,
598         origin: SubregionOrigin<'tcx>,
599         kind: GenericKind<'tcx>,
600         sub: Region<'tcx>,
601         bound: VerifyBound<'tcx>,
602     ) {
603         self.add_verify(Verify { kind, origin, region: sub, bound });
604     }
605
606     pub fn lub_regions(
607         &mut self,
608         tcx: TyCtxt<'tcx>,
609         origin: SubregionOrigin<'tcx>,
610         a: Region<'tcx>,
611         b: Region<'tcx>,
612     ) -> Region<'tcx> {
613         // cannot add constraints once regions are resolved
614         debug!("RegionConstraintCollector: lub_regions({:?}, {:?})", a, b);
615         match (a, b) {
616             (r @ &ReStatic, _) | (_, r @ &ReStatic) => {
617                 r // nothing lives longer than static
618             }
619
620             _ if a == b => {
621                 a // LUB(a,a) = a
622             }
623
624             _ => self.combine_vars(tcx, Lub, a, b, origin),
625         }
626     }
627
628     pub fn glb_regions(
629         &mut self,
630         tcx: TyCtxt<'tcx>,
631         origin: SubregionOrigin<'tcx>,
632         a: Region<'tcx>,
633         b: Region<'tcx>,
634     ) -> Region<'tcx> {
635         // cannot add constraints once regions are resolved
636         debug!("RegionConstraintCollector: glb_regions({:?}, {:?})", a, b);
637         match (a, b) {
638             (&ReStatic, r) | (r, &ReStatic) => {
639                 r // static lives longer than everything else
640             }
641
642             _ if a == b => {
643                 a // GLB(a,a) = a
644             }
645
646             _ => self.combine_vars(tcx, Glb, a, b, origin),
647         }
648     }
649
650     pub fn opportunistic_resolve_var(&mut self, rid: RegionVid) -> ty::RegionVid {
651         self.unification_table().probe_value(rid).min_vid
652     }
653
654     fn combine_map(&mut self, t: CombineMapType) -> &mut CombineMap<'tcx> {
655         match t {
656             Glb => &mut self.glbs,
657             Lub => &mut self.lubs,
658         }
659     }
660
661     fn combine_vars(
662         &mut self,
663         tcx: TyCtxt<'tcx>,
664         t: CombineMapType,
665         a: Region<'tcx>,
666         b: Region<'tcx>,
667         origin: SubregionOrigin<'tcx>,
668     ) -> Region<'tcx> {
669         let vars = TwoRegions { a, b };
670         if let Some(&c) = self.combine_map(t).get(&vars) {
671             return tcx.mk_region(ReVar(c));
672         }
673         let a_universe = self.universe(a);
674         let b_universe = self.universe(b);
675         let c_universe = cmp::max(a_universe, b_universe);
676         let c = self.new_region_var(c_universe, MiscVariable(origin.span()));
677         self.combine_map(t).insert(vars, c);
678         self.undo_log.push(AddCombination(t, vars));
679         let new_r = tcx.mk_region(ReVar(c));
680         for &old_r in &[a, b] {
681             match t {
682                 Glb => self.make_subregion(origin.clone(), new_r, old_r),
683                 Lub => self.make_subregion(origin.clone(), old_r, new_r),
684             }
685         }
686         debug!("combine_vars() c={:?}", c);
687         new_r
688     }
689
690     pub fn universe(&self, region: Region<'tcx>) -> ty::UniverseIndex {
691         match *region {
692             ty::ReStatic | ty::ReErased | ty::ReFree(..) | ty::ReEarlyBound(..) => {
693                 ty::UniverseIndex::ROOT
694             }
695             ty::ReEmpty(ui) => ui,
696             ty::RePlaceholder(placeholder) => placeholder.universe,
697             ty::ReVar(vid) => self.var_universe(vid),
698             ty::ReLateBound(..) => bug!("universe(): encountered bound region {:?}", region),
699         }
700     }
701
702     pub fn vars_since_snapshot(
703         &self,
704         value_count: usize,
705     ) -> (Range<RegionVid>, Vec<RegionVariableOrigin>) {
706         let range = RegionVid::from_index(value_count as u32)
707             ..RegionVid::from_index(self.unification_table.len() as u32);
708         (
709             range.clone(),
710             (range.start.index()..range.end.index())
711                 .map(|index| self.var_infos[ty::RegionVid::from(index)].origin)
712                 .collect(),
713         )
714     }
715
716     /// See `InferCtxt::region_constraints_added_in_snapshot`.
717     pub fn region_constraints_added_in_snapshot(&self, mark: &Snapshot<'tcx>) -> Option<bool> {
718         self.undo_log
719             .region_constraints_in_snapshot(mark)
720             .map(|&elt| match elt {
721                 AddConstraint(constraint) => Some(constraint.involves_placeholders()),
722                 _ => None,
723             })
724             .max()
725             .unwrap_or(None)
726     }
727
728     #[inline]
729     fn unification_table(&mut self) -> super::UnificationTable<'_, 'tcx, ty::RegionVid> {
730         ut::UnificationTable::with_log(&mut self.storage.unification_table, self.undo_log)
731     }
732 }
733
734 impl fmt::Debug for RegionSnapshot {
735     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
736         write!(f, "RegionSnapshot")
737     }
738 }
739
740 impl<'tcx> fmt::Debug for GenericKind<'tcx> {
741     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
742         match *self {
743             GenericKind::Param(ref p) => write!(f, "{:?}", p),
744             GenericKind::Projection(ref p) => write!(f, "{:?}", p),
745         }
746     }
747 }
748
749 impl<'tcx> fmt::Display for GenericKind<'tcx> {
750     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
751         match *self {
752             GenericKind::Param(ref p) => write!(f, "{}", p),
753             GenericKind::Projection(ref p) => write!(f, "{}", p),
754         }
755     }
756 }
757
758 impl<'tcx> GenericKind<'tcx> {
759     pub fn to_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
760         match *self {
761             GenericKind::Param(ref p) => p.to_ty(tcx),
762             GenericKind::Projection(ref p) => tcx.mk_projection(p.item_def_id, p.substs),
763         }
764     }
765 }
766
767 impl<'tcx> VerifyBound<'tcx> {
768     pub fn must_hold(&self) -> bool {
769         match self {
770             VerifyBound::IfEq(..) => false,
771             VerifyBound::OutlivedBy(ty::ReStatic) => true,
772             VerifyBound::OutlivedBy(_) => false,
773             VerifyBound::IsEmpty => false,
774             VerifyBound::AnyBound(bs) => bs.iter().any(|b| b.must_hold()),
775             VerifyBound::AllBounds(bs) => bs.iter().all(|b| b.must_hold()),
776         }
777     }
778
779     pub fn cannot_hold(&self) -> bool {
780         match self {
781             VerifyBound::IfEq(_, b) => b.cannot_hold(),
782             VerifyBound::IsEmpty => false,
783             VerifyBound::OutlivedBy(_) => false,
784             VerifyBound::AnyBound(bs) => bs.iter().all(|b| b.cannot_hold()),
785             VerifyBound::AllBounds(bs) => bs.iter().any(|b| b.cannot_hold()),
786         }
787     }
788
789     pub fn or(self, vb: VerifyBound<'tcx>) -> VerifyBound<'tcx> {
790         if self.must_hold() || vb.cannot_hold() {
791             self
792         } else if self.cannot_hold() || vb.must_hold() {
793             vb
794         } else {
795             VerifyBound::AnyBound(vec![self, vb])
796         }
797     }
798
799     pub fn and(self, vb: VerifyBound<'tcx>) -> VerifyBound<'tcx> {
800         if self.must_hold() && vb.must_hold() {
801             self
802         } else if self.cannot_hold() && vb.cannot_hold() {
803             self
804         } else {
805             VerifyBound::AllBounds(vec![self, vb])
806         }
807     }
808 }
809
810 impl<'tcx> RegionConstraintData<'tcx> {
811     /// Returns `true` if this region constraint data contains no constraints, and `false`
812     /// otherwise.
813     pub fn is_empty(&self) -> bool {
814         let RegionConstraintData { constraints, member_constraints, verifys, givens } = self;
815         constraints.is_empty()
816             && member_constraints.is_empty()
817             && verifys.is_empty()
818             && givens.is_empty()
819     }
820 }
821
822 impl<'tcx> Rollback<UndoLog<'tcx>> for RegionConstraintStorage<'tcx> {
823     fn reverse(&mut self, undo: UndoLog<'tcx>) {
824         self.rollback_undo_entry(undo)
825     }
826 }