1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use super::universal_regions::UniversalRegions;
12 use borrow_check::nll::constraints::graph::NormalConstraintGraph;
13 use borrow_check::nll::constraints::{ConstraintSccIndex, ConstraintSet, OutlivesConstraint};
14 use borrow_check::nll::region_infer::values::{PlaceholderIndices, RegionElement, ToElementIndex};
15 use borrow_check::nll::type_check::free_region_relations::UniversalRegionRelations;
16 use borrow_check::nll::type_check::Locations;
17 use rustc::hir::def_id::DefId;
18 use rustc::infer::canonical::QueryRegionConstraint;
19 use rustc::infer::region_constraints::{GenericKind, VarInfos, VerifyBound};
20 use rustc::infer::{InferCtxt, NLLRegionVariableOrigin, RegionVariableOrigin};
22 ClosureOutlivesRequirement, ClosureOutlivesSubject, ClosureRegionRequirements,
23 ConstraintCategory, Local, Location, Mir,
25 use rustc::ty::{self, RegionVid, Ty, TyCtxt, TypeFoldable};
26 use rustc::util::common;
27 use rustc_data_structures::bit_set::BitSet;
28 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
29 use rustc_data_structures::graph::scc::Sccs;
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<ConstraintSet>,
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 graph. Used to
67 /// compute the values of each region.
68 constraint_sccs: Rc<Sccs<RegionVid, ConstraintSccIndex>>,
70 /// Map closure bounds to a `Span` that should be used for error reporting.
71 closure_bounds_mapping:
72 FxHashMap<Location, FxHashMap<(RegionVid, RegionVid), (ConstraintCategory, Span)>>,
74 /// Contains the minimum universe of any variable within the same
75 /// SCC. We will ensure that no SCC contains values that are not
76 /// visible from this index.
77 scc_universes: IndexVec<ConstraintSccIndex, ty::UniverseIndex>,
79 /// Contains a "representative" from each SCC. This will be the
80 /// minimal RegionVid belonging to that universe. It is used as a
81 /// kind of hacky way to manage checking outlives relationships,
82 /// since we can 'canonicalize' each region to the representative
83 /// of its SCC and be sure that -- if they have the same repr --
84 /// they *must* be equal (though not having the same repr does not
85 /// mean they are unequal).
86 scc_representatives: IndexVec<ConstraintSccIndex, ty::RegionVid>,
88 /// The final inferred values of the region variables; we compute
89 /// one value per SCC. To get the value for any given *region*,
90 /// you first find which scc it is a part of.
91 scc_values: RegionValues<ConstraintSccIndex>,
93 /// Type constraints that we check after solving.
94 type_tests: Vec<TypeTest<'tcx>>,
96 /// Information about the universally quantified regions in scope
98 universal_regions: Rc<UniversalRegions<'tcx>>,
100 /// Information about how the universally quantified regions in
101 /// scope on this function relate to one another.
102 universal_region_relations: Rc<UniversalRegionRelations<'tcx>>,
105 struct RegionDefinition<'tcx> {
106 /// What kind of variable is this -- a free region? existential
107 /// variable? etc. (See the `NLLRegionVariableOrigin` for more
109 origin: NLLRegionVariableOrigin,
111 /// Which universe is this region variable defined in? This is
112 /// most often `ty::UniverseIndex::ROOT`, but when we encounter
113 /// forall-quantifiers like `for<'a> { 'a = 'b }`, we would create
114 /// the variable for `'a` in a fresh universe that extends ROOT.
115 universe: ty::UniverseIndex,
117 /// If this is 'static or an early-bound region, then this is
118 /// `Some(X)` where `X` is the name of the region.
119 external_name: Option<ty::Region<'tcx>>,
122 /// NB: The variants in `Cause` are intentionally ordered. Lower
123 /// values are preferred when it comes to error messages. Do not
124 /// reorder willy nilly.
125 #[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
126 pub(crate) enum Cause {
127 /// point inserted because Local was live at the given Location
128 LiveVar(Local, Location),
130 /// point inserted because Local was dropped at the given Location
131 DropVar(Local, Location),
134 /// A "type test" corresponds to an outlives constraint between a type
135 /// and a lifetime, like `T: 'x` or `<T as Foo>::Bar: 'x`. They are
136 /// translated from the `Verify` region constraints in the ordinary
137 /// inference context.
139 /// These sorts of constraints are handled differently than ordinary
140 /// constraints, at least at present. During type checking, the
141 /// `InferCtxt::process_registered_region_obligations` method will
142 /// attempt to convert a type test like `T: 'x` into an ordinary
143 /// outlives constraint when possible (for example, `&'a T: 'b` will
144 /// be converted into `'a: 'b` and registered as a `Constraint`).
146 /// In some cases, however, there are outlives relationships that are
147 /// not converted into a region constraint, but rather into one of
148 /// these "type tests". The distinction is that a type test does not
149 /// influence the inference result, but instead just examines the
150 /// values that we ultimately inferred for each region variable and
151 /// checks that they meet certain extra criteria. If not, an error
154 /// One reason for this is that these type tests typically boil down
155 /// to a check like `'a: 'x` where `'a` is a universally quantified
156 /// region -- and therefore not one whose value is really meant to be
157 /// *inferred*, precisely (this is not always the case: one can have a
158 /// type test like `<Foo as Trait<'?0>>::Bar: 'x`, where `'?0` is an
159 /// inference variable). Another reason is that these type tests can
160 /// involve *disjunction* -- that is, they can be satisfied in more
163 /// For more information about this translation, see
164 /// `InferCtxt::process_registered_region_obligations` and
165 /// `InferCtxt::type_must_outlive` in `rustc::infer::outlives`.
166 #[derive(Clone, Debug)]
167 pub struct TypeTest<'tcx> {
168 /// The type `T` that must outlive the region.
169 pub generic_kind: GenericKind<'tcx>,
171 /// The region `'x` that the type must outlive.
172 pub lower_bound: RegionVid,
174 /// Where did this constraint arise and why?
175 pub locations: Locations,
177 /// A test which, if met by the region `'x`, proves that this type
178 /// constraint is satisfied.
179 pub verify_bound: VerifyBound<'tcx>,
182 impl<'tcx> RegionInferenceContext<'tcx> {
183 /// Creates a new region inference context with a total of
184 /// `num_region_variables` valid inference variables; the first N
185 /// of those will be constant regions representing the free
186 /// regions defined in `universal_regions`.
188 /// The `outlives_constraints` and `type_tests` are an initial set
189 /// of constraints produced by the MIR type check.
192 universal_regions: Rc<UniversalRegions<'tcx>>,
193 placeholder_indices: Rc<PlaceholderIndices>,
194 universal_region_relations: Rc<UniversalRegionRelations<'tcx>>,
196 outlives_constraints: ConstraintSet,
197 closure_bounds_mapping: FxHashMap<
199 FxHashMap<(RegionVid, RegionVid), (ConstraintCategory, Span)>,
201 type_tests: Vec<TypeTest<'tcx>>,
202 liveness_constraints: LivenessValues<RegionVid>,
203 elements: &Rc<RegionValueElements>,
205 // Create a RegionDefinition for each inference variable.
206 let definitions: IndexVec<_, _> = var_infos
208 .map(|info| RegionDefinition::new(info.universe, info.origin))
211 let constraints = Rc::new(outlives_constraints); // freeze constraints
212 let constraint_graph = Rc::new(constraints.graph(definitions.len()));
213 let fr_static = universal_regions.fr_static;
214 let constraint_sccs = Rc::new(constraints.compute_sccs(&constraint_graph, fr_static));
217 RegionValues::new(elements, universal_regions.len(), &placeholder_indices);
219 for region in liveness_constraints.rows() {
220 let scc = constraint_sccs.scc(region);
221 scc_values.merge_liveness(scc, region, &liveness_constraints);
224 let scc_universes = Self::compute_scc_universes(&constraint_sccs, &definitions);
226 let scc_representatives = Self::compute_scc_representatives(&constraint_sccs, &definitions);
228 let mut result = Self {
230 liveness_constraints,
234 closure_bounds_mapping,
240 universal_region_relations,
243 result.init_free_and_bound_regions();
248 /// Each SCC is the combination of many region variables which
249 /// have been equated. Therefore, we can associate a universe with
250 /// each SCC which is minimum of all the universes of its
251 /// constituent regions -- this is because whatever value the SCC
252 /// takes on must be a value that each of the regions within the
253 /// SCC could have as well. This implies that the SCC must have
254 /// the minimum, or narrowest, universe.
255 fn compute_scc_universes(
256 constraints_scc: &Sccs<RegionVid, ConstraintSccIndex>,
257 definitions: &IndexVec<RegionVid, RegionDefinition<'tcx>>,
258 ) -> IndexVec<ConstraintSccIndex, ty::UniverseIndex> {
259 let num_sccs = constraints_scc.num_sccs();
260 let mut scc_universes = IndexVec::from_elem_n(ty::UniverseIndex::MAX, num_sccs);
262 for (region_vid, region_definition) in definitions.iter_enumerated() {
263 let scc = constraints_scc.scc(region_vid);
264 let scc_universe = &mut scc_universes[scc];
265 *scc_universe = ::std::cmp::min(*scc_universe, region_definition.universe);
268 debug!("compute_scc_universes: scc_universe = {:#?}", scc_universes);
273 /// For each SCC, we compute a unique `RegionVid` (in fact, the
274 /// minimal one that belongs to the SCC). See
275 /// `scc_representatives` field of `RegionInferenceContext` for
277 fn compute_scc_representatives(
278 constraints_scc: &Sccs<RegionVid, ConstraintSccIndex>,
279 definitions: &IndexVec<RegionVid, RegionDefinition<'tcx>>,
280 ) -> IndexVec<ConstraintSccIndex, ty::RegionVid> {
281 let num_sccs = constraints_scc.num_sccs();
282 let next_region_vid = definitions.next_index();
283 let mut scc_representatives = IndexVec::from_elem_n(next_region_vid, num_sccs);
285 for region_vid in definitions.indices() {
286 let scc = constraints_scc.scc(region_vid);
287 let prev_min = scc_representatives[scc];
288 scc_representatives[scc] = region_vid.min(prev_min);
294 /// Initializes the region variables for each universally
295 /// quantified region (lifetime parameter). The first N variables
296 /// always correspond to the regions appearing in the function
297 /// signature (both named and anonymous) and where clauses. This
298 /// function iterates over those regions and initializes them with
303 /// fn foo<'a, 'b>(..) where 'a: 'b
305 /// would initialize two variables like so:
307 /// R0 = { CFG, R0 } // 'a
308 /// R1 = { CFG, R0, R1 } // 'b
310 /// Here, R0 represents `'a`, and it contains (a) the entire CFG
311 /// and (b) any universally quantified regions that it outlives,
312 /// which in this case is just itself. R1 (`'b`) in contrast also
313 /// outlives `'a` and hence contains R0 and R1.
314 fn init_free_and_bound_regions(&mut self) {
315 // Update the names (if any)
316 for (external_name, variable) in self.universal_regions.named_universal_regions() {
318 "init_universal_regions: region {:?} has external name {:?}",
319 variable, external_name
321 self.definitions[variable].external_name = Some(external_name);
324 for variable in self.definitions.indices() {
325 let scc = self.constraint_sccs.scc(variable);
327 match self.definitions[variable].origin {
328 NLLRegionVariableOrigin::FreeRegion => {
329 // For each free, universally quantified region X:
331 // Add all nodes in the CFG to liveness constraints
332 self.liveness_constraints.add_all_points(variable);
333 self.scc_values.add_all_points(scc);
335 // Add `end(X)` into the set for X.
336 self.scc_values.add_element(scc, variable);
339 NLLRegionVariableOrigin::Placeholder(placeholder) => {
340 // Each placeholder region is only visible from
341 // its universe `ui` and its extensions. So we
342 // can't just add it into `scc` unless the
343 // universe of the scc can name this region.
344 let scc_universe = self.scc_universes[scc];
345 if scc_universe.can_name(placeholder.universe) {
346 self.scc_values.add_element(scc, placeholder);
349 "init_free_and_bound_regions: placeholder {:?} is \
350 not compatible with universe {:?} of its SCC {:?}",
355 self.add_incompatible_universe(scc);
359 NLLRegionVariableOrigin::Existential => {
360 // For existential, regions, nothing to do.
366 /// Returns an iterator over all the region indices.
367 pub fn regions(&self) -> impl Iterator<Item = RegionVid> {
368 self.definitions.indices()
371 /// Given a universal region in scope on the MIR, returns the
372 /// corresponding index.
374 /// (Panics if `r` is not a registered universal region.)
375 pub fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
376 self.universal_regions.to_region_vid(r)
379 /// Add annotations for `#[rustc_regions]`; see `UniversalRegions::annotate`.
380 crate fn annotate(&self, tcx: TyCtxt<'_, '_, 'tcx>, err: &mut DiagnosticBuilder<'_>) {
381 self.universal_regions.annotate(tcx, err)
384 /// Returns true if the region `r` contains the point `p`.
386 /// Panics if called before `solve()` executes,
387 crate fn region_contains(&self, r: impl ToRegionVid, p: impl ToElementIndex) -> bool {
388 let scc = self.constraint_sccs.scc(r.to_region_vid());
389 self.scc_values.contains(scc, p)
392 /// Returns access to the value of `r` for debugging purposes.
393 crate fn region_value_str(&self, r: RegionVid) -> String {
394 let scc = self.constraint_sccs.scc(r.to_region_vid());
395 self.scc_values.region_value_str(scc)
398 /// Returns access to the value of `r` for debugging purposes.
399 crate fn region_universe(&self, r: RegionVid) -> ty::UniverseIndex {
400 let scc = self.constraint_sccs.scc(r.to_region_vid());
401 self.scc_universes[scc]
404 /// Perform region inference and report errors if we see any
405 /// unsatisfiable constraints. If this is a closure, returns the
406 /// region requirements to propagate to our creator, if any.
407 pub(super) fn solve<'gcx>(
409 infcx: &InferCtxt<'_, 'gcx, 'tcx>,
412 errors_buffer: &mut Vec<Diagnostic>,
413 ) -> Option<ClosureRegionRequirements<'gcx>> {
416 &format!("solve_nll_region_constraints({:?})", mir_def_id),
417 || self.solve_inner(infcx, mir, mir_def_id, errors_buffer),
421 fn solve_inner<'gcx>(
423 infcx: &InferCtxt<'_, 'gcx, 'tcx>,
426 errors_buffer: &mut Vec<Diagnostic>,
427 ) -> Option<ClosureRegionRequirements<'gcx>> {
428 self.propagate_constraints(mir);
430 // If this is a closure, we can propagate unsatisfied
431 // `outlives_requirements` to our creator, so create a vector
432 // to store those. Otherwise, we'll pass in `None` to the
433 // functions below, which will trigger them to report errors
435 let mut outlives_requirements = if infcx.tcx.is_closure(mir_def_id) {
441 self.check_type_tests(
445 outlives_requirements.as_mut(),
449 self.check_universal_regions(
453 outlives_requirements.as_mut(),
457 let outlives_requirements = outlives_requirements.unwrap_or(vec![]);
459 if outlives_requirements.is_empty() {
462 let num_external_vids = self.universal_regions.num_global_and_external_regions();
463 Some(ClosureRegionRequirements {
465 outlives_requirements,
470 /// Propagate the region constraints: this will grow the values
471 /// for each region variable until all the constraints are
472 /// satisfied. Note that some values may grow **too** large to be
473 /// feasible, but we check this later.
474 fn propagate_constraints(&mut self, _mir: &Mir<'tcx>) {
475 debug!("propagate_constraints()");
477 debug!("propagate_constraints: constraints={:#?}", {
478 let mut constraints: Vec<_> = self.constraints.iter().collect();
482 .map(|c| (c, self.constraint_sccs.scc(c.sup), self.constraint_sccs.scc(c.sub)))
486 // To propagate constraints, we walk the DAG induced by the
487 // SCC. For each SCC, we visit its successors and compute
488 // their values, then we union all those values to get our
490 let visited = &mut BitSet::new_empty(self.constraint_sccs.num_sccs());
491 for scc_index in self.constraint_sccs.all_sccs() {
492 self.propagate_constraint_sccs_if_new(scc_index, visited);
497 fn propagate_constraint_sccs_if_new(
499 scc_a: ConstraintSccIndex,
500 visited: &mut BitSet<ConstraintSccIndex>,
502 if visited.insert(scc_a) {
503 self.propagate_constraint_sccs_new(scc_a, visited);
507 fn propagate_constraint_sccs_new(
509 scc_a: ConstraintSccIndex,
510 visited: &mut BitSet<ConstraintSccIndex>,
512 let constraint_sccs = self.constraint_sccs.clone();
514 // Walk each SCC `B` such that `A: B`...
515 for &scc_b in constraint_sccs.successors(scc_a) {
517 "propagate_constraint_sccs: scc_a = {:?} scc_b = {:?}",
521 // ...compute the value of `B`...
522 self.propagate_constraint_sccs_if_new(scc_b, visited);
524 // ...and add elements from `B` into `A`. One complication
525 // arises because of universes: If `B` contains something
526 // that `A` cannot name, then `A` can only contain `B` if
527 // it outlives static.
528 if self.universe_compatible(scc_b, scc_a) {
529 // `A` can name everything that is in `B`, so just
531 self.scc_values.add_region(scc_a, scc_b);
533 self.add_incompatible_universe(scc_a);
538 "propagate_constraint_sccs: scc_a = {:?} has value {:?}",
540 self.scc_values.region_value_str(scc_a),
544 /// True if all the elements in the value of `scc_b` are nameable
545 /// in `scc_a`. Used during constraint propagation, and only once
546 /// the value of `scc_b` has been computed.
547 fn universe_compatible(&self, scc_b: ConstraintSccIndex, scc_a: ConstraintSccIndex) -> bool {
548 let universe_a = self.scc_universes[scc_a];
550 // Quick check: if scc_b's declared universe is a subset of
551 // scc_a's declared univese (typically, both are ROOT), then
552 // it cannot contain any problematic universe elements.
553 if universe_a.can_name(self.scc_universes[scc_b]) {
557 // Otherwise, we have to iterate over the universe elements in
558 // B's value, and check whether all of them are nameable
561 .placeholders_contained_in(scc_b)
562 .all(|p| universe_a.can_name(p.universe))
565 /// Extend `scc` so that it can outlive some placeholder region
566 /// from a universe it can't name; at present, the only way for
567 /// this to be true is if `scc` outlives `'static`. This is
568 /// actually stricter than necessary: ideally, we'd support bounds
569 /// like `for<'a: 'b`>` that might then allow us to approximate
570 /// `'a` with `'b` and not `'static`. But it will have to do for
572 fn add_incompatible_universe(&mut self, scc: ConstraintSccIndex) {
573 debug!("add_incompatible_universe(scc={:?})", scc);
575 let fr_static = self.universal_regions.fr_static;
576 self.scc_values.add_all_points(scc);
577 self.scc_values.add_element(scc, fr_static);
580 /// Once regions have been propagated, this method is used to see
581 /// whether the "type tests" produced by typeck were satisfied;
582 /// type tests encode type-outlives relationships like `T:
583 /// 'a`. See `TypeTest` for more details.
584 fn check_type_tests<'gcx>(
586 infcx: &InferCtxt<'_, 'gcx, 'tcx>,
589 mut propagated_outlives_requirements: Option<&mut Vec<ClosureOutlivesRequirement<'gcx>>>,
590 errors_buffer: &mut Vec<Diagnostic>,
594 // Sometimes we register equivalent type-tests that would
595 // result in basically the exact same error being reported to
596 // the user. Avoid that.
597 let mut deduplicate_errors = FxHashSet::default();
599 for type_test in &self.type_tests {
600 debug!("check_type_test: {:?}", type_test);
602 let generic_ty = type_test.generic_kind.to_ty(tcx);
603 if self.eval_verify_bound(
607 type_test.lower_bound,
608 &type_test.verify_bound,
613 if let Some(propagated_outlives_requirements) = &mut propagated_outlives_requirements {
614 if self.try_promote_type_test(
618 propagated_outlives_requirements,
624 // Type-test failed. Report the error.
626 // Try to convert the lower-bound region into something named we can print for the user.
627 let lower_bound_region = self.to_error_region(type_test.lower_bound);
629 // Skip duplicate-ish errors.
630 let type_test_span = type_test.locations.span(mir);
631 let erased_generic_kind = tcx.erase_regions(&type_test.generic_kind);
632 if !deduplicate_errors.insert((
640 "check_type_test: reporting error for erased_generic_kind={:?}, \
641 lower_bound_region={:?}, \
642 type_test.locations={:?}",
643 erased_generic_kind, lower_bound_region, type_test.locations,
647 if let Some(lower_bound_region) = lower_bound_region {
648 let region_scope_tree = &tcx.region_scope_tree(mir_def_id);
650 .construct_generic_bound_failure(
654 type_test.generic_kind,
657 .buffer(errors_buffer);
659 // FIXME. We should handle this case better. It
660 // indicates that we have e.g. some region variable
661 // whose value is like `'a+'b` where `'a` and `'b` are
662 // distinct unrelated univesal regions that are not
663 // known to outlive one another. It'd be nice to have
664 // some examples where this arises to decide how best
665 // to report it; we could probably handle it by
666 // iterating over the universal regions and reporting
667 // an error that multiple bounds are required.
671 &format!("`{}` does not live long enough", type_test.generic_kind,),
673 .buffer(errors_buffer);
678 /// Converts a region inference variable into a `ty::Region` that
679 /// we can use for error reporting. If `r` is universally bound,
680 /// then we use the name that we have on record for it. If `r` is
681 /// existentially bound, then we check its inferred value and try
682 /// to find a good name from that. Returns `None` if we can't find
683 /// one (e.g., this is just some random part of the CFG).
684 pub fn to_error_region(&self, r: RegionVid) -> Option<ty::Region<'tcx>> {
685 self.to_error_region_vid(r).and_then(|r| self.definitions[r].external_name)
688 /// Returns the [RegionVid] corresponding to the region returned by
689 /// `to_error_region`.
690 pub fn to_error_region_vid(&self, r: RegionVid) -> Option<RegionVid> {
691 if self.universal_regions.is_universal_region(r) {
694 let r_scc = self.constraint_sccs.scc(r);
695 let upper_bound = self.universal_upper_bound(r);
696 if self.scc_values.contains(r_scc, upper_bound) {
697 self.to_error_region_vid(upper_bound)
704 /// Invoked when we have some type-test (e.g., `T: 'X`) that we cannot
705 /// prove to be satisfied. If this is a closure, we will attempt to
706 /// "promote" this type-test into our `ClosureRegionRequirements` and
707 /// hence pass it up the creator. To do this, we have to phrase the
708 /// type-test in terms of external free regions, as local free
709 /// regions are not nameable by the closure's creator.
711 /// Promotion works as follows: we first check that the type `T`
712 /// contains only regions that the creator knows about. If this is
713 /// true, then -- as a consequence -- we know that all regions in
714 /// the type `T` are free regions that outlive the closure body. If
715 /// false, then promotion fails.
717 /// Once we've promoted T, we have to "promote" `'X` to some region
718 /// that is "external" to the closure. Generally speaking, a region
719 /// may be the union of some points in the closure body as well as
720 /// various free lifetimes. We can ignore the points in the closure
721 /// body: if the type T can be expressed in terms of external regions,
722 /// we know it outlives the points in the closure body. That
723 /// just leaves the free regions.
725 /// The idea then is to lower the `T: 'X` constraint into multiple
726 /// bounds -- e.g., if `'X` is the union of two free lifetimes,
727 /// `'1` and `'2`, then we would create `T: '1` and `T: '2`.
728 fn try_promote_type_test<'gcx>(
730 infcx: &InferCtxt<'_, 'gcx, 'tcx>,
732 type_test: &TypeTest<'tcx>,
733 propagated_outlives_requirements: &mut Vec<ClosureOutlivesRequirement<'gcx>>,
744 let generic_ty = generic_kind.to_ty(tcx);
745 let subject = match self.try_promote_type_test_subject(infcx, generic_ty) {
747 None => return false,
750 // For each region outlived by lower_bound find a non-local,
751 // universal region (it may be the same region) and add it to
752 // `ClosureOutlivesRequirement`.
753 let r_scc = self.constraint_sccs.scc(*lower_bound);
754 for ur in self.scc_values.universal_regions_outlived_by(r_scc) {
755 // Check whether we can already prove that the "subject" outlives `ur`.
756 // If so, we don't have to propagate this requirement to our caller.
758 // To continue the example from the function, if we are trying to promote
759 // a requirement that `T: 'X`, and we know that `'X = '1 + '2` (i.e., the union
760 // `'1` and `'2`), then in this loop `ur` will be `'1` (and `'2`). So here
761 // we check whether `T: '1` is something we *can* prove. If so, no need
762 // to propagate that requirement.
764 // This is needed because -- particularly in the case
765 // where `ur` is a local bound -- we are sometimes in a
766 // position to prove things that our caller cannot. See
767 // #53570 for an example.
768 if self.eval_verify_bound(tcx, mir, generic_ty, ur, &type_test.verify_bound) {
772 debug!("try_promote_type_test: ur={:?}", ur);
774 let non_local_ub = self.universal_region_relations.non_local_upper_bound(ur);
775 debug!("try_promote_type_test: non_local_ub={:?}", non_local_ub);
777 assert!(self.universal_regions.is_universal_region(non_local_ub));
778 assert!(!self.universal_regions.is_local_free_region(non_local_ub));
780 let requirement = ClosureOutlivesRequirement {
782 outlived_free_region: non_local_ub,
783 blame_span: locations.span(mir),
784 category: ConstraintCategory::Boring,
786 debug!("try_promote_type_test: pushing {:#?}", requirement);
787 propagated_outlives_requirements.push(requirement);
792 /// When we promote a type test `T: 'r`, we have to convert the
793 /// type `T` into something we can store in a query result (so
794 /// something allocated for `'gcx`). This is problematic if `ty`
795 /// contains regions. During the course of NLL region checking, we
796 /// will have replaced all of those regions with fresh inference
797 /// variables. To create a test subject, we want to replace those
798 /// inference variables with some region from the closure
799 /// signature -- this is not always possible, so this is a
800 /// fallible process. Presuming we do find a suitable region, we
801 /// will represent it with a `ReClosureBound`, which is a
802 /// `RegionKind` variant that can be allocated in the gcx.
803 fn try_promote_type_test_subject<'gcx>(
805 infcx: &InferCtxt<'_, 'gcx, 'tcx>,
807 ) -> Option<ClosureOutlivesSubject<'gcx>> {
809 let gcx = tcx.global_tcx();
811 debug!("try_promote_type_test_subject(ty = {:?})", ty);
813 let ty = tcx.fold_regions(&ty, &mut false, |r, _depth| {
814 let region_vid = self.to_region_vid(r);
816 // The challenge if this. We have some region variable `r`
817 // whose value is a set of CFG points and universal
818 // regions. We want to find if that set is *equivalent* to
819 // any of the named regions found in the closure.
821 // To do so, we compute the
822 // `non_local_universal_upper_bound`. This will be a
823 // non-local, universal region that is greater than `r`.
824 // However, it might not be *contained* within `r`, so
825 // then we further check whether this bound is contained
826 // in `r`. If so, we can say that `r` is equivalent to the
829 // Let's work through a few examples. For these, imagine
830 // that we have 3 non-local regions (I'll denote them as
831 // `'static`, `'a`, and `'b`, though of course in the code
832 // they would be represented with indices) where:
837 // First, let's assume that `r` is some existential
838 // variable with an inferred value `{'a, 'static}` (plus
839 // some CFG nodes). In this case, the non-local upper
840 // bound is `'static`, since that outlives `'a`. `'static`
841 // is also a member of `r` and hence we consider `r`
842 // equivalent to `'static` (and replace it with
845 // Now let's consider the inferred value `{'a, 'b}`. This
846 // means `r` is effectively `'a | 'b`. I'm not sure if
847 // this can come about, actually, but assuming it did, we
848 // would get a non-local upper bound of `'static`. Since
849 // `'static` is not contained in `r`, we would fail to
850 // find an equivalent.
851 let upper_bound = self.non_local_universal_upper_bound(region_vid);
852 if self.region_contains(region_vid, upper_bound) {
853 tcx.mk_region(ty::ReClosureBound(upper_bound))
855 // In the case of a failure, use a `ReVar`
856 // result. This will cause the `lift` later on to
861 debug!("try_promote_type_test_subject: folded ty = {:?}", ty);
863 // `lift` will only fail if we failed to promote some region.
864 let ty = gcx.lift(&ty)?;
866 Some(ClosureOutlivesSubject::Ty(ty))
869 /// Given some universal or existential region `r`, finds a
870 /// non-local, universal region `r+` that outlives `r` at entry to (and
871 /// exit from) the closure. In the worst case, this will be
874 /// This is used for two purposes. First, if we are propagated
875 /// some requirement `T: r`, we can use this method to enlarge `r`
876 /// to something we can encode for our creator (which only knows
877 /// about non-local, universal regions). It is also used when
878 /// encoding `T` as part of `try_promote_type_test_subject` (see
879 /// that fn for details).
881 /// This is based on the result `'y` of `universal_upper_bound`,
882 /// except that it converts further takes the non-local upper
883 /// bound of `'y`, so that the final result is non-local.
884 fn non_local_universal_upper_bound(&self, r: RegionVid) -> RegionVid {
886 "non_local_universal_upper_bound(r={:?}={})",
888 self.region_value_str(r)
891 let lub = self.universal_upper_bound(r);
893 // Grow further to get smallest universal region known to
895 let non_local_lub = self.universal_region_relations.non_local_upper_bound(lub);
898 "non_local_universal_upper_bound: non_local_lub={:?}",
905 /// Returns a universally quantified region that outlives the
906 /// value of `r` (`r` may be existentially or universally
909 /// Since `r` is (potentially) an existential region, it has some
910 /// value which may include (a) any number of points in the CFG
911 /// and (b) any number of `end('x)` elements of universally
912 /// quantified regions. To convert this into a single universal
913 /// region we do as follows:
915 /// - Ignore the CFG points in `'r`. All universally quantified regions
916 /// include the CFG anyhow.
917 /// - For each `end('x)` element in `'r`, compute the mutual LUB, yielding
919 fn universal_upper_bound(&self, r: RegionVid) -> RegionVid {
921 "universal_upper_bound(r={:?}={})",
923 self.region_value_str(r)
926 // Find the smallest universal region that contains all other
927 // universal regions within `region`.
928 let mut lub = self.universal_regions.fr_fn_body;
929 let r_scc = self.constraint_sccs.scc(r);
930 for ur in self.scc_values.universal_regions_outlived_by(r_scc) {
931 lub = self.universal_region_relations.postdom_upper_bound(lub, ur);
934 debug!("universal_upper_bound: r={:?} lub={:?}", r, lub);
939 /// Test if `test` is true when applied to `lower_bound` at
940 /// `point`, and returns true or false.
941 fn eval_verify_bound(
943 tcx: TyCtxt<'_, '_, 'tcx>,
945 generic_ty: Ty<'tcx>,
946 lower_bound: RegionVid,
947 verify_bound: &VerifyBound<'tcx>,
950 "eval_verify_bound(lower_bound={:?}, verify_bound={:?})",
951 lower_bound, verify_bound
955 VerifyBound::IfEq(test_ty, verify_bound1) => {
956 self.eval_if_eq(tcx, mir, generic_ty, lower_bound, test_ty, verify_bound1)
959 VerifyBound::OutlivedBy(r) => {
960 let r_vid = self.to_region_vid(r);
961 self.eval_outlives(mir, r_vid, lower_bound)
964 VerifyBound::AnyBound(verify_bounds) => verify_bounds.iter().any(|verify_bound| {
965 self.eval_verify_bound(tcx, mir, generic_ty, lower_bound, verify_bound)
968 VerifyBound::AllBounds(verify_bounds) => verify_bounds.iter().all(|verify_bound| {
969 self.eval_verify_bound(tcx, mir, generic_ty, lower_bound, verify_bound)
976 tcx: TyCtxt<'_, '_, 'tcx>,
978 generic_ty: Ty<'tcx>,
979 lower_bound: RegionVid,
981 verify_bound: &VerifyBound<'tcx>,
983 let generic_ty_normalized = self.normalize_to_scc_representatives(tcx, generic_ty);
984 let test_ty_normalized = self.normalize_to_scc_representatives(tcx, test_ty);
985 if generic_ty_normalized == test_ty_normalized {
986 self.eval_verify_bound(tcx, mir, generic_ty, lower_bound, verify_bound)
992 /// This is a conservative normalization procedure. It takes every
993 /// free region in `value` and replaces it with the
994 /// "representative" of its SCC (see `scc_representatives` field).
995 /// We are guaranteed that if two values normalize to the same
996 /// thing, then they are equal; this is a conservative check in
997 /// that they could still be equal even if they normalize to
998 /// different results. (For example, there might be two regions
999 /// with the same value that are not in the same SCC).
1001 /// NB. This is not an ideal approach and I would like to revisit
1002 /// it. However, it works pretty well in practice. In particular,
1003 /// this is needed to deal with projection outlives bounds like
1005 /// <T as Foo<'0>>::Item: '1
1007 /// In particular, this routine winds up being important when
1008 /// there are bounds like `where <T as Foo<'a>>::Item: 'b` in the
1009 /// environment. In this case, if we can show that `'0 == 'a`,
1010 /// and that `'b: '1`, then we know that the clause is
1011 /// satisfied. In such cases, particularly due to limitations of
1012 /// the trait solver =), we usually wind up with a where-clause like
1013 /// `T: Foo<'a>` in scope, which thus forces `'0 == 'a` to be added as
1014 /// a constraint, and thus ensures that they are in the same SCC.
1016 /// So why can't we do a more correct routine? Well, we could
1017 /// *almost* use the `relate_tys` code, but the way it is
1018 /// currently setup it creates inference variables to deal with
1019 /// higher-ranked things and so forth, and right now the inference
1020 /// context is not permitted to make more inference variables. So
1021 /// we use this kind of hacky solution.
1022 fn normalize_to_scc_representatives<T>(&self, tcx: TyCtxt<'_, '_, 'tcx>, value: T) -> T
1024 T: TypeFoldable<'tcx>,
1026 tcx.fold_regions(&value, &mut false, |r, _db| {
1027 let vid = self.to_region_vid(r);
1028 let scc = self.constraint_sccs.scc(vid);
1029 let repr = self.scc_representatives[scc];
1030 tcx.mk_region(ty::ReVar(repr))
1034 // Evaluate whether `sup_region: sub_region @ point`.
1038 sup_region: RegionVid,
1039 sub_region: RegionVid,
1041 debug!("eval_outlives({:?}: {:?})", sup_region, sub_region);
1044 "eval_outlives: sup_region's value = {:?}",
1045 self.region_value_str(sup_region),
1048 "eval_outlives: sub_region's value = {:?}",
1049 self.region_value_str(sub_region),
1052 let sub_region_scc = self.constraint_sccs.scc(sub_region);
1053 let sup_region_scc = self.constraint_sccs.scc(sup_region);
1055 // Both the `sub_region` and `sup_region` consist of the union
1056 // of some number of universal regions (along with the union
1057 // of various points in the CFG; ignore those points for
1058 // now). Therefore, the sup-region outlives the sub-region if,
1059 // for each universal region R1 in the sub-region, there
1060 // exists some region R2 in the sup-region that outlives R1.
1061 let universal_outlives = self.scc_values
1062 .universal_regions_outlived_by(sub_region_scc)
1065 .universal_regions_outlived_by(sup_region_scc)
1066 .any(|r2| self.universal_region_relations.outlives(r2, r1))
1069 if !universal_outlives {
1073 // Now we have to compare all the points in the sub region and make
1074 // sure they exist in the sup region.
1076 if self.universal_regions.is_universal_region(sup_region) {
1077 // Micro-opt: universal regions contain all points.
1082 .contains_points(sup_region_scc, sub_region_scc)
1085 /// Once regions have been propagated, this method is used to see
1086 /// whether any of the constraints were too strong. In particular,
1087 /// we want to check for a case where a universally quantified
1088 /// region exceeded its bounds. Consider:
1090 /// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
1092 /// In this case, returning `x` requires `&'a u32 <: &'b u32`
1093 /// and hence we establish (transitively) a constraint that
1094 /// `'a: 'b`. The `propagate_constraints` code above will
1095 /// therefore add `end('a)` into the region for `'b` -- but we
1096 /// have no evidence that `'b` outlives `'a`, so we want to report
1099 /// If `propagated_outlives_requirements` is `Some`, then we will
1100 /// push unsatisfied obligations into there. Otherwise, we'll
1101 /// report them as errors.
1102 fn check_universal_regions<'gcx>(
1104 infcx: &InferCtxt<'_, 'gcx, 'tcx>,
1107 mut propagated_outlives_requirements: Option<&mut Vec<ClosureOutlivesRequirement<'gcx>>>,
1108 errors_buffer: &mut Vec<Diagnostic>,
1110 for (fr, fr_definition) in self.definitions.iter_enumerated() {
1111 match fr_definition.origin {
1112 NLLRegionVariableOrigin::FreeRegion => {
1113 // Go through each of the universal regions `fr` and check that
1114 // they did not grow too large, accumulating any requirements
1115 // for our caller into the `outlives_requirements` vector.
1116 self.check_universal_region(
1121 &mut propagated_outlives_requirements,
1126 NLLRegionVariableOrigin::Placeholder(placeholder) => {
1127 self.check_bound_universal_region(infcx, mir, mir_def_id, fr, placeholder);
1130 NLLRegionVariableOrigin::Existential => {
1131 // nothing to check here
1137 /// Check the final value for the free region `fr` to see if it
1138 /// grew too large. In particular, examine what `end(X)` points
1139 /// wound up in `fr`'s final value; for each `end(X)` where `X !=
1140 /// fr`, we want to check that `fr: X`. If not, that's either an
1141 /// error, or something we have to propagate to our creator.
1143 /// Things that are to be propagated are accumulated into the
1144 /// `outlives_requirements` vector.
1145 fn check_universal_region<'gcx>(
1147 infcx: &InferCtxt<'_, 'gcx, 'tcx>,
1150 longer_fr: RegionVid,
1151 propagated_outlives_requirements: &mut Option<&mut Vec<ClosureOutlivesRequirement<'gcx>>>,
1152 errors_buffer: &mut Vec<Diagnostic>,
1154 debug!("check_universal_region(fr={:?})", longer_fr);
1156 let longer_fr_scc = self.constraint_sccs.scc(longer_fr);
1158 // Because this free region must be in the ROOT universe, we
1159 // know it cannot contain any bound universes.
1160 assert!(self.scc_universes[longer_fr_scc] == ty::UniverseIndex::ROOT);
1163 .placeholders_contained_in(longer_fr_scc)
1168 // Find every region `o` such that `fr: o`
1169 // (because `fr` includes `end(o)`).
1170 for shorter_fr in self.scc_values.universal_regions_outlived_by(longer_fr_scc) {
1171 // If it is known that `fr: o`, carry on.
1172 if self.universal_region_relations
1173 .outlives(longer_fr, shorter_fr)
1179 "check_universal_region: fr={:?} does not outlive shorter_fr={:?}",
1180 longer_fr, shorter_fr,
1183 let blame_span_category = self.find_outlives_blame_span(mir, longer_fr, shorter_fr);
1185 if let Some(propagated_outlives_requirements) = propagated_outlives_requirements {
1186 // Shrink `fr` until we find a non-local region (if we do).
1187 // We'll call that `fr-` -- it's ever so slightly smaller than `fr`.
1188 if let Some(fr_minus) = self.universal_region_relations
1189 .non_local_lower_bound(longer_fr)
1191 debug!("check_universal_region: fr_minus={:?}", fr_minus);
1193 // Grow `shorter_fr` until we find a non-local
1194 // region. (We always will.) We'll call that
1195 // `shorter_fr+` -- it's ever so slightly larger than
1197 let shorter_fr_plus = self.universal_region_relations
1198 .non_local_upper_bound(shorter_fr);
1200 "check_universal_region: shorter_fr_plus={:?}",
1204 // Push the constraint `fr-: shorter_fr+`
1205 propagated_outlives_requirements.push(ClosureOutlivesRequirement {
1206 subject: ClosureOutlivesSubject::Region(fr_minus),
1207 outlived_free_region: shorter_fr_plus,
1208 blame_span: blame_span_category.1,
1209 category: blame_span_category.0,
1215 // If we are not in a context where we can propagate
1216 // errors, or we could not shrink `fr` to something
1217 // smaller, then just report an error.
1219 // Note: in this case, we use the unapproximated regions
1220 // to report the error. This gives better error messages
1222 self.report_error(mir, infcx, mir_def_id, longer_fr, shorter_fr, errors_buffer);
1223 return; // continuing to iterate just reports more errors than necessary
1227 fn check_bound_universal_region<'gcx>(
1229 infcx: &InferCtxt<'_, 'gcx, 'tcx>,
1232 longer_fr: RegionVid,
1233 placeholder: ty::PlaceholderRegion,
1236 "check_bound_universal_region(fr={:?}, placeholder={:?})",
1237 longer_fr, placeholder,
1240 let longer_fr_scc = self.constraint_sccs.scc(longer_fr);
1242 "check_bound_universal_region: longer_fr_scc={:?}",
1246 // If we have some bound universal region `'a`, then the only
1247 // elements it can contain is itself -- we don't know anything
1249 let error_element = match {
1251 .elements_contained_in(longer_fr_scc)
1252 .find(|element| match element {
1253 RegionElement::Location(_) => true,
1254 RegionElement::RootUniversalRegion(_) => true,
1255 RegionElement::PlaceholderRegion(placeholder1) => placeholder != *placeholder1,
1261 debug!("check_bound_universal_region: error_element = {:?}", error_element);
1263 // Find the region that introduced this `error_element`.
1264 let error_region = match error_element {
1265 RegionElement::Location(l) => self.find_sub_region_live_at(longer_fr, l),
1266 RegionElement::RootUniversalRegion(r) => r,
1267 RegionElement::PlaceholderRegion(error_placeholder) => self.definitions
1269 .filter_map(|(r, definition)| match definition.origin {
1270 NLLRegionVariableOrigin::Placeholder(p) if p == error_placeholder => Some(r),
1277 // Find the code to blame for the fact that `longer_fr` outlives `error_fr`.
1278 let (_, span) = self.find_outlives_blame_span(mir, longer_fr, error_region);
1280 // Obviously, this error message is far from satisfactory.
1281 // At present, though, it only appears in unit tests --
1282 // the AST-based checker uses a more conservative check,
1283 // so to even see this error, one must pass in a special
1285 let mut diag = infcx
1288 .struct_span_err(span, "higher-ranked subtype error");
1293 impl<'tcx> RegionDefinition<'tcx> {
1294 fn new(universe: ty::UniverseIndex, rv_origin: RegionVariableOrigin) -> Self {
1295 // Create a new region definition. Note that, for free
1296 // regions, the `external_name` field gets updated later in
1297 // `init_universal_regions`.
1299 let origin = match rv_origin {
1300 RegionVariableOrigin::NLL(origin) => origin,
1301 _ => NLLRegionVariableOrigin::Existential,
1307 external_name: None,
1312 pub trait ClosureRegionRequirementsExt<'gcx, 'tcx> {
1313 fn apply_requirements(
1315 tcx: TyCtxt<'_, 'gcx, 'tcx>,
1317 closure_def_id: DefId,
1318 closure_substs: &'tcx ty::subst::Substs<'tcx>,
1319 ) -> Vec<QueryRegionConstraint<'tcx>>;
1321 fn subst_closure_mapping<T>(
1323 tcx: TyCtxt<'_, 'gcx, 'tcx>,
1324 closure_mapping: &IndexVec<RegionVid, ty::Region<'tcx>>,
1328 T: TypeFoldable<'tcx>;
1331 impl<'gcx, 'tcx> ClosureRegionRequirementsExt<'gcx, 'tcx> for ClosureRegionRequirements<'gcx> {
1332 /// Given an instance T of the closure type, this method
1333 /// instantiates the "extra" requirements that we computed for the
1334 /// closure into the inference context. This has the effect of
1335 /// adding new outlives obligations to existing variables.
1337 /// As described on `ClosureRegionRequirements`, the extra
1338 /// requirements are expressed in terms of regionvids that index
1339 /// into the free regions that appear on the closure type. So, to
1340 /// do this, we first copy those regions out from the type T into
1341 /// a vector. Then we can just index into that vector to extract
1342 /// out the corresponding region from T and apply the
1344 fn apply_requirements(
1346 tcx: TyCtxt<'_, 'gcx, 'tcx>,
1348 closure_def_id: DefId,
1349 closure_substs: &'tcx ty::subst::Substs<'tcx>,
1350 ) -> Vec<QueryRegionConstraint<'tcx>> {
1352 "apply_requirements(location={:?}, closure_def_id={:?}, closure_substs={:?})",
1353 location, closure_def_id, closure_substs
1356 // Extract the values of the free regions in `closure_substs`
1357 // into a vector. These are the regions that we will be
1358 // relating to one another.
1359 let closure_mapping = &UniversalRegions::closure_mapping(
1362 self.num_external_vids,
1363 tcx.closure_base_def_id(closure_def_id),
1365 debug!("apply_requirements: closure_mapping={:?}", closure_mapping);
1367 // Create the predicates.
1368 self.outlives_requirements
1370 .map(|outlives_requirement| {
1371 let outlived_region = closure_mapping[outlives_requirement.outlived_free_region];
1373 match outlives_requirement.subject {
1374 ClosureOutlivesSubject::Region(region) => {
1375 let region = closure_mapping[region];
1377 "apply_requirements: region={:?} \
1378 outlived_region={:?} \
1379 outlives_requirement={:?}",
1380 region, outlived_region, outlives_requirement,
1382 ty::Binder::dummy(ty::OutlivesPredicate(region.into(), outlived_region))
1385 ClosureOutlivesSubject::Ty(ty) => {
1386 let ty = self.subst_closure_mapping(tcx, closure_mapping, &ty);
1388 "apply_requirements: ty={:?} \
1389 outlived_region={:?} \
1390 outlives_requirement={:?}",
1391 ty, outlived_region, outlives_requirement,
1393 ty::Binder::dummy(ty::OutlivesPredicate(ty.into(), outlived_region))
1400 fn subst_closure_mapping<T>(
1402 tcx: TyCtxt<'_, 'gcx, 'tcx>,
1403 closure_mapping: &IndexVec<RegionVid, ty::Region<'tcx>>,
1407 T: TypeFoldable<'tcx>,
1409 tcx.fold_regions(value, &mut false, |r, _depth| {
1410 if let ty::ReClosureBound(vid) = r {
1411 closure_mapping[*vid]
1414 "subst_closure_mapping: encountered non-closure bound free region {:?}",