1 use super::universal_regions::UniversalRegions;
2 use crate::borrow_check::nll::constraints::graph::NormalConstraintGraph;
3 use crate::borrow_check::nll::constraints::{
4 ConstraintSccIndex, OutlivesConstraint, OutlivesConstraintSet,
6 use crate::borrow_check::nll::pick_constraints::{PickConstraintSet, NllPickConstraintIndex};
7 use crate::borrow_check::nll::region_infer::values::{
8 PlaceholderIndices, RegionElement, ToElementIndex,
10 use crate::borrow_check::nll::type_check::free_region_relations::UniversalRegionRelations;
11 use crate::borrow_check::nll::type_check::Locations;
12 use crate::borrow_check::Upvar;
13 use rustc::hir::def_id::DefId;
14 use rustc::infer::canonical::QueryOutlivesConstraint;
15 use rustc::infer::opaque_types;
16 use rustc::infer::region_constraints::{GenericKind, VarInfos, VerifyBound};
17 use rustc::infer::{InferCtxt, NLLRegionVariableOrigin, RegionVariableOrigin};
19 Body, ClosureOutlivesRequirement, ClosureOutlivesSubject, ClosureRegionRequirements,
20 ConstraintCategory, Local, Location,
22 use rustc::ty::{self, subst::SubstsRef, RegionVid, Ty, TyCtxt, TypeFoldable};
23 use rustc::util::common::{self, ErrorReported};
24 use rustc_data_structures::binary_search_util;
25 use rustc_data_structures::bit_set::BitSet;
26 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
27 use rustc_data_structures::graph::WithSuccessors;
28 use rustc_data_structures::graph::scc::Sccs;
29 use rustc_data_structures::graph::vec_graph::VecGraph;
30 use rustc_data_structures::indexed_vec::IndexVec;
31 use rustc_errors::{Diagnostic, DiagnosticBuilder};
38 crate use self::error_reporting::{RegionName, RegionNameSource};
41 use self::values::{LivenessValues, RegionValueElements, RegionValues};
43 use super::ToRegionVid;
45 pub struct RegionInferenceContext<'tcx> {
46 /// Contains the definition for every region variable. Region
47 /// variables are identified by their index (`RegionVid`). The
48 /// definition contains information about where the region came
49 /// from as well as its final inferred value.
50 definitions: IndexVec<RegionVid, RegionDefinition<'tcx>>,
52 /// The liveness constraints added to each region. For most
53 /// regions, these start out empty and steadily grow, though for
54 /// each universally quantified region R they start out containing
55 /// the entire CFG and `end(R)`.
56 liveness_constraints: LivenessValues<RegionVid>,
58 /// The outlives constraints computed by the type-check.
59 constraints: Rc<OutlivesConstraintSet>,
61 /// The constraint-set, but in graph form, making it easy to traverse
62 /// the constraints adjacent to a particular region. Used to construct
63 /// the SCC (see `constraint_sccs`) and for error reporting.
64 constraint_graph: Rc<NormalConstraintGraph>,
66 /// The SCC computed from `constraints` and the constraint
67 /// graph. We have an edge from SCC A to SCC B if `A: B`. Used to
68 /// compute the values of each region.
69 constraint_sccs: Rc<Sccs<RegionVid, ConstraintSccIndex>>,
71 /// Reverse of the SCC constraint graph -- i.e., an edge `A -> B`
72 /// exists if `B: A`. Computed lazilly.
73 rev_constraint_graph: Option<Rc<VecGraph<ConstraintSccIndex>>>,
75 /// The "pick R0 from [R1..Rn]" constraints, indexed by SCC.
76 pick_constraints: Rc<PickConstraintSet<'tcx, ConstraintSccIndex>>,
78 /// Records the pick-constraints that we applied to each scc.
79 /// This is useful for error reporting. Once constraint
80 /// propagation is done, this vector is sorted according to
81 /// `pick_region_scc`.
82 pick_constraints_applied: Vec<AppliedPickConstraint>,
84 /// Map closure bounds to a `Span` that should be used for error reporting.
85 closure_bounds_mapping:
86 FxHashMap<Location, FxHashMap<(RegionVid, RegionVid), (ConstraintCategory, Span)>>,
88 /// Contains the minimum universe of any variable within the same
89 /// SCC. We will ensure that no SCC contains values that are not
90 /// visible from this index.
91 scc_universes: IndexVec<ConstraintSccIndex, ty::UniverseIndex>,
93 /// Contains a "representative" from each SCC. This will be the
94 /// minimal RegionVid belonging to that universe. It is used as a
95 /// kind of hacky way to manage checking outlives relationships,
96 /// since we can 'canonicalize' each region to the representative
97 /// of its SCC and be sure that -- if they have the same repr --
98 /// they *must* be equal (though not having the same repr does not
99 /// mean they are unequal).
100 scc_representatives: IndexVec<ConstraintSccIndex, ty::RegionVid>,
102 /// The final inferred values of the region variables; we compute
103 /// one value per SCC. To get the value for any given *region*,
104 /// you first find which scc it is a part of.
105 scc_values: RegionValues<ConstraintSccIndex>,
107 /// Type constraints that we check after solving.
108 type_tests: Vec<TypeTest<'tcx>>,
110 /// Information about the universally quantified regions in scope
111 /// on this function.
112 universal_regions: Rc<UniversalRegions<'tcx>>,
114 /// Information about how the universally quantified regions in
115 /// scope on this function relate to one another.
116 universal_region_relations: Rc<UniversalRegionRelations<'tcx>>,
119 /// Each time that `apply_pick_constraint` is successful, it appends
120 /// one of these structs to the `pick_constraints_applied` field.
121 /// This is used in error reporting to trace out what happened.
123 /// The way that `apply_pick_constraint` works is that it effectively
124 /// adds a new lower bound to the SCC it is analyzing: so you wind up
125 /// with `'R: 'O` where `'R` is the pick-region and `'O` is the
126 /// minimal viable option.
127 #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
128 struct AppliedPickConstraint {
129 /// The SCC that was affected. (The "pick region".)
131 /// The vector if `AppliedPickConstraint` elements is kept sorted
133 pick_region_scc: ConstraintSccIndex,
135 /// The "best option" that `apply_pick_constraint` found -- this was
136 /// added as an "ad-hoc" lower-bound to `pick_region_scc`.
137 best_option: ty::RegionVid,
139 /// The "pick constraint index" -- we can find out details about
140 /// the constraint from
141 /// `set.pick_constraints[pick_constraint_index]`.
142 pick_constraint_index: NllPickConstraintIndex,
145 struct RegionDefinition<'tcx> {
146 /// What kind of variable is this -- a free region? existential
147 /// variable? etc. (See the `NLLRegionVariableOrigin` for more
149 origin: NLLRegionVariableOrigin,
151 /// Which universe is this region variable defined in? This is
152 /// most often `ty::UniverseIndex::ROOT`, but when we encounter
153 /// forall-quantifiers like `for<'a> { 'a = 'b }`, we would create
154 /// the variable for `'a` in a fresh universe that extends ROOT.
155 universe: ty::UniverseIndex,
157 /// If this is 'static or an early-bound region, then this is
158 /// `Some(X)` where `X` is the name of the region.
159 external_name: Option<ty::Region<'tcx>>,
162 /// N.B., the variants in `Cause` are intentionally ordered. Lower
163 /// values are preferred when it comes to error messages. Do not
164 /// reorder willy nilly.
165 #[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
166 pub(crate) enum Cause {
167 /// point inserted because Local was live at the given Location
168 LiveVar(Local, Location),
170 /// point inserted because Local was dropped at the given Location
171 DropVar(Local, Location),
174 /// A "type test" corresponds to an outlives constraint between a type
175 /// and a lifetime, like `T: 'x` or `<T as Foo>::Bar: 'x`. They are
176 /// translated from the `Verify` region constraints in the ordinary
177 /// inference context.
179 /// These sorts of constraints are handled differently than ordinary
180 /// constraints, at least at present. During type checking, the
181 /// `InferCtxt::process_registered_region_obligations` method will
182 /// attempt to convert a type test like `T: 'x` into an ordinary
183 /// outlives constraint when possible (for example, `&'a T: 'b` will
184 /// be converted into `'a: 'b` and registered as a `Constraint`).
186 /// In some cases, however, there are outlives relationships that are
187 /// not converted into a region constraint, but rather into one of
188 /// these "type tests". The distinction is that a type test does not
189 /// influence the inference result, but instead just examines the
190 /// values that we ultimately inferred for each region variable and
191 /// checks that they meet certain extra criteria. If not, an error
194 /// One reason for this is that these type tests typically boil down
195 /// to a check like `'a: 'x` where `'a` is a universally quantified
196 /// region -- and therefore not one whose value is really meant to be
197 /// *inferred*, precisely (this is not always the case: one can have a
198 /// type test like `<Foo as Trait<'?0>>::Bar: 'x`, where `'?0` is an
199 /// inference variable). Another reason is that these type tests can
200 /// involve *disjunction* -- that is, they can be satisfied in more
203 /// For more information about this translation, see
204 /// `InferCtxt::process_registered_region_obligations` and
205 /// `InferCtxt::type_must_outlive` in `rustc::infer::outlives`.
206 #[derive(Clone, Debug)]
207 pub struct TypeTest<'tcx> {
208 /// The type `T` that must outlive the region.
209 pub generic_kind: GenericKind<'tcx>,
211 /// The region `'x` that the type must outlive.
212 pub lower_bound: RegionVid,
214 /// Where did this constraint arise and why?
215 pub locations: Locations,
217 /// A test which, if met by the region `'x`, proves that this type
218 /// constraint is satisfied.
219 pub verify_bound: VerifyBound<'tcx>,
222 impl<'tcx> RegionInferenceContext<'tcx> {
223 /// Creates a new region inference context with a total of
224 /// `num_region_variables` valid inference variables; the first N
225 /// of those will be constant regions representing the free
226 /// regions defined in `universal_regions`.
228 /// The `outlives_constraints` and `type_tests` are an initial set
229 /// of constraints produced by the MIR type check.
232 universal_regions: Rc<UniversalRegions<'tcx>>,
233 placeholder_indices: Rc<PlaceholderIndices>,
234 universal_region_relations: Rc<UniversalRegionRelations<'tcx>>,
236 outlives_constraints: OutlivesConstraintSet,
237 pick_constraints_in: PickConstraintSet<'tcx, RegionVid>,
238 closure_bounds_mapping: FxHashMap<
240 FxHashMap<(RegionVid, RegionVid), (ConstraintCategory, Span)>,
242 type_tests: Vec<TypeTest<'tcx>>,
243 liveness_constraints: LivenessValues<RegionVid>,
244 elements: &Rc<RegionValueElements>,
246 // Create a RegionDefinition for each inference variable.
247 let definitions: IndexVec<_, _> = var_infos
249 .map(|info| RegionDefinition::new(info.universe, info.origin))
252 let constraints = Rc::new(outlives_constraints); // freeze constraints
253 let constraint_graph = Rc::new(constraints.graph(definitions.len()));
254 let fr_static = universal_regions.fr_static;
255 let constraint_sccs = Rc::new(constraints.compute_sccs(&constraint_graph, fr_static));
258 RegionValues::new(elements, universal_regions.len(), &placeholder_indices);
260 for region in liveness_constraints.rows() {
261 let scc = constraint_sccs.scc(region);
262 scc_values.merge_liveness(scc, region, &liveness_constraints);
265 let scc_universes = Self::compute_scc_universes(&constraint_sccs, &definitions);
267 let scc_representatives = Self::compute_scc_representatives(&constraint_sccs, &definitions);
269 let pick_constraints = Rc::new(pick_constraints_in.into_mapped(|r| constraint_sccs.scc(r)));
271 let mut result = Self {
273 liveness_constraints,
277 rev_constraint_graph: None,
279 pick_constraints_applied: Vec::new(),
280 closure_bounds_mapping,
286 universal_region_relations,
289 result.init_free_and_bound_regions();
294 /// Each SCC is the combination of many region variables which
295 /// have been equated. Therefore, we can associate a universe with
296 /// each SCC which is minimum of all the universes of its
297 /// constituent regions -- this is because whatever value the SCC
298 /// takes on must be a value that each of the regions within the
299 /// SCC could have as well. This implies that the SCC must have
300 /// the minimum, or narrowest, universe.
301 fn compute_scc_universes(
302 constraints_scc: &Sccs<RegionVid, ConstraintSccIndex>,
303 definitions: &IndexVec<RegionVid, RegionDefinition<'tcx>>,
304 ) -> IndexVec<ConstraintSccIndex, ty::UniverseIndex> {
305 let num_sccs = constraints_scc.num_sccs();
306 let mut scc_universes = IndexVec::from_elem_n(ty::UniverseIndex::MAX, num_sccs);
308 for (region_vid, region_definition) in definitions.iter_enumerated() {
309 let scc = constraints_scc.scc(region_vid);
310 let scc_universe = &mut scc_universes[scc];
311 *scc_universe = ::std::cmp::min(*scc_universe, region_definition.universe);
314 debug!("compute_scc_universes: scc_universe = {:#?}", scc_universes);
319 /// For each SCC, we compute a unique `RegionVid` (in fact, the
320 /// minimal one that belongs to the SCC). See
321 /// `scc_representatives` field of `RegionInferenceContext` for
323 fn compute_scc_representatives(
324 constraints_scc: &Sccs<RegionVid, ConstraintSccIndex>,
325 definitions: &IndexVec<RegionVid, RegionDefinition<'tcx>>,
326 ) -> IndexVec<ConstraintSccIndex, ty::RegionVid> {
327 let num_sccs = constraints_scc.num_sccs();
328 let next_region_vid = definitions.next_index();
329 let mut scc_representatives = IndexVec::from_elem_n(next_region_vid, num_sccs);
331 for region_vid in definitions.indices() {
332 let scc = constraints_scc.scc(region_vid);
333 let prev_min = scc_representatives[scc];
334 scc_representatives[scc] = region_vid.min(prev_min);
340 /// Initializes the region variables for each universally
341 /// quantified region (lifetime parameter). The first N variables
342 /// always correspond to the regions appearing in the function
343 /// signature (both named and anonymous) and where-clauses. This
344 /// function iterates over those regions and initializes them with
349 /// fn foo<'a, 'b>(..) where 'a: 'b
351 /// would initialize two variables like so:
353 /// R0 = { CFG, R0 } // 'a
354 /// R1 = { CFG, R0, R1 } // 'b
356 /// Here, R0 represents `'a`, and it contains (a) the entire CFG
357 /// and (b) any universally quantified regions that it outlives,
358 /// which in this case is just itself. R1 (`'b`) in contrast also
359 /// outlives `'a` and hence contains R0 and R1.
360 fn init_free_and_bound_regions(&mut self) {
361 // Update the names (if any)
362 for (external_name, variable) in self.universal_regions.named_universal_regions() {
364 "init_universal_regions: region {:?} has external name {:?}",
365 variable, external_name
367 self.definitions[variable].external_name = Some(external_name);
370 for variable in self.definitions.indices() {
371 let scc = self.constraint_sccs.scc(variable);
373 match self.definitions[variable].origin {
374 NLLRegionVariableOrigin::FreeRegion => {
375 // For each free, universally quantified region X:
377 // Add all nodes in the CFG to liveness constraints
378 self.liveness_constraints.add_all_points(variable);
379 self.scc_values.add_all_points(scc);
381 // Add `end(X)` into the set for X.
382 self.scc_values.add_element(scc, variable);
385 NLLRegionVariableOrigin::Placeholder(placeholder) => {
386 // Each placeholder region is only visible from
387 // its universe `ui` and its extensions. So we
388 // can't just add it into `scc` unless the
389 // universe of the scc can name this region.
390 let scc_universe = self.scc_universes[scc];
391 if scc_universe.can_name(placeholder.universe) {
392 self.scc_values.add_element(scc, placeholder);
395 "init_free_and_bound_regions: placeholder {:?} is \
396 not compatible with universe {:?} of its SCC {:?}",
397 placeholder, scc_universe, scc,
399 self.add_incompatible_universe(scc);
403 NLLRegionVariableOrigin::Existential => {
404 // For existential, regions, nothing to do.
410 /// Returns an iterator over all the region indices.
411 pub fn regions(&self) -> impl Iterator<Item = RegionVid> {
412 self.definitions.indices()
415 /// Given a universal region in scope on the MIR, returns the
416 /// corresponding index.
418 /// (Panics if `r` is not a registered universal region.)
419 pub fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
420 self.universal_regions.to_region_vid(r)
423 /// Adds annotations for `#[rustc_regions]`; see `UniversalRegions::annotate`.
424 crate fn annotate(&self, tcx: TyCtxt<'tcx>, err: &mut DiagnosticBuilder<'_>) {
425 self.universal_regions.annotate(tcx, err)
428 /// Returns `true` if the region `r` contains the point `p`.
430 /// Panics if called before `solve()` executes,
431 crate fn region_contains(&self, r: impl ToRegionVid, p: impl ToElementIndex) -> bool {
432 let scc = self.constraint_sccs.scc(r.to_region_vid());
433 self.scc_values.contains(scc, p)
436 /// Returns access to the value of `r` for debugging purposes.
437 crate fn region_value_str(&self, r: RegionVid) -> String {
438 let scc = self.constraint_sccs.scc(r.to_region_vid());
439 self.scc_values.region_value_str(scc)
442 /// Returns access to the value of `r` for debugging purposes.
443 crate fn region_universe(&self, r: RegionVid) -> ty::UniverseIndex {
444 let scc = self.constraint_sccs.scc(r.to_region_vid());
445 self.scc_universes[scc]
448 /// Once region solving has completed, this function will return
449 /// the pick-constraints that were applied to the value of a given
450 /// region `r`. See `AppliedPickConstraint`.
451 fn applied_pick_constraints(&self, r: impl ToRegionVid) -> &[AppliedPickConstraint] {
452 let scc = self.constraint_sccs.scc(r.to_region_vid());
453 binary_search_util::binary_search_slice(
454 &self.pick_constraints_applied,
455 |applied| applied.pick_region_scc,
460 /// Performs region inference and report errors if we see any
461 /// unsatisfiable constraints. If this is a closure, returns the
462 /// region requirements to propagate to our creator, if any.
465 infcx: &InferCtxt<'_, 'tcx>,
469 errors_buffer: &mut Vec<Diagnostic>,
470 ) -> Option<ClosureRegionRequirements<'tcx>> {
472 infcx.tcx.sess.time_extended(),
473 Some(infcx.tcx.sess),
474 &format!("solve_nll_region_constraints({:?})", mir_def_id),
475 || self.solve_inner(infcx, body, upvars, mir_def_id, errors_buffer),
481 infcx: &InferCtxt<'_, 'tcx>,
485 errors_buffer: &mut Vec<Diagnostic>,
486 ) -> Option<ClosureRegionRequirements<'tcx>> {
487 self.propagate_constraints(body);
489 // If this is a closure, we can propagate unsatisfied
490 // `outlives_requirements` to our creator, so create a vector
491 // to store those. Otherwise, we'll pass in `None` to the
492 // functions below, which will trigger them to report errors
494 let mut outlives_requirements =
495 if infcx.tcx.is_closure(mir_def_id) { Some(vec![]) } else { None };
497 self.check_type_tests(
501 outlives_requirements.as_mut(),
505 self.check_universal_regions(
510 outlives_requirements.as_mut(),
514 self.check_pick_constraints(infcx, mir_def_id, errors_buffer);
516 let outlives_requirements = outlives_requirements.unwrap_or(vec![]);
518 if outlives_requirements.is_empty() {
521 let num_external_vids = self.universal_regions.num_global_and_external_regions();
522 Some(ClosureRegionRequirements { num_external_vids, outlives_requirements })
526 /// Propagate the region constraints: this will grow the values
527 /// for each region variable until all the constraints are
528 /// satisfied. Note that some values may grow **too** large to be
529 /// feasible, but we check this later.
530 fn propagate_constraints(&mut self, _body: &Body<'tcx>) {
531 debug!("propagate_constraints()");
533 debug!("propagate_constraints: constraints={:#?}", {
534 let mut constraints: Vec<_> = self.constraints.outlives().iter().collect();
538 .map(|c| (c, self.constraint_sccs.scc(c.sup), self.constraint_sccs.scc(c.sub)))
542 // To propagate constraints, we walk the DAG induced by the
543 // SCC. For each SCC, we visit its successors and compute
544 // their values, then we union all those values to get our
546 let visited = &mut BitSet::new_empty(self.constraint_sccs.num_sccs());
547 for scc_index in self.constraint_sccs.all_sccs() {
548 self.propagate_constraint_sccs_if_new(scc_index, visited);
551 // Sort the applied pick constraints so we can binary search
552 // through them later.
553 self.pick_constraints_applied.sort_by_key(|applied| applied.pick_region_scc);
556 /// Computes the value of the SCC `scc_a` if it has not already
557 /// been computed. The `visited` parameter is a bitset
559 fn propagate_constraint_sccs_if_new(
561 scc_a: ConstraintSccIndex,
562 visited: &mut BitSet<ConstraintSccIndex>,
564 if visited.insert(scc_a) {
565 self.propagate_constraint_sccs_new(scc_a, visited);
569 /// Computes the value of the SCC `scc_a`, which has not yet been
570 /// computed. This works by first computing all successors of the
571 /// SCC (if they haven't been computed already) and then unioning
572 /// together their elements.
573 fn propagate_constraint_sccs_new(
575 scc_a: ConstraintSccIndex,
576 visited: &mut BitSet<ConstraintSccIndex>,
578 let constraint_sccs = self.constraint_sccs.clone();
580 // Walk each SCC `B` such that `A: B`...
581 for &scc_b in constraint_sccs.successors(scc_a) {
582 debug!("propagate_constraint_sccs: scc_a = {:?} scc_b = {:?}", scc_a, scc_b);
584 // ...compute the value of `B`...
585 self.propagate_constraint_sccs_if_new(scc_b, visited);
587 // ...and add elements from `B` into `A`. One complication
588 // arises because of universes: If `B` contains something
589 // that `A` cannot name, then `A` can only contain `B` if
590 // it outlives static.
591 if self.universe_compatible(scc_b, scc_a) {
592 // `A` can name everything that is in `B`, so just
594 self.scc_values.add_region(scc_a, scc_b);
596 self.add_incompatible_universe(scc_a);
600 // Now take pick constraints into account
601 let pick_constraints = self.pick_constraints.clone();
602 for p_c_i in pick_constraints.indices(scc_a) {
603 self.apply_pick_constraint(
606 pick_constraints.option_regions(p_c_i),
611 "propagate_constraint_sccs: scc_a = {:?} has value {:?}",
613 self.scc_values.region_value_str(scc_a),
617 /// Invoked for each `pick R0 from [R1..Rn]` constraint.
619 /// `scc` is the SCC containing R0, and `option_regions` are the
620 /// `R1..Rn` regions -- they are always known to be universal
621 /// regions (and if that's not true, we just don't attempt to
622 /// enforce the constraint).
624 /// The current value of `scc` at the time the method is invoked
625 /// is considered a *lower bound*. If possible, we will modify
626 /// the constraint to set it equal to one of the option regions.
627 /// If we make any changes, returns true, else false.
628 fn apply_pick_constraint(
630 scc: ConstraintSccIndex,
631 pick_constraint_index: NllPickConstraintIndex,
632 option_regions: &[ty::RegionVid],
634 debug!("apply_pick_constraint(scc={:?}, option_regions={:#?})", scc, option_regions,);
637 option_regions.iter().find(|&&r| !self.universal_regions.is_universal_region(r))
639 // FIXME(#61773): This case can only occur with
640 // `impl_trait_in_bindings`, I believe, and we are just
641 // opting not to handle it for now. See #61773 for
644 "pick constraint for `{:?}` has an option region `{:?}` \
645 that is not a universal region",
646 self.pick_constraints[pick_constraint_index].opaque_type_def_id,
651 // Create a mutable vector of the options. We'll try to winnow
653 let mut option_regions: Vec<ty::RegionVid> = option_regions.to_vec();
655 // The 'pick-region' in a pick-constraint is part of the
656 // hidden type, which must be in the root universe. Therefore,
657 // it cannot have any placeholders in its value.
658 assert!(self.scc_universes[scc] == ty::UniverseIndex::ROOT);
660 self.scc_values.placeholders_contained_in(scc).next().is_none(),
661 "scc {:?} in a pick-constraint has placeholder value: {:?}",
663 self.scc_values.region_value_str(scc),
666 // The existing value for `scc` is a lower-bound. This will
667 // consist of some set {P} + {LB} of points {P} and
668 // lower-bound free regions {LB}. As each option region O is a
669 // free region, it will outlive the points. But we can only
670 // consider the option O if O: LB.
671 option_regions.retain(|&o_r| {
673 .universal_regions_outlived_by(scc)
674 .all(|lb| self.universal_region_relations.outlives(o_r, lb))
676 debug!("apply_pick_constraint: after lb, option_regions={:?}", option_regions);
678 // Now find all the *upper bounds* -- that is, each UB is a free
679 // region that must outlive pick region R0 (`UB: R0`). Therefore,
680 // we need only keep an option O if `UB: O` for all UB.
681 if option_regions.len() > 1 {
682 let universal_region_relations = self.universal_region_relations.clone();
683 for ub in self.upper_bounds(scc) {
684 debug!("apply_pick_constraint: ub={:?}", ub);
685 option_regions.retain(|&o_r| universal_region_relations.outlives(ub, o_r));
687 debug!("apply_pick_constraint: after ub, option_regions={:?}", option_regions);
690 // If we ruled everything out, we're done.
691 if option_regions.is_empty() {
695 // Otherwise, we need to find the minimum option, if any, and take that.
696 debug!("apply_pick_constraint: option_regions remaining are {:#?}", option_regions);
697 let min = |r1: ty::RegionVid, r2: ty::RegionVid| -> Option<ty::RegionVid> {
698 let r1_outlives_r2 = self.universal_region_relations.outlives(r1, r2);
699 let r2_outlives_r1 = self.universal_region_relations.outlives(r2, r1);
700 if r1_outlives_r2 && r2_outlives_r1 {
702 } else if r1_outlives_r2 {
704 } else if r2_outlives_r1 {
710 let mut best_option = option_regions[0];
711 for &other_option in &option_regions[1..] {
713 "apply_pick_constraint: best_option={:?} other_option={:?}",
714 best_option, other_option,
716 match min(best_option, other_option) {
717 Some(m) => best_option = m,
720 "apply_pick_constraint: {:?} and {:?} are incomparable --> no best choice",
721 best_option, other_option,
728 let best_option_scc = self.constraint_sccs.scc(best_option);
730 "apply_pick_constraint: best_choice={:?} best_option_scc={:?}",
734 if self.scc_values.add_region(scc, best_option_scc) {
735 self.pick_constraints_applied.push(AppliedPickConstraint {
736 pick_region_scc: scc,
738 pick_constraint_index,
747 /// Compute and return the reverse SCC-based constraint graph (lazilly).
750 scc0: ConstraintSccIndex,
751 ) -> Vec<RegionVid> {
752 // I wanted to return an `impl Iterator` here, but it's
753 // annoying because the `rev_constraint_graph` is in a local
754 // variable. We'd need a "once-cell" or some such thing to let
755 // us borrow it for the right amount of time.
756 let rev_constraint_graph = self.rev_constraint_graph();
757 let scc_values = &self.scc_values;
758 let mut duplicates = FxHashSet::default();
760 .depth_first_search(scc0)
762 .flat_map(|scc1| scc_values.universal_regions_outlived_by(scc1))
763 .filter(|&r| duplicates.insert(r))
767 /// Compute and return the reverse SCC-based constraint graph (lazilly).
768 fn rev_constraint_graph(
770 ) -> Rc<VecGraph<ConstraintSccIndex>> {
771 if let Some(g) = &self.rev_constraint_graph {
775 let rev_graph = Rc::new(self.constraint_sccs.reverse());
776 self.rev_constraint_graph = Some(rev_graph.clone());
780 /// Returns `true` if all the elements in the value of `scc_b` are nameable
781 /// in `scc_a`. Used during constraint propagation, and only once
782 /// the value of `scc_b` has been computed.
783 fn universe_compatible(&self, scc_b: ConstraintSccIndex, scc_a: ConstraintSccIndex) -> bool {
784 let universe_a = self.scc_universes[scc_a];
786 // Quick check: if scc_b's declared universe is a subset of
787 // scc_a's declared univese (typically, both are ROOT), then
788 // it cannot contain any problematic universe elements.
789 if universe_a.can_name(self.scc_universes[scc_b]) {
793 // Otherwise, we have to iterate over the universe elements in
794 // B's value, and check whether all of them are nameable
796 self.scc_values.placeholders_contained_in(scc_b).all(|p| universe_a.can_name(p.universe))
799 /// Extend `scc` so that it can outlive some placeholder region
800 /// from a universe it can't name; at present, the only way for
801 /// this to be true is if `scc` outlives `'static`. This is
802 /// actually stricter than necessary: ideally, we'd support bounds
803 /// like `for<'a: 'b`>` that might then allow us to approximate
804 /// `'a` with `'b` and not `'static`. But it will have to do for
806 fn add_incompatible_universe(&mut self, scc: ConstraintSccIndex) {
807 debug!("add_incompatible_universe(scc={:?})", scc);
809 let fr_static = self.universal_regions.fr_static;
810 self.scc_values.add_all_points(scc);
811 self.scc_values.add_element(scc, fr_static);
814 /// Once regions have been propagated, this method is used to see
815 /// whether the "type tests" produced by typeck were satisfied;
816 /// type tests encode type-outlives relationships like `T:
817 /// 'a`. See `TypeTest` for more details.
820 infcx: &InferCtxt<'_, 'tcx>,
823 mut propagated_outlives_requirements: Option<&mut Vec<ClosureOutlivesRequirement<'tcx>>>,
824 errors_buffer: &mut Vec<Diagnostic>,
828 // Sometimes we register equivalent type-tests that would
829 // result in basically the exact same error being reported to
830 // the user. Avoid that.
831 let mut deduplicate_errors = FxHashSet::default();
833 for type_test in &self.type_tests {
834 debug!("check_type_test: {:?}", type_test);
836 let generic_ty = type_test.generic_kind.to_ty(tcx);
837 if self.eval_verify_bound(
841 type_test.lower_bound,
842 &type_test.verify_bound,
847 if let Some(propagated_outlives_requirements) = &mut propagated_outlives_requirements {
848 if self.try_promote_type_test(
852 propagated_outlives_requirements,
858 // Type-test failed. Report the error.
860 // Try to convert the lower-bound region into something named we can print for the user.
861 let lower_bound_region = self.to_error_region(type_test.lower_bound);
863 // Skip duplicate-ish errors.
864 let type_test_span = type_test.locations.span(body);
865 let erased_generic_kind = tcx.erase_regions(&type_test.generic_kind);
866 if !deduplicate_errors.insert((
874 "check_type_test: reporting error for erased_generic_kind={:?}, \
875 lower_bound_region={:?}, \
876 type_test.locations={:?}",
877 erased_generic_kind, lower_bound_region, type_test.locations,
881 if let Some(lower_bound_region) = lower_bound_region {
882 let region_scope_tree = &tcx.region_scope_tree(mir_def_id);
884 .construct_generic_bound_failure(
888 type_test.generic_kind,
891 .buffer(errors_buffer);
893 // FIXME. We should handle this case better. It
894 // indicates that we have e.g., some region variable
895 // whose value is like `'a+'b` where `'a` and `'b` are
896 // distinct unrelated univesal regions that are not
897 // known to outlive one another. It'd be nice to have
898 // some examples where this arises to decide how best
899 // to report it; we could probably handle it by
900 // iterating over the universal regions and reporting
901 // an error that multiple bounds are required.
905 &format!("`{}` does not live long enough", type_test.generic_kind,),
907 .buffer(errors_buffer);
912 /// Converts a region inference variable into a `ty::Region` that
913 /// we can use for error reporting. If `r` is universally bound,
914 /// then we use the name that we have on record for it. If `r` is
915 /// existentially bound, then we check its inferred value and try
916 /// to find a good name from that. Returns `None` if we can't find
917 /// one (e.g., this is just some random part of the CFG).
918 pub fn to_error_region(&self, r: RegionVid) -> Option<ty::Region<'tcx>> {
919 self.to_error_region_vid(r).and_then(|r| self.definitions[r].external_name)
922 /// Returns the [RegionVid] corresponding to the region returned by
923 /// `to_error_region`.
924 pub fn to_error_region_vid(&self, r: RegionVid) -> Option<RegionVid> {
925 if self.universal_regions.is_universal_region(r) {
928 let r_scc = self.constraint_sccs.scc(r);
929 let upper_bound = self.universal_upper_bound(r);
930 if self.scc_values.contains(r_scc, upper_bound) {
931 self.to_error_region_vid(upper_bound)
938 /// Invoked when we have some type-test (e.g., `T: 'X`) that we cannot
939 /// prove to be satisfied. If this is a closure, we will attempt to
940 /// "promote" this type-test into our `ClosureRegionRequirements` and
941 /// hence pass it up the creator. To do this, we have to phrase the
942 /// type-test in terms of external free regions, as local free
943 /// regions are not nameable by the closure's creator.
945 /// Promotion works as follows: we first check that the type `T`
946 /// contains only regions that the creator knows about. If this is
947 /// true, then -- as a consequence -- we know that all regions in
948 /// the type `T` are free regions that outlive the closure body. If
949 /// false, then promotion fails.
951 /// Once we've promoted T, we have to "promote" `'X` to some region
952 /// that is "external" to the closure. Generally speaking, a region
953 /// may be the union of some points in the closure body as well as
954 /// various free lifetimes. We can ignore the points in the closure
955 /// body: if the type T can be expressed in terms of external regions,
956 /// we know it outlives the points in the closure body. That
957 /// just leaves the free regions.
959 /// The idea then is to lower the `T: 'X` constraint into multiple
960 /// bounds -- e.g., if `'X` is the union of two free lifetimes,
961 /// `'1` and `'2`, then we would create `T: '1` and `T: '2`.
962 fn try_promote_type_test(
964 infcx: &InferCtxt<'_, 'tcx>,
966 type_test: &TypeTest<'tcx>,
967 propagated_outlives_requirements: &mut Vec<ClosureOutlivesRequirement<'tcx>>,
971 let TypeTest { generic_kind, lower_bound, locations, verify_bound: _ } = type_test;
973 let generic_ty = generic_kind.to_ty(tcx);
974 let subject = match self.try_promote_type_test_subject(infcx, generic_ty) {
976 None => return false,
979 // For each region outlived by lower_bound find a non-local,
980 // universal region (it may be the same region) and add it to
981 // `ClosureOutlivesRequirement`.
982 let r_scc = self.constraint_sccs.scc(*lower_bound);
983 for ur in self.scc_values.universal_regions_outlived_by(r_scc) {
984 // Check whether we can already prove that the "subject" outlives `ur`.
985 // If so, we don't have to propagate this requirement to our caller.
987 // To continue the example from the function, if we are trying to promote
988 // a requirement that `T: 'X`, and we know that `'X = '1 + '2` (i.e., the union
989 // `'1` and `'2`), then in this loop `ur` will be `'1` (and `'2`). So here
990 // we check whether `T: '1` is something we *can* prove. If so, no need
991 // to propagate that requirement.
993 // This is needed because -- particularly in the case
994 // where `ur` is a local bound -- we are sometimes in a
995 // position to prove things that our caller cannot. See
996 // #53570 for an example.
997 if self.eval_verify_bound(tcx, body, generic_ty, ur, &type_test.verify_bound) {
1001 debug!("try_promote_type_test: ur={:?}", ur);
1003 let non_local_ub = self.universal_region_relations.non_local_upper_bounds(&ur);
1004 debug!("try_promote_type_test: non_local_ub={:?}", non_local_ub);
1006 // This is slightly too conservative. To show T: '1, given `'2: '1`
1007 // and `'3: '1` we only need to prove that T: '2 *or* T: '3, but to
1008 // avoid potential non-determinism we approximate this by requiring
1010 for &upper_bound in non_local_ub {
1011 debug_assert!(self.universal_regions.is_universal_region(upper_bound));
1012 debug_assert!(!self.universal_regions.is_local_free_region(upper_bound));
1014 let requirement = ClosureOutlivesRequirement {
1016 outlived_free_region: upper_bound,
1017 blame_span: locations.span(body),
1018 category: ConstraintCategory::Boring,
1020 debug!("try_promote_type_test: pushing {:#?}", requirement);
1021 propagated_outlives_requirements.push(requirement);
1027 /// When we promote a type test `T: 'r`, we have to convert the
1028 /// type `T` into something we can store in a query result (so
1029 /// something allocated for `'tcx`). This is problematic if `ty`
1030 /// contains regions. During the course of NLL region checking, we
1031 /// will have replaced all of those regions with fresh inference
1032 /// variables. To create a test subject, we want to replace those
1033 /// inference variables with some region from the closure
1034 /// signature -- this is not always possible, so this is a
1035 /// fallible process. Presuming we do find a suitable region, we
1036 /// will represent it with a `ReClosureBound`, which is a
1037 /// `RegionKind` variant that can be allocated in the gcx.
1038 fn try_promote_type_test_subject(
1040 infcx: &InferCtxt<'_, 'tcx>,
1042 ) -> Option<ClosureOutlivesSubject<'tcx>> {
1043 let tcx = infcx.tcx;
1045 debug!("try_promote_type_test_subject(ty = {:?})", ty);
1047 let ty = tcx.fold_regions(&ty, &mut false, |r, _depth| {
1048 let region_vid = self.to_region_vid(r);
1050 // The challenge if this. We have some region variable `r`
1051 // whose value is a set of CFG points and universal
1052 // regions. We want to find if that set is *equivalent* to
1053 // any of the named regions found in the closure.
1055 // To do so, we compute the
1056 // `non_local_universal_upper_bound`. This will be a
1057 // non-local, universal region that is greater than `r`.
1058 // However, it might not be *contained* within `r`, so
1059 // then we further check whether this bound is contained
1060 // in `r`. If so, we can say that `r` is equivalent to the
1063 // Let's work through a few examples. For these, imagine
1064 // that we have 3 non-local regions (I'll denote them as
1065 // `'static`, `'a`, and `'b`, though of course in the code
1066 // they would be represented with indices) where:
1071 // First, let's assume that `r` is some existential
1072 // variable with an inferred value `{'a, 'static}` (plus
1073 // some CFG nodes). In this case, the non-local upper
1074 // bound is `'static`, since that outlives `'a`. `'static`
1075 // is also a member of `r` and hence we consider `r`
1076 // equivalent to `'static` (and replace it with
1079 // Now let's consider the inferred value `{'a, 'b}`. This
1080 // means `r` is effectively `'a | 'b`. I'm not sure if
1081 // this can come about, actually, but assuming it did, we
1082 // would get a non-local upper bound of `'static`. Since
1083 // `'static` is not contained in `r`, we would fail to
1084 // find an equivalent.
1085 let upper_bound = self.non_local_universal_upper_bound(region_vid);
1086 if self.region_contains(region_vid, upper_bound) {
1087 tcx.mk_region(ty::ReClosureBound(upper_bound))
1089 // In the case of a failure, use a `ReVar`
1090 // result. This will cause the `lift` later on to
1095 debug!("try_promote_type_test_subject: folded ty = {:?}", ty);
1097 // `has_local_value` will only be true if we failed to promote some region.
1098 if ty.has_local_value() {
1102 Some(ClosureOutlivesSubject::Ty(ty))
1105 /// Given some universal or existential region `r`, finds a
1106 /// non-local, universal region `r+` that outlives `r` at entry to (and
1107 /// exit from) the closure. In the worst case, this will be
1110 /// This is used for two purposes. First, if we are propagated
1111 /// some requirement `T: r`, we can use this method to enlarge `r`
1112 /// to something we can encode for our creator (which only knows
1113 /// about non-local, universal regions). It is also used when
1114 /// encoding `T` as part of `try_promote_type_test_subject` (see
1115 /// that fn for details).
1117 /// This is based on the result `'y` of `universal_upper_bound`,
1118 /// except that it converts further takes the non-local upper
1119 /// bound of `'y`, so that the final result is non-local.
1120 fn non_local_universal_upper_bound(&self, r: RegionVid) -> RegionVid {
1121 debug!("non_local_universal_upper_bound(r={:?}={})", r, self.region_value_str(r));
1123 let lub = self.universal_upper_bound(r);
1125 // Grow further to get smallest universal region known to
1127 let non_local_lub = self.universal_region_relations.non_local_upper_bound(lub);
1129 debug!("non_local_universal_upper_bound: non_local_lub={:?}", non_local_lub);
1134 /// Returns a universally quantified region that outlives the
1135 /// value of `r` (`r` may be existentially or universally
1138 /// Since `r` is (potentially) an existential region, it has some
1139 /// value which may include (a) any number of points in the CFG
1140 /// and (b) any number of `end('x)` elements of universally
1141 /// quantified regions. To convert this into a single universal
1142 /// region we do as follows:
1144 /// - Ignore the CFG points in `'r`. All universally quantified regions
1145 /// include the CFG anyhow.
1146 /// - For each `end('x)` element in `'r`, compute the mutual LUB, yielding
1148 fn universal_upper_bound(&self, r: RegionVid) -> RegionVid {
1149 debug!("universal_upper_bound(r={:?}={})", r, self.region_value_str(r));
1151 // Find the smallest universal region that contains all other
1152 // universal regions within `region`.
1153 let mut lub = self.universal_regions.fr_fn_body;
1154 let r_scc = self.constraint_sccs.scc(r);
1155 for ur in self.scc_values.universal_regions_outlived_by(r_scc) {
1156 lub = self.universal_region_relations.postdom_upper_bound(lub, ur);
1159 debug!("universal_upper_bound: r={:?} lub={:?}", r, lub);
1164 /// Tests if `test` is true when applied to `lower_bound` at
1166 fn eval_verify_bound(
1170 generic_ty: Ty<'tcx>,
1171 lower_bound: RegionVid,
1172 verify_bound: &VerifyBound<'tcx>,
1174 debug!("eval_verify_bound(lower_bound={:?}, verify_bound={:?})", lower_bound, verify_bound);
1176 match verify_bound {
1177 VerifyBound::IfEq(test_ty, verify_bound1) => {
1178 self.eval_if_eq(tcx, body, generic_ty, lower_bound, test_ty, verify_bound1)
1181 VerifyBound::OutlivedBy(r) => {
1182 let r_vid = self.to_region_vid(r);
1183 self.eval_outlives(r_vid, lower_bound)
1186 VerifyBound::AnyBound(verify_bounds) => verify_bounds.iter().any(|verify_bound| {
1187 self.eval_verify_bound(tcx, body, generic_ty, lower_bound, verify_bound)
1190 VerifyBound::AllBounds(verify_bounds) => verify_bounds.iter().all(|verify_bound| {
1191 self.eval_verify_bound(tcx, body, generic_ty, lower_bound, verify_bound)
1200 generic_ty: Ty<'tcx>,
1201 lower_bound: RegionVid,
1203 verify_bound: &VerifyBound<'tcx>,
1205 let generic_ty_normalized = self.normalize_to_scc_representatives(tcx, generic_ty);
1206 let test_ty_normalized = self.normalize_to_scc_representatives(tcx, test_ty);
1207 if generic_ty_normalized == test_ty_normalized {
1208 self.eval_verify_bound(tcx, body, generic_ty, lower_bound, verify_bound)
1214 /// This is a conservative normalization procedure. It takes every
1215 /// free region in `value` and replaces it with the
1216 /// "representative" of its SCC (see `scc_representatives` field).
1217 /// We are guaranteed that if two values normalize to the same
1218 /// thing, then they are equal; this is a conservative check in
1219 /// that they could still be equal even if they normalize to
1220 /// different results. (For example, there might be two regions
1221 /// with the same value that are not in the same SCC).
1223 /// N.B., this is not an ideal approach and I would like to revisit
1224 /// it. However, it works pretty well in practice. In particular,
1225 /// this is needed to deal with projection outlives bounds like
1227 /// <T as Foo<'0>>::Item: '1
1229 /// In particular, this routine winds up being important when
1230 /// there are bounds like `where <T as Foo<'a>>::Item: 'b` in the
1231 /// environment. In this case, if we can show that `'0 == 'a`,
1232 /// and that `'b: '1`, then we know that the clause is
1233 /// satisfied. In such cases, particularly due to limitations of
1234 /// the trait solver =), we usually wind up with a where-clause like
1235 /// `T: Foo<'a>` in scope, which thus forces `'0 == 'a` to be added as
1236 /// a constraint, and thus ensures that they are in the same SCC.
1238 /// So why can't we do a more correct routine? Well, we could
1239 /// *almost* use the `relate_tys` code, but the way it is
1240 /// currently setup it creates inference variables to deal with
1241 /// higher-ranked things and so forth, and right now the inference
1242 /// context is not permitted to make more inference variables. So
1243 /// we use this kind of hacky solution.
1244 fn normalize_to_scc_representatives<T>(&self, tcx: TyCtxt<'tcx>, value: T) -> T
1246 T: TypeFoldable<'tcx>,
1248 tcx.fold_regions(&value, &mut false, |r, _db| {
1249 let vid = self.to_region_vid(r);
1250 let scc = self.constraint_sccs.scc(vid);
1251 let repr = self.scc_representatives[scc];
1252 tcx.mk_region(ty::ReVar(repr))
1256 // Evaluate whether `sup_region == sub_region`.
1257 fn eval_equal(&self, r1: RegionVid, r2: RegionVid) -> bool {
1258 self.eval_outlives(r1, r2) && self.eval_outlives(r2, r1)
1261 // Evaluate whether `sup_region: sub_region`.
1262 fn eval_outlives(&self, sup_region: RegionVid, sub_region: RegionVid) -> bool {
1263 debug!("eval_outlives({:?}: {:?})", sup_region, sub_region);
1266 "eval_outlives: sup_region's value = {:?} universal={:?}",
1267 self.region_value_str(sup_region),
1268 self.universal_regions.is_universal_region(sup_region),
1271 "eval_outlives: sub_region's value = {:?} universal={:?}",
1272 self.region_value_str(sub_region),
1273 self.universal_regions.is_universal_region(sub_region),
1276 let sub_region_scc = self.constraint_sccs.scc(sub_region);
1277 let sup_region_scc = self.constraint_sccs.scc(sup_region);
1279 // Both the `sub_region` and `sup_region` consist of the union
1280 // of some number of universal regions (along with the union
1281 // of various points in the CFG; ignore those points for
1282 // now). Therefore, the sup-region outlives the sub-region if,
1283 // for each universal region R1 in the sub-region, there
1284 // exists some region R2 in the sup-region that outlives R1.
1285 let universal_outlives =
1286 self.scc_values.universal_regions_outlived_by(sub_region_scc).all(|r1| {
1288 .universal_regions_outlived_by(sup_region_scc)
1289 .any(|r2| self.universal_region_relations.outlives(r2, r1))
1292 if !universal_outlives {
1296 // Now we have to compare all the points in the sub region and make
1297 // sure they exist in the sup region.
1299 if self.universal_regions.is_universal_region(sup_region) {
1300 // Micro-opt: universal regions contain all points.
1304 self.scc_values.contains_points(sup_region_scc, sub_region_scc)
1307 /// Once regions have been propagated, this method is used to see
1308 /// whether any of the constraints were too strong. In particular,
1309 /// we want to check for a case where a universally quantified
1310 /// region exceeded its bounds. Consider:
1312 /// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
1314 /// In this case, returning `x` requires `&'a u32 <: &'b u32`
1315 /// and hence we establish (transitively) a constraint that
1316 /// `'a: 'b`. The `propagate_constraints` code above will
1317 /// therefore add `end('a)` into the region for `'b` -- but we
1318 /// have no evidence that `'b` outlives `'a`, so we want to report
1321 /// If `propagated_outlives_requirements` is `Some`, then we will
1322 /// push unsatisfied obligations into there. Otherwise, we'll
1323 /// report them as errors.
1324 fn check_universal_regions(
1326 infcx: &InferCtxt<'_, 'tcx>,
1330 mut propagated_outlives_requirements: Option<&mut Vec<ClosureOutlivesRequirement<'tcx>>>,
1331 errors_buffer: &mut Vec<Diagnostic>,
1333 for (fr, fr_definition) in self.definitions.iter_enumerated() {
1334 match fr_definition.origin {
1335 NLLRegionVariableOrigin::FreeRegion => {
1336 // Go through each of the universal regions `fr` and check that
1337 // they did not grow too large, accumulating any requirements
1338 // for our caller into the `outlives_requirements` vector.
1339 self.check_universal_region(
1345 &mut propagated_outlives_requirements,
1350 NLLRegionVariableOrigin::Placeholder(placeholder) => {
1351 self.check_bound_universal_region(infcx, body, mir_def_id, fr, placeholder);
1354 NLLRegionVariableOrigin::Existential => {
1355 // nothing to check here
1361 /// Checks the final value for the free region `fr` to see if it
1362 /// grew too large. In particular, examine what `end(X)` points
1363 /// wound up in `fr`'s final value; for each `end(X)` where `X !=
1364 /// fr`, we want to check that `fr: X`. If not, that's either an
1365 /// error, or something we have to propagate to our creator.
1367 /// Things that are to be propagated are accumulated into the
1368 /// `outlives_requirements` vector.
1369 fn check_universal_region(
1371 infcx: &InferCtxt<'_, 'tcx>,
1375 longer_fr: RegionVid,
1376 propagated_outlives_requirements: &mut Option<&mut Vec<ClosureOutlivesRequirement<'tcx>>>,
1377 errors_buffer: &mut Vec<Diagnostic>,
1379 debug!("check_universal_region(fr={:?})", longer_fr);
1381 let longer_fr_scc = self.constraint_sccs.scc(longer_fr);
1383 // Because this free region must be in the ROOT universe, we
1384 // know it cannot contain any bound universes.
1385 assert!(self.scc_universes[longer_fr_scc] == ty::UniverseIndex::ROOT);
1386 debug_assert!(self.scc_values.placeholders_contained_in(longer_fr_scc).next().is_none());
1388 // Only check all of the relations for the main representative of each
1389 // SCC, otherwise just check that we outlive said representative. This
1390 // reduces the number of redundant relations propagated out of
1392 // Note that the representative will be a universal region if there is
1393 // one in this SCC, so we will always check the representative here.
1394 let representative = self.scc_representatives[longer_fr_scc];
1395 if representative != longer_fr {
1396 self.check_universal_region_relation(
1403 propagated_outlives_requirements,
1409 // Find every region `o` such that `fr: o`
1410 // (because `fr` includes `end(o)`).
1411 for shorter_fr in self.scc_values.universal_regions_outlived_by(longer_fr_scc) {
1412 if let Some(ErrorReported) = self.check_universal_region_relation(
1419 propagated_outlives_requirements,
1422 // continuing to iterate just reports more errors than necessary
1428 fn check_universal_region_relation(
1430 longer_fr: RegionVid,
1431 shorter_fr: RegionVid,
1432 infcx: &InferCtxt<'_, 'tcx>,
1436 propagated_outlives_requirements: &mut Option<&mut Vec<ClosureOutlivesRequirement<'tcx>>>,
1437 errors_buffer: &mut Vec<Diagnostic>,
1438 ) -> Option<ErrorReported> {
1439 // If it is known that `fr: o`, carry on.
1440 if self.universal_region_relations.outlives(longer_fr, shorter_fr) {
1445 "check_universal_region_relation: fr={:?} does not outlive shorter_fr={:?}",
1446 longer_fr, shorter_fr,
1449 if let Some(propagated_outlives_requirements) = propagated_outlives_requirements {
1450 // Shrink `longer_fr` until we find a non-local region (if we do).
1451 // We'll call it `fr-` -- it's ever so slightly smaller than
1454 if let Some(fr_minus) = self.universal_region_relations.non_local_lower_bound(longer_fr)
1456 debug!("check_universal_region: fr_minus={:?}", fr_minus);
1458 let blame_span_category =
1459 self.find_outlives_blame_span(body, longer_fr, shorter_fr);
1461 // Grow `shorter_fr` until we find some non-local regions. (We
1462 // always will.) We'll call them `shorter_fr+` -- they're ever
1463 // so slightly larger than `shorter_fr`.
1464 let shorter_fr_plus =
1465 self.universal_region_relations.non_local_upper_bounds(&shorter_fr);
1466 debug!("check_universal_region: shorter_fr_plus={:?}", shorter_fr_plus);
1467 for &&fr in &shorter_fr_plus {
1468 // Push the constraint `fr-: shorter_fr+`
1469 propagated_outlives_requirements.push(ClosureOutlivesRequirement {
1470 subject: ClosureOutlivesSubject::Region(fr_minus),
1471 outlived_free_region: fr,
1472 blame_span: blame_span_category.1,
1473 category: blame_span_category.0,
1480 // If we are not in a context where we can't propagate errors, or we
1481 // could not shrink `fr` to something smaller, then just report an
1484 // Note: in this case, we use the unapproximated regions to report the
1485 // error. This gives better error messages in some cases.
1486 self.report_error(body, upvars, infcx, mir_def_id, longer_fr, shorter_fr, errors_buffer);
1490 fn check_bound_universal_region(
1492 infcx: &InferCtxt<'_, 'tcx>,
1495 longer_fr: RegionVid,
1496 placeholder: ty::PlaceholderRegion,
1498 debug!("check_bound_universal_region(fr={:?}, placeholder={:?})", longer_fr, placeholder,);
1500 let longer_fr_scc = self.constraint_sccs.scc(longer_fr);
1501 debug!("check_bound_universal_region: longer_fr_scc={:?}", longer_fr_scc,);
1503 // If we have some bound universal region `'a`, then the only
1504 // elements it can contain is itself -- we don't know anything
1506 let error_element = match {
1507 self.scc_values.elements_contained_in(longer_fr_scc).find(|element| match element {
1508 RegionElement::Location(_) => true,
1509 RegionElement::RootUniversalRegion(_) => true,
1510 RegionElement::PlaceholderRegion(placeholder1) => placeholder != *placeholder1,
1516 debug!("check_bound_universal_region: error_element = {:?}", error_element);
1518 // Find the region that introduced this `error_element`.
1519 let error_region = match error_element {
1520 RegionElement::Location(l) => self.find_sub_region_live_at(longer_fr, l),
1521 RegionElement::RootUniversalRegion(r) => r,
1522 RegionElement::PlaceholderRegion(error_placeholder) => self
1525 .filter_map(|(r, definition)| match definition.origin {
1526 NLLRegionVariableOrigin::Placeholder(p) if p == error_placeholder => Some(r),
1533 // Find the code to blame for the fact that `longer_fr` outlives `error_fr`.
1534 let (_, span) = self.find_outlives_blame_span(body, longer_fr, error_region);
1536 // Obviously, this error message is far from satisfactory.
1537 // At present, though, it only appears in unit tests --
1538 // the AST-based checker uses a more conservative check,
1539 // so to even see this error, one must pass in a special
1541 let mut diag = infcx.tcx.sess.struct_span_err(span, "higher-ranked subtype error");
1545 fn check_pick_constraints(
1547 infcx: &InferCtxt<'_, 'tcx>,
1549 errors_buffer: &mut Vec<Diagnostic>,
1551 let pick_constraints = self.pick_constraints.clone();
1552 for p_c_i in pick_constraints.all_indices() {
1553 debug!("check_pick_constraint(p_c_i={:?})", p_c_i);
1554 let p_c = &pick_constraints[p_c_i];
1555 let pick_region_vid = p_c.pick_region_vid;
1557 "check_pick_constraint: pick_region_vid={:?} with value {}",
1559 self.region_value_str(pick_region_vid),
1561 let option_regions = pick_constraints.option_regions(p_c_i);
1562 debug!("check_pick_constraint: option_regions={:?}", option_regions);
1564 // did the pick-region wind up equal to any of the option regions?
1565 if let Some(o) = option_regions.iter().find(|&&o_r| {
1566 self.eval_equal(o_r, p_c.pick_region_vid)
1568 debug!("check_pick_constraint: evaluated as equal to {:?}", o);
1572 // if not, report an error
1573 let region_scope_tree = &infcx.tcx.region_scope_tree(mir_def_id);
1574 let pick_region = infcx.tcx.mk_region(ty::ReVar(pick_region_vid));
1575 opaque_types::unexpected_hidden_region_diagnostic(
1577 Some(region_scope_tree),
1578 p_c.opaque_type_def_id,
1582 .buffer(errors_buffer);
1587 impl<'tcx> RegionDefinition<'tcx> {
1588 fn new(universe: ty::UniverseIndex, rv_origin: RegionVariableOrigin) -> Self {
1589 // Create a new region definition. Note that, for free
1590 // regions, the `external_name` field gets updated later in
1591 // `init_universal_regions`.
1593 let origin = match rv_origin {
1594 RegionVariableOrigin::NLL(origin) => origin,
1595 _ => NLLRegionVariableOrigin::Existential,
1598 Self { origin, universe, external_name: None }
1602 pub trait ClosureRegionRequirementsExt<'tcx> {
1603 fn apply_requirements(
1606 closure_def_id: DefId,
1607 closure_substs: SubstsRef<'tcx>,
1608 ) -> Vec<QueryOutlivesConstraint<'tcx>>;
1610 fn subst_closure_mapping<T>(
1613 closure_mapping: &IndexVec<RegionVid, ty::Region<'tcx>>,
1617 T: TypeFoldable<'tcx>;
1620 impl<'tcx> ClosureRegionRequirementsExt<'tcx> for ClosureRegionRequirements<'tcx> {
1621 /// Given an instance T of the closure type, this method
1622 /// instantiates the "extra" requirements that we computed for the
1623 /// closure into the inference context. This has the effect of
1624 /// adding new outlives obligations to existing variables.
1626 /// As described on `ClosureRegionRequirements`, the extra
1627 /// requirements are expressed in terms of regionvids that index
1628 /// into the free regions that appear on the closure type. So, to
1629 /// do this, we first copy those regions out from the type T into
1630 /// a vector. Then we can just index into that vector to extract
1631 /// out the corresponding region from T and apply the
1633 fn apply_requirements(
1636 closure_def_id: DefId,
1637 closure_substs: SubstsRef<'tcx>,
1638 ) -> Vec<QueryOutlivesConstraint<'tcx>> {
1640 "apply_requirements(closure_def_id={:?}, closure_substs={:?})",
1641 closure_def_id, closure_substs
1644 // Extract the values of the free regions in `closure_substs`
1645 // into a vector. These are the regions that we will be
1646 // relating to one another.
1647 let closure_mapping = &UniversalRegions::closure_mapping(
1650 self.num_external_vids,
1651 tcx.closure_base_def_id(closure_def_id),
1653 debug!("apply_requirements: closure_mapping={:?}", closure_mapping);
1655 // Create the predicates.
1656 self.outlives_requirements
1658 .map(|outlives_requirement| {
1659 let outlived_region = closure_mapping[outlives_requirement.outlived_free_region];
1661 match outlives_requirement.subject {
1662 ClosureOutlivesSubject::Region(region) => {
1663 let region = closure_mapping[region];
1665 "apply_requirements: region={:?} \
1666 outlived_region={:?} \
1667 outlives_requirement={:?}",
1668 region, outlived_region, outlives_requirement,
1670 ty::Binder::dummy(ty::OutlivesPredicate(region.into(), outlived_region))
1673 ClosureOutlivesSubject::Ty(ty) => {
1674 let ty = self.subst_closure_mapping(tcx, closure_mapping, &ty);
1676 "apply_requirements: ty={:?} \
1677 outlived_region={:?} \
1678 outlives_requirement={:?}",
1679 ty, outlived_region, outlives_requirement,
1681 ty::Binder::dummy(ty::OutlivesPredicate(ty.into(), outlived_region))
1688 fn subst_closure_mapping<T>(
1691 closure_mapping: &IndexVec<RegionVid, ty::Region<'tcx>>,
1695 T: TypeFoldable<'tcx>,
1697 tcx.fold_regions(value, &mut false, |r, _depth| {
1698 if let ty::ReClosureBound(vid) = r {
1699 closure_mapping[*vid]
1701 bug!("subst_closure_mapping: encountered non-closure bound free region {:?}", r)