rustc_infer/infer/outlives/
obligations.rs

1//! Code that handles "type-outlives" constraints like `T: 'a`. This
2//! is based on the `push_outlives_components` function defined in rustc_infer,
3//! but it adds a bit of heuristics on top, in particular to deal with
4//! associated types and projections.
5//!
6//! When we process a given `T: 'a` obligation, we may produce two
7//! kinds of constraints for the region inferencer:
8//!
9//! - Relationships between inference variables and other regions.
10//!   For example, if we have `&'?0 u32: 'a`, then we would produce
11//!   a constraint that `'a <= '?0`.
12//! - "Verifys" that must be checked after inferencing is done.
13//!   For example, if we know that, for some type parameter `T`,
14//!   `T: 'a + 'b`, and we have a requirement that `T: '?1`,
15//!   then we add a "verify" that checks that `'?1 <= 'a || '?1 <= 'b`.
16//!   - Note the difference with the previous case: here, the region
17//!     variable must be less than something else, so this doesn't
18//!     affect how inference works (it finds the smallest region that
19//!     will do); it's just a post-condition that we have to check.
20//!
21//! **The key point is that once this function is done, we have
22//! reduced all of our "type-region outlives" obligations into relationships
23//! between individual regions.**
24//!
25//! One key input to this function is the set of "region-bound pairs".
26//! These are basically the relationships between type parameters and
27//! regions that are in scope at the point where the outlives
28//! obligation was incurred. **When type-checking a function,
29//! particularly in the face of closures, this is not known until
30//! regionck runs!** This is because some of those bounds come
31//! from things we have yet to infer.
32//!
33//! Consider:
34//!
35//! ```
36//! fn bar<T>(a: T, b: impl for<'a> Fn(&'a T)) {}
37//! fn foo<T>(x: T) {
38//!     bar(x, |y| { /* ... */})
39//!          // ^ closure arg
40//! }
41//! ```
42//!
43//! Here, the type of `y` may involve inference variables and the
44//! like, and it may also contain implied bounds that are needed to
45//! type-check the closure body (e.g., here it informs us that `T`
46//! outlives the late-bound region `'a`).
47//!
48//! Note that by delaying the gathering of implied bounds until all
49//! inference information is known, we may find relationships between
50//! bound regions and other regions in the environment. For example,
51//! when we first check a closure like the one expected as argument
52//! to `foo`:
53//!
54//! ```
55//! fn foo<U, F: for<'a> FnMut(&'a U)>(_f: F) {}
56//! ```
57//!
58//! the type of the closure's first argument would be `&'a ?U`. We
59//! might later infer `?U` to something like `&'b u32`, which would
60//! imply that `'b: 'a`.
61
62use rustc_data_structures::undo_log::UndoLogs;
63use rustc_middle::bug;
64use rustc_middle::mir::ConstraintCategory;
65use rustc_middle::traits::query::NoSolution;
66use rustc_middle::ty::outlives::{Component, push_outlives_components};
67use rustc_middle::ty::{
68    self, GenericArgKind, GenericArgsRef, PolyTypeOutlivesPredicate, Region, Ty, TyCtxt,
69    TypeFoldable as _, TypeVisitableExt,
70};
71use smallvec::smallvec;
72use tracing::{debug, instrument};
73
74use super::env::OutlivesEnvironment;
75use crate::infer::outlives::env::RegionBoundPairs;
76use crate::infer::outlives::verify::VerifyBoundCx;
77use crate::infer::resolve::OpportunisticRegionResolver;
78use crate::infer::snapshot::undo_log::UndoLog;
79use crate::infer::{
80    self, GenericKind, InferCtxt, SubregionOrigin, TypeOutlivesConstraint, VerifyBound,
81};
82use crate::traits::{ObligationCause, ObligationCauseCode};
83
84impl<'tcx> InferCtxt<'tcx> {
85    pub fn register_outlives_constraint(
86        &self,
87        ty::OutlivesPredicate(arg, r2): ty::ArgOutlivesPredicate<'tcx>,
88        cause: &ObligationCause<'tcx>,
89    ) {
90        match arg.kind() {
91            ty::GenericArgKind::Lifetime(r1) => {
92                self.register_region_outlives_constraint(ty::OutlivesPredicate(r1, r2), cause);
93            }
94            ty::GenericArgKind::Type(ty1) => {
95                self.register_type_outlives_constraint(ty1, r2, cause);
96            }
97            ty::GenericArgKind::Const(_) => unreachable!(),
98        }
99    }
100
101    pub fn register_region_outlives_constraint(
102        &self,
103        ty::OutlivesPredicate(r_a, r_b): ty::RegionOutlivesPredicate<'tcx>,
104        cause: &ObligationCause<'tcx>,
105    ) {
106        let origin = SubregionOrigin::from_obligation_cause(cause, || {
107            SubregionOrigin::RelateRegionParamBound(cause.span, None)
108        });
109        // `'a: 'b` ==> `'b <= 'a`
110        self.sub_regions(origin, r_b, r_a);
111    }
112
113    /// Registers that the given region obligation must be resolved
114    /// from within the scope of `body_id`. These regions are enqueued
115    /// and later processed by regionck, when full type information is
116    /// available (see `region_obligations` field for more
117    /// information).
118    #[instrument(level = "debug", skip(self))]
119    pub fn register_type_outlives_constraint_inner(
120        &self,
121        obligation: TypeOutlivesConstraint<'tcx>,
122    ) {
123        let mut inner = self.inner.borrow_mut();
124        inner.undo_log.push(UndoLog::PushTypeOutlivesConstraint);
125        inner.region_obligations.push(obligation);
126    }
127
128    pub fn register_type_outlives_constraint(
129        &self,
130        sup_type: Ty<'tcx>,
131        sub_region: Region<'tcx>,
132        cause: &ObligationCause<'tcx>,
133    ) {
134        // `is_global` means the type has no params, infer, placeholder, or non-`'static`
135        // free regions. If the type has none of these things, then we can skip registering
136        // this outlives obligation since it has no components which affect lifetime
137        // checking in an interesting way.
138        if sup_type.is_global() {
139            return;
140        }
141
142        debug!(?sup_type, ?sub_region, ?cause);
143        let origin = SubregionOrigin::from_obligation_cause(cause, || {
144            SubregionOrigin::RelateParamBound(
145                cause.span,
146                sup_type,
147                match cause.code().peel_derives() {
148                    ObligationCauseCode::WhereClause(_, span)
149                    | ObligationCauseCode::WhereClauseInExpr(_, span, ..)
150                    | ObligationCauseCode::OpaqueTypeBound(span, _)
151                        if !span.is_dummy() =>
152                    {
153                        Some(*span)
154                    }
155                    _ => None,
156                },
157            )
158        });
159
160        self.register_type_outlives_constraint_inner(TypeOutlivesConstraint {
161            sup_type,
162            sub_region,
163            origin,
164        });
165    }
166
167    /// Trait queries just want to pass back type obligations "as is"
168    pub fn take_registered_region_obligations(&self) -> Vec<TypeOutlivesConstraint<'tcx>> {
169        assert!(!self.in_snapshot(), "cannot take registered region obligations in a snapshot");
170        std::mem::take(&mut self.inner.borrow_mut().region_obligations)
171    }
172
173    pub fn register_region_assumption(&self, assumption: ty::ArgOutlivesPredicate<'tcx>) {
174        let mut inner = self.inner.borrow_mut();
175        inner.undo_log.push(UndoLog::PushRegionAssumption);
176        inner.region_assumptions.push(assumption);
177    }
178
179    pub fn take_registered_region_assumptions(&self) -> Vec<ty::ArgOutlivesPredicate<'tcx>> {
180        std::mem::take(&mut self.inner.borrow_mut().region_assumptions)
181    }
182
183    /// Process the region obligations that must be proven (during
184    /// `regionck`) for the given `body_id`, given information about
185    /// the region bounds in scope and so forth.
186    ///
187    /// See the `region_obligations` field of `InferCtxt` for some
188    /// comments about how this function fits into the overall expected
189    /// flow of the inferencer. The key point is that it is
190    /// invoked after all type-inference variables have been bound --
191    /// right before lexical region resolution.
192    #[instrument(level = "debug", skip(self, outlives_env, deeply_normalize_ty))]
193    pub fn process_registered_region_obligations(
194        &self,
195        outlives_env: &OutlivesEnvironment<'tcx>,
196        mut deeply_normalize_ty: impl FnMut(
197            PolyTypeOutlivesPredicate<'tcx>,
198            SubregionOrigin<'tcx>,
199        )
200            -> Result<PolyTypeOutlivesPredicate<'tcx>, NoSolution>,
201    ) -> Result<(), (PolyTypeOutlivesPredicate<'tcx>, SubregionOrigin<'tcx>)> {
202        assert!(!self.in_snapshot(), "cannot process registered region obligations in a snapshot");
203
204        // Must loop since the process of normalizing may itself register region obligations.
205        for iteration in 0.. {
206            let my_region_obligations = self.take_registered_region_obligations();
207            if my_region_obligations.is_empty() {
208                break;
209            }
210
211            if !self.tcx.recursion_limit().value_within_limit(iteration) {
212                // This may actually be reachable. If so, we should convert
213                // this to a proper error/consider whether we should detect
214                // this somewhere else.
215                bug!(
216                    "unexpected overflowed when processing region obligations: {my_region_obligations:#?}"
217                );
218            }
219
220            for TypeOutlivesConstraint { sup_type, sub_region, origin } in my_region_obligations {
221                let outlives = ty::Binder::dummy(ty::OutlivesPredicate(sup_type, sub_region));
222                let ty::OutlivesPredicate(sup_type, sub_region) =
223                    deeply_normalize_ty(outlives, origin.clone())
224                        .map_err(|NoSolution| (outlives, origin.clone()))?
225                        .no_bound_vars()
226                        .expect("started with no bound vars, should end with no bound vars");
227                // `TypeOutlives` is structural, so we should try to opportunistically resolve all
228                // region vids before processing regions, so we have a better chance to match clauses
229                // in our param-env.
230                let (sup_type, sub_region) =
231                    (sup_type, sub_region).fold_with(&mut OpportunisticRegionResolver::new(self));
232
233                if self.tcx.sess.opts.unstable_opts.higher_ranked_assumptions
234                    && outlives_env
235                        .higher_ranked_assumptions()
236                        .contains(&ty::OutlivesPredicate(sup_type.into(), sub_region))
237                {
238                    continue;
239                }
240
241                debug!(?sup_type, ?sub_region, ?origin);
242
243                let outlives = &mut TypeOutlives::new(
244                    self,
245                    self.tcx,
246                    outlives_env.region_bound_pairs(),
247                    None,
248                    outlives_env.known_type_outlives(),
249                );
250                let category = origin.to_constraint_category();
251                outlives.type_must_outlive(origin, sup_type, sub_region, category);
252            }
253        }
254
255        Ok(())
256    }
257}
258
259/// The `TypeOutlives` struct has the job of "lowering" a `T: 'a`
260/// obligation into a series of `'a: 'b` constraints and "verify"s, as
261/// described on the module comment. The final constraints are emitted
262/// via a "delegate" of type `D` -- this is usually the `infcx`, which
263/// accrues them into the `region_obligations` code, but for NLL we
264/// use something else.
265pub struct TypeOutlives<'cx, 'tcx, D>
266where
267    D: TypeOutlivesDelegate<'tcx>,
268{
269    // See the comments on `process_registered_region_obligations` for the meaning
270    // of these fields.
271    delegate: D,
272    tcx: TyCtxt<'tcx>,
273    verify_bound: VerifyBoundCx<'cx, 'tcx>,
274}
275
276pub trait TypeOutlivesDelegate<'tcx> {
277    fn push_sub_region_constraint(
278        &mut self,
279        origin: SubregionOrigin<'tcx>,
280        a: ty::Region<'tcx>,
281        b: ty::Region<'tcx>,
282        constraint_category: ConstraintCategory<'tcx>,
283    );
284
285    fn push_verify(
286        &mut self,
287        origin: SubregionOrigin<'tcx>,
288        kind: GenericKind<'tcx>,
289        a: ty::Region<'tcx>,
290        bound: VerifyBound<'tcx>,
291    );
292}
293
294impl<'cx, 'tcx, D> TypeOutlives<'cx, 'tcx, D>
295where
296    D: TypeOutlivesDelegate<'tcx>,
297{
298    pub fn new(
299        delegate: D,
300        tcx: TyCtxt<'tcx>,
301        region_bound_pairs: &'cx RegionBoundPairs<'tcx>,
302        implicit_region_bound: Option<ty::Region<'tcx>>,
303        caller_bounds: &'cx [ty::PolyTypeOutlivesPredicate<'tcx>],
304    ) -> Self {
305        Self {
306            delegate,
307            tcx,
308            verify_bound: VerifyBoundCx::new(
309                tcx,
310                region_bound_pairs,
311                implicit_region_bound,
312                caller_bounds,
313            ),
314        }
315    }
316
317    /// Adds constraints to inference such that `T: 'a` holds (or
318    /// reports an error if it cannot).
319    ///
320    /// # Parameters
321    ///
322    /// - `origin`, the reason we need this constraint
323    /// - `ty`, the type `T`
324    /// - `region`, the region `'a`
325    #[instrument(level = "debug", skip(self))]
326    pub fn type_must_outlive(
327        &mut self,
328        origin: infer::SubregionOrigin<'tcx>,
329        ty: Ty<'tcx>,
330        region: ty::Region<'tcx>,
331        category: ConstraintCategory<'tcx>,
332    ) {
333        assert!(!ty.has_escaping_bound_vars());
334
335        let mut components = smallvec![];
336        push_outlives_components(self.tcx, ty, &mut components);
337        self.components_must_outlive(origin, &components, region, category);
338    }
339
340    fn components_must_outlive(
341        &mut self,
342        origin: infer::SubregionOrigin<'tcx>,
343        components: &[Component<TyCtxt<'tcx>>],
344        region: ty::Region<'tcx>,
345        category: ConstraintCategory<'tcx>,
346    ) {
347        for component in components.iter() {
348            let origin = origin.clone();
349            match component {
350                Component::Region(region1) => {
351                    self.delegate.push_sub_region_constraint(origin, region, *region1, category);
352                }
353                Component::Param(param_ty) => {
354                    self.param_ty_must_outlive(origin, region, *param_ty);
355                }
356                Component::Placeholder(placeholder_ty) => {
357                    self.placeholder_ty_must_outlive(origin, region, *placeholder_ty);
358                }
359                Component::Alias(alias_ty) => self.alias_ty_must_outlive(origin, region, *alias_ty),
360                Component::EscapingAlias(subcomponents) => {
361                    self.components_must_outlive(origin, subcomponents, region, category);
362                }
363                Component::UnresolvedInferenceVariable(v) => {
364                    // Ignore this, we presume it will yield an error later,
365                    // since if a type variable is not resolved by this point
366                    // it never will be.
367                    self.tcx.dcx().span_delayed_bug(
368                        origin.span(),
369                        format!("unresolved inference variable in outlives: {v:?}"),
370                    );
371                }
372            }
373        }
374    }
375
376    #[instrument(level = "debug", skip(self))]
377    fn param_ty_must_outlive(
378        &mut self,
379        origin: infer::SubregionOrigin<'tcx>,
380        region: ty::Region<'tcx>,
381        param_ty: ty::ParamTy,
382    ) {
383        let verify_bound = self.verify_bound.param_or_placeholder_bound(param_ty.to_ty(self.tcx));
384        self.delegate.push_verify(origin, GenericKind::Param(param_ty), region, verify_bound);
385    }
386
387    #[instrument(level = "debug", skip(self))]
388    fn placeholder_ty_must_outlive(
389        &mut self,
390        origin: infer::SubregionOrigin<'tcx>,
391        region: ty::Region<'tcx>,
392        placeholder_ty: ty::PlaceholderType,
393    ) {
394        let verify_bound = self
395            .verify_bound
396            .param_or_placeholder_bound(Ty::new_placeholder(self.tcx, placeholder_ty));
397        self.delegate.push_verify(
398            origin,
399            GenericKind::Placeholder(placeholder_ty),
400            region,
401            verify_bound,
402        );
403    }
404
405    #[instrument(level = "debug", skip(self))]
406    fn alias_ty_must_outlive(
407        &mut self,
408        origin: infer::SubregionOrigin<'tcx>,
409        region: ty::Region<'tcx>,
410        alias_ty: ty::AliasTy<'tcx>,
411    ) {
412        // An optimization for a common case with opaque types.
413        if alias_ty.args.is_empty() {
414            return;
415        }
416
417        if alias_ty.has_non_region_infer() {
418            self.tcx
419                .dcx()
420                .span_delayed_bug(origin.span(), "an alias has infers during region solving");
421            return;
422        }
423
424        // This case is thorny for inference. The fundamental problem is
425        // that there are many cases where we have choice, and inference
426        // doesn't like choice (the current region inference in
427        // particular). :) First off, we have to choose between using the
428        // OutlivesProjectionEnv, OutlivesProjectionTraitDef, and
429        // OutlivesProjectionComponent rules, any one of which is
430        // sufficient. If there are no inference variables involved, it's
431        // not hard to pick the right rule, but if there are, we're in a
432        // bit of a catch 22: if we picked which rule we were going to
433        // use, we could add constraints to the region inference graph
434        // that make it apply, but if we don't add those constraints, the
435        // rule might not apply (but another rule might). For now, we err
436        // on the side of adding too few edges into the graph.
437
438        // Compute the bounds we can derive from the trait definition.
439        // These are guaranteed to apply, no matter the inference
440        // results.
441        let trait_bounds: Vec<_> =
442            self.verify_bound.declared_bounds_from_definition(alias_ty).collect();
443
444        debug!(?trait_bounds);
445
446        // Compute the bounds we can derive from the environment. This
447        // is an "approximate" match -- in some cases, these bounds
448        // may not apply.
449        let approx_env_bounds = self.verify_bound.approx_declared_bounds_from_env(alias_ty);
450        debug!(?approx_env_bounds);
451
452        // If declared bounds list is empty, the only applicable rule is
453        // OutlivesProjectionComponent. If there are inference variables,
454        // then, we can break down the outlives into more primitive
455        // components without adding unnecessary edges.
456        //
457        // If there are *no* inference variables, however, we COULD do
458        // this, but we choose not to, because the error messages are less
459        // good. For example, a requirement like `T::Item: 'r` would be
460        // translated to a requirement that `T: 'r`; when this is reported
461        // to the user, it will thus say "T: 'r must hold so that T::Item:
462        // 'r holds". But that makes it sound like the only way to fix
463        // the problem is to add `T: 'r`, which isn't true. So, if there are no
464        // inference variables, we use a verify constraint instead of adding
465        // edges, which winds up enforcing the same condition.
466        let kind = alias_ty.kind(self.tcx);
467        if approx_env_bounds.is_empty()
468            && trait_bounds.is_empty()
469            && (alias_ty.has_infer_regions() || kind == ty::Opaque)
470        {
471            debug!("no declared bounds");
472            let opt_variances = self.tcx.opt_alias_variances(kind, alias_ty.def_id);
473            self.args_must_outlive(alias_ty.args, origin, region, opt_variances);
474            return;
475        }
476
477        // If we found a unique bound `'b` from the trait, and we
478        // found nothing else from the environment, then the best
479        // action is to require that `'b: 'r`, so do that.
480        //
481        // This is best no matter what rule we use:
482        //
483        // - OutlivesProjectionEnv: these would translate to the requirement that `'b:'r`
484        // - OutlivesProjectionTraitDef: these would translate to the requirement that `'b:'r`
485        // - OutlivesProjectionComponent: this would require `'b:'r`
486        //   in addition to other conditions
487        if !trait_bounds.is_empty()
488            && trait_bounds[1..]
489                .iter()
490                .map(|r| Some(*r))
491                .chain(
492                    // NB: The environment may contain `for<'a> T: 'a` style bounds.
493                    // In that case, we don't know if they are equal to the trait bound
494                    // or not (since we don't *know* whether the environment bound even applies),
495                    // so just map to `None` here if there are bound vars, ensuring that
496                    // the call to `all` will fail below.
497                    approx_env_bounds.iter().map(|b| b.map_bound(|b| b.1).no_bound_vars()),
498                )
499                .all(|b| b == Some(trait_bounds[0]))
500        {
501            let unique_bound = trait_bounds[0];
502            debug!(?unique_bound);
503            debug!("unique declared bound appears in trait ref");
504            let category = origin.to_constraint_category();
505            self.delegate.push_sub_region_constraint(origin, region, unique_bound, category);
506            return;
507        }
508
509        // Fallback to verifying after the fact that there exists a
510        // declared bound, or that all the components appearing in the
511        // projection outlive; in some cases, this may add insufficient
512        // edges into the inference graph, leading to inference failures
513        // even though a satisfactory solution exists.
514        let verify_bound = self.verify_bound.alias_bound(alias_ty);
515        debug!("alias_must_outlive: pushing {:?}", verify_bound);
516        self.delegate.push_verify(origin, GenericKind::Alias(alias_ty), region, verify_bound);
517    }
518
519    #[instrument(level = "debug", skip(self))]
520    fn args_must_outlive(
521        &mut self,
522        args: GenericArgsRef<'tcx>,
523        origin: infer::SubregionOrigin<'tcx>,
524        region: ty::Region<'tcx>,
525        opt_variances: Option<&[ty::Variance]>,
526    ) {
527        let constraint = origin.to_constraint_category();
528        for (index, arg) in args.iter().enumerate() {
529            match arg.kind() {
530                GenericArgKind::Lifetime(lt) => {
531                    let variance = if let Some(variances) = opt_variances {
532                        variances[index]
533                    } else {
534                        ty::Invariant
535                    };
536                    if variance == ty::Invariant {
537                        self.delegate.push_sub_region_constraint(
538                            origin.clone(),
539                            region,
540                            lt,
541                            constraint,
542                        );
543                    }
544                }
545                GenericArgKind::Type(ty) => {
546                    self.type_must_outlive(origin.clone(), ty, region, constraint);
547                }
548                GenericArgKind::Const(_) => {
549                    // Const parameters don't impose constraints.
550                }
551            }
552        }
553    }
554}
555
556impl<'cx, 'tcx> TypeOutlivesDelegate<'tcx> for &'cx InferCtxt<'tcx> {
557    fn push_sub_region_constraint(
558        &mut self,
559        origin: SubregionOrigin<'tcx>,
560        a: ty::Region<'tcx>,
561        b: ty::Region<'tcx>,
562        _constraint_category: ConstraintCategory<'tcx>,
563    ) {
564        self.sub_regions(origin, a, b)
565    }
566
567    fn push_verify(
568        &mut self,
569        origin: SubregionOrigin<'tcx>,
570        kind: GenericKind<'tcx>,
571        a: ty::Region<'tcx>,
572        bound: VerifyBound<'tcx>,
573    ) {
574        self.verify_generic_bound(origin, kind, a, bound)
575    }
576}