Pass ObligationCtxt from enter_canonical_trait_query and use ObligationCtxt API

This commit is contained in:
Santiago Pastorino 2022-11-16 19:40:55 -03:00
parent 5b3a06a3c2
commit 859b147d4f
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF

View File

@ -5,16 +5,15 @@
use rustc_hir as hir; use rustc_hir as hir;
use rustc_infer::infer::canonical::{self, Canonical}; use rustc_infer::infer::canonical::{self, Canonical};
use rustc_infer::infer::outlives::components::{push_outlives_components, Component}; use rustc_infer::infer::outlives::components::{push_outlives_components, Component};
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt}; use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::query::OutlivesBound; use rustc_infer::traits::query::OutlivesBound;
use rustc_infer::traits::TraitEngineExt as _;
use rustc_middle::ty::query::Providers; use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable}; use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable};
use rustc_span::source_map::DUMMY_SP; use rustc_span::source_map::DUMMY_SP;
use rustc_trait_selection::infer::InferCtxtBuilderExt; use rustc_trait_selection::infer::InferCtxtBuilderExt;
use rustc_trait_selection::traits::query::{CanonicalTyGoal, Fallible, NoSolution}; use rustc_trait_selection::traits::query::{CanonicalTyGoal, Fallible, NoSolution};
use rustc_trait_selection::traits::wf; use rustc_trait_selection::traits::wf;
use rustc_trait_selection::traits::{TraitEngine, TraitEngineExt}; use rustc_trait_selection::traits::ObligationCtxt;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
pub(crate) fn provide(p: &mut Providers) { pub(crate) fn provide(p: &mut Providers) {
@ -30,16 +29,16 @@ fn implied_outlives_bounds<'tcx>(
> { > {
tcx.infer_ctxt().enter_canonical_trait_query(&goal, |ocx, key| { tcx.infer_ctxt().enter_canonical_trait_query(&goal, |ocx, key| {
let (param_env, ty) = key.into_parts(); let (param_env, ty) = key.into_parts();
compute_implied_outlives_bounds(&ocx.infcx, param_env, ty) compute_implied_outlives_bounds(ocx, param_env, ty)
}) })
} }
fn compute_implied_outlives_bounds<'tcx>( fn compute_implied_outlives_bounds<'tcx>(
infcx: &InferCtxt<'tcx>, ocx: &ObligationCtxt<'_, 'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
ty: Ty<'tcx>, ty: Ty<'tcx>,
) -> Fallible<Vec<OutlivesBound<'tcx>>> { ) -> Fallible<Vec<OutlivesBound<'tcx>>> {
let tcx = infcx.tcx; let tcx = ocx.infcx.tcx;
// Sometimes when we ask what it takes for T: WF, we get back that // Sometimes when we ask what it takes for T: WF, we get back that
// U: WF is required; in that case, we push U onto this stack and // U: WF is required; in that case, we push U onto this stack and
@ -52,8 +51,6 @@ fn compute_implied_outlives_bounds<'tcx>(
let mut outlives_bounds: Vec<ty::OutlivesPredicate<ty::GenericArg<'tcx>, ty::Region<'tcx>>> = let mut outlives_bounds: Vec<ty::OutlivesPredicate<ty::GenericArg<'tcx>, ty::Region<'tcx>>> =
vec![]; vec![];
let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new(tcx);
while let Some(arg) = wf_args.pop() { while let Some(arg) = wf_args.pop() {
if !checked_wf_args.insert(arg) { if !checked_wf_args.insert(arg) {
continue; continue;
@ -70,15 +67,15 @@ fn compute_implied_outlives_bounds<'tcx>(
// FIXME(@lcnr): It's not really "always fine", having fewer implied // FIXME(@lcnr): It's not really "always fine", having fewer implied
// bounds can be backward incompatible, e.g. #101951 was caused by // bounds can be backward incompatible, e.g. #101951 was caused by
// us not dealing with inference vars in `TypeOutlives` predicates. // us not dealing with inference vars in `TypeOutlives` predicates.
let obligations = wf::obligations(infcx, param_env, hir::CRATE_HIR_ID, 0, arg, DUMMY_SP) let obligations =
.unwrap_or_default(); wf::obligations(ocx.infcx, param_env, hir::CRATE_HIR_ID, 0, arg, DUMMY_SP)
.unwrap_or_default();
// While these predicates should all be implied by other parts of // While these predicates should all be implied by other parts of
// the program, they are still relevant as they may constrain // the program, they are still relevant as they may constrain
// inference variables, which is necessary to add the correct // inference variables, which is necessary to add the correct
// implied bounds in some cases, mostly when dealing with projections. // implied bounds in some cases, mostly when dealing with projections.
fulfill_cx.register_predicate_obligations( ocx.register_obligations(
infcx,
obligations.iter().filter(|o| o.predicate.has_non_region_infer()).cloned(), obligations.iter().filter(|o| o.predicate.has_non_region_infer()).cloned(),
); );
@ -116,9 +113,9 @@ fn compute_implied_outlives_bounds<'tcx>(
})); }));
} }
// Ensure that those obligations that we had to solve // This call to `select_all_or_error` is necessary to constrain inference variables, which we
// get solved *here*. // use further down when computing the implied bounds.
match fulfill_cx.select_all_or_error(infcx).as_slice() { match ocx.select_all_or_error().as_slice() {
[] => (), [] => (),
_ => return Err(NoSolution), _ => return Err(NoSolution),
} }
@ -130,7 +127,7 @@ fn compute_implied_outlives_bounds<'tcx>(
.flat_map(|ty::OutlivesPredicate(a, r_b)| match a.unpack() { .flat_map(|ty::OutlivesPredicate(a, r_b)| match a.unpack() {
ty::GenericArgKind::Lifetime(r_a) => vec![OutlivesBound::RegionSubRegion(r_b, r_a)], ty::GenericArgKind::Lifetime(r_a) => vec![OutlivesBound::RegionSubRegion(r_b, r_a)],
ty::GenericArgKind::Type(ty_a) => { ty::GenericArgKind::Type(ty_a) => {
let ty_a = infcx.resolve_vars_if_possible(ty_a); let ty_a = ocx.infcx.resolve_vars_if_possible(ty_a);
let mut components = smallvec![]; let mut components = smallvec![];
push_outlives_components(tcx, ty_a, &mut components); push_outlives_components(tcx, ty_a, &mut components);
implied_bounds_from_components(r_b, components) implied_bounds_from_components(r_b, components)