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
12impl<'tcx> OutlivesEnvironmentBuildExt<'tcx> for OutlivesEnvironment<'tcx> {
fn new(infcx: &InferCtxt<'tcx>, body_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>) -> Self {
Self::new_with_implied_bounds_compat(infcx, body_id, param_env,
assumed_wf_tys, false)
}
fn new_with_implied_bounds_compat(infcx: &InferCtxt<'tcx>,
body_id: LocalDefId, param_env: ty::ParamEnv<'tcx>,
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
disable_implied_bounds_hack: bool) -> Self {
let mut bounds = ::alloc::vec::Vec::new();
for bound in param_env.caller_bounds() {
if let Some(mut type_outlives) = bound.as_type_outlives_clause() {
if infcx.next_trait_solver() {
match crate::solve::deeply_normalize::<_,
ScrubbedTraitError<'tcx>>(infcx.at(&ObligationCause::dummy(),
param_env), type_outlives) {
Ok(new) => type_outlives = new,
Err(_) => {
infcx.dcx().delayed_bug(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("could not normalize `{0}`",
bound))
}));
}
}
}
bounds.push(type_outlives);
}
}
let higher_ranked_assumptions =
infcx.take_registered_region_assumptions();
let higher_ranked_assumptions =
elaborate::elaborate_outlives_assumptions(infcx.tcx,
higher_ranked_assumptions);
OutlivesEnvironment::from_normalized_bounds(param_env, bounds,
infcx.implied_bounds_tys(body_id, param_env, assumed_wf_tys,
disable_implied_bounds_hack), higher_ranked_assumptions)
}
}#[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
72impl<'tcx> InferCtxtRegionExt<'tcx> for InferCtxt<'tcx> {
#[doc =
" Resolve regions, using the deep normalizer to normalize any type-outlives"]
#[doc =
" obligations in the process. This is in `rustc_trait_selection` because"]
#[doc = " we need to normalize."]
#[doc = ""]
#[doc =
" Prefer this method over `resolve_regions_with_normalize`, unless you are"]
#[doc = " doing something specific for normalization."]
#[doc = ""]
#[doc =
" This function assumes that all infer variables are already constrained."]
fn resolve_regions(&self, body_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>)
-> Vec<RegionResolutionError<'tcx>> {
self.resolve_regions_with_outlives_env(&OutlivesEnvironment::new(self,
body_id, param_env, assumed_wf_tys))
}
#[doc = " Don\'t call this directly unless you know what you\'re doing."]
fn resolve_regions_with_outlives_env(&self,
outlives_env: &OutlivesEnvironment<'tcx>)
-> Vec<RegionResolutionError<'tcx>> {
self.resolve_regions_with_normalize(&outlives_env,
|ty, origin|
{
let ty = self.resolve_vars_if_possible(ty);
if self.next_trait_solver() {
crate::solve::deeply_normalize(self.at(&ObligationCause::dummy_with_span(origin.span()),
outlives_env.param_env),
ty).map_err(|_: Vec<ScrubbedTraitError<'tcx>>| NoSolution)
} else { Ok(ty) }
})
}
}#[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}