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(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        // FIXME: This needs to be modified so that we normalize the known type
50        // outlives obligations then elaborate them into their region/type components.
51        // Otherwise, `<W<'a> as Mirror>::Assoc: 'b` will not imply `'a: 'b` even
52        // if we can normalize `'a`.
53        OutlivesEnvironment::from_normalized_bounds(
54            param_env,
55            bounds,
56            infcx.implied_bounds_tys(
57                body_id,
58                param_env,
59                assumed_wf_tys,
60                disable_implied_bounds_hack,
61            ),
62        )
63    }
64}
65
66#[extension(pub trait InferCtxtRegionExt<'tcx>)]
67impl<'tcx> InferCtxt<'tcx> {
68    /// Resolve regions, using the deep normalizer to normalize any type-outlives
69    /// obligations in the process. This is in `rustc_trait_selection` because
70    /// we need to normalize.
71    ///
72    /// Prefer this method over `resolve_regions_with_normalize`, unless you are
73    /// doing something specific for normalization.
74    fn resolve_regions(
75        &self,
76        body_id: LocalDefId,
77        param_env: ty::ParamEnv<'tcx>,
78        assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
79    ) -> Vec<RegionResolutionError<'tcx>> {
80        self.resolve_regions_with_outlives_env(&OutlivesEnvironment::new(
81            self,
82            body_id,
83            param_env,
84            assumed_wf_tys,
85        ))
86    }
87
88    /// Don't call this directly unless you know what you're doing.
89    fn resolve_regions_with_outlives_env(
90        &self,
91        outlives_env: &OutlivesEnvironment<'tcx>,
92    ) -> Vec<RegionResolutionError<'tcx>> {
93        self.resolve_regions_with_normalize(&outlives_env, |ty, origin| {
94            let ty = self.resolve_vars_if_possible(ty);
95
96            if self.next_trait_solver() {
97                crate::solve::deeply_normalize(
98                    self.at(
99                        &ObligationCause::dummy_with_span(origin.span()),
100                        outlives_env.param_env,
101                    ),
102                    ty,
103                )
104                .map_err(|_: Vec<ScrubbedTraitError<'tcx>>| NoSolution)
105            } else {
106                Ok(ty)
107            }
108        })
109    }
110}