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(
81 &self,
82 body_id: LocalDefId,
83 param_env: ty::ParamEnv<'tcx>,
84 assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
85 ) -> Vec<RegionResolutionError<'tcx>> {
86 self.resolve_regions_with_outlives_env(&OutlivesEnvironment::new(
87 self,
88 body_id,
89 param_env,
90 assumed_wf_tys,
91 ))
92 }
93
94 fn resolve_regions_with_outlives_env(
96 &self,
97 outlives_env: &OutlivesEnvironment<'tcx>,
98 ) -> Vec<RegionResolutionError<'tcx>> {
99 self.resolve_regions_with_normalize(&outlives_env, |ty, origin| {
100 let ty = self.resolve_vars_if_possible(ty);
101
102 if self.next_trait_solver() {
103 crate::solve::deeply_normalize(
104 self.at(
105 &ObligationCause::dummy_with_span(origin.span()),
106 outlives_env.param_env,
107 ),
108 ty,
109 )
110 .map_err(|_: Vec<ScrubbedTraitError<'tcx>>| NoSolution)
111 } else {
112 Ok(ty)
113 }
114 })
115 }
116}