rustc_trait_selection/
regions.rs1use rustc_hir::def_id::LocalDefId;
2use rustc_infer::infer::outlives::env::OutlivesEnvironment;
3use rustc_infer::infer::{InferCtxt, RegionResolutionError};
4use rustc_macros::extension;
5use rustc_middle::traits::ObligationCause;
6use rustc_middle::traits::query::NoSolution;
7use rustc_middle::ty::{self, Ty, elaborate};
8
9use crate::traits::ScrubbedTraitError;
10use crate::traits::outlives_bounds::InferCtxtExt;
11
12#[extension(pub trait OutlivesEnvironmentBuildExt<'tcx>)]
13impl<'tcx> OutlivesEnvironment<'tcx> {
14 fn new(
15 infcx: &InferCtxt<'tcx>,
16 body_id: LocalDefId,
17 param_env: ty::ParamEnv<'tcx>,
18 assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
19 ) -> Self {
20 Self::new_with_implied_bounds_compat(infcx, body_id, param_env, assumed_wf_tys, false)
21 }
22
23 fn new_with_implied_bounds_compat(
24 infcx: &InferCtxt<'tcx>,
25 body_id: LocalDefId,
26 param_env: ty::ParamEnv<'tcx>,
27 assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
28 disable_implied_bounds_hack: bool,
29 ) -> Self {
30 let mut bounds = vec![];
31
32 for bound in param_env.caller_bounds() {
33 if let Some(mut type_outlives) = bound.as_type_outlives_clause() {
34 if infcx.next_trait_solver() {
35 match crate::solve::deeply_normalize::<_, ScrubbedTraitError<'tcx>>(
36 infcx.at(&ObligationCause::dummy(), param_env),
37 type_outlives,
38 ) {
39 Ok(new) => type_outlives = new,
40 Err(_) => {
41 infcx.dcx().delayed_bug(format!("could not normalize `{bound}`"));
42 }
43 }
44 }
45 bounds.push(type_outlives);
46 }
47 }
48
49 let higher_ranked_assumptions = infcx.take_registered_region_assumptions();
51 let higher_ranked_assumptions =
52 elaborate::elaborate_outlives_assumptions(infcx.tcx, higher_ranked_assumptions);
53
54 OutlivesEnvironment::from_normalized_bounds(
59 param_env,
60 bounds,
61 infcx.implied_bounds_tys(
62 body_id,
63 param_env,
64 assumed_wf_tys,
65 disable_implied_bounds_hack,
66 ),
67 higher_ranked_assumptions,
68 )
69 }
70}
71
72#[extension(pub trait InferCtxtRegionExt<'tcx>)]
73impl<'tcx> InferCtxt<'tcx> {
74 fn resolve_regions(
83 &self,
84 body_id: LocalDefId,
85 param_env: ty::ParamEnv<'tcx>,
86 assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
87 ) -> Vec<RegionResolutionError<'tcx>> {
88 self.resolve_regions_with_outlives_env(&OutlivesEnvironment::new(
89 self,
90 body_id,
91 param_env,
92 assumed_wf_tys,
93 ))
94 }
95
96 fn resolve_regions_with_outlives_env(
98 &self,
99 outlives_env: &OutlivesEnvironment<'tcx>,
100 ) -> Vec<RegionResolutionError<'tcx>> {
101 self.resolve_regions_with_normalize(&outlives_env, |ty, origin| {
102 let ty = self.resolve_vars_if_possible(ty);
103
104 if self.next_trait_solver() {
105 crate::solve::deeply_normalize(
106 self.at(
107 &ObligationCause::dummy_with_span(origin.span()),
108 outlives_env.param_env,
109 ),
110 ty,
111 )
112 .map_err(|_: Vec<ScrubbedTraitError<'tcx>>| NoSolution)
113 } else {
114 Ok(ty)
115 }
116 })
117 }
118}