dedup some code

This commit is contained in:
lcnr 2022-08-17 18:14:25 +02:00
parent f57e6fa453
commit 736288f221
4 changed files with 30 additions and 32 deletions

View File

@ -3,7 +3,8 @@ use std::cell::RefCell;
use super::TraitEngine; use super::TraitEngine;
use super::{ChalkFulfillmentContext, FulfillmentContext}; use super::{ChalkFulfillmentContext, FulfillmentContext};
use crate::infer::InferCtxtExt; use crate::infer::InferCtxtExt;
use rustc_hir::def_id::DefId; use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_infer::infer::{InferCtxt, InferOk}; use rustc_infer::infer::{InferCtxt, InferOk};
use rustc_infer::traits::{ use rustc_infer::traits::{
FulfillmentError, Obligation, ObligationCause, PredicateObligation, TraitEngineExt as _, FulfillmentError, Obligation, ObligationCause, PredicateObligation, TraitEngineExt as _,
@ -12,6 +13,7 @@ use rustc_middle::ty::error::TypeError;
use rustc_middle::ty::ToPredicate; use rustc_middle::ty::ToPredicate;
use rustc_middle::ty::TypeFoldable; use rustc_middle::ty::TypeFoldable;
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::Span;
pub trait TraitEngineExt<'tcx> { pub trait TraitEngineExt<'tcx> {
fn new(tcx: TyCtxt<'tcx>) -> Box<Self>; fn new(tcx: TyCtxt<'tcx>) -> Box<Self>;
@ -109,4 +111,23 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
pub fn select_all_or_error(&self) -> Vec<FulfillmentError<'tcx>> { pub fn select_all_or_error(&self) -> Vec<FulfillmentError<'tcx>> {
self.engine.borrow_mut().select_all_or_error(self.infcx) self.engine.borrow_mut().select_all_or_error(self.infcx)
} }
pub fn assumed_wf_types(
&self,
param_env: ty::ParamEnv<'tcx>,
span: Span,
def_id: LocalDefId,
) -> FxHashSet<Ty<'tcx>> {
let tcx = self.infcx.tcx;
let assumed_wf_types = tcx.assumed_wf_types(def_id);
let mut implied_bounds = FxHashSet::default();
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let cause = ObligationCause::misc(span, hir_id);
for ty in assumed_wf_types {
implied_bounds.insert(ty);
let normalized = self.normalize(cause.clone(), param_env, ty);
implied_bounds.insert(normalized);
}
implied_bounds
}
} }

View File

