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