hir_ty/next_solver/infer/region_constraints/
mod.rs

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