rustc_borrowck/region_infer/
mod.rs

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