rustc_trait_selection/
regions.rs
1use 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};
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(
21 infcx,
22 body_id,
23 param_env,
24 assumed_wf_tys,
25 !infcx.tcx.sess.opts.unstable_opts.no_implied_bounds_compat,
26 )
27 }
28
29 fn new_with_implied_bounds_compat(
30 infcx: &InferCtxt<'tcx>,
31 body_id: LocalDefId,
32 param_env: ty::ParamEnv<'tcx>,
33 assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
34 implied_bounds_compat: bool,
35 ) -> Self {
36 let mut bounds = vec![];
37
38 for bound in param_env.caller_bounds() {
39 if let Some(mut type_outlives) = bound.as_type_outlives_clause() {
40 if infcx.next_trait_solver() {
41 match crate::solve::deeply_normalize::<_, ScrubbedTraitError<'tcx>>(
42 infcx.at(&ObligationCause::dummy(), param_env),
43 type_outlives,
44 ) {
45 Ok(new) => type_outlives = new,
46 Err(_) => {
47 infcx.dcx().delayed_bug(format!("could not normalize `{bound}`"));
48 }
49 }
50 }
51 bounds.push(type_outlives);
52 }
53 }
54
55 OutlivesEnvironment::from_normalized_bounds(
60 param_env,
61 bounds,
62 infcx.implied_bounds_tys_with_compat(
63 body_id,
64 param_env,
65 assumed_wf_tys,
66 implied_bounds_compat,
67 ),
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}