]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_trait_selection/src/traits/outlives_bounds.rs
New upstream version 1.70.0+dfsg1
[rustc.git] / compiler / rustc_trait_selection / src / traits / outlives_bounds.rs
CommitLineData
f2b60f7d
FG
1use crate::infer::InferCtxt;
2use crate::traits::query::type_op::{self, TypeOp, TypeOpOutput};
3use crate::traits::query::NoSolution;
353b0b11 4use crate::traits::{ObligationCause, ObligationCtxt};
487cf647 5use rustc_data_structures::fx::FxIndexSet;
353b0b11
FG
6use rustc_infer::infer::resolve::OpportunisticRegionResolver;
7use rustc_middle::ty::{self, ParamEnv, Ty, TypeFolder, TypeVisitableExt};
9ffffee4 8use rustc_span::def_id::LocalDefId;
8faf50e0 9
ba9703b0 10pub use rustc_middle::traits::query::OutlivesBound;
8faf50e0 11
f2b60f7d
FG
12type Bounds<'a, 'tcx: 'a> = impl Iterator<Item = OutlivesBound<'tcx>> + 'a;
13pub trait InferCtxtExt<'a, 'tcx> {
ba9703b0
XL
14 fn implied_outlives_bounds(
15 &self,
16 param_env: ty::ParamEnv<'tcx>,
9ffffee4 17 body_id: LocalDefId,
ba9703b0 18 ty: Ty<'tcx>,
ba9703b0 19 ) -> Vec<OutlivesBound<'tcx>>;
f2b60f7d
FG
20
21 fn implied_bounds_tys(
22 &'a self,
23 param_env: ty::ParamEnv<'tcx>,
9ffffee4 24 body_id: LocalDefId,
487cf647 25 tys: FxIndexSet<Ty<'tcx>>,
f2b60f7d 26 ) -> Bounds<'a, 'tcx>;
ba9703b0
XL
27}
28
2b03887a 29impl<'a, 'tcx: 'a> InferCtxtExt<'a, 'tcx> for InferCtxt<'tcx> {
8faf50e0 30 /// Implied bounds are region relationships that we deduce
9fa01778 31 /// automatically. The idea is that (e.g.) a caller must check that a
8faf50e0
XL
32 /// function's argument types are well-formed immediately before
33 /// calling that fn, and hence the *callee* can assume that its
34 /// argument types are well-formed. This may imply certain relationships
35 /// between generic parameters. For example:
04454e1e 36 /// ```
9c376795 37 /// fn foo<T>(x: &T) {}
04454e1e 38 /// ```
8faf50e0
XL
39 /// can only be called with a `'a` and `T` such that `&'a T` is WF.
40 /// For `&'a T` to be WF, `T: 'a` must hold. So we can assume `T: 'a`.
41 ///
42 /// # Parameters
43 ///
44 /// - `param_env`, the where-clauses in scope
45 /// - `body_id`, the body-id to use when normalizing assoc types.
46 /// Note that this may cause outlives obligations to be injected
47 /// into the inference context with this body-id.
48 /// - `ty`, the type that we are supposed to assume is WF.
f2b60f7d 49 #[instrument(level = "debug", skip(self, param_env, body_id), ret)]
ba9703b0 50 fn implied_outlives_bounds(
8faf50e0
XL
51 &self,
52 param_env: ty::ParamEnv<'tcx>,
9ffffee4 53 body_id: LocalDefId,
8faf50e0 54 ty: Ty<'tcx>,
8faf50e0 55 ) -> Vec<OutlivesBound<'tcx>> {
353b0b11
FG
56 let ty = self.resolve_vars_if_possible(ty);
57 let ty = OpportunisticRegionResolver::new(self).fold_ty(ty);
58 assert!(!ty.needs_infer());
59
9ffffee4 60 let span = self.tcx.def_span(body_id);
923072b8
FG
61 let result = param_env
62 .and(type_op::implied_outlives_bounds::ImpliedOutlivesBounds { ty })
63 .fully_perform(self);
64 let result = match result {
8faf50e0
XL
65 Ok(r) => r,
66 Err(NoSolution) => {
67 self.tcx.sess.delay_span_bug(
68 span,
dfeec247 69 "implied_outlives_bounds failed to solve all obligations",
8faf50e0
XL
70 );
71 return vec![];
72 }
73 };
8faf50e0 74
923072b8 75 let TypeOpOutput { output, constraints, .. } = result;
8faf50e0 76
923072b8 77 if let Some(constraints) = constraints {
f2b60f7d 78 debug!(?constraints);
353b0b11
FG
79 if !constraints.member_constraints.is_empty() {
80 span_bug!(span, "{:#?}", constraints.member_constraints);
81 }
82
923072b8
FG
83 // Instantiation may have produced new inference variables and constraints on those
84 // variables. Process these constraints.
353b0b11 85 let ocx = ObligationCtxt::new(self);
923072b8 86 let cause = ObligationCause::misc(span, body_id);
353b0b11
FG
87 for &constraint in &constraints.outlives {
88 ocx.register_obligation(self.query_outlives_constraint_to_obligation(
89 constraint,
90 cause.clone(),
91 param_env,
92 ));
923072b8 93 }
353b0b11
FG
94
95 let errors = ocx.select_all_or_error();
923072b8
FG
96 if !errors.is_empty() {
97 self.tcx.sess.delay_span_bug(
98 span,
99 "implied_outlives_bounds failed to solve obligations from instantiation",
100 );
101 }
102 };
8faf50e0 103
923072b8 104 output
8faf50e0 105 }
f2b60f7d
FG
106
107 fn implied_bounds_tys(
108 &'a self,
109 param_env: ParamEnv<'tcx>,
9ffffee4 110 body_id: LocalDefId,
487cf647 111 tys: FxIndexSet<Ty<'tcx>>,
f2b60f7d 112 ) -> Bounds<'a, 'tcx> {
353b0b11 113 tys.into_iter().flat_map(move |ty| self.implied_outlives_bounds(param_env, body_id, ty))
f2b60f7d 114 }
8faf50e0 115}