@ -1454,15 +1454,8 @@ pub fn check_type_bounds<'tcx>(
tcx.infer_ctxt().enter(move |infcx| { tcx.infer_ctxt().enter(move |infcx| {
let ocx = ObligationCtxt::new(&infcx); let ocx = ObligationCtxt::new(&infcx);
let assumed_wf_types = tcx.assumed_wf_types(impl_ty.def_id); let assumed_wf_types =
let mut implied_bounds = FxHashSet::default(); ocx.assumed_wf_types(param_env, impl_ty_span, impl_ty.def_id.expect_local());
let cause = ObligationCause::misc(impl_ty_span, impl_ty_hir_id);
for ty in assumed_wf_types {
implied_bounds.insert(ty);
let normalized = ocx.normalize(cause.clone(), param_env, ty);
implied_bounds.insert(normalized);
}
let implied_bounds = implied_bounds;
let mut selcx = traits::SelectionContext::new(&infcx); let mut selcx = traits::SelectionContext::new(&infcx);
let normalize_cause = ObligationCause::new( let normalize_cause = ObligationCause::new(
@ -1521,7 +1514,7 @@ pub fn check_type_bounds<'tcx>(
// Finally, resolve all regions. This catches wily misuses of // Finally, resolve all regions. This catches wily misuses of
// lifetime parameters. // lifetime parameters.
let mut outlives_environment = OutlivesEnvironment::new(param_env); let mut outlives_environment = OutlivesEnvironment::new(param_env);
outlives_environment.add_implied_bounds(&infcx, implied_bounds, impl_ty_hir_id); outlives_environment.add_implied_bounds(&infcx, assumed_wf_types, impl_ty_hir_id);
infcx.check_region_obligations_and_report_errors( infcx.check_region_obligations_and_report_errors(
impl_ty.def_id.expect_local(), impl_ty.def_id.expect_local(),
&outlives_environment, &outlives_environment,

View File

@ -90,15 +90,7 @@ pub(super) fn enter_wf_checking_ctxt<'tcx, F>(
tcx.infer_ctxt().enter(|ref infcx| { tcx.infer_ctxt().enter(|ref infcx| {
let ocx = ObligationCtxt::new(infcx); let ocx = ObligationCtxt::new(infcx);
let assumed_wf_types = tcx.assumed_wf_types(body_def_id); let assumed_wf_types = ocx.assumed_wf_types(param_env, span, body_def_id);
let mut implied_bounds = FxHashSet::default();
let cause = ObligationCause::misc(span, body_id);
for ty in assumed_wf_types {
implied_bounds.insert(ty);
let normalized = ocx.normalize(cause.clone(), param_env, ty);
implied_bounds.insert(normalized);
}
let implied_bounds = implied_bounds;
let mut wfcx = WfCheckingCtxt { ocx, span, body_id, param_env }; let mut wfcx = WfCheckingCtxt { ocx, span, body_id, param_env };
@ -113,7 +105,7 @@ pub(super) fn enter_wf_checking_ctxt<'tcx, F>(
} }
let mut outlives_environment = OutlivesEnvironment::new(param_env); let mut outlives_environment = OutlivesEnvironment::new(param_env);
outlives_environment.add_implied_bounds(infcx, implied_bounds, body_id); outlives_environment.add_implied_bounds(infcx, assumed_wf_types, body_id);
infcx.check_region_obligations_and_report_errors(body_def_id, &outlives_environment); infcx.check_region_obligations_and_report_errors(body_def_id, &outlives_environment);
}) })
} }

View File

@ -74,7 +74,6 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_infer::infer::outlives::env::OutlivesEnvironment; use rustc_infer::infer::outlives::env::OutlivesEnvironment;
use rustc_infer::infer::TyCtxtInferExt; use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::specialization_graph::Node; use rustc_infer::traits::specialization_graph::Node;
use rustc_infer::traits::ObligationCause;
use rustc_middle::ty::subst::{GenericArg, InternalSubsts, SubstsRef}; use rustc_middle::ty::subst::{GenericArg, InternalSubsts, SubstsRef};
use rustc_middle::ty::trait_def::TraitSpecializationKind; use rustc_middle::ty::trait_def::TraitSpecializationKind;
use rustc_middle::ty::{self, TyCtxt, TypeVisitable}; use rustc_middle::ty::{self, TyCtxt, TypeVisitable};
@ -145,15 +144,8 @@ fn get_impl_substs<'tcx>(
let param_env = tcx.param_env(impl1_def_id); let param_env = tcx.param_env(impl1_def_id);
let impl1_hir_id = tcx.hir().local_def_id_to_hir_id(impl1_def_id); let impl1_hir_id = tcx.hir().local_def_id_to_hir_id(impl1_def_id);
let assumed_wf_types = tcx.assumed_wf_types(impl1_def_id); let assumed_wf_types =
let mut implied_bounds = FxHashSet::default(); ocx.assumed_wf_types(param_env, tcx.def_span(impl1_def_id), impl1_def_id);
let cause = ObligationCause::misc(tcx.def_span(impl1_def_id), impl1_hir_id);
for ty in assumed_wf_types {
implied_bounds.insert(ty);
let normalized = ocx.normalize(cause.clone(), param_env, ty);
implied_bounds.insert(normalized);
}
let implied_bounds = implied_bounds;
let impl1_substs = InternalSubsts::identity_for_item(tcx, impl1_def_id.to_def_id()); let impl1_substs = InternalSubsts::identity_for_item(tcx, impl1_def_id.to_def_id());
let impl2_substs = let impl2_substs =
@ -166,7 +158,7 @@ fn get_impl_substs<'tcx>(
} }
let mut outlives_env = OutlivesEnvironment::new(param_env); let mut outlives_env = OutlivesEnvironment::new(param_env);
outlives_env.add_implied_bounds(infcx, implied_bounds, impl1_hir_id); outlives_env.add_implied_bounds(infcx, assumed_wf_types, impl1_hir_id);
infcx.check_region_obligations_and_report_errors(impl1_def_id, &outlives_env); infcx.check_region_obligations_and_report_errors(impl1_def_id, &outlives_env);
let Ok(impl2_substs) = infcx.fully_resolve(impl2_substs) else { let Ok(impl2_substs) = infcx.fully_resolve(impl2_substs) else {
let span = tcx.def_span(impl1_def_id); let span = tcx.def_span(impl1_def_id);