hir_ty/next_solver/infer/region_constraints/
mod.rs

1//! See `README.md`.
2
3use std::ops::Range;
4use std::{cmp, fmt, mem};
5
6use ena::undo_log::{Rollback, UndoLogs};
7use ena::unify as ut;
8use rustc_hash::FxHashMap;
9use rustc_index::IndexVec;
10use rustc_type_ir::inherent::IntoKind;
11use rustc_type_ir::{RegionKind, RegionVid, UniverseIndex};
12use tracing::{debug, instrument};
13
14use self::CombineMapType::*;
15use self::UndoLog::*;
16use super::MemberConstraint;
17use super::unify_key::RegionVidKey;
18use crate::next_solver::infer::snapshot::undo_log::{InferCtxtUndoLogs, Snapshot};
19use crate::next_solver::infer::unify_key::RegionVariableValue;
20use crate::next_solver::{AliasTy, Binder, DbInterner, ParamTy, PlaceholderTy, Region, Ty};
21
22#[derive(Debug, Clone, Default)]
23pub struct RegionConstraintStorage<'db> {
24    /// For each `RegionVid`, the corresponding `RegionVariableOrigin`.
25    pub(super) var_infos: IndexVec<RegionVid, RegionVariableInfo>,
26
27    pub(super) data: RegionConstraintData<'db>,
28
29    /// For a given pair of regions (R1, R2), maps to a region R3 that
30    /// is designated as their LUB (edges R1 <= R3 and R2 <= R3
31    /// exist). This prevents us from making many such regions.
32    lubs: CombineMap<'db>,
33
34    /// For a given pair of regions (R1, R2), maps to a region R3 that
35    /// is designated as their GLB (edges R3 <= R1 and R3 <= R2
36    /// exist). This prevents us from making many such regions.
37    glbs: CombineMap<'db>,
38
39    /// When we add a R1 == R2 constraint, we currently add (a) edges
40    /// R1 <= R2 and R2 <= R1 and (b) we unify the two regions in this
41    /// table. You can then call `opportunistic_resolve_var` early
42    /// which will map R1 and R2 to some common region (i.e., either
43    /// R1 or R2). This is important when fulfillment, dropck and other such
44    /// code is iterating to a fixed point, because otherwise we sometimes
45    /// would wind up with a fresh stream of region variables that have been
46    /// equated but appear distinct.
47    pub(super) unification_table: ut::UnificationTableStorage<RegionVidKey<'db>>,
48
49    /// a flag set to true when we perform any unifications; this is used
50    /// to micro-optimize `take_and_reset_data`
51    any_unifications: bool,
52}
53
54pub struct RegionConstraintCollector<'db, 'a> {
55    storage: &'a mut RegionConstraintStorage<'db>,
56    undo_log: &'a mut InferCtxtUndoLogs<'db>,
57}
58
59pub type VarInfos = IndexVec<RegionVid, RegionVariableInfo>;
60
61/// The full set of region constraints gathered up by the collector.
62/// Describes constraints between the region variables and other
63/// regions, as well as other conditions that must be verified, or
64/// assumptions that can be made.
65#[derive(Debug, Default, Clone)]
66pub struct RegionConstraintData<'db> {
67    /// Constraints of the form `A <= B`, where either `A` or `B` can
68    /// be a region variable (or neither, as it happens).
69    pub constraints: Vec<Constraint<'db>>,
70
71    /// Constraints of the form `R0 member of [R1, ..., Rn]`, meaning that
72    /// `R0` must be equal to one of the regions `R1..Rn`. These occur
73    /// with `impl Trait` quite frequently.
74    pub member_constraints: Vec<MemberConstraint<'db>>,
75
76    /// A "verify" is something that we need to verify after inference
77    /// is done, but which does not directly affect inference in any
78    /// way.
79    ///
80    /// An example is a `A <= B` where neither `A` nor `B` are
81    /// inference variables.
82    pub verifys: Vec<Verify<'db>>,
83}
84
85/// Represents a constraint that influences the inference process.
86#[derive(Clone, PartialEq, Eq, Debug, Hash)]
87pub enum Constraint<'db> {
88    /// A region variable is a subregion of another.
89    VarSubVar(RegionVid, RegionVid),
90
91    /// A concrete region is a subregion of region variable.
92    RegSubVar(Region<'db>, RegionVid),
93
94    /// A region variable is a subregion of a concrete region. This does not
95    /// directly affect inference, but instead is checked after
96    /// inference is complete.
97    VarSubReg(RegionVid, Region<'db>),
98
99    /// A constraint where neither side is a variable. This does not
100    /// directly affect inference, but instead is checked after
101    /// inference is complete.
102    RegSubReg(Region<'db>, Region<'db>),
103}
104
105impl<'db> Constraint<'db> {
106    pub fn involves_placeholders(&self) -> bool {
107        match self {
108            Constraint::VarSubVar(_, _) => false,
109            Constraint::VarSubReg(_, r) | Constraint::RegSubVar(r, _) => r.is_placeholder(),
110            Constraint::RegSubReg(r, s) => r.is_placeholder() || s.is_placeholder(),
111        }
112    }
113}
114
115#[derive(Debug, Clone)]
116pub struct Verify<'db> {
117    pub kind: GenericKind<'db>,
118    pub region: Region<'db>,
119    pub bound: VerifyBound<'db>,
120}
121
122#[derive(Clone, PartialEq, Eq, Hash)]
123pub enum GenericKind<'db> {
124    Param(ParamTy),
125    Placeholder(PlaceholderTy),
126    Alias(AliasTy<'db>),
127}
128
129/// Describes the things that some `GenericKind` value `G` is known to
130/// outlive. Each variant of `VerifyBound` can be thought of as a
131/// function:
132/// ```ignore (pseudo-rust)
133/// fn(min: Region) -> bool { .. }
134/// ```
135/// where `true` means that the region `min` meets that `G: min`.
136/// (False means nothing.)
137///
138/// So, for example, if we have the type `T` and we have in scope that
139/// `T: 'a` and `T: 'b`, then the verify bound might be:
140/// ```ignore (pseudo-rust)
141/// fn(min: Region) -> bool {
142///    ('a: min) || ('b: min)
143/// }
144/// ```
145/// This is described with an `AnyRegion('a, 'b)` node.
146#[derive(Debug, Clone)]
147pub enum VerifyBound<'db> {
148    /// See [`VerifyIfEq`] docs
149    IfEq(Binder<'db, VerifyIfEq<'db>>),
150
151    /// Given a region `R`, expands to the function:
152    ///
153    /// ```ignore (pseudo-rust)
154    /// fn(min) -> bool {
155    ///     R: min
156    /// }
157    /// ```
158    ///
159    /// This is used when we can establish that `G: R` -- therefore,
160    /// if `R: min`, then by transitivity `G: min`.
161    OutlivedBy(Region<'db>),
162
163    /// Given a region `R`, true if it is `'empty`.
164    IsEmpty,
165
166    /// Given a set of bounds `B`, expands to the function:
167    ///
168    /// ```ignore (pseudo-rust)
169    /// fn(min) -> bool {
170    ///     exists (b in B) { b(min) }
171    /// }
172    /// ```
173    ///
174    /// In other words, if we meet some bound in `B`, that suffices.
175    /// This is used when all the bounds in `B` are known to apply to `G`.
176    AnyBound(Vec<VerifyBound<'db>>),
177
178    /// Given a set of bounds `B`, expands to the function:
179    ///
180    /// ```ignore (pseudo-rust)
181    /// fn(min) -> bool {
182    ///     forall (b in B) { b(min) }
183    /// }
184    /// ```
185    ///
186    /// In other words, if we meet *all* bounds in `B`, that suffices.
187    /// This is used when *some* bound in `B` is known to suffice, but
188    /// we don't know which.
189    AllBounds(Vec<VerifyBound<'db>>),
190}
191
192/// This is a "conditional bound" that checks the result of inference
193/// and supplies a bound if it ended up being relevant. It's used in situations
194/// like this:
195///
196/// ```rust,ignore (pseudo-Rust)
197/// fn foo<'a, 'b, T: SomeTrait<'a>>
198/// where
199///    <T as SomeTrait<'a>>::Item: 'b
200/// ```
201///
202/// If we have an obligation like `<T as SomeTrait<'?x>>::Item: 'c`, then
203/// we don't know yet whether it suffices to show that `'b: 'c`. If `'?x` winds
204/// up being equal to `'a`, then the where-clauses on function applies, and
205/// in that case we can show `'b: 'c`. But if `'?x` winds up being something
206/// else, the bound isn't relevant.
207///
208/// In the [`VerifyBound`], this struct is enclosed in `Binder` to account
209/// for cases like
210///
211/// ```rust,ignore (pseudo-Rust)
212/// where for<'a> <T as SomeTrait<'a>::Item: 'a
213/// ```
214///
215/// The idea is that we have to find some instantiation of `'a` that can
216/// make `<T as SomeTrait<'a>>::Item` equal to the final value of `G`,
217/// the generic we are checking.
218///
219/// ```ignore (pseudo-rust)
220/// fn(min) -> bool {
221///     exists<'a> {
222///         if G == K {
223///             B(min)
224///         } else {
225///             false
226///         }
227///     }
228/// }
229/// ```
230#[derive(Debug, Clone)]
231pub struct VerifyIfEq<'db> {
232    /// Type which must match the generic `G`
233    pub ty: Ty<'db>,
234
235    /// Bound that applies if `ty` is equal.
236    pub bound: Region<'db>,
237}
238
239#[derive(Debug, Clone, PartialEq, Eq, Hash)]
240pub(crate) struct TwoRegions<'db> {
241    a: Region<'db>,
242    b: Region<'db>,
243}
244
245#[derive(Clone, PartialEq)]
246pub(crate) enum UndoLog<'db> {
247    /// We added `RegionVid`.
248    AddVar(RegionVid),
249
250    /// We added the given `constraint`.
251    AddConstraint(usize),
252
253    /// We added the given `verify`.
254    #[expect(dead_code, reason = "this is used in rustc")]
255    AddVerify(usize),
256
257    /// We added a GLB/LUB "combination variable".
258    AddCombination(CombineMapType, TwoRegions<'db>),
259}
260
261#[derive(Clone, PartialEq)]
262pub(crate) enum CombineMapType {
263    Lub,
264    Glb,
265}
266
267type CombineMap<'db> = FxHashMap<TwoRegions<'db>, RegionVid>;
268
269#[derive(Debug, Clone)]
270pub struct RegionVariableInfo {
271    // FIXME: This is only necessary for `fn take_and_reset_data` and
272    // `lexical_region_resolve`. We should rework `lexical_region_resolve`
273    // in the near/medium future anyways and could move the unverse info
274    // for `fn take_and_reset_data` into a separate table which is
275    // only populated when needed.
276    //
277    // For both of these cases it is fine that this can diverge from the
278    // actual universe of the variable, which is directly stored in the
279    // unification table for unknown region variables. At some point we could
280    // stop emitting bidirectional outlives constraints if equate succeeds.
281    // This would be currently unsound as it would cause us to drop the universe
282    // changes in `lexical_region_resolve`.
283    pub universe: UniverseIndex,
284}
285
286pub(crate) struct RegionSnapshot {
287    any_unifications: bool,
288}
289
290impl<'db> RegionConstraintStorage<'db> {
291    #[inline]
292    pub(crate) fn with_log<'a>(
293        &'a mut self,
294        undo_log: &'a mut InferCtxtUndoLogs<'db>,
295    ) -> RegionConstraintCollector<'db, 'a> {
296        RegionConstraintCollector { storage: self, undo_log }
297    }
298}
299
300impl<'db> RegionConstraintCollector<'db, '_> {
301    pub fn num_region_vars(&self) -> usize {
302        self.storage.var_infos.len()
303    }
304
305    pub fn region_constraint_data(&self) -> &RegionConstraintData<'db> {
306        &self.storage.data
307    }
308
309    /// Takes (and clears) the current set of constraints. Note that
310    /// the set of variables remains intact, but all relationships
311    /// between them are reset. This is used during NLL checking to
312    /// grab the set of constraints that arose from a particular
313    /// operation.
314    ///
315    /// We don't want to leak relationships between variables between
316    /// points because just because (say) `r1 == r2` was true at some
317    /// point P in the graph doesn't imply that it will be true at
318    /// some other point Q, in NLL.
319    ///
320    /// Not legal during a snapshot.
321    pub fn take_and_reset_data(&mut self) -> RegionConstraintData<'db> {
322        assert!(!UndoLogs::<UndoLog<'db>>::in_snapshot(&self.undo_log));
323
324        // If you add a new field to `RegionConstraintCollector`, you
325        // should think carefully about whether it needs to be cleared
326        // or updated in some way.
327        let RegionConstraintStorage {
328            var_infos: _,
329            data,
330            lubs,
331            glbs,
332            unification_table: _,
333            any_unifications,
334        } = self.storage;
335
336        // Clear the tables of (lubs, glbs), so that we will create
337        // fresh regions if we do a LUB operation. As it happens,
338        // LUB/GLB are not performed by the MIR type-checker, which is
339        // the one that uses this method, but it's good to be correct.
340        lubs.clear();
341        glbs.clear();
342
343        let data = mem::take(data);
344
345        // Clear all unifications and recreate the variables a "now
346        // un-unified" state. Note that when we unify `a` and `b`, we
347        // also insert `a <= b` and a `b <= a` edges, so the
348        // `RegionConstraintData` contains the relationship here.
349        if *any_unifications {
350            *any_unifications = false;
351            // Manually inlined `self.unification_table_mut()` as `self` is used in the closure.
352            ut::UnificationTable::with_log(&mut self.storage.unification_table, &mut self.undo_log)
353                .reset_unifications(|key| RegionVariableValue::Unknown {
354                    universe: self.storage.var_infos[key.vid].universe,
355                });
356        }
357
358        data
359    }
360
361    pub fn data(&self) -> &RegionConstraintData<'db> {
362        &self.storage.data
363    }
364
365    pub(super) fn start_snapshot(&self) -> RegionSnapshot {
366        debug!("RegionConstraintCollector: start_snapshot");
367        RegionSnapshot { any_unifications: self.storage.any_unifications }
368    }
369
370    pub(super) fn rollback_to(&mut self, snapshot: RegionSnapshot) {
371        debug!("RegionConstraintCollector: rollback_to({:?})", snapshot);
372        self.storage.any_unifications = snapshot.any_unifications;
373    }
374
375    pub(super) fn new_region_var(&mut self, universe: UniverseIndex) -> RegionVid {
376        let vid = self.storage.var_infos.push(RegionVariableInfo { universe });
377
378        let u_vid = self.unification_table_mut().new_key(RegionVariableValue::Unknown { universe });
379        assert_eq!(vid, u_vid.vid);
380        self.undo_log.push(AddVar(vid));
381        debug!("created new region variable {:?} in {:?}", vid, universe);
382        vid
383    }
384
385    fn add_constraint(&mut self, constraint: Constraint<'db>) {
386        // cannot add constraints once regions are resolved
387        debug!("RegionConstraintCollector: add_constraint({:?})", constraint);
388
389        let index = self.storage.data.constraints.len();
390        self.storage.data.constraints.push(constraint);
391        self.undo_log.push(AddConstraint(index));
392    }
393
394    pub(super) fn make_eqregion(&mut self, a: Region<'db>, b: Region<'db>) {
395        if a != b {
396            // Eventually, it would be nice to add direct support for
397            // equating regions.
398            self.make_subregion(a, b);
399            self.make_subregion(b, a);
400
401            match (a.kind(), b.kind()) {
402                (RegionKind::ReVar(a), RegionKind::ReVar(b)) => {
403                    debug!("make_eqregion: unifying {:?} with {:?}", a, b);
404                    if self.unification_table_mut().unify_var_var(a, b).is_ok() {
405                        self.storage.any_unifications = true;
406                    }
407                }
408                (RegionKind::ReVar(vid), _) => {
409                    debug!("make_eqregion: unifying {:?} with {:?}", vid, b);
410                    if self
411                        .unification_table_mut()
412                        .unify_var_value(vid, RegionVariableValue::Known { value: b })
413                        .is_ok()
414                    {
415                        self.storage.any_unifications = true;
416                    };
417                }
418                (_, RegionKind::ReVar(vid)) => {
419                    debug!("make_eqregion: unifying {:?} with {:?}", a, vid);
420                    if self
421                        .unification_table_mut()
422                        .unify_var_value(vid, RegionVariableValue::Known { value: a })
423                        .is_ok()
424                    {
425                        self.storage.any_unifications = true;
426                    };
427                }
428                (_, _) => {}
429            }
430        }
431    }
432
433    #[instrument(skip(self), level = "debug")]
434    pub(super) fn make_subregion(&mut self, sub: Region<'db>, sup: Region<'db>) {
435        // cannot add constraints once regions are resolved
436
437        match (sub.kind(), sup.kind()) {
438            (RegionKind::ReBound(..), _) | (_, RegionKind::ReBound(..)) => {
439                panic!("cannot relate bound region: {sub:?} <= {sup:?}");
440            }
441            (_, RegionKind::ReStatic) => {
442                // all regions are subregions of static, so we can ignore this
443            }
444            (RegionKind::ReVar(sub_id), RegionKind::ReVar(sup_id)) => {
445                self.add_constraint(Constraint::VarSubVar(sub_id, sup_id));
446            }
447            (_, RegionKind::ReVar(sup_id)) => {
448                self.add_constraint(Constraint::RegSubVar(sub, sup_id));
449            }
450            (RegionKind::ReVar(sub_id), _) => {
451                self.add_constraint(Constraint::VarSubReg(sub_id, sup));
452            }
453            _ => {
454                self.add_constraint(Constraint::RegSubReg(sub, sup));
455            }
456        }
457    }
458
459    pub(super) fn lub_regions(
460        &mut self,
461        db: DbInterner<'db>,
462        a: Region<'db>,
463        b: Region<'db>,
464    ) -> Region<'db> {
465        // cannot add constraints once regions are resolved
466        debug!("RegionConstraintCollector: lub_regions({:?}, {:?})", a, b);
467        #[expect(clippy::if_same_then_else)]
468        if a.is_static() || b.is_static() {
469            a // nothing lives longer than static
470        } else if a == b {
471            a // LUB(a,a) = a
472        } else {
473            self.combine_vars(db, Lub, a, b)
474        }
475    }
476
477    pub(super) fn glb_regions(
478        &mut self,
479        db: DbInterner<'db>,
480        a: Region<'db>,
481        b: Region<'db>,
482    ) -> Region<'db> {
483        // cannot add constraints once regions are resolved
484        debug!("RegionConstraintCollector: glb_regions({:?}, {:?})", a, b);
485        #[expect(clippy::if_same_then_else)]
486        if a.is_static() {
487            b // static lives longer than everything else
488        } else if b.is_static() {
489            a // static lives longer than everything else
490        } else if a == b {
491            a // GLB(a,a) = a
492        } else {
493            self.combine_vars(db, Glb, a, b)
494        }
495    }
496
497    /// Resolves a region var to its value in the unification table, if it exists.
498    /// Otherwise, it is resolved to the root `ReVar` in the table.
499    pub fn opportunistic_resolve_var(
500        &mut self,
501        cx: DbInterner<'db>,
502        vid: RegionVid,
503    ) -> Region<'db> {
504        let mut ut = self.unification_table_mut();
505        let root_vid = ut.find(vid).vid;
506        match ut.probe_value(root_vid) {
507            RegionVariableValue::Known { value } => value,
508            RegionVariableValue::Unknown { .. } => Region::new_var(cx, root_vid),
509        }
510    }
511
512    pub fn probe_value(&mut self, vid: RegionVid) -> Result<Region<'db>, UniverseIndex> {
513        match self.unification_table_mut().probe_value(vid) {
514            RegionVariableValue::Known { value } => Ok(value),
515            RegionVariableValue::Unknown { universe } => Err(universe),
516        }
517    }
518
519    fn combine_map(&mut self, t: CombineMapType) -> &mut CombineMap<'db> {
520        match t {
521            Glb => &mut self.storage.glbs,
522            Lub => &mut self.storage.lubs,
523        }
524    }
525
526    fn combine_vars(
527        &mut self,
528        cx: DbInterner<'db>,
529        t: CombineMapType,
530        a: Region<'db>,
531        b: Region<'db>,
532    ) -> Region<'db> {
533        let vars = TwoRegions { a, b };
534        if let Some(c) = self.combine_map(t.clone()).get(&vars) {
535            return Region::new_var(cx, *c);
536        }
537        let a_universe = self.universe(a);
538        let b_universe = self.universe(b);
539        let c_universe = cmp::max(a_universe, b_universe);
540        let c = self.new_region_var(c_universe);
541        self.combine_map(t.clone()).insert(vars.clone(), c);
542        self.undo_log.push(AddCombination(t.clone(), vars));
543        let new_r = Region::new_var(cx, c);
544        for old_r in [a, b] {
545            match t {
546                Glb => self.make_subregion(new_r, old_r),
547                Lub => self.make_subregion(old_r, new_r),
548            }
549        }
550        debug!("combine_vars() c={:?}", c);
551        new_r
552    }
553
554    pub fn universe(&mut self, region: Region<'db>) -> UniverseIndex {
555        match region.kind() {
556            RegionKind::ReStatic
557            | RegionKind::ReErased
558            | RegionKind::ReLateParam(..)
559            | RegionKind::ReEarlyParam(..)
560            | RegionKind::ReError(_) => UniverseIndex::ROOT,
561            RegionKind::RePlaceholder(placeholder) => placeholder.universe,
562            RegionKind::ReVar(vid) => match self.probe_value(vid) {
563                Ok(value) => self.universe(value),
564                Err(universe) => universe,
565            },
566            RegionKind::ReBound(..) => panic!("universe(): encountered bound region {region:?}"),
567        }
568    }
569
570    pub fn vars_since_snapshot(&self, value_count: usize) -> Range<RegionVid> {
571        RegionVid::from(value_count)..RegionVid::from(self.storage.unification_table.len())
572    }
573
574    /// See `InferCtxt::region_constraints_added_in_snapshot`.
575    pub fn region_constraints_added_in_snapshot(&self, mark: &Snapshot) -> bool {
576        self.undo_log
577            .region_constraints_in_snapshot(mark)
578            .any(|elt| matches!(elt, AddConstraint(_)))
579    }
580
581    #[inline]
582    fn unification_table_mut(&mut self) -> super::UnificationTable<'_, 'db, RegionVidKey<'db>> {
583        ut::UnificationTable::with_log(&mut self.storage.unification_table, self.undo_log)
584    }
585}
586
587impl fmt::Debug for RegionSnapshot {
588    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
589        write!(f, "RegionSnapshot")
590    }
591}
592
593impl<'db> fmt::Debug for GenericKind<'db> {
594    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
595        match *self {
596            GenericKind::Param(ref p) => write!(f, "{p:?}"),
597            GenericKind::Placeholder(ref p) => write!(f, "{p:?}"),
598            GenericKind::Alias(ref p) => write!(f, "{p:?}"),
599        }
600    }
601}
602
603impl<'db> fmt::Display for GenericKind<'db> {
604    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
605        match *self {
606            GenericKind::Param(ref p) => write!(f, "{p:?}"),
607            GenericKind::Placeholder(ref p) => write!(f, "{p:?}"),
608            GenericKind::Alias(ref p) => write!(f, "{p}"),
609        }
610    }
611}
612
613impl<'db> GenericKind<'db> {
614    pub fn to_ty(&self, interner: DbInterner<'db>) -> Ty<'db> {
615        match *self {
616            GenericKind::Param(ref p) => (*p).to_ty(interner),
617            GenericKind::Placeholder(ref p) => Ty::new_placeholder(interner, *p),
618            GenericKind::Alias(ref p) => (*p).to_ty(interner),
619        }
620    }
621}
622
623impl<'db> VerifyBound<'db> {
624    pub fn must_hold(&self) -> bool {
625        match self {
626            VerifyBound::IfEq(..) => false,
627            VerifyBound::OutlivedBy(re) => re.is_static(),
628            VerifyBound::IsEmpty => false,
629            VerifyBound::AnyBound(bs) => bs.iter().any(|b| b.must_hold()),
630            VerifyBound::AllBounds(bs) => bs.iter().all(|b| b.must_hold()),
631        }
632    }
633
634    pub fn cannot_hold(&self) -> bool {
635        match self {
636            VerifyBound::IfEq(..) => false,
637            VerifyBound::IsEmpty => false,
638            VerifyBound::OutlivedBy(_) => false,
639            VerifyBound::AnyBound(bs) => bs.iter().all(|b| b.cannot_hold()),
640            VerifyBound::AllBounds(bs) => bs.iter().any(|b| b.cannot_hold()),
641        }
642    }
643
644    pub fn or(self, vb: VerifyBound<'db>) -> VerifyBound<'db> {
645        if self.must_hold() || vb.cannot_hold() {
646            self
647        } else if self.cannot_hold() || vb.must_hold() {
648            vb
649        } else {
650            VerifyBound::AnyBound(vec![self, vb])
651        }
652    }
653}
654
655impl<'db> RegionConstraintData<'db> {
656    /// Returns `true` if this region constraint data contains no constraints, and `false`
657    /// otherwise.
658    pub fn is_empty(&self) -> bool {
659        let RegionConstraintData { constraints, member_constraints, verifys } = self;
660        constraints.is_empty() && member_constraints.is_empty() && verifys.is_empty()
661    }
662}
663
664impl<'db> Rollback<UndoLog<'db>> for RegionConstraintStorage<'db> {
665    fn reverse(&mut self, undo: UndoLog<'db>) {
666        match undo {
667            AddVar(vid) => {
668                self.var_infos.pop().unwrap();
669                assert_eq!(self.var_infos.len(), vid.index());
670            }
671            AddConstraint(index) => {
672                self.data.constraints.pop().unwrap();
673                assert_eq!(self.data.constraints.len(), index);
674            }
675            AddVerify(index) => {
676                self.data.verifys.pop();
677                assert_eq!(self.data.verifys.len(), index);
678            }
679            AddCombination(Glb, ref regions) => {
680                self.glbs.remove(regions);
681            }
682            AddCombination(Lub, ref regions) => {
683                self.lubs.remove(regions);
684            }
685        }
686    }
687}