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::{
67 self, GenericArgKind, GenericArgsRef, PolyTypeOutlivesPredicate, Region, Ty, TyCtxt,
68 TypeFoldable as _, TypeVisitableExt,
69};
70use rustc_type_ir::outlives::{Component, push_outlives_components};
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::{self, GenericKind, InferCtxt, RegionObligation, SubregionOrigin, VerifyBound};
80use crate::traits::{ObligationCause, ObligationCauseCode};
81
82impl<'tcx> InferCtxt<'tcx> {
83 /// Registers that the given region obligation must be resolved
84 /// from within the scope of `body_id`. These regions are enqueued
85 /// and later processed by regionck, when full type information is
86 /// available (see `region_obligations` field for more
87 /// information).
88 #[instrument(level = "debug", skip(self))]
89 pub fn register_region_obligation(&self, obligation: RegionObligation<'tcx>) {
90 let mut inner = self.inner.borrow_mut();
91 inner.undo_log.push(UndoLog::PushRegionObligation);
92 inner.region_obligations.push(obligation);
93 }
94
95 pub fn register_region_obligation_with_cause(
96 &self,
97 sup_type: Ty<'tcx>,
98 sub_region: Region<'tcx>,
99 cause: &ObligationCause<'tcx>,
100 ) {
101 debug!(?sup_type, ?sub_region, ?cause);
102 let origin = SubregionOrigin::from_obligation_cause(cause, || {
103 infer::RelateParamBound(
104 cause.span,
105 sup_type,
106 match cause.code().peel_derives() {
107 ObligationCauseCode::WhereClause(_, span)
108 | ObligationCauseCode::WhereClauseInExpr(_, span, ..)
109 | ObligationCauseCode::OpaqueTypeBound(span, _)
110 if !span.is_dummy() =>
111 {
112 Some(*span)
113 }
114 _ => None,
115 },
116 )
117 });
118
119 self.register_region_obligation(RegionObligation { sup_type, sub_region, origin });
120 }
121
122 /// Trait queries just want to pass back type obligations "as is"
123 pub fn take_registered_region_obligations(&self) -> Vec<RegionObligation<'tcx>> {
124 std::mem::take(&mut self.inner.borrow_mut().region_obligations)
125 }
126
127 /// Process the region obligations that must be proven (during
128 /// `regionck`) for the given `body_id`, given information about
129 /// the region bounds in scope and so forth.
130 ///
131 /// See the `region_obligations` field of `InferCtxt` for some
132 /// comments about how this function fits into the overall expected
133 /// flow of the inferencer. The key point is that it is
134 /// invoked after all type-inference variables have been bound --
135 /// right before lexical region resolution.
136 #[instrument(level = "debug", skip(self, outlives_env, deeply_normalize_ty))]
137 pub fn process_registered_region_obligations(
138 &self,
139 outlives_env: &OutlivesEnvironment<'tcx>,
140 mut deeply_normalize_ty: impl FnMut(
141 PolyTypeOutlivesPredicate<'tcx>,
142 SubregionOrigin<'tcx>,
143 )
144 -> Result<PolyTypeOutlivesPredicate<'tcx>, NoSolution>,
145 ) -> Result<(), (PolyTypeOutlivesPredicate<'tcx>, SubregionOrigin<'tcx>)> {
146 assert!(!self.in_snapshot(), "cannot process registered region obligations in a snapshot");
147
148 // Must loop since the process of normalizing may itself register region obligations.
149 for iteration in 0.. {
150 let my_region_obligations = self.take_registered_region_obligations();
151 if my_region_obligations.is_empty() {
152 break;
153 }
154
155 if !self.tcx.recursion_limit().value_within_limit(iteration) {
156 bug!(
157 "FIXME(-Znext-solver): Overflowed when processing region obligations: {my_region_obligations:#?}"
158 );
159 }
160
161 for RegionObligation { sup_type, sub_region, origin } in my_region_obligations {
162 let outlives = ty::Binder::dummy(ty::OutlivesPredicate(sup_type, sub_region));
163 let ty::OutlivesPredicate(sup_type, sub_region) =
164 deeply_normalize_ty(outlives, origin.clone())
165 .map_err(|NoSolution| (outlives, origin.clone()))?
166 .no_bound_vars()
167 .expect("started with no bound vars, should end with no bound vars");
168 // `TypeOutlives` is structural, so we should try to opportunistically resolve all
169 // region vids before processing regions, so we have a better chance to match clauses
170 // in our param-env.
171 let (sup_type, sub_region) =
172 (sup_type, sub_region).fold_with(&mut OpportunisticRegionResolver::new(self));
173
174 debug!(?sup_type, ?sub_region, ?origin);
175
176 let outlives = &mut TypeOutlives::new(
177 self,
178 self.tcx,
179 outlives_env.region_bound_pairs(),
180 None,
181 outlives_env.known_type_outlives(),
182 );
183 let category = origin.to_constraint_category();
184 outlives.type_must_outlive(origin, sup_type, sub_region, category);
185 }
186 }
187
188 Ok(())
189 }
190}
191
192/// The `TypeOutlives` struct has the job of "lowering" a `T: 'a`
193/// obligation into a series of `'a: 'b` constraints and "verify"s, as
194/// described on the module comment. The final constraints are emitted
195/// via a "delegate" of type `D` -- this is usually the `infcx`, which
196/// accrues them into the `region_obligations` code, but for NLL we
197/// use something else.
198pub struct TypeOutlives<'cx, 'tcx, D>
199where
200 D: TypeOutlivesDelegate<'tcx>,
201{
202 // See the comments on `process_registered_region_obligations` for the meaning
203 // of these fields.
204 delegate: D,
205 tcx: TyCtxt<'tcx>,
206 verify_bound: VerifyBoundCx<'cx, 'tcx>,
207}
208
209pub trait TypeOutlivesDelegate<'tcx> {
210 fn push_sub_region_constraint(
211 &mut self,
212 origin: SubregionOrigin<'tcx>,
213 a: ty::Region<'tcx>,
214 b: ty::Region<'tcx>,
215 constraint_category: ConstraintCategory<'tcx>,
216 );
217
218 fn push_verify(
219 &mut self,
220 origin: SubregionOrigin<'tcx>,
221 kind: GenericKind<'tcx>,
222 a: ty::Region<'tcx>,
223 bound: VerifyBound<'tcx>,
224 );
225}
226
227impl<'cx, 'tcx, D> TypeOutlives<'cx, 'tcx, D>
228where
229 D: TypeOutlivesDelegate<'tcx>,
230{
231 pub fn new(
232 delegate: D,
233 tcx: TyCtxt<'tcx>,
234 region_bound_pairs: &'cx RegionBoundPairs<'tcx>,
235 implicit_region_bound: Option<ty::Region<'tcx>>,
236 caller_bounds: &'cx [ty::PolyTypeOutlivesPredicate<'tcx>],
237 ) -> Self {
238 Self {
239 delegate,
240 tcx,
241 verify_bound: VerifyBoundCx::new(
242 tcx,
243 region_bound_pairs,
244 implicit_region_bound,
245 caller_bounds,
246 ),
247 }
248 }
249
250 /// Adds constraints to inference such that `T: 'a` holds (or
251 /// reports an error if it cannot).
252 ///
253 /// # Parameters
254 ///
255 /// - `origin`, the reason we need this constraint
256 /// - `ty`, the type `T`
257 /// - `region`, the region `'a`
258 #[instrument(level = "debug", skip(self))]
259 pub fn type_must_outlive(
260 &mut self,
261 origin: infer::SubregionOrigin<'tcx>,
262 ty: Ty<'tcx>,
263 region: ty::Region<'tcx>,
264 category: ConstraintCategory<'tcx>,
265 ) {
266 assert!(!ty.has_escaping_bound_vars());
267
268 let mut components = smallvec![];
269 push_outlives_components(self.tcx, ty, &mut components);
270 self.components_must_outlive(origin, &components, region, category);
271 }
272
273 fn components_must_outlive(
274 &mut self,
275 origin: infer::SubregionOrigin<'tcx>,
276 components: &[Component<TyCtxt<'tcx>>],
277 region: ty::Region<'tcx>,
278 category: ConstraintCategory<'tcx>,
279 ) {
280 for component in components.iter() {
281 let origin = origin.clone();
282 match component {
283 Component::Region(region1) => {
284 self.delegate.push_sub_region_constraint(origin, region, *region1, category);
285 }
286 Component::Param(param_ty) => {
287 self.param_ty_must_outlive(origin, region, *param_ty);
288 }
289 Component::Placeholder(placeholder_ty) => {
290 self.placeholder_ty_must_outlive(origin, region, *placeholder_ty);
291 }
292 Component::Alias(alias_ty) => self.alias_ty_must_outlive(origin, region, *alias_ty),
293 Component::EscapingAlias(subcomponents) => {
294 self.components_must_outlive(origin, subcomponents, region, category);
295 }
296 Component::UnresolvedInferenceVariable(v) => {
297 // Ignore this, we presume it will yield an error later,
298 // since if a type variable is not resolved by this point
299 // it never will be.
300 self.tcx.dcx().span_delayed_bug(
301 origin.span(),
302 format!("unresolved inference variable in outlives: {v:?}"),
303 );
304 }
305 }
306 }
307 }
308
309 #[instrument(level = "debug", skip(self))]
310 fn param_ty_must_outlive(
311 &mut self,
312 origin: infer::SubregionOrigin<'tcx>,
313 region: ty::Region<'tcx>,
314 param_ty: ty::ParamTy,
315 ) {
316 let verify_bound = self.verify_bound.param_or_placeholder_bound(param_ty.to_ty(self.tcx));
317 self.delegate.push_verify(origin, GenericKind::Param(param_ty), region, verify_bound);
318 }
319
320 #[instrument(level = "debug", skip(self))]
321 fn placeholder_ty_must_outlive(
322 &mut self,
323 origin: infer::SubregionOrigin<'tcx>,
324 region: ty::Region<'tcx>,
325 placeholder_ty: ty::PlaceholderType,
326 ) {
327 let verify_bound = self
328 .verify_bound
329 .param_or_placeholder_bound(Ty::new_placeholder(self.tcx, placeholder_ty));
330 self.delegate.push_verify(
331 origin,
332 GenericKind::Placeholder(placeholder_ty),
333 region,
334 verify_bound,
335 );
336 }
337
338 #[instrument(level = "debug", skip(self))]
339 fn alias_ty_must_outlive(
340 &mut self,
341 origin: infer::SubregionOrigin<'tcx>,
342 region: ty::Region<'tcx>,
343 alias_ty: ty::AliasTy<'tcx>,
344 ) {
345 // An optimization for a common case with opaque types.
346 if alias_ty.args.is_empty() {
347 return;
348 }
349
350 if alias_ty.has_non_region_infer() {
351 self.tcx
352 .dcx()
353 .span_delayed_bug(origin.span(), "an alias has infers during region solving");
354 return;
355 }
356
357 // This case is thorny for inference. The fundamental problem is
358 // that there are many cases where we have choice, and inference
359 // doesn't like choice (the current region inference in
360 // particular). :) First off, we have to choose between using the
361 // OutlivesProjectionEnv, OutlivesProjectionTraitDef, and
362 // OutlivesProjectionComponent rules, any one of which is
363 // sufficient. If there are no inference variables involved, it's
364 // not hard to pick the right rule, but if there are, we're in a
365 // bit of a catch 22: if we picked which rule we were going to
366 // use, we could add constraints to the region inference graph
367 // that make it apply, but if we don't add those constraints, the
368 // rule might not apply (but another rule might). For now, we err
369 // on the side of adding too few edges into the graph.
370
371 // Compute the bounds we can derive from the trait definition.
372 // These are guaranteed to apply, no matter the inference
373 // results.
374 let trait_bounds: Vec<_> =
375 self.verify_bound.declared_bounds_from_definition(alias_ty).collect();
376
377 debug!(?trait_bounds);
378
379 // Compute the bounds we can derive from the environment. This
380 // is an "approximate" match -- in some cases, these bounds
381 // may not apply.
382 let approx_env_bounds = self.verify_bound.approx_declared_bounds_from_env(alias_ty);
383 debug!(?approx_env_bounds);
384
385 // If declared bounds list is empty, the only applicable rule is
386 // OutlivesProjectionComponent. If there are inference variables,
387 // then, we can break down the outlives into more primitive
388 // components without adding unnecessary edges.
389 //
390 // If there are *no* inference variables, however, we COULD do
391 // this, but we choose not to, because the error messages are less
392 // good. For example, a requirement like `T::Item: 'r` would be
393 // translated to a requirement that `T: 'r`; when this is reported
394 // to the user, it will thus say "T: 'r must hold so that T::Item:
395 // 'r holds". But that makes it sound like the only way to fix
396 // the problem is to add `T: 'r`, which isn't true. So, if there are no
397 // inference variables, we use a verify constraint instead of adding
398 // edges, which winds up enforcing the same condition.
399 let kind = alias_ty.kind(self.tcx);
400 if approx_env_bounds.is_empty()
401 && trait_bounds.is_empty()
402 && (alias_ty.has_infer_regions() || kind == ty::Opaque)
403 {
404 debug!("no declared bounds");
405 let opt_variances = self.tcx.opt_alias_variances(kind, alias_ty.def_id);
406 self.args_must_outlive(alias_ty.args, origin, region, opt_variances);
407 return;
408 }
409
410 // If we found a unique bound `'b` from the trait, and we
411 // found nothing else from the environment, then the best
412 // action is to require that `'b: 'r`, so do that.
413 //
414 // This is best no matter what rule we use:
415 //
416 // - OutlivesProjectionEnv: these would translate to the requirement that `'b:'r`
417 // - OutlivesProjectionTraitDef: these would translate to the requirement that `'b:'r`
418 // - OutlivesProjectionComponent: this would require `'b:'r`
419 // in addition to other conditions
420 if !trait_bounds.is_empty()
421 && trait_bounds[1..]
422 .iter()
423 .map(|r| Some(*r))
424 .chain(
425 // NB: The environment may contain `for<'a> T: 'a` style bounds.
426 // In that case, we don't know if they are equal to the trait bound
427 // or not (since we don't *know* whether the environment bound even applies),
428 // so just map to `None` here if there are bound vars, ensuring that
429 // the call to `all` will fail below.
430 approx_env_bounds.iter().map(|b| b.map_bound(|b| b.1).no_bound_vars()),
431 )
432 .all(|b| b == Some(trait_bounds[0]))
433 {
434 let unique_bound = trait_bounds[0];
435 debug!(?unique_bound);
436 debug!("unique declared bound appears in trait ref");
437 let category = origin.to_constraint_category();
438 self.delegate.push_sub_region_constraint(origin, region, unique_bound, category);
439 return;
440 }
441
442 // Fallback to verifying after the fact that there exists a
443 // declared bound, or that all the components appearing in the
444 // projection outlive; in some cases, this may add insufficient
445 // edges into the inference graph, leading to inference failures
446 // even though a satisfactory solution exists.
447 let verify_bound = self.verify_bound.alias_bound(alias_ty);
448 debug!("alias_must_outlive: pushing {:?}", verify_bound);
449 self.delegate.push_verify(origin, GenericKind::Alias(alias_ty), region, verify_bound);
450 }
451
452 #[instrument(level = "debug", skip(self))]
453 fn args_must_outlive(
454 &mut self,
455 args: GenericArgsRef<'tcx>,
456 origin: infer::SubregionOrigin<'tcx>,
457 region: ty::Region<'tcx>,
458 opt_variances: Option<&[ty::Variance]>,
459 ) {
460 let constraint = origin.to_constraint_category();
461 for (index, k) in args.iter().enumerate() {
462 match k.unpack() {
463 GenericArgKind::Lifetime(lt) => {
464 let variance = if let Some(variances) = opt_variances {
465 variances[index]
466 } else {
467 ty::Invariant
468 };
469 if variance == ty::Invariant {
470 self.delegate.push_sub_region_constraint(
471 origin.clone(),
472 region,
473 lt,
474 constraint,
475 );
476 }
477 }
478 GenericArgKind::Type(ty) => {
479 self.type_must_outlive(origin.clone(), ty, region, constraint);
480 }
481 GenericArgKind::Const(_) => {
482 // Const parameters don't impose constraints.
483 }
484 }
485 }
486 }
487}
488
489impl<'cx, 'tcx> TypeOutlivesDelegate<'tcx> for &'cx InferCtxt<'tcx> {
490 fn push_sub_region_constraint(
491 &mut self,
492 origin: SubregionOrigin<'tcx>,
493 a: ty::Region<'tcx>,
494 b: ty::Region<'tcx>,
495 _constraint_category: ConstraintCategory<'tcx>,
496 ) {
497 self.sub_regions(origin, a, b)
498 }
499
500 fn push_verify(
501 &mut self,
502 origin: SubregionOrigin<'tcx>,
503 kind: GenericKind<'tcx>,
504 a: ty::Region<'tcx>,
505 bound: VerifyBound<'tcx>,
506 ) {
507 self.verify_generic_bound(origin, kind, a, bound)
508 }
509}