1use rustc_data_structures::fx::FxIndexSet;
2use rustc_hir::def_id::LocalDefId;
3use rustc_infer::infer::outlives::env::OutlivesEnvironment;
4use rustc_infer::infer::{
5InferCtxt, RegionResolutionError, SubregionOrigin, TyCtxtInferExt, TypeOutlivesConstraint,
6};
7use rustc_macros::extension;
8use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, TypingMode, elaborate};
9use rustc_span::DUMMY_SP;
1011use crate::traits::outlives_bounds::InferCtxtExt;
1213impl<'tcx> OutlivesEnvironmentBuildExt<'tcx> for OutlivesEnvironment<'tcx> {
fn new(infcx: &InferCtxt<'tcx>, body_def_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>) -> Self {
Self::new_with_implied_bounds_compat(infcx, body_def_id, param_env,
assumed_wf_tys, false)
}
fn new_with_implied_bounds_compat(infcx: &InferCtxt<'tcx>,
body_def_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(type_outlives) = bound.as_type_outlives_clause() {
if true {
if !(!infcx.next_trait_solver() ||
!type_outlives.has_non_rigid_aliases()) {
::core::panicking::panic("assertion failed: !infcx.next_trait_solver() || !type_outlives.has_non_rigid_aliases()")
};
};
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_def_id, param_env, assumed_wf_tys,
disable_implied_bounds_hack), higher_ranked_assumptions)
}
}#[extension(pub trait OutlivesEnvironmentBuildExt<'tcx>)]14impl<'tcx> OutlivesEnvironment<'tcx> {
15fn new(
16 infcx: &InferCtxt<'tcx>,
17 body_def_id: LocalDefId,
18 param_env: ty::ParamEnv<'tcx>,
19 assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
20 ) -> Self {
21Self::new_with_implied_bounds_compat(infcx, body_def_id, param_env, assumed_wf_tys, false)
22 }
2324fn new_with_implied_bounds_compat(
25 infcx: &InferCtxt<'tcx>,
26 body_def_id: LocalDefId,
27 param_env: ty::ParamEnv<'tcx>,
28 assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
29 disable_implied_bounds_hack: bool,
30 ) -> Self {
31let mut bounds = vec![];
3233for bound in param_env.caller_bounds() {
34if let Some(type_outlives) = bound.as_type_outlives_clause() {
35debug_assert!(!infcx.next_trait_solver() || !type_outlives.has_non_rigid_aliases());
36 bounds.push(type_outlives);
37 }
38 }
3940// FIXME(-Znext-trait-solver): Normalize these.
41let higher_ranked_assumptions = infcx.take_registered_region_assumptions();
42let higher_ranked_assumptions =
43 elaborate::elaborate_outlives_assumptions(infcx.tcx, higher_ranked_assumptions);
4445// FIXME: This needs to be modified so that we normalize the known type
46 // outlives obligations then elaborate them into their region/type components.
47 // Otherwise, `<W<'a> as Mirror>::Assoc: 'b` will not imply `'a: 'b` even
48 // if we can normalize `'a`.
49OutlivesEnvironment::from_normalized_bounds(
50param_env,
51bounds,
52infcx.implied_bounds_tys(
53body_def_id,
54param_env,
55assumed_wf_tys,
56disable_implied_bounds_hack,
57 ),
58higher_ranked_assumptions,
59 )
60 }
61}
6263impl<'tcx> InferCtxtRegionExt<'tcx> for InferCtxt<'tcx> {
#[doc = " Resolve regions lexically."]
#[doc = ""]
#[doc =
" This function assumes that all infer variables are already constrained."]
#[doc = ""]
#[doc =
" FIXME(#155345): this can probably be moved back to `rustc_infer` now that normalization is"]
#[doc =
" no longer required. These two extension traits won\'t be needed then."]
fn resolve_regions(&self, body_def_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_def_id, param_env, assumed_wf_tys),
self.tcx.def_span(body_def_id))
}
}#[extension(pub trait InferCtxtRegionExt<'tcx>)]64impl<'tcx> InferCtxt<'tcx> {
65/// Resolve regions lexically.
66 ///
67 /// This function assumes that all infer variables are already constrained.
68 ///
69 /// FIXME(#155345): this can probably be moved back to `rustc_infer` now that normalization is
70 /// no longer required. These two extension traits won't be needed then.
71fn resolve_regions(
72&self,
73 body_def_id: LocalDefId,
74 param_env: ty::ParamEnv<'tcx>,
75 assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
76 ) -> Vec<RegionResolutionError<'tcx>> {
77self.resolve_regions_with_outlives_env(
78&OutlivesEnvironment::new(self, body_def_id, param_env, assumed_wf_tys),
79self.tcx.def_span(body_def_id),
80 )
81 }
82}
8384/// Given a known `param_env` and a set of well formed types, can we prove that
85/// `ty` outlives `region`.
86pub fn ty_known_to_outlive<'tcx>(
87 tcx: TyCtxt<'tcx>,
88 id: LocalDefId,
89 param_env: ty::ParamEnv<'tcx>,
90 wf_tys: &FxIndexSet<Ty<'tcx>>,
91 ty: Ty<'tcx>,
92 region: ty::Region<'tcx>,
93) -> bool {
94test_region_obligations(tcx, id, param_env, wf_tys, |infcx| {
95infcx.register_type_outlives_constraint_inner(TypeOutlivesConstraint {
96 sub_region: region,
97 sup_type: ty,
98 origin: SubregionOrigin::RelateParamBound(DUMMY_SP, ty, None),
99 });
100 })
101}
102103/// Given a known `param_env` and a set of well formed types, can we prove that
104/// `region_a` outlives `region_b`
105pub fn region_known_to_outlive<'tcx>(
106 tcx: TyCtxt<'tcx>,
107 id: LocalDefId,
108 param_env: ty::ParamEnv<'tcx>,
109 wf_tys: &FxIndexSet<Ty<'tcx>>,
110 region_a: ty::Region<'tcx>,
111 region_b: ty::Region<'tcx>,
112) -> bool {
113test_region_obligations(tcx, id, param_env, wf_tys, |infcx| {
114infcx.sub_regions(
115 SubregionOrigin::RelateRegionParamBound(DUMMY_SP, None),
116region_b,
117region_a,
118 ty::VisibleForLeakCheck::Unreachable,
119 );
120 })
121}
122123/// Given a known `param_env` and a set of well formed types, set up an
124/// `InferCtxt`, call the passed function (to e.g. set up region constraints
125/// to be tested), then resolve region and return errors
126pub fn test_region_obligations<'tcx>(
127 tcx: TyCtxt<'tcx>,
128 id: LocalDefId,
129 param_env: ty::ParamEnv<'tcx>,
130 wf_tys: &FxIndexSet<Ty<'tcx>>,
131 add_constraints: impl FnOnce(&InferCtxt<'tcx>),
132) -> bool {
133// Unfortunately, we have to use a new `InferCtxt` each call, because
134 // region constraints get added and solved there and we need to test each
135 // call individually.
136let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
137138add_constraints(&infcx);
139140let errors = infcx.resolve_regions(id, param_env, wf_tys.iter().copied());
141{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/regions.rs:141",
"rustc_trait_selection::regions", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/regions.rs"),
::tracing_core::__macro_support::Option::Some(141u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::regions"),
::tracing_core::field::FieldSet::new(&["message",
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("errors")
}> =
::tracing::__macro_support::FieldName::new("errors");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("errors")
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&errors)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};tracing::debug!(?errors, "errors");
142143// If we were able to prove that the type outlives the region without
144 // an error, it must be because of the implied or explicit bounds...
145errors.is_empty()
146}