rustc_borrowck/region_infer/
mod.rs

1use std::collections::VecDeque;
2use std::rc::Rc;
3
4use rustc_data_structures::binary_search_util;
5use rustc_data_structures::frozen::Frozen;
6use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
7use rustc_data_structures::graph::scc::{self, Sccs};
8use rustc_errors::Diag;
9use rustc_hir::def_id::CRATE_DEF_ID;
10use rustc_index::IndexVec;
11use rustc_infer::infer::outlives::test_type_match;
12use rustc_infer::infer::region_constraints::{GenericKind, VarInfos, VerifyBound, VerifyIfEq};
13use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin};
14use rustc_middle::bug;
15use rustc_middle::mir::{
16    AnnotationSource, BasicBlock, Body, ClosureOutlivesRequirement, ClosureOutlivesSubject,
17    ClosureOutlivesSubjectTy, ClosureRegionRequirements, ConstraintCategory, Local, Location,
18    ReturnConstraint, TerminatorKind,
19};
20use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
21use rustc_middle::ty::fold::fold_regions;
22use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt, TypeFoldable, UniverseIndex};
23use rustc_mir_dataflow::points::DenseLocationMap;
24use rustc_span::Span;
25use rustc_span::hygiene::DesugaringKind;
26use tracing::{debug, instrument, trace};
27
28use crate::BorrowckInferCtxt;
29use crate::constraints::graph::{self, NormalConstraintGraph, RegionGraph};
30use crate::constraints::{ConstraintSccIndex, OutlivesConstraint, OutlivesConstraintSet};
31use crate::dataflow::BorrowIndex;
32use crate::diagnostics::{RegionErrorKind, RegionErrors, UniverseInfo};
33use crate::member_constraints::{MemberConstraintSet, NllMemberConstraintIndex};
34use crate::polonius::LiveLoans;
35use crate::polonius::legacy::PoloniusOutput;
36use crate::region_infer::reverse_sccs::ReverseSccGraph;
37use crate::region_infer::values::{LivenessValues, RegionElement, RegionValues, ToElementIndex};
38use crate::type_check::free_region_relations::UniversalRegionRelations;
39use crate::type_check::{Locations, MirTypeckRegionConstraints};
40use crate::universal_regions::UniversalRegions;
41
42mod dump_mir;
43mod graphviz;
44mod opaque_types;
45mod reverse_sccs;
46
47pub(crate) mod values;
48
49pub(crate) type ConstraintSccs = Sccs<RegionVid, ConstraintSccIndex, RegionTracker>;
50
51/// An annotation for region graph SCCs that tracks
52/// the values of its elements.
53#[derive(Copy, Debug, Clone)]
54pub struct RegionTracker {
55    /// The largest universe of a placeholder reached from this SCC.
56    /// This includes placeholders within this SCC.
57    max_placeholder_universe_reached: UniverseIndex,
58
59    /// The smallest universe index reachable form the nodes of this SCC.
60    min_reachable_universe: UniverseIndex,
61
62    /// The representative Region Variable Id for this SCC. We prefer
63    /// placeholders over existentially quantified variables, otherwise
64    ///  it's the one with the smallest Region Variable ID.
65    pub(crate) representative: RegionVid,
66
67    /// Is the current representative a placeholder?
68    representative_is_placeholder: bool,
69
70    /// Is the current representative existentially quantified?
71    representative_is_existential: bool,
72}
73
74impl scc::Annotation for RegionTracker {
75    fn merge_scc(mut self, mut other: Self) -> Self {
76        // Prefer any placeholder over any existential
77        if other.representative_is_placeholder && self.representative_is_existential {
78            other.merge_min_max_seen(&self);
79            return other;
80        }
81
82        if self.representative_is_placeholder && other.representative_is_existential
83            || (self.representative <= other.representative)
84        {
85            self.merge_min_max_seen(&other);
86            return self;
87        }
88        other.merge_min_max_seen(&self);
89        other
90    }
91
92    fn merge_reached(mut self, other: Self) -> Self {
93        // No update to in-component values, only add seen values.
94        self.merge_min_max_seen(&other);
95        self
96    }
97}
98
99impl RegionTracker {
100    pub(crate) fn new(rvid: RegionVid, definition: &RegionDefinition<'_>) -> Self {
101        let (representative_is_placeholder, representative_is_existential) = match definition.origin
102        {
103            NllRegionVariableOrigin::FreeRegion => (false, false),
104            NllRegionVariableOrigin::Placeholder(_) => (true, false),
105            NllRegionVariableOrigin::Existential { .. } => (false, true),
106        };
107
108        let placeholder_universe =
109            if representative_is_placeholder { definition.universe } else { UniverseIndex::ROOT };
110
111        Self {
112            max_placeholder_universe_reached: placeholder_universe,
113            min_reachable_universe: definition.universe,
114            representative: rvid,
115            representative_is_placeholder,
116            representative_is_existential,
117        }
118    }
119
120    /// The smallest-indexed universe reachable from and/or in this SCC.
121    fn min_universe(self) -> UniverseIndex {
122        self.min_reachable_universe
123    }
124
125    fn merge_min_max_seen(&mut self, other: &Self) {
126        self.max_placeholder_universe_reached = std::cmp::max(
127            self.max_placeholder_universe_reached,
128            other.max_placeholder_universe_reached,
129        );
130
131        self.min_reachable_universe =
132            std::cmp::min(self.min_reachable_universe, other.min_reachable_universe);
133    }
134
135    /// Returns `true` if during the annotated SCC reaches a placeholder
136    /// with a universe larger than the smallest reachable one, `false` otherwise.
137    pub(crate) fn has_incompatible_universes(&self) -> bool {
138        self.min_universe().cannot_name(self.max_placeholder_universe_reached)
139    }
140}
141
142pub struct RegionInferenceContext<'tcx> {
143    pub var_infos: VarInfos,
144
145    /// Contains the definition for every region variable. Region
146    /// variables are identified by their index (`RegionVid`). The
147    /// definition contains information about where the region came
148    /// from as well as its final inferred value.
149    definitions: IndexVec<RegionVid, RegionDefinition<'tcx>>,
150
151    /// The liveness constraints added to each region. For most
152    /// regions, these start out empty and steadily grow, though for
153    /// each universally quantified region R they start out containing
154    /// the entire CFG and `end(R)`.
155    liveness_constraints: LivenessValues,
156
157    /// The outlives constraints computed by the type-check.
158    constraints: Frozen<OutlivesConstraintSet<'tcx>>,
159
160    /// The constraint-set, but in graph form, making it easy to traverse
161    /// the constraints adjacent to a particular region. Used to construct
162    /// the SCC (see `constraint_sccs`) and for error reporting.
163    constraint_graph: Frozen<NormalConstraintGraph>,
164
165    /// The SCC computed from `constraints` and the constraint
166    /// graph. We have an edge from SCC A to SCC B if `A: B`. Used to
167    /// compute the values of each region.
168    constraint_sccs: ConstraintSccs,
169
170    /// Reverse of the SCC constraint graph --  i.e., an edge `A -> B` exists if
171    /// `B: A`. This is used to compute the universal regions that are required
172    /// to outlive a given SCC. Computed lazily.
173    rev_scc_graph: Option<ReverseSccGraph>,
174
175    /// The "R0 member of [R1..Rn]" constraints, indexed by SCC.
176    member_constraints: Rc<MemberConstraintSet<'tcx, ConstraintSccIndex>>,
177
178    /// Records the member constraints that we applied to each scc.
179    /// This is useful for error reporting. Once constraint
180    /// propagation is done, this vector is sorted according to
181    /// `member_region_scc`.
182    member_constraints_applied: Vec<AppliedMemberConstraint>,
183
184    /// Map universe indexes to information on why we created it.
185    universe_causes: FxIndexMap<ty::UniverseIndex, UniverseInfo<'tcx>>,
186
187    /// The final inferred values of the region variables; we compute
188    /// one value per SCC. To get the value for any given *region*,
189    /// you first find which scc it is a part of.
190    scc_values: RegionValues<ConstraintSccIndex>,
191
192    /// Type constraints that we check after solving.
193    type_tests: Vec<TypeTest<'tcx>>,
194
195    /// Information about how the universally quantified regions in
196    /// scope on this function relate to one another.
197    universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
198}
199
200/// Each time that `apply_member_constraint` is successful, it appends
201/// one of these structs to the `member_constraints_applied` field.
202/// This is used in error reporting to trace out what happened.
203///
204/// The way that `apply_member_constraint` works is that it effectively
205/// adds a new lower bound to the SCC it is analyzing: so you wind up
206/// with `'R: 'O` where `'R` is the pick-region and `'O` is the
207/// minimal viable option.
208#[derive(Debug)]
209pub(crate) struct AppliedMemberConstraint {
210    /// The SCC that was affected. (The "member region".)
211    ///
212    /// The vector if `AppliedMemberConstraint` elements is kept sorted
213    /// by this field.
214    pub(crate) member_region_scc: ConstraintSccIndex,
215
216    /// The "best option" that `apply_member_constraint` found -- this was
217    /// added as an "ad-hoc" lower-bound to `member_region_scc`.
218    pub(crate) min_choice: ty::RegionVid,
219
220    /// The "member constraint index" -- we can find out details about
221    /// the constraint from
222    /// `set.member_constraints[member_constraint_index]`.
223    pub(crate) member_constraint_index: NllMemberConstraintIndex,
224}
225
226#[derive(Debug)]
227pub(crate) struct RegionDefinition<'tcx> {
228    /// What kind of variable is this -- a free region? existential
229    /// variable? etc. (See the `NllRegionVariableOrigin` for more
230    /// info.)
231    pub(crate) origin: NllRegionVariableOrigin,
232
233    /// Which universe is this region variable defined in? This is
234    /// most often `ty::UniverseIndex::ROOT`, but when we encounter
235    /// forall-quantifiers like `for<'a> { 'a = 'b }`, we would create
236    /// the variable for `'a` in a fresh universe that extends ROOT.
237    pub(crate) universe: ty::UniverseIndex,
238
239    /// If this is 'static or an early-bound region, then this is
240    /// `Some(X)` where `X` is the name of the region.
241    pub(crate) external_name: Option<ty::Region<'tcx>>,
242}
243
244/// N.B., the variants in `Cause` are intentionally ordered. Lower
245/// values are preferred when it comes to error messages. Do not
246/// reorder willy nilly.
247#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
248pub(crate) enum Cause {
249    /// point inserted because Local was live at the given Location
250    LiveVar(Local, Location),
251
252    /// point inserted because Local was dropped at the given Location
253    DropVar(Local, Location),
254}
255
256/// A "type test" corresponds to an outlives constraint between a type
257/// and a lifetime, like `T: 'x` or `<T as Foo>::Bar: 'x`. They are
258/// translated from the `Verify` region constraints in the ordinary
259/// inference context.
260///
261/// These sorts of constraints are handled differently than ordinary
262/// constraints, at least at present. During type checking, the
263/// `InferCtxt::process_registered_region_obligations` method will
264/// attempt to convert a type test like `T: 'x` into an ordinary
265/// outlives constraint when possible (for example, `&'a T: 'b` will
266/// be converted into `'a: 'b` and registered as a `Constraint`).
267///
268/// In some cases, however, there are outlives relationships that are
269/// not converted into a region constraint, but rather into one of
270/// these "type tests". The distinction is that a type test does not
271/// influence the inference result, but instead just examines the
272/// values that we ultimately inferred for each region variable and
273/// checks that they meet certain extra criteria. If not, an error
274/// can be issued.
275///
276/// One reason for this is that these type tests typically boil down
277/// to a check like `'a: 'x` where `'a` is a universally quantified
278/// region -- and therefore not one whose value is really meant to be
279/// *inferred*, precisely (this is not always the case: one can have a
280/// type test like `<Foo as Trait<'?0>>::Bar: 'x`, where `'?0` is an
281/// inference variable). Another reason is that these type tests can
282/// involve *disjunction* -- that is, they can be satisfied in more
283/// than one way.
284///
285/// For more information about this translation, see
286/// `InferCtxt::process_registered_region_obligations` and
287/// `InferCtxt::type_must_outlive` in `rustc_infer::infer::InferCtxt`.
288#[derive(Clone, Debug)]
289pub(crate) struct TypeTest<'tcx> {
290    /// The type `T` that must outlive the region.
291    pub generic_kind: GenericKind<'tcx>,
292
293    /// The region `'x` that the type must outlive.
294    pub lower_bound: RegionVid,
295
296    /// The span to blame.
297    pub span: Span,
298
299    /// A test which, if met by the region `'x`, proves that this type
300    /// constraint is satisfied.
301    pub verify_bound: VerifyBound<'tcx>,
302}
303
304/// When we have an unmet lifetime constraint, we try to propagate it outward (e.g. to a closure
305/// environment). If we can't, it is an error.
306#[derive(Clone, Copy, Debug, Eq, PartialEq)]
307enum RegionRelationCheckResult {
308    Ok,
309    Propagated,
310    Error,
311}
312
313#[derive(Clone, PartialEq, Eq, Debug)]
314enum Trace<'tcx> {
315    StartRegion,
316    FromOutlivesConstraint(OutlivesConstraint<'tcx>),
317    NotVisited,
318}
319
320#[instrument(skip(infcx, sccs), level = "debug")]
321fn sccs_info<'tcx>(infcx: &BorrowckInferCtxt<'tcx>, sccs: &ConstraintSccs) {
322    use crate::renumber::RegionCtxt;
323
324    let var_to_origin = infcx.reg_var_to_origin.borrow();
325
326    let mut var_to_origin_sorted = var_to_origin.clone().into_iter().collect::<Vec<_>>();
327    var_to_origin_sorted.sort_by_key(|vto| vto.0);
328
329    let mut reg_vars_to_origins_str = "region variables to origins:\n".to_string();
330    for (reg_var, origin) in var_to_origin_sorted.into_iter() {
331        reg_vars_to_origins_str.push_str(&format!("{reg_var:?}: {origin:?}\n"));
332    }
333    debug!("{}", reg_vars_to_origins_str);
334
335    let num_components = sccs.num_sccs();
336    let mut components = vec![FxIndexSet::default(); num_components];
337
338    for (reg_var_idx, scc_idx) in sccs.scc_indices().iter().enumerate() {
339        let reg_var = ty::RegionVid::from_usize(reg_var_idx);
340        let origin = var_to_origin.get(&reg_var).unwrap_or(&RegionCtxt::Unknown);
341        components[scc_idx.as_usize()].insert((reg_var, *origin));
342    }
343
344    let mut components_str = "strongly connected components:".to_string();
345    for (scc_idx, reg_vars_origins) in components.iter().enumerate() {
346        let regions_info = reg_vars_origins.clone().into_iter().collect::<Vec<_>>();
347        components_str.push_str(&format!(
348            "{:?}: {:?},\n)",
349            ConstraintSccIndex::from_usize(scc_idx),
350            regions_info,
351        ))
352    }
353    debug!("{}", components_str);
354
355    // calculate the best representative for each component
356    let components_representatives = components
357        .into_iter()
358        .enumerate()
359        .map(|(scc_idx, region_ctxts)| {
360            let repr = region_ctxts
361                .into_iter()
362                .map(|reg_var_origin| reg_var_origin.1)
363                .max_by(|x, y| x.preference_value().cmp(&y.preference_value()))
364                .unwrap();
365
366            (ConstraintSccIndex::from_usize(scc_idx), repr)
367        })
368        .collect::<FxIndexMap<_, _>>();
369
370    let mut scc_node_to_edges = FxIndexMap::default();
371    for (scc_idx, repr) in components_representatives.iter() {
372        let edge_representatives = sccs
373            .successors(*scc_idx)
374            .iter()
375            .map(|scc_idx| components_representatives[scc_idx])
376            .collect::<Vec<_>>();
377        scc_node_to_edges.insert((scc_idx, repr), edge_representatives);
378    }
379
380    debug!("SCC edges {:#?}", scc_node_to_edges);
381}
382
383impl<'tcx> RegionInferenceContext<'tcx> {
384    /// Creates a new region inference context with a total of
385    /// `num_region_variables` valid inference variables; the first N
386    /// of those will be constant regions representing the free
387    /// regions defined in `universal_regions`.
388    ///
389    /// The `outlives_constraints` and `type_tests` are an initial set
390    /// of constraints produced by the MIR type check.
391    pub(crate) fn new(
392        infcx: &BorrowckInferCtxt<'tcx>,
393        var_infos: VarInfos,
394        constraints: MirTypeckRegionConstraints<'tcx>,
395        universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
396        location_map: Rc<DenseLocationMap>,
397    ) -> Self {
398        let universal_regions = &universal_region_relations.universal_regions;
399        let MirTypeckRegionConstraints {
400            placeholder_indices,
401            placeholder_index_to_region: _,
402            liveness_constraints,
403            mut outlives_constraints,
404            mut member_constraints,
405            universe_causes,
406            type_tests,
407        } = constraints;
408
409        debug!("universal_regions: {:#?}", universal_region_relations.universal_regions);
410        debug!("outlives constraints: {:#?}", outlives_constraints);
411        debug!("placeholder_indices: {:#?}", placeholder_indices);
412        debug!("type tests: {:#?}", type_tests);
413
414        if let Some(guar) = universal_region_relations.universal_regions.tainted_by_errors() {
415            // Suppress unhelpful extra errors in `infer_opaque_types` by clearing out all
416            // outlives bounds that we may end up checking.
417            outlives_constraints = Default::default();
418            member_constraints = Default::default();
419
420            // Also taint the entire scope.
421            infcx.set_tainted_by_errors(guar);
422        }
423
424        // Create a RegionDefinition for each inference variable.
425        let definitions: IndexVec<_, _> = var_infos
426            .iter()
427            .map(|info| RegionDefinition::new(info.universe, info.origin))
428            .collect();
429
430        let constraint_sccs =
431            outlives_constraints.add_outlives_static(&universal_regions, &definitions);
432        let constraints = Frozen::freeze(outlives_constraints);
433        let constraint_graph = Frozen::freeze(constraints.graph(definitions.len()));
434
435        if cfg!(debug_assertions) {
436            sccs_info(infcx, &constraint_sccs);
437        }
438
439        let mut scc_values =
440            RegionValues::new(location_map, universal_regions.len(), placeholder_indices);
441
442        for region in liveness_constraints.regions() {
443            let scc = constraint_sccs.scc(region);
444            scc_values.merge_liveness(scc, region, &liveness_constraints);
445        }
446
447        let member_constraints =
448            Rc::new(member_constraints.into_mapped(|r| constraint_sccs.scc(r)));
449
450        let mut result = Self {
451            var_infos,
452            definitions,
453            liveness_constraints,
454            constraints,
455            constraint_graph,
456            constraint_sccs,
457            rev_scc_graph: None,
458            member_constraints,
459            member_constraints_applied: Vec::new(),
460            universe_causes,
461            scc_values,
462            type_tests,
463            universal_region_relations,
464        };
465
466        result.init_free_and_bound_regions();
467
468        result
469    }
470
471    /// Initializes the region variables for each universally
472    /// quantified region (lifetime parameter). The first N variables
473    /// always correspond to the regions appearing in the function
474    /// signature (both named and anonymous) and where-clauses. This
475    /// function iterates over those regions and initializes them with
476    /// minimum values.
477    ///
478    /// For example:
479    /// ```
480    /// fn foo<'a, 'b>( /* ... */ ) where 'a: 'b { /* ... */ }
481    /// ```
482    /// would initialize two variables like so:
483    /// ```ignore (illustrative)
484    /// R0 = { CFG, R0 } // 'a
485    /// R1 = { CFG, R0, R1 } // 'b
486    /// ```
487    /// Here, R0 represents `'a`, and it contains (a) the entire CFG
488    /// and (b) any universally quantified regions that it outlives,
489    /// which in this case is just itself. R1 (`'b`) in contrast also
490    /// outlives `'a` and hence contains R0 and R1.
491    ///
492    /// This bit of logic also handles invalid universe relations
493    /// for higher-kinded types.
494    ///
495    /// We Walk each SCC `A` and `B` such that `A: B`
496    /// and ensure that universe(A) can see universe(B).
497    ///
498    /// This serves to enforce the 'empty/placeholder' hierarchy
499    /// (described in more detail on `RegionKind`):
500    ///
501    /// ```ignore (illustrative)
502    /// static -----+
503    ///   |         |
504    /// empty(U0) placeholder(U1)
505    ///   |      /
506    /// empty(U1)
507    /// ```
508    ///
509    /// In particular, imagine we have variables R0 in U0 and R1
510    /// created in U1, and constraints like this;
511    ///
512    /// ```ignore (illustrative)
513    /// R1: !1 // R1 outlives the placeholder in U1
514    /// R1: R0 // R1 outlives R0
515    /// ```
516    ///
517    /// Here, we wish for R1 to be `'static`, because it
518    /// cannot outlive `placeholder(U1)` and `empty(U0)` any other way.
519    ///
520    /// Thanks to this loop, what happens is that the `R1: R0`
521    /// constraint has lowered the universe of `R1` to `U0`, which in turn
522    /// means that the `R1: !1` constraint here will cause
523    /// `R1` to become `'static`.
524    fn init_free_and_bound_regions(&mut self) {
525        // Update the names (if any)
526        // This iterator has unstable order but we collect it all into an IndexVec
527        for (external_name, variable) in
528            self.universal_region_relations.universal_regions.named_universal_regions_iter()
529        {
530            debug!(
531                "init_free_and_bound_regions: region {:?} has external name {:?}",
532                variable, external_name
533            );
534            self.definitions[variable].external_name = Some(external_name);
535        }
536
537        for variable in self.definitions.indices() {
538            let scc = self.constraint_sccs.scc(variable);
539
540            match self.definitions[variable].origin {
541                NllRegionVariableOrigin::FreeRegion => {
542                    // For each free, universally quantified region X:
543
544                    // Add all nodes in the CFG to liveness constraints
545                    self.liveness_constraints.add_all_points(variable);
546                    self.scc_values.add_all_points(scc);
547
548                    // Add `end(X)` into the set for X.
549                    self.scc_values.add_element(scc, variable);
550                }
551
552                NllRegionVariableOrigin::Placeholder(placeholder) => {
553                    self.scc_values.add_element(scc, placeholder);
554                }
555
556                NllRegionVariableOrigin::Existential { .. } => {
557                    // For existential, regions, nothing to do.
558                }
559            }
560        }
561    }
562
563    /// Returns an iterator over all the region indices.
564    pub(crate) fn regions(&self) -> impl Iterator<Item = RegionVid> + 'tcx {
565        self.definitions.indices()
566    }
567
568    /// Given a universal region in scope on the MIR, returns the
569    /// corresponding index.
570    ///
571    /// Panics if `r` is not a registered universal region, most notably
572    /// if it is a placeholder. Handling placeholders requires access to the
573    /// `MirTypeckRegionConstraints`.
574    pub(crate) fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
575        self.universal_regions().to_region_vid(r)
576    }
577
578    /// Returns an iterator over all the outlives constraints.
579    pub(crate) fn outlives_constraints(
580        &self,
581    ) -> impl Iterator<Item = OutlivesConstraint<'tcx>> + '_ {
582        self.constraints.outlives().iter().copied()
583    }
584
585    /// Adds annotations for `#[rustc_regions]`; see `UniversalRegions::annotate`.
586    pub(crate) fn annotate(&self, tcx: TyCtxt<'tcx>, err: &mut Diag<'_, ()>) {
587        self.universal_regions().annotate(tcx, err)
588    }
589
590    /// Returns `true` if the region `r` contains the point `p`.
591    ///
592    /// Panics if called before `solve()` executes,
593    pub(crate) fn region_contains(&self, r: RegionVid, p: impl ToElementIndex) -> bool {
594        let scc = self.constraint_sccs.scc(r);
595        self.scc_values.contains(scc, p)
596    }
597
598    /// Returns the lowest statement index in `start..=end` which is not contained by `r`.
599    ///
600    /// Panics if called before `solve()` executes.
601    pub(crate) fn first_non_contained_inclusive(
602        &self,
603        r: RegionVid,
604        block: BasicBlock,
605        start: usize,
606        end: usize,
607    ) -> Option<usize> {
608        let scc = self.constraint_sccs.scc(r);
609        self.scc_values.first_non_contained_inclusive(scc, block, start, end)
610    }
611
612    /// Returns access to the value of `r` for debugging purposes.
613    pub(crate) fn region_value_str(&self, r: RegionVid) -> String {
614        let scc = self.constraint_sccs.scc(r);
615        self.scc_values.region_value_str(scc)
616    }
617
618    pub(crate) fn placeholders_contained_in<'a>(
619        &'a self,
620        r: RegionVid,
621    ) -> impl Iterator<Item = ty::PlaceholderRegion> + 'a {
622        let scc = self.constraint_sccs.scc(r);
623        self.scc_values.placeholders_contained_in(scc)
624    }
625
626    /// Returns access to the value of `r` for debugging purposes.
627    pub(crate) fn region_universe(&self, r: RegionVid) -> ty::UniverseIndex {
628        self.scc_universe(self.constraint_sccs.scc(r))
629    }
630
631    /// Once region solving has completed, this function will return the member constraints that
632    /// were applied to the value of a given SCC `scc`. See `AppliedMemberConstraint`.
633    pub(crate) fn applied_member_constraints(
634        &self,
635        scc: ConstraintSccIndex,
636    ) -> &[AppliedMemberConstraint] {
637        binary_search_util::binary_search_slice(
638            &self.member_constraints_applied,
639            |applied| applied.member_region_scc,
640            &scc,
641        )
642    }
643
644    /// Performs region inference and report errors if we see any
645    /// unsatisfiable constraints. If this is a closure, returns the
646    /// region requirements to propagate to our creator, if any.
647    #[instrument(skip(self, infcx, body, polonius_output), level = "debug")]
648    pub(super) fn solve(
649        &mut self,
650        infcx: &InferCtxt<'tcx>,
651        body: &Body<'tcx>,
652        polonius_output: Option<Box<PoloniusOutput>>,
653    ) -> (Option<ClosureRegionRequirements<'tcx>>, RegionErrors<'tcx>) {
654        let mir_def_id = body.source.def_id();
655        self.propagate_constraints();
656
657        let mut errors_buffer = RegionErrors::new(infcx.tcx);
658
659        // If this is a closure, we can propagate unsatisfied
660        // `outlives_requirements` to our creator, so create a vector
661        // to store those. Otherwise, we'll pass in `None` to the
662        // functions below, which will trigger them to report errors
663        // eagerly.
664        let mut outlives_requirements = infcx.tcx.is_typeck_child(mir_def_id).then(Vec::new);
665
666        self.check_type_tests(infcx, outlives_requirements.as_mut(), &mut errors_buffer);
667
668        debug!(?errors_buffer);
669        debug!(?outlives_requirements);
670
671        // In Polonius mode, the errors about missing universal region relations are in the output
672        // and need to be emitted or propagated. Otherwise, we need to check whether the
673        // constraints were too strong, and if so, emit or propagate those errors.
674        if infcx.tcx.sess.opts.unstable_opts.polonius.is_legacy_enabled() {
675            self.check_polonius_subset_errors(
676                outlives_requirements.as_mut(),
677                &mut errors_buffer,
678                polonius_output
679                    .as_ref()
680                    .expect("Polonius output is unavailable despite `-Z polonius`"),
681            );
682        } else {
683            self.check_universal_regions(outlives_requirements.as_mut(), &mut errors_buffer);
684        }
685
686        debug!(?errors_buffer);
687
688        if errors_buffer.is_empty() {
689            self.check_member_constraints(infcx, &mut errors_buffer);
690        }
691
692        debug!(?errors_buffer);
693
694        let outlives_requirements = outlives_requirements.unwrap_or_default();
695
696        if outlives_requirements.is_empty() {
697            (None, errors_buffer)
698        } else {
699            let num_external_vids = self.universal_regions().num_global_and_external_regions();
700            (
701                Some(ClosureRegionRequirements { num_external_vids, outlives_requirements }),
702                errors_buffer,
703            )
704        }
705    }
706
707    /// Propagate the region constraints: this will grow the values
708    /// for each region variable until all the constraints are
709    /// satisfied. Note that some values may grow **too** large to be
710    /// feasible, but we check this later.
711    #[instrument(skip(self), level = "debug")]
712    fn propagate_constraints(&mut self) {
713        debug!("constraints={:#?}", {
714            let mut constraints: Vec<_> = self.outlives_constraints().collect();
715            constraints.sort_by_key(|c| (c.sup, c.sub));
716            constraints
717                .into_iter()
718                .map(|c| (c, self.constraint_sccs.scc(c.sup), self.constraint_sccs.scc(c.sub)))
719                .collect::<Vec<_>>()
720        });
721
722        // To propagate constraints, we walk the DAG induced by the
723        // SCC. For each SCC, we visit its successors and compute
724        // their values, then we union all those values to get our
725        // own.
726        for scc in self.constraint_sccs.all_sccs() {
727            self.compute_value_for_scc(scc);
728        }
729
730        // Sort the applied member constraints so we can binary search
731        // through them later.
732        self.member_constraints_applied.sort_by_key(|applied| applied.member_region_scc);
733    }
734
735    /// Computes the value of the SCC `scc_a`, which has not yet been
736    /// computed, by unioning the values of its successors.
737    /// Assumes that all successors have been computed already
738    /// (which is assured by iterating over SCCs in dependency order).
739    #[instrument(skip(self), level = "debug")]
740    fn compute_value_for_scc(&mut self, scc_a: ConstraintSccIndex) {
741        // Walk each SCC `B` such that `A: B`...
742        for &scc_b in self.constraint_sccs.successors(scc_a) {
743            debug!(?scc_b);
744            self.scc_values.add_region(scc_a, scc_b);
745        }
746
747        // Now take member constraints into account.
748        let member_constraints = Rc::clone(&self.member_constraints);
749        for m_c_i in member_constraints.indices(scc_a) {
750            self.apply_member_constraint(scc_a, m_c_i, member_constraints.choice_regions(m_c_i));
751        }
752
753        debug!(value = ?self.scc_values.region_value_str(scc_a));
754    }
755
756    /// Invoked for each `R0 member of [R1..Rn]` constraint.
757    ///
758    /// `scc` is the SCC containing R0, and `choice_regions` are the
759    /// `R1..Rn` regions -- they are always known to be universal
760    /// regions (and if that's not true, we just don't attempt to
761    /// enforce the constraint).
762    ///
763    /// The current value of `scc` at the time the method is invoked
764    /// is considered a *lower bound*. If possible, we will modify
765    /// the constraint to set it equal to one of the option regions.
766    /// If we make any changes, returns true, else false.
767    ///
768    /// This function only adds the member constraints to the region graph,
769    /// it does not check them. They are later checked in
770    /// `check_member_constraints` after the region graph has been computed.
771    #[instrument(skip(self, member_constraint_index), level = "debug")]
772    fn apply_member_constraint(
773        &mut self,
774        scc: ConstraintSccIndex,
775        member_constraint_index: NllMemberConstraintIndex,
776        choice_regions: &[ty::RegionVid],
777    ) {
778        // Lazily compute the reverse graph, we'll need it later.
779        self.compute_reverse_scc_graph();
780
781        // Create a mutable vector of the options. We'll try to winnow
782        // them down.
783        let mut choice_regions: Vec<ty::RegionVid> = choice_regions.to_vec();
784
785        // Convert to the SCC representative: sometimes we have inference
786        // variables in the member constraint that wind up equated with
787        // universal regions. The scc representative is the minimal numbered
788        // one from the corresponding scc so it will be the universal region
789        // if one exists.
790        for c_r in &mut choice_regions {
791            let scc = self.constraint_sccs.scc(*c_r);
792            *c_r = self.scc_representative(scc);
793        }
794
795        // If the member region lives in a higher universe, we currently choose
796        // the most conservative option by leaving it unchanged.
797        if !self.constraint_sccs().annotation(scc).min_universe().is_root() {
798            return;
799        }
800
801        // The existing value for `scc` is a lower-bound. This will
802        // consist of some set `{P} + {LB}` of points `{P}` and
803        // lower-bound free regions `{LB}`. As each choice region `O`
804        // is a free region, it will outlive the points. But we can
805        // only consider the option `O` if `O: LB`.
806        choice_regions.retain(|&o_r| {
807            self.scc_values
808                .universal_regions_outlived_by(scc)
809                .all(|lb| self.universal_region_relations.outlives(o_r, lb))
810        });
811        debug!(?choice_regions, "after lb");
812
813        // Now find all the *upper bounds* -- that is, each UB is a
814        // free region that must outlive the member region `R0` (`UB:
815        // R0`). Therefore, we need only keep an option `O` if `UB: O`
816        // for all UB.
817        let universal_region_relations = &self.universal_region_relations;
818        for ub in self.rev_scc_graph.as_ref().unwrap().upper_bounds(scc) {
819            debug!(?ub);
820            choice_regions.retain(|&o_r| universal_region_relations.outlives(ub, o_r));
821        }
822        debug!(?choice_regions, "after ub");
823
824        // At this point we can pick any member of `choice_regions` and would like to choose
825        // it to be a small as possible. To avoid potential non-determinism we will pick the
826        // smallest such choice.
827        //
828        // Because universal regions are only partially ordered (i.e, not every two regions are
829        // comparable), we will ignore any region that doesn't compare to all others when picking
830        // the minimum choice.
831        //
832        // For example, consider `choice_regions = ['static, 'a, 'b, 'c, 'd, 'e]`, where
833        // `'static: 'a, 'static: 'b, 'a: 'c, 'b: 'c, 'c: 'd, 'c: 'e`.
834        // `['d, 'e]` are ignored because they do not compare - the same goes for `['a, 'b]`.
835        let totally_ordered_subset = choice_regions.iter().copied().filter(|&r1| {
836            choice_regions.iter().all(|&r2| {
837                self.universal_region_relations.outlives(r1, r2)
838                    || self.universal_region_relations.outlives(r2, r1)
839            })
840        });
841        // Now we're left with `['static, 'c]`. Pick `'c` as the minimum!
842        let Some(min_choice) = totally_ordered_subset.reduce(|r1, r2| {
843            let r1_outlives_r2 = self.universal_region_relations.outlives(r1, r2);
844            let r2_outlives_r1 = self.universal_region_relations.outlives(r2, r1);
845            match (r1_outlives_r2, r2_outlives_r1) {
846                (true, true) => r1.min(r2),
847                (true, false) => r2,
848                (false, true) => r1,
849                (false, false) => bug!("incomparable regions in total order"),
850            }
851        }) else {
852            debug!("no unique minimum choice");
853            return;
854        };
855
856        // As we require `'scc: 'min_choice`, we have definitely already computed
857        // its `scc_values` at this point.
858        let min_choice_scc = self.constraint_sccs.scc(min_choice);
859        debug!(?min_choice, ?min_choice_scc);
860        if self.scc_values.add_region(scc, min_choice_scc) {
861            self.member_constraints_applied.push(AppliedMemberConstraint {
862                member_region_scc: scc,
863                min_choice,
864                member_constraint_index,
865            });
866        }
867    }
868
869    /// Returns `true` if all the elements in the value of `scc_b` are nameable
870    /// in `scc_a`. Used during constraint propagation, and only once
871    /// the value of `scc_b` has been computed.
872    fn universe_compatible(&self, scc_b: ConstraintSccIndex, scc_a: ConstraintSccIndex) -> bool {
873        let a_annotation = self.constraint_sccs().annotation(scc_a);
874        let b_annotation = self.constraint_sccs().annotation(scc_b);
875        let a_universe = a_annotation.min_universe();
876
877        // If scc_b's declared universe is a subset of
878        // scc_a's declared universe (typically, both are ROOT), then
879        // it cannot contain any problematic universe elements.
880        if a_universe.can_name(b_annotation.min_universe()) {
881            return true;
882        }
883
884        // Otherwise, there can be no placeholder in `b` with a too high
885        // universe index to name from `a`.
886        a_universe.can_name(b_annotation.max_placeholder_universe_reached)
887    }
888
889    /// Once regions have been propagated, this method is used to see
890    /// whether the "type tests" produced by typeck were satisfied;
891    /// type tests encode type-outlives relationships like `T:
892    /// 'a`. See `TypeTest` for more details.
893    fn check_type_tests(
894        &self,
895        infcx: &InferCtxt<'tcx>,
896        mut propagated_outlives_requirements: Option<&mut Vec<ClosureOutlivesRequirement<'tcx>>>,
897        errors_buffer: &mut RegionErrors<'tcx>,
898    ) {
899        let tcx = infcx.tcx;
900
901        // Sometimes we register equivalent type-tests that would
902        // result in basically the exact same error being reported to
903        // the user. Avoid that.
904        let mut deduplicate_errors = FxIndexSet::default();
905
906        for type_test in &self.type_tests {
907            debug!("check_type_test: {:?}", type_test);
908
909            let generic_ty = type_test.generic_kind.to_ty(tcx);
910            if self.eval_verify_bound(
911                infcx,
912                generic_ty,
913                type_test.lower_bound,
914                &type_test.verify_bound,
915            ) {
916                continue;
917            }
918
919            if let Some(propagated_outlives_requirements) = &mut propagated_outlives_requirements {
920                if self.try_promote_type_test(infcx, type_test, propagated_outlives_requirements) {
921                    continue;
922                }
923            }
924
925            // Type-test failed. Report the error.
926            let erased_generic_kind = infcx.tcx.erase_regions(type_test.generic_kind);
927
928            // Skip duplicate-ish errors.
929            if deduplicate_errors.insert((
930                erased_generic_kind,
931                type_test.lower_bound,
932                type_test.span,
933            )) {
934                debug!(
935                    "check_type_test: reporting error for erased_generic_kind={:?}, \
936                     lower_bound_region={:?}, \
937                     type_test.span={:?}",
938                    erased_generic_kind, type_test.lower_bound, type_test.span,
939                );
940
941                errors_buffer.push(RegionErrorKind::TypeTestError { type_test: type_test.clone() });
942            }
943        }
944    }
945
946    /// Invoked when we have some type-test (e.g., `T: 'X`) that we cannot
947    /// prove to be satisfied. If this is a closure, we will attempt to
948    /// "promote" this type-test into our `ClosureRegionRequirements` and
949    /// hence pass it up the creator. To do this, we have to phrase the
950    /// type-test in terms of external free regions, as local free
951    /// regions are not nameable by the closure's creator.
952    ///
953    /// Promotion works as follows: we first check that the type `T`
954    /// contains only regions that the creator knows about. If this is
955    /// true, then -- as a consequence -- we know that all regions in
956    /// the type `T` are free regions that outlive the closure body. If
957    /// false, then promotion fails.
958    ///
959    /// Once we've promoted T, we have to "promote" `'X` to some region
960    /// that is "external" to the closure. Generally speaking, a region
961    /// may be the union of some points in the closure body as well as
962    /// various free lifetimes. We can ignore the points in the closure
963    /// body: if the type T can be expressed in terms of external regions,
964    /// we know it outlives the points in the closure body. That
965    /// just leaves the free regions.
966    ///
967    /// The idea then is to lower the `T: 'X` constraint into multiple
968    /// bounds -- e.g., if `'X` is the union of two free lifetimes,
969    /// `'1` and `'2`, then we would create `T: '1` and `T: '2`.
970    #[instrument(level = "debug", skip(self, infcx, propagated_outlives_requirements))]
971    fn try_promote_type_test(
972        &self,
973        infcx: &InferCtxt<'tcx>,
974        type_test: &TypeTest<'tcx>,
975        propagated_outlives_requirements: &mut Vec<ClosureOutlivesRequirement<'tcx>>,
976    ) -> bool {
977        let tcx = infcx.tcx;
978        let TypeTest { generic_kind, lower_bound, span: blame_span, verify_bound: _ } = *type_test;
979
980        let generic_ty = generic_kind.to_ty(tcx);
981        let Some(subject) = self.try_promote_type_test_subject(infcx, generic_ty) else {
982            return false;
983        };
984
985        let r_scc = self.constraint_sccs.scc(lower_bound);
986        debug!(
987            "lower_bound = {:?} r_scc={:?} universe={:?}",
988            lower_bound,
989            r_scc,
990            self.constraint_sccs.annotation(r_scc).min_universe()
991        );
992        // If the type test requires that `T: 'a` where `'a` is a
993        // placeholder from another universe, that effectively requires
994        // `T: 'static`, so we have to propagate that requirement.
995        //
996        // It doesn't matter *what* universe because the promoted `T` will
997        // always be in the root universe.
998        if let Some(p) = self.scc_values.placeholders_contained_in(r_scc).next() {
999            debug!("encountered placeholder in higher universe: {:?}, requiring 'static", p);
1000            let static_r = self.universal_regions().fr_static;
1001            propagated_outlives_requirements.push(ClosureOutlivesRequirement {
1002                subject,
1003                outlived_free_region: static_r,
1004                blame_span,
1005                category: ConstraintCategory::Boring,
1006            });
1007
1008            // we can return here -- the code below might push add'l constraints
1009            // but they would all be weaker than this one.
1010            return true;
1011        }
1012
1013        // For each region outlived by lower_bound find a non-local,
1014        // universal region (it may be the same region) and add it to
1015        // `ClosureOutlivesRequirement`.
1016        let mut found_outlived_universal_region = false;
1017        for ur in self.scc_values.universal_regions_outlived_by(r_scc) {
1018            found_outlived_universal_region = true;
1019            debug!("universal_region_outlived_by ur={:?}", ur);
1020            let non_local_ub = self.universal_region_relations.non_local_upper_bounds(ur);
1021            debug!(?non_local_ub);
1022
1023            // This is slightly too conservative. To show T: '1, given `'2: '1`
1024            // and `'3: '1` we only need to prove that T: '2 *or* T: '3, but to
1025            // avoid potential non-determinism we approximate this by requiring
1026            // T: '1 and T: '2.
1027            for upper_bound in non_local_ub {
1028                debug_assert!(self.universal_regions().is_universal_region(upper_bound));
1029                debug_assert!(!self.universal_regions().is_local_free_region(upper_bound));
1030
1031                let requirement = ClosureOutlivesRequirement {
1032                    subject,
1033                    outlived_free_region: upper_bound,
1034                    blame_span,
1035                    category: ConstraintCategory::Boring,
1036                };
1037                debug!(?requirement, "adding closure requirement");
1038                propagated_outlives_requirements.push(requirement);
1039            }
1040        }
1041        // If we succeed to promote the subject, i.e. it only contains non-local regions,
1042        // and fail to prove the type test inside of the closure, the `lower_bound` has to
1043        // also be at least as large as some universal region, as the type test is otherwise
1044        // trivial.
1045        assert!(found_outlived_universal_region);
1046        true
1047    }
1048
1049    /// When we promote a type test `T: 'r`, we have to replace all region
1050    /// variables in the type `T` with an equal universal region from the
1051    /// closure signature.
1052    /// This is not always possible, so this is a fallible process.
1053    #[instrument(level = "debug", skip(self, infcx), ret)]
1054    fn try_promote_type_test_subject(
1055        &self,
1056        infcx: &InferCtxt<'tcx>,
1057        ty: Ty<'tcx>,
1058    ) -> Option<ClosureOutlivesSubject<'tcx>> {
1059        let tcx = infcx.tcx;
1060        let mut failed = false;
1061        let ty = fold_regions(tcx, ty, |r, _depth| {
1062            let r_vid = self.to_region_vid(r);
1063            let r_scc = self.constraint_sccs.scc(r_vid);
1064
1065            // The challenge is this. We have some region variable `r`
1066            // whose value is a set of CFG points and universal
1067            // regions. We want to find if that set is *equivalent* to
1068            // any of the named regions found in the closure.
1069            // To do so, we simply check every candidate `u_r` for equality.
1070            self.scc_values
1071                .universal_regions_outlived_by(r_scc)
1072                .filter(|&u_r| !self.universal_regions().is_local_free_region(u_r))
1073                .find(|&u_r| self.eval_equal(u_r, r_vid))
1074                .map(|u_r| ty::Region::new_var(tcx, u_r))
1075                // In case we could not find a named region to map to,
1076                // we will return `None` below.
1077                .unwrap_or_else(|| {
1078                    failed = true;
1079                    r
1080                })
1081        });
1082
1083        debug!("try_promote_type_test_subject: folded ty = {:?}", ty);
1084
1085        // This will be true if we failed to promote some region.
1086        if failed {
1087            return None;
1088        }
1089
1090        Some(ClosureOutlivesSubject::Ty(ClosureOutlivesSubjectTy::bind(tcx, ty)))
1091    }
1092
1093    /// Like `universal_upper_bound`, but returns an approximation more suitable
1094    /// for diagnostics. If `r` contains multiple disjoint universal regions
1095    /// (e.g. 'a and 'b in `fn foo<'a, 'b> { ... }`, we pick the lower-numbered region.
1096    /// This corresponds to picking named regions over unnamed regions
1097    /// (e.g. picking early-bound regions over a closure late-bound region).
1098    ///
1099    /// This means that the returned value may not be a true upper bound, since
1100    /// only 'static is known to outlive disjoint universal regions.
1101    /// Therefore, this method should only be used in diagnostic code,
1102    /// where displaying *some* named universal region is better than
1103    /// falling back to 'static.
1104    #[instrument(level = "debug", skip(self))]
1105    pub(crate) fn approx_universal_upper_bound(&self, r: RegionVid) -> RegionVid {
1106        debug!("{}", self.region_value_str(r));
1107
1108        // Find the smallest universal region that contains all other
1109        // universal regions within `region`.
1110        let mut lub = self.universal_regions().fr_fn_body;
1111        let r_scc = self.constraint_sccs.scc(r);
1112        let static_r = self.universal_regions().fr_static;
1113        for ur in self.scc_values.universal_regions_outlived_by(r_scc) {
1114            let new_lub = self.universal_region_relations.postdom_upper_bound(lub, ur);
1115            debug!(?ur, ?lub, ?new_lub);
1116            // The upper bound of two non-static regions is static: this
1117            // means we know nothing about the relationship between these
1118            // two regions. Pick a 'better' one to use when constructing
1119            // a diagnostic
1120            if ur != static_r && lub != static_r && new_lub == static_r {
1121                // Prefer the region with an `external_name` - this
1122                // indicates that the region is early-bound, so working with
1123                // it can produce a nicer error.
1124                if self.region_definition(ur).external_name.is_some() {
1125                    lub = ur;
1126                } else if self.region_definition(lub).external_name.is_some() {
1127                    // Leave lub unchanged
1128                } else {
1129                    // If we get here, we don't have any reason to prefer
1130                    // one region over the other. Just pick the
1131                    // one with the lower index for now.
1132                    lub = std::cmp::min(ur, lub);
1133                }
1134            } else {
1135                lub = new_lub;
1136            }
1137        }
1138
1139        debug!(?r, ?lub);
1140
1141        lub
1142    }
1143
1144    /// Tests if `test` is true when applied to `lower_bound` at
1145    /// `point`.
1146    fn eval_verify_bound(
1147        &self,
1148        infcx: &InferCtxt<'tcx>,
1149        generic_ty: Ty<'tcx>,
1150        lower_bound: RegionVid,
1151        verify_bound: &VerifyBound<'tcx>,
1152    ) -> bool {
1153        debug!("eval_verify_bound(lower_bound={:?}, verify_bound={:?})", lower_bound, verify_bound);
1154
1155        match verify_bound {
1156            VerifyBound::IfEq(verify_if_eq_b) => {
1157                self.eval_if_eq(infcx, generic_ty, lower_bound, *verify_if_eq_b)
1158            }
1159
1160            VerifyBound::IsEmpty => {
1161                let lower_bound_scc = self.constraint_sccs.scc(lower_bound);
1162                self.scc_values.elements_contained_in(lower_bound_scc).next().is_none()
1163            }
1164
1165            VerifyBound::OutlivedBy(r) => {
1166                let r_vid = self.to_region_vid(*r);
1167                self.eval_outlives(r_vid, lower_bound)
1168            }
1169
1170            VerifyBound::AnyBound(verify_bounds) => verify_bounds.iter().any(|verify_bound| {
1171                self.eval_verify_bound(infcx, generic_ty, lower_bound, verify_bound)
1172            }),
1173
1174            VerifyBound::AllBounds(verify_bounds) => verify_bounds.iter().all(|verify_bound| {
1175                self.eval_verify_bound(infcx, generic_ty, lower_bound, verify_bound)
1176            }),
1177        }
1178    }
1179
1180    fn eval_if_eq(
1181        &self,
1182        infcx: &InferCtxt<'tcx>,
1183        generic_ty: Ty<'tcx>,
1184        lower_bound: RegionVid,
1185        verify_if_eq_b: ty::Binder<'tcx, VerifyIfEq<'tcx>>,
1186    ) -> bool {
1187        let generic_ty = self.normalize_to_scc_representatives(infcx.tcx, generic_ty);
1188        let verify_if_eq_b = self.normalize_to_scc_representatives(infcx.tcx, verify_if_eq_b);
1189        match test_type_match::extract_verify_if_eq(infcx.tcx, &verify_if_eq_b, generic_ty) {
1190            Some(r) => {
1191                let r_vid = self.to_region_vid(r);
1192                self.eval_outlives(r_vid, lower_bound)
1193            }
1194            None => false,
1195        }
1196    }
1197
1198    /// This is a conservative normalization procedure. It takes every
1199    /// free region in `value` and replaces it with the
1200    /// "representative" of its SCC (see `scc_representatives` field).
1201    /// We are guaranteed that if two values normalize to the same
1202    /// thing, then they are equal; this is a conservative check in
1203    /// that they could still be equal even if they normalize to
1204    /// different results. (For example, there might be two regions
1205    /// with the same value that are not in the same SCC).
1206    ///
1207    /// N.B., this is not an ideal approach and I would like to revisit
1208    /// it. However, it works pretty well in practice. In particular,
1209    /// this is needed to deal with projection outlives bounds like
1210    ///
1211    /// ```text
1212    /// <T as Foo<'0>>::Item: '1
1213    /// ```
1214    ///
1215    /// In particular, this routine winds up being important when
1216    /// there are bounds like `where <T as Foo<'a>>::Item: 'b` in the
1217    /// environment. In this case, if we can show that `'0 == 'a`,
1218    /// and that `'b: '1`, then we know that the clause is
1219    /// satisfied. In such cases, particularly due to limitations of
1220    /// the trait solver =), we usually wind up with a where-clause like
1221    /// `T: Foo<'a>` in scope, which thus forces `'0 == 'a` to be added as
1222    /// a constraint, and thus ensures that they are in the same SCC.
1223    ///
1224    /// So why can't we do a more correct routine? Well, we could
1225    /// *almost* use the `relate_tys` code, but the way it is
1226    /// currently setup it creates inference variables to deal with
1227    /// higher-ranked things and so forth, and right now the inference
1228    /// context is not permitted to make more inference variables. So
1229    /// we use this kind of hacky solution.
1230    fn normalize_to_scc_representatives<T>(&self, tcx: TyCtxt<'tcx>, value: T) -> T
1231    where
1232        T: TypeFoldable<TyCtxt<'tcx>>,
1233    {
1234        fold_regions(tcx, value, |r, _db| {
1235            let vid = self.to_region_vid(r);
1236            let scc = self.constraint_sccs.scc(vid);
1237            let repr = self.scc_representative(scc);
1238            ty::Region::new_var(tcx, repr)
1239        })
1240    }
1241
1242    /// Evaluate whether `sup_region == sub_region`.
1243    ///
1244    /// Panics if called before `solve()` executes,
1245    // This is `pub` because it's used by unstable external borrowck data users, see `consumers.rs`.
1246    pub fn eval_equal(&self, r1: RegionVid, r2: RegionVid) -> bool {
1247        self.eval_outlives(r1, r2) && self.eval_outlives(r2, r1)
1248    }
1249
1250    /// Evaluate whether `sup_region: sub_region`.
1251    ///
1252    /// Panics if called before `solve()` executes,
1253    // This is `pub` because it's used by unstable external borrowck data users, see `consumers.rs`.
1254    #[instrument(skip(self), level = "debug", ret)]
1255    pub fn eval_outlives(&self, sup_region: RegionVid, sub_region: RegionVid) -> bool {
1256        debug!(
1257            "sup_region's value = {:?} universal={:?}",
1258            self.region_value_str(sup_region),
1259            self.universal_regions().is_universal_region(sup_region),
1260        );
1261        debug!(
1262            "sub_region's value = {:?} universal={:?}",
1263            self.region_value_str(sub_region),
1264            self.universal_regions().is_universal_region(sub_region),
1265        );
1266
1267        let sub_region_scc = self.constraint_sccs.scc(sub_region);
1268        let sup_region_scc = self.constraint_sccs.scc(sup_region);
1269
1270        // If we are checking that `'sup: 'sub`, and `'sub` contains
1271        // some placeholder that `'sup` cannot name, then this is only
1272        // true if `'sup` outlives static.
1273        if !self.universe_compatible(sub_region_scc, sup_region_scc) {
1274            debug!(
1275                "sub universe `{sub_region_scc:?}` is not nameable \
1276                by super `{sup_region_scc:?}`, promoting to static",
1277            );
1278
1279            return self.eval_outlives(sup_region, self.universal_regions().fr_static);
1280        }
1281
1282        // Both the `sub_region` and `sup_region` consist of the union
1283        // of some number of universal regions (along with the union
1284        // of various points in the CFG; ignore those points for
1285        // now). Therefore, the sup-region outlives the sub-region if,
1286        // for each universal region R1 in the sub-region, there
1287        // exists some region R2 in the sup-region that outlives R1.
1288        let universal_outlives =
1289            self.scc_values.universal_regions_outlived_by(sub_region_scc).all(|r1| {
1290                self.scc_values
1291                    .universal_regions_outlived_by(sup_region_scc)
1292                    .any(|r2| self.universal_region_relations.outlives(r2, r1))
1293            });
1294
1295        if !universal_outlives {
1296            debug!("sub region contains a universal region not present in super");
1297            return false;
1298        }
1299
1300        // Now we have to compare all the points in the sub region and make
1301        // sure they exist in the sup region.
1302
1303        if self.universal_regions().is_universal_region(sup_region) {
1304            // Micro-opt: universal regions contain all points.
1305            debug!("super is universal and hence contains all points");
1306            return true;
1307        }
1308
1309        debug!("comparison between points in sup/sub");
1310
1311        self.scc_values.contains_points(sup_region_scc, sub_region_scc)
1312    }
1313
1314    /// Once regions have been propagated, this method is used to see
1315    /// whether any of the constraints were too strong. In particular,
1316    /// we want to check for a case where a universally quantified
1317    /// region exceeded its bounds. Consider:
1318    /// ```compile_fail
1319    /// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
1320    /// ```
1321    /// In this case, returning `x` requires `&'a u32 <: &'b u32`
1322    /// and hence we establish (transitively) a constraint that
1323    /// `'a: 'b`. The `propagate_constraints` code above will
1324    /// therefore add `end('a)` into the region for `'b` -- but we
1325    /// have no evidence that `'b` outlives `'a`, so we want to report
1326    /// an error.
1327    ///
1328    /// If `propagated_outlives_requirements` is `Some`, then we will
1329    /// push unsatisfied obligations into there. Otherwise, we'll
1330    /// report them as errors.
1331    fn check_universal_regions(
1332        &self,
1333        mut propagated_outlives_requirements: Option<&mut Vec<ClosureOutlivesRequirement<'tcx>>>,
1334        errors_buffer: &mut RegionErrors<'tcx>,
1335    ) {
1336        for (fr, fr_definition) in self.definitions.iter_enumerated() {
1337            debug!(?fr, ?fr_definition);
1338            match fr_definition.origin {
1339                NllRegionVariableOrigin::FreeRegion => {
1340                    // Go through each of the universal regions `fr` and check that
1341                    // they did not grow too large, accumulating any requirements
1342                    // for our caller into the `outlives_requirements` vector.
1343                    self.check_universal_region(
1344                        fr,
1345                        &mut propagated_outlives_requirements,
1346                        errors_buffer,
1347                    );
1348                }
1349
1350                NllRegionVariableOrigin::Placeholder(placeholder) => {
1351                    self.check_bound_universal_region(fr, placeholder, errors_buffer);
1352                }
1353
1354                NllRegionVariableOrigin::Existential { .. } => {
1355                    // nothing to check here
1356                }
1357            }
1358        }
1359    }
1360
1361    /// Checks if Polonius has found any unexpected free region relations.
1362    ///
1363    /// In Polonius terms, a "subset error" (or "illegal subset relation error") is the equivalent
1364    /// of NLL's "checking if any region constraints were too strong": a placeholder origin `'a`
1365    /// was unexpectedly found to be a subset of another placeholder origin `'b`, and means in NLL
1366    /// terms that the "longer free region" `'a` outlived the "shorter free region" `'b`.
1367    ///
1368    /// More details can be found in this blog post by Niko:
1369    /// <https://smallcultfollowing.com/babysteps/blog/2019/01/17/polonius-and-region-errors/>
1370    ///
1371    /// In the canonical example
1372    /// ```compile_fail
1373    /// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
1374    /// ```
1375    /// returning `x` requires `&'a u32 <: &'b u32` and hence we establish (transitively) a
1376    /// constraint that `'a: 'b`. It is an error that we have no evidence that this
1377    /// constraint holds.
1378    ///
1379    /// If `propagated_outlives_requirements` is `Some`, then we will
1380    /// push unsatisfied obligations into there. Otherwise, we'll
1381    /// report them as errors.
1382    fn check_polonius_subset_errors(
1383        &self,
1384        mut propagated_outlives_requirements: Option<&mut Vec<ClosureOutlivesRequirement<'tcx>>>,
1385        errors_buffer: &mut RegionErrors<'tcx>,
1386        polonius_output: &PoloniusOutput,
1387    ) {
1388        debug!(
1389            "check_polonius_subset_errors: {} subset_errors",
1390            polonius_output.subset_errors.len()
1391        );
1392
1393        // Similarly to `check_universal_regions`: a free region relation, which was not explicitly
1394        // declared ("known") was found by Polonius, so emit an error, or propagate the
1395        // requirements for our caller into the `propagated_outlives_requirements` vector.
1396        //
1397        // Polonius doesn't model regions ("origins") as CFG-subsets or durations, but the
1398        // `longer_fr` and `shorter_fr` terminology will still be used here, for consistency with
1399        // the rest of the NLL infrastructure. The "subset origin" is the "longer free region",
1400        // and the "superset origin" is the outlived "shorter free region".
1401        //
1402        // Note: Polonius will produce a subset error at every point where the unexpected
1403        // `longer_fr`'s "placeholder loan" is contained in the `shorter_fr`. This can be helpful
1404        // for diagnostics in the future, e.g. to point more precisely at the key locations
1405        // requiring this constraint to hold. However, the error and diagnostics code downstream
1406        // expects that these errors are not duplicated (and that they are in a certain order).
1407        // Otherwise, diagnostics messages such as the ones giving names like `'1` to elided or
1408        // anonymous lifetimes for example, could give these names differently, while others like
1409        // the outlives suggestions or the debug output from `#[rustc_regions]` would be
1410        // duplicated. The polonius subset errors are deduplicated here, while keeping the
1411        // CFG-location ordering.
1412        // We can iterate the HashMap here because the result is sorted afterwards.
1413        #[allow(rustc::potential_query_instability)]
1414        let mut subset_errors: Vec<_> = polonius_output
1415            .subset_errors
1416            .iter()
1417            .flat_map(|(_location, subset_errors)| subset_errors.iter())
1418            .collect();
1419        subset_errors.sort();
1420        subset_errors.dedup();
1421
1422        for &(longer_fr, shorter_fr) in subset_errors.into_iter() {
1423            debug!(
1424                "check_polonius_subset_errors: subset_error longer_fr={:?},\
1425                 shorter_fr={:?}",
1426                longer_fr, shorter_fr
1427            );
1428
1429            let propagated = self.try_propagate_universal_region_error(
1430                longer_fr.into(),
1431                shorter_fr.into(),
1432                &mut propagated_outlives_requirements,
1433            );
1434            if propagated == RegionRelationCheckResult::Error {
1435                errors_buffer.push(RegionErrorKind::RegionError {
1436                    longer_fr: longer_fr.into(),
1437                    shorter_fr: shorter_fr.into(),
1438                    fr_origin: NllRegionVariableOrigin::FreeRegion,
1439                    is_reported: true,
1440                });
1441            }
1442        }
1443
1444        // Handle the placeholder errors as usual, until the chalk-rustc-polonius triumvirate has
1445        // a more complete picture on how to separate this responsibility.
1446        for (fr, fr_definition) in self.definitions.iter_enumerated() {
1447            match fr_definition.origin {
1448                NllRegionVariableOrigin::FreeRegion => {
1449                    // handled by polonius above
1450                }
1451
1452                NllRegionVariableOrigin::Placeholder(placeholder) => {
1453                    self.check_bound_universal_region(fr, placeholder, errors_buffer);
1454                }
1455
1456                NllRegionVariableOrigin::Existential { .. } => {
1457                    // nothing to check here
1458                }
1459            }
1460        }
1461    }
1462
1463    /// The minimum universe of any variable reachable from this
1464    /// SCC, inside or outside of it.
1465    fn scc_universe(&self, scc: ConstraintSccIndex) -> UniverseIndex {
1466        self.constraint_sccs().annotation(scc).min_universe()
1467    }
1468
1469    /// Checks the final value for the free region `fr` to see if it
1470    /// grew too large. In particular, examine what `end(X)` points
1471    /// wound up in `fr`'s final value; for each `end(X)` where `X !=
1472    /// fr`, we want to check that `fr: X`. If not, that's either an
1473    /// error, or something we have to propagate to our creator.
1474    ///
1475    /// Things that are to be propagated are accumulated into the
1476    /// `outlives_requirements` vector.
1477    #[instrument(skip(self, propagated_outlives_requirements, errors_buffer), level = "debug")]
1478    fn check_universal_region(
1479        &self,
1480        longer_fr: RegionVid,
1481        propagated_outlives_requirements: &mut Option<&mut Vec<ClosureOutlivesRequirement<'tcx>>>,
1482        errors_buffer: &mut RegionErrors<'tcx>,
1483    ) {
1484        let longer_fr_scc = self.constraint_sccs.scc(longer_fr);
1485
1486        // Because this free region must be in the ROOT universe, we
1487        // know it cannot contain any bound universes.
1488        assert!(self.scc_universe(longer_fr_scc).is_root());
1489
1490        // Only check all of the relations for the main representative of each
1491        // SCC, otherwise just check that we outlive said representative. This
1492        // reduces the number of redundant relations propagated out of
1493        // closures.
1494        // Note that the representative will be a universal region if there is
1495        // one in this SCC, so we will always check the representative here.
1496        let representative = self.scc_representative(longer_fr_scc);
1497        if representative != longer_fr {
1498            if let RegionRelationCheckResult::Error = self.check_universal_region_relation(
1499                longer_fr,
1500                representative,
1501                propagated_outlives_requirements,
1502            ) {
1503                errors_buffer.push(RegionErrorKind::RegionError {
1504                    longer_fr,
1505                    shorter_fr: representative,
1506                    fr_origin: NllRegionVariableOrigin::FreeRegion,
1507                    is_reported: true,
1508                });
1509            }
1510            return;
1511        }
1512
1513        // Find every region `o` such that `fr: o`
1514        // (because `fr` includes `end(o)`).
1515        let mut error_reported = false;
1516        for shorter_fr in self.scc_values.universal_regions_outlived_by(longer_fr_scc) {
1517            if let RegionRelationCheckResult::Error = self.check_universal_region_relation(
1518                longer_fr,
1519                shorter_fr,
1520                propagated_outlives_requirements,
1521            ) {
1522                // We only report the first region error. Subsequent errors are hidden so as
1523                // not to overwhelm the user, but we do record them so as to potentially print
1524                // better diagnostics elsewhere...
1525                errors_buffer.push(RegionErrorKind::RegionError {
1526                    longer_fr,
1527                    shorter_fr,
1528                    fr_origin: NllRegionVariableOrigin::FreeRegion,
1529                    is_reported: !error_reported,
1530                });
1531
1532                error_reported = true;
1533            }
1534        }
1535    }
1536
1537    /// Checks that we can prove that `longer_fr: shorter_fr`. If we can't we attempt to propagate
1538    /// the constraint outward (e.g. to a closure environment), but if that fails, there is an
1539    /// error.
1540    fn check_universal_region_relation(
1541        &self,
1542        longer_fr: RegionVid,
1543        shorter_fr: RegionVid,
1544        propagated_outlives_requirements: &mut Option<&mut Vec<ClosureOutlivesRequirement<'tcx>>>,
1545    ) -> RegionRelationCheckResult {
1546        // If it is known that `fr: o`, carry on.
1547        if self.universal_region_relations.outlives(longer_fr, shorter_fr) {
1548            RegionRelationCheckResult::Ok
1549        } else {
1550            // If we are not in a context where we can't propagate errors, or we
1551            // could not shrink `fr` to something smaller, then just report an
1552            // error.
1553            //
1554            // Note: in this case, we use the unapproximated regions to report the
1555            // error. This gives better error messages in some cases.
1556            self.try_propagate_universal_region_error(
1557                longer_fr,
1558                shorter_fr,
1559                propagated_outlives_requirements,
1560            )
1561        }
1562    }
1563
1564    /// Attempt to propagate a region error (e.g. `'a: 'b`) that is not met to a closure's
1565    /// creator. If we cannot, then the caller should report an error to the user.
1566    fn try_propagate_universal_region_error(
1567        &self,
1568        longer_fr: RegionVid,
1569        shorter_fr: RegionVid,
1570        propagated_outlives_requirements: &mut Option<&mut Vec<ClosureOutlivesRequirement<'tcx>>>,
1571    ) -> RegionRelationCheckResult {
1572        if let Some(propagated_outlives_requirements) = propagated_outlives_requirements {
1573            // Shrink `longer_fr` until we find a non-local region (if we do).
1574            // We'll call it `fr-` -- it's ever so slightly smaller than
1575            // `longer_fr`.
1576            if let Some(fr_minus) = self.universal_region_relations.non_local_lower_bound(longer_fr)
1577            {
1578                debug!("try_propagate_universal_region_error: fr_minus={:?}", fr_minus);
1579
1580                let blame_span_category = self.find_outlives_blame_span(
1581                    longer_fr,
1582                    NllRegionVariableOrigin::FreeRegion,
1583                    shorter_fr,
1584                );
1585
1586                // Grow `shorter_fr` until we find some non-local regions. (We
1587                // always will.)  We'll call them `shorter_fr+` -- they're ever
1588                // so slightly larger than `shorter_fr`.
1589                let shorter_fr_plus =
1590                    self.universal_region_relations.non_local_upper_bounds(shorter_fr);
1591                debug!(
1592                    "try_propagate_universal_region_error: shorter_fr_plus={:?}",
1593                    shorter_fr_plus
1594                );
1595                for fr in shorter_fr_plus {
1596                    // Push the constraint `fr-: shorter_fr+`
1597                    propagated_outlives_requirements.push(ClosureOutlivesRequirement {
1598                        subject: ClosureOutlivesSubject::Region(fr_minus),
1599                        outlived_free_region: fr,
1600                        blame_span: blame_span_category.1.span,
1601                        category: blame_span_category.0,
1602                    });
1603                }
1604                return RegionRelationCheckResult::Propagated;
1605            }
1606        }
1607
1608        RegionRelationCheckResult::Error
1609    }
1610
1611    fn check_bound_universal_region(
1612        &self,
1613        longer_fr: RegionVid,
1614        placeholder: ty::PlaceholderRegion,
1615        errors_buffer: &mut RegionErrors<'tcx>,
1616    ) {
1617        debug!("check_bound_universal_region(fr={:?}, placeholder={:?})", longer_fr, placeholder,);
1618
1619        let longer_fr_scc = self.constraint_sccs.scc(longer_fr);
1620        debug!("check_bound_universal_region: longer_fr_scc={:?}", longer_fr_scc,);
1621
1622        for error_element in self.scc_values.elements_contained_in(longer_fr_scc) {
1623            match error_element {
1624                RegionElement::Location(_) | RegionElement::RootUniversalRegion(_) => {}
1625                // If we have some bound universal region `'a`, then the only
1626                // elements it can contain is itself -- we don't know anything
1627                // else about it!
1628                RegionElement::PlaceholderRegion(placeholder1) => {
1629                    if placeholder == placeholder1 {
1630                        continue;
1631                    }
1632                }
1633            }
1634
1635            errors_buffer.push(RegionErrorKind::BoundUniversalRegionError {
1636                longer_fr,
1637                error_element,
1638                placeholder,
1639            });
1640
1641            // Stop after the first error, it gets too noisy otherwise, and does not provide more
1642            // information.
1643            break;
1644        }
1645        debug!("check_bound_universal_region: all bounds satisfied");
1646    }
1647
1648    #[instrument(level = "debug", skip(self, infcx, errors_buffer))]
1649    fn check_member_constraints(
1650        &self,
1651        infcx: &InferCtxt<'tcx>,
1652        errors_buffer: &mut RegionErrors<'tcx>,
1653    ) {
1654        let member_constraints = Rc::clone(&self.member_constraints);
1655        for m_c_i in member_constraints.all_indices() {
1656            debug!(?m_c_i);
1657            let m_c = &member_constraints[m_c_i];
1658            let member_region_vid = m_c.member_region_vid;
1659            debug!(
1660                ?member_region_vid,
1661                value = ?self.region_value_str(member_region_vid),
1662            );
1663            let choice_regions = member_constraints.choice_regions(m_c_i);
1664            debug!(?choice_regions);
1665
1666            // Did the member region wind up equal to any of the option regions?
1667            if let Some(o) =
1668                choice_regions.iter().find(|&&o_r| self.eval_equal(o_r, m_c.member_region_vid))
1669            {
1670                debug!("evaluated as equal to {:?}", o);
1671                continue;
1672            }
1673
1674            // If not, report an error.
1675            let member_region = ty::Region::new_var(infcx.tcx, member_region_vid);
1676            errors_buffer.push(RegionErrorKind::UnexpectedHiddenRegion {
1677                span: m_c.definition_span,
1678                hidden_ty: m_c.hidden_ty,
1679                key: m_c.key,
1680                member_region,
1681            });
1682        }
1683    }
1684
1685    /// We have a constraint `fr1: fr2` that is not satisfied, where
1686    /// `fr2` represents some universal region. Here, `r` is some
1687    /// region where we know that `fr1: r` and this function has the
1688    /// job of determining whether `r` is "to blame" for the fact that
1689    /// `fr1: fr2` is required.
1690    ///
1691    /// This is true under two conditions:
1692    ///
1693    /// - `r == fr2`
1694    /// - `fr2` is `'static` and `r` is some placeholder in a universe
1695    ///   that cannot be named by `fr1`; in that case, we will require
1696    ///   that `fr1: 'static` because it is the only way to `fr1: r` to
1697    ///   be satisfied. (See `add_incompatible_universe`.)
1698    pub(crate) fn provides_universal_region(
1699        &self,
1700        r: RegionVid,
1701        fr1: RegionVid,
1702        fr2: RegionVid,
1703    ) -> bool {
1704        debug!("provides_universal_region(r={:?}, fr1={:?}, fr2={:?})", r, fr1, fr2);
1705        let result = {
1706            r == fr2 || {
1707                fr2 == self.universal_regions().fr_static && self.cannot_name_placeholder(fr1, r)
1708            }
1709        };
1710        debug!("provides_universal_region: result = {:?}", result);
1711        result
1712    }
1713
1714    /// If `r2` represents a placeholder region, then this returns
1715    /// `true` if `r1` cannot name that placeholder in its
1716    /// value; otherwise, returns `false`.
1717    pub(crate) fn cannot_name_placeholder(&self, r1: RegionVid, r2: RegionVid) -> bool {
1718        match self.definitions[r2].origin {
1719            NllRegionVariableOrigin::Placeholder(placeholder) => {
1720                let r1_universe = self.definitions[r1].universe;
1721                debug!(
1722                    "cannot_name_value_of: universe1={r1_universe:?} placeholder={:?}",
1723                    placeholder
1724                );
1725                r1_universe.cannot_name(placeholder.universe)
1726            }
1727
1728            NllRegionVariableOrigin::FreeRegion | NllRegionVariableOrigin::Existential { .. } => {
1729                false
1730            }
1731        }
1732    }
1733
1734    /// Finds a good `ObligationCause` to blame for the fact that `fr1` outlives `fr2`.
1735    pub(crate) fn find_outlives_blame_span(
1736        &self,
1737        fr1: RegionVid,
1738        fr1_origin: NllRegionVariableOrigin,
1739        fr2: RegionVid,
1740    ) -> (ConstraintCategory<'tcx>, ObligationCause<'tcx>) {
1741        let BlameConstraint { category, cause, .. } = self
1742            .best_blame_constraint(fr1, fr1_origin, |r| self.provides_universal_region(r, fr1, fr2))
1743            .0;
1744        (category, cause)
1745    }
1746
1747    /// Walks the graph of constraints (where `'a: 'b` is considered
1748    /// an edge `'a -> 'b`) to find all paths from `from_region` to
1749    /// `to_region`. The paths are accumulated into the vector
1750    /// `results`. The paths are stored as a series of
1751    /// `ConstraintIndex` values -- in other words, a list of *edges*.
1752    ///
1753    /// Returns: a series of constraints as well as the region `R`
1754    /// that passed the target test.
1755    #[instrument(skip(self, target_test), ret)]
1756    pub(crate) fn find_constraint_paths_between_regions(
1757        &self,
1758        from_region: RegionVid,
1759        target_test: impl Fn(RegionVid) -> bool,
1760    ) -> Option<(Vec<OutlivesConstraint<'tcx>>, RegionVid)> {
1761        let mut context = IndexVec::from_elem(Trace::NotVisited, &self.definitions);
1762        context[from_region] = Trace::StartRegion;
1763
1764        // Use a deque so that we do a breadth-first search. We will
1765        // stop at the first match, which ought to be the shortest
1766        // path (fewest constraints).
1767        let mut deque = VecDeque::new();
1768        deque.push_back(from_region);
1769
1770        while let Some(r) = deque.pop_front() {
1771            debug!(
1772                "find_constraint_paths_between_regions: from_region={:?} r={:?} value={}",
1773                from_region,
1774                r,
1775                self.region_value_str(r),
1776            );
1777
1778            // Check if we reached the region we were looking for. If so,
1779            // we can reconstruct the path that led to it and return it.
1780            if target_test(r) {
1781                let mut result = vec![];
1782                let mut p = r;
1783                loop {
1784                    match context[p].clone() {
1785                        Trace::NotVisited => {
1786                            bug!("found unvisited region {:?} on path to {:?}", p, r)
1787                        }
1788
1789                        Trace::FromOutlivesConstraint(c) => {
1790                            p = c.sup;
1791                            result.push(c);
1792                        }
1793
1794                        Trace::StartRegion => {
1795                            result.reverse();
1796                            return Some((result, r));
1797                        }
1798                    }
1799                }
1800            }
1801
1802            // Otherwise, walk over the outgoing constraints and
1803            // enqueue any regions we find, keeping track of how we
1804            // reached them.
1805
1806            // A constraint like `'r: 'x` can come from our constraint
1807            // graph.
1808            let fr_static = self.universal_regions().fr_static;
1809            let outgoing_edges_from_graph =
1810                self.constraint_graph.outgoing_edges(r, &self.constraints, fr_static);
1811
1812            // Always inline this closure because it can be hot.
1813            let mut handle_constraint = #[inline(always)]
1814            |constraint: OutlivesConstraint<'tcx>| {
1815                debug_assert_eq!(constraint.sup, r);
1816                let sub_region = constraint.sub;
1817                if let Trace::NotVisited = context[sub_region] {
1818                    context[sub_region] = Trace::FromOutlivesConstraint(constraint);
1819                    deque.push_back(sub_region);
1820                }
1821            };
1822
1823            // This loop can be hot.
1824            for constraint in outgoing_edges_from_graph {
1825                if matches!(constraint.category, ConstraintCategory::IllegalUniverse) {
1826                    debug!("Ignoring illegal universe constraint: {constraint:?}");
1827                    continue;
1828                }
1829                handle_constraint(constraint);
1830            }
1831
1832            // Member constraints can also give rise to `'r: 'x` edges that
1833            // were not part of the graph initially, so watch out for those.
1834            // (But they are extremely rare; this loop is very cold.)
1835            for constraint in self.applied_member_constraints(self.constraint_sccs.scc(r)) {
1836                let p_c = &self.member_constraints[constraint.member_constraint_index];
1837                let constraint = OutlivesConstraint {
1838                    sup: r,
1839                    sub: constraint.min_choice,
1840                    locations: Locations::All(p_c.definition_span),
1841                    span: p_c.definition_span,
1842                    category: ConstraintCategory::OpaqueType,
1843                    variance_info: ty::VarianceDiagInfo::default(),
1844                    from_closure: false,
1845                };
1846                handle_constraint(constraint);
1847            }
1848        }
1849
1850        None
1851    }
1852
1853    /// Finds some region R such that `fr1: R` and `R` is live at `location`.
1854    #[instrument(skip(self), level = "trace", ret)]
1855    pub(crate) fn find_sub_region_live_at(&self, fr1: RegionVid, location: Location) -> RegionVid {
1856        trace!(scc = ?self.constraint_sccs.scc(fr1));
1857        trace!(universe = ?self.region_universe(fr1));
1858        self.find_constraint_paths_between_regions(fr1, |r| {
1859            // First look for some `r` such that `fr1: r` and `r` is live at `location`
1860            trace!(?r, liveness_constraints=?self.liveness_constraints.pretty_print_live_points(r));
1861            self.liveness_constraints.is_live_at(r, location)
1862        })
1863        .or_else(|| {
1864            // If we fail to find that, we may find some `r` such that
1865            // `fr1: r` and `r` is a placeholder from some universe
1866            // `fr1` cannot name. This would force `fr1` to be
1867            // `'static`.
1868            self.find_constraint_paths_between_regions(fr1, |r| {
1869                self.cannot_name_placeholder(fr1, r)
1870            })
1871        })
1872        .or_else(|| {
1873            // If we fail to find THAT, it may be that `fr1` is a
1874            // placeholder that cannot "fit" into its SCC. In that
1875            // case, there should be some `r` where `fr1: r` and `fr1` is a
1876            // placeholder that `r` cannot name. We can blame that
1877            // edge.
1878            //
1879            // Remember that if `R1: R2`, then the universe of R1
1880            // must be able to name the universe of R2, because R2 will
1881            // be at least `'empty(Universe(R2))`, and `R1` must be at
1882            // larger than that.
1883            self.find_constraint_paths_between_regions(fr1, |r| {
1884                self.cannot_name_placeholder(r, fr1)
1885            })
1886        })
1887        .map(|(_path, r)| r)
1888        .unwrap()
1889    }
1890
1891    /// Get the region outlived by `longer_fr` and live at `element`.
1892    pub(crate) fn region_from_element(
1893        &self,
1894        longer_fr: RegionVid,
1895        element: &RegionElement,
1896    ) -> RegionVid {
1897        match *element {
1898            RegionElement::Location(l) => self.find_sub_region_live_at(longer_fr, l),
1899            RegionElement::RootUniversalRegion(r) => r,
1900            RegionElement::PlaceholderRegion(error_placeholder) => self
1901                .definitions
1902                .iter_enumerated()
1903                .find_map(|(r, definition)| match definition.origin {
1904                    NllRegionVariableOrigin::Placeholder(p) if p == error_placeholder => Some(r),
1905                    _ => None,
1906                })
1907                .unwrap(),
1908        }
1909    }
1910
1911    /// Get the region definition of `r`.
1912    pub(crate) fn region_definition(&self, r: RegionVid) -> &RegionDefinition<'tcx> {
1913        &self.definitions[r]
1914    }
1915
1916    /// Check if the SCC of `r` contains `upper`.
1917    pub(crate) fn upper_bound_in_region_scc(&self, r: RegionVid, upper: RegionVid) -> bool {
1918        let r_scc = self.constraint_sccs.scc(r);
1919        self.scc_values.contains(r_scc, upper)
1920    }
1921
1922    pub(crate) fn universal_regions(&self) -> &UniversalRegions<'tcx> {
1923        &self.universal_region_relations.universal_regions
1924    }
1925
1926    /// Tries to find the best constraint to blame for the fact that
1927    /// `R: from_region`, where `R` is some region that meets
1928    /// `target_test`. This works by following the constraint graph,
1929    /// creating a constraint path that forces `R` to outlive
1930    /// `from_region`, and then finding the best choices within that
1931    /// path to blame.
1932    #[instrument(level = "debug", skip(self, target_test))]
1933    pub(crate) fn best_blame_constraint(
1934        &self,
1935        from_region: RegionVid,
1936        from_region_origin: NllRegionVariableOrigin,
1937        target_test: impl Fn(RegionVid) -> bool,
1938    ) -> (BlameConstraint<'tcx>, Vec<OutlivesConstraint<'tcx>>) {
1939        // Find all paths
1940        let (path, target_region) = self
1941            .find_constraint_paths_between_regions(from_region, target_test)
1942            .or_else(|| {
1943                self.find_constraint_paths_between_regions(from_region, |r| {
1944                    self.cannot_name_placeholder(from_region, r)
1945                })
1946            })
1947            .unwrap();
1948        debug!(
1949            "path={:#?}",
1950            path.iter()
1951                .map(|c| format!(
1952                    "{:?} ({:?}: {:?})",
1953                    c,
1954                    self.constraint_sccs.scc(c.sup),
1955                    self.constraint_sccs.scc(c.sub),
1956                ))
1957                .collect::<Vec<_>>()
1958        );
1959
1960        // We try to avoid reporting a `ConstraintCategory::Predicate` as our best constraint.
1961        // Instead, we use it to produce an improved `ObligationCauseCode`.
1962        // FIXME - determine what we should do if we encounter multiple
1963        // `ConstraintCategory::Predicate` constraints. Currently, we just pick the first one.
1964        let cause_code = path
1965            .iter()
1966            .find_map(|constraint| {
1967                if let ConstraintCategory::Predicate(predicate_span) = constraint.category {
1968                    // We currently do not store the `DefId` in the `ConstraintCategory`
1969                    // for performances reasons. The error reporting code used by NLL only
1970                    // uses the span, so this doesn't cause any problems at the moment.
1971                    Some(ObligationCauseCode::WhereClause(CRATE_DEF_ID.to_def_id(), predicate_span))
1972                } else {
1973                    None
1974                }
1975            })
1976            .unwrap_or_else(|| ObligationCauseCode::Misc);
1977
1978        // When reporting an error, there is typically a chain of constraints leading from some
1979        // "source" region which must outlive some "target" region.
1980        // In most cases, we prefer to "blame" the constraints closer to the target --
1981        // but there is one exception. When constraints arise from higher-ranked subtyping,
1982        // we generally prefer to blame the source value,
1983        // as the "target" in this case tends to be some type annotation that the user gave.
1984        // Therefore, if we find that the region origin is some instantiation
1985        // of a higher-ranked region, we start our search from the "source" point
1986        // rather than the "target", and we also tweak a few other things.
1987        //
1988        // An example might be this bit of Rust code:
1989        //
1990        // ```rust
1991        // let x: fn(&'static ()) = |_| {};
1992        // let y: for<'a> fn(&'a ()) = x;
1993        // ```
1994        //
1995        // In MIR, this will be converted into a combination of assignments and type ascriptions.
1996        // In particular, the 'static is imposed through a type ascription:
1997        //
1998        // ```rust
1999        // x = ...;
2000        // AscribeUserType(x, fn(&'static ())
2001        // y = x;
2002        // ```
2003        //
2004        // We wind up ultimately with constraints like
2005        //
2006        // ```rust
2007        // !a: 'temp1 // from the `y = x` statement
2008        // 'temp1: 'temp2
2009        // 'temp2: 'static // from the AscribeUserType
2010        // ```
2011        //
2012        // and here we prefer to blame the source (the y = x statement).
2013        let blame_source = match from_region_origin {
2014            NllRegionVariableOrigin::FreeRegion
2015            | NllRegionVariableOrigin::Existential { from_forall: false } => true,
2016            NllRegionVariableOrigin::Placeholder(_)
2017            | NllRegionVariableOrigin::Existential { from_forall: true } => false,
2018        };
2019
2020        // To pick a constraint to blame, we organize constraints by how interesting we expect them
2021        // to be in diagnostics, then pick the most interesting one closest to either the source or
2022        // the target on our constraint path.
2023        let constraint_interest = |constraint: &OutlivesConstraint<'tcx>| {
2024            // Try to avoid blaming constraints from desugarings, since they may not clearly match
2025            // match what users have written. As an exception, allow blaming returns generated by
2026            // `?` desugaring, since the correspondence is fairly clear.
2027            let category = if let Some(kind) = constraint.span.desugaring_kind()
2028                && (kind != DesugaringKind::QuestionMark
2029                    || !matches!(constraint.category, ConstraintCategory::Return(_)))
2030            {
2031                ConstraintCategory::Boring
2032            } else {
2033                constraint.category
2034            };
2035
2036            match category {
2037                // Returns usually provide a type to blame and have specially written diagnostics,
2038                // so prioritize them.
2039                ConstraintCategory::Return(_) => 0,
2040                // Unsizing coercions are interesting, since we have a note for that:
2041                // `BorrowExplanation::add_object_lifetime_default_note`.
2042                // FIXME(dianne): That note shouldn't depend on a coercion being blamed; see issue
2043                // #131008 for an example of where we currently don't emit it but should.
2044                // Once the note is handled properly, this case should be removed. Until then, it
2045                // should be as limited as possible; the note is prone to false positives and this
2046                // constraint usually isn't best to blame.
2047                ConstraintCategory::Cast {
2048                    unsize_to: Some(unsize_ty),
2049                    is_implicit_coercion: true,
2050                } if target_region == self.universal_regions().fr_static
2051                    // Mirror the note's condition, to minimize how often this diverts blame.
2052                    && let ty::Adt(_, args) = unsize_ty.kind()
2053                    && args.iter().any(|arg| arg.as_type().is_some_and(|ty| ty.is_trait()))
2054                    // Mimic old logic for this, to minimize false positives in tests.
2055                    && !path
2056                        .iter()
2057                        .any(|c| matches!(c.category, ConstraintCategory::TypeAnnotation(_))) =>
2058                {
2059                    1
2060                }
2061                // Between other interesting constraints, order by their position on the `path`.
2062                ConstraintCategory::Yield
2063                | ConstraintCategory::UseAsConst
2064                | ConstraintCategory::UseAsStatic
2065                | ConstraintCategory::TypeAnnotation(
2066                    AnnotationSource::Ascription
2067                    | AnnotationSource::Declaration
2068                    | AnnotationSource::OpaqueCast,
2069                )
2070                | ConstraintCategory::Cast { .. }
2071                | ConstraintCategory::CallArgument(_)
2072                | ConstraintCategory::CopyBound
2073                | ConstraintCategory::SizedBound
2074                | ConstraintCategory::Assignment
2075                | ConstraintCategory::Usage
2076                | ConstraintCategory::ClosureUpvar(_) => 2,
2077                // Generic arguments are unlikely to be what relates regions together
2078                ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg) => 3,
2079                // We handle predicates and opaque types specially; don't prioritize them here.
2080                ConstraintCategory::Predicate(_) | ConstraintCategory::OpaqueType => 4,
2081                // `Boring` constraints can correspond to user-written code and have useful spans,
2082                // but don't provide any other useful information for diagnostics.
2083                ConstraintCategory::Boring => 5,
2084                // `BoringNoLocation` constraints can point to user-written code, but are less
2085                // specific, and are not used for relations that would make sense to blame.
2086                ConstraintCategory::BoringNoLocation => 6,
2087                // Do not blame internal constraints.
2088                ConstraintCategory::Internal => 7,
2089                ConstraintCategory::IllegalUniverse => 8,
2090            }
2091        };
2092
2093        let best_choice = if blame_source {
2094            path.iter().enumerate().rev().min_by_key(|(_, c)| constraint_interest(c)).unwrap().0
2095        } else {
2096            path.iter().enumerate().min_by_key(|(_, c)| constraint_interest(c)).unwrap().0
2097        };
2098
2099        debug!(?best_choice, ?blame_source);
2100
2101        let best_constraint = if let Some(next) = path.get(best_choice + 1)
2102            && matches!(path[best_choice].category, ConstraintCategory::Return(_))
2103            && next.category == ConstraintCategory::OpaqueType
2104        {
2105            // The return expression is being influenced by the return type being
2106            // impl Trait, point at the return type and not the return expr.
2107            *next
2108        } else if path[best_choice].category == ConstraintCategory::Return(ReturnConstraint::Normal)
2109            && let Some(field) = path.iter().find_map(|p| {
2110                if let ConstraintCategory::ClosureUpvar(f) = p.category { Some(f) } else { None }
2111            })
2112        {
2113            OutlivesConstraint {
2114                category: ConstraintCategory::Return(ReturnConstraint::ClosureUpvar(field)),
2115                ..path[best_choice]
2116            }
2117        } else {
2118            path[best_choice]
2119        };
2120
2121        let blame_constraint = BlameConstraint {
2122            category: best_constraint.category,
2123            from_closure: best_constraint.from_closure,
2124            cause: ObligationCause::new(best_constraint.span, CRATE_DEF_ID, cause_code.clone()),
2125            variance_info: best_constraint.variance_info,
2126        };
2127        (blame_constraint, path)
2128    }
2129
2130    pub(crate) fn universe_info(&self, universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
2131        // Query canonicalization can create local superuniverses (for example in
2132        // `InferCtx::query_response_instantiation_guess`), but they don't have an associated
2133        // `UniverseInfo` explaining why they were created.
2134        // This can cause ICEs if these causes are accessed in diagnostics, for example in issue
2135        // #114907 where this happens via liveness and dropck outlives results.
2136        // Therefore, we return a default value in case that happens, which should at worst emit a
2137        // suboptimal error, instead of the ICE.
2138        self.universe_causes.get(&universe).cloned().unwrap_or_else(UniverseInfo::other)
2139    }
2140
2141    /// Tries to find the terminator of the loop in which the region 'r' resides.
2142    /// Returns the location of the terminator if found.
2143    pub(crate) fn find_loop_terminator_location(
2144        &self,
2145        r: RegionVid,
2146        body: &Body<'_>,
2147    ) -> Option<Location> {
2148        let scc = self.constraint_sccs.scc(r);
2149        let locations = self.scc_values.locations_outlived_by(scc);
2150        for location in locations {
2151            let bb = &body[location.block];
2152            if let Some(terminator) = &bb.terminator {
2153                // terminator of a loop should be TerminatorKind::FalseUnwind
2154                if let TerminatorKind::FalseUnwind { .. } = terminator.kind {
2155                    return Some(location);
2156                }
2157            }
2158        }
2159        None
2160    }
2161
2162    /// Access to the SCC constraint graph.
2163    /// This can be used to quickly under-approximate the regions which are equal to each other
2164    /// and their relative orderings.
2165    // This is `pub` because it's used by unstable external borrowck data users, see `consumers.rs`.
2166    pub fn constraint_sccs(&self) -> &ConstraintSccs {
2167        &self.constraint_sccs
2168    }
2169
2170    /// Access to the region graph, built from the outlives constraints.
2171    pub(crate) fn region_graph(&self) -> RegionGraph<'_, 'tcx, graph::Normal> {
2172        self.constraint_graph.region_graph(&self.constraints, self.universal_regions().fr_static)
2173    }
2174
2175    /// Returns the representative `RegionVid` for a given SCC.
2176    /// See `RegionTracker` for how a region variable ID is chosen.
2177    ///
2178    /// It is a hacky way to manage checking regions for equality,
2179    /// since we can 'canonicalize' each region to the representative
2180    /// of its SCC and be sure that -- if they have the same repr --
2181    /// they *must* be equal (though not having the same repr does not
2182    /// mean they are unequal).
2183    fn scc_representative(&self, scc: ConstraintSccIndex) -> RegionVid {
2184        self.constraint_sccs.annotation(scc).representative
2185    }
2186
2187    pub(crate) fn liveness_constraints(&self) -> &LivenessValues {
2188        &self.liveness_constraints
2189    }
2190
2191    /// When using `-Zpolonius=next`, records the given live loans for the loan scopes and active
2192    /// loans dataflow computations.
2193    pub(crate) fn record_live_loans(&mut self, live_loans: LiveLoans) {
2194        self.liveness_constraints.record_live_loans(live_loans);
2195    }
2196
2197    /// Returns whether the `loan_idx` is live at the given `location`: whether its issuing
2198    /// region is contained within the type of a variable that is live at this point.
2199    /// Note: for now, the sets of live loans is only available when using `-Zpolonius=next`.
2200    pub(crate) fn is_loan_live_at(&self, loan_idx: BorrowIndex, location: Location) -> bool {
2201        let point = self.liveness_constraints.point_from_location(location);
2202        self.liveness_constraints.is_loan_live_at(loan_idx, point)
2203    }
2204}
2205
2206impl<'tcx> RegionDefinition<'tcx> {
2207    fn new(universe: ty::UniverseIndex, rv_origin: RegionVariableOrigin) -> Self {
2208        // Create a new region definition. Note that, for free
2209        // regions, the `external_name` field gets updated later in
2210        // `init_free_and_bound_regions`.
2211
2212        let origin = match rv_origin {
2213            RegionVariableOrigin::Nll(origin) => origin,
2214            _ => NllRegionVariableOrigin::Existential { from_forall: false },
2215        };
2216
2217        Self { origin, universe, external_name: None }
2218    }
2219}
2220
2221#[derive(Clone, Debug)]
2222pub(crate) struct BlameConstraint<'tcx> {
2223    pub category: ConstraintCategory<'tcx>,
2224    pub from_closure: bool,
2225    pub cause: ObligationCause<'tcx>,
2226    pub variance_info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
2227}