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