Skip to content

Commit

Permalink
dedup some code
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Aug 17, 2022
1 parent f57e6fa commit 736288f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
23 changes: 22 additions & 1 deletion compiler/rustc_trait_selection/src/traits/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::cell::RefCell;
use super::TraitEngine;
use super::{ChalkFulfillmentContext, FulfillmentContext};
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::traits::{
FulfillmentError, Obligation, ObligationCause, PredicateObligation, TraitEngineExt as _,
Expand All @@ -12,6 +13,7 @@ use rustc_middle::ty::error::TypeError;
use rustc_middle::ty::ToPredicate;
use rustc_middle::ty::TypeFoldable;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::Span;

pub trait TraitEngineExt<'tcx> {
fn new(tcx: TyCtxt<'tcx>) -> Box<Self>;
Expand Down Expand Up @@ -109,4 +111,23 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
pub fn select_all_or_error(&self) -> Vec<FulfillmentError<'tcx>> {
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
}
}
13 changes: 3 additions & 10 deletions compiler/rustc_typeck/src/check/compare_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1454,15 +1454,8 @@ pub fn check_type_bounds<'tcx>(
tcx.infer_ctxt().enter(move |infcx| {
let ocx = ObligationCtxt::new(&infcx);

let assumed_wf_types = tcx.assumed_wf_types(impl_ty.def_id);
let mut implied_bounds = FxHashSet::default();
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 assumed_wf_types =
ocx.assumed_wf_types(param_env, impl_ty_span, impl_ty.def_id.expect_local());

let mut selcx = traits::SelectionContext::new(&infcx);
let normalize_cause = ObligationCause::new(
Expand Down Expand Up @@ -1521,7 +1514,7 @@ pub fn check_type_bounds<'tcx>(
// Finally, resolve all regions. This catches wily misuses of
// lifetime parameters.
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(
impl_ty.def_id.expect_local(),
&outlives_environment,
Expand Down
12 changes: 2 additions & 10 deletions compiler/rustc_typeck/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,7 @@ pub(super) fn enter_wf_checking_ctxt<'tcx, F>(
tcx.infer_ctxt().enter(|ref infcx| {
let ocx = ObligationCtxt::new(infcx);

let assumed_wf_types = tcx.assumed_wf_types(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 assumed_wf_types = ocx.assumed_wf_types(param_env, span, body_def_id);

let mut wfcx = WfCheckingCtxt { ocx, span, body_id, param_env };

Expand All @@ -113,7 +105,7 @@ pub(super) fn enter_wf_checking_ctxt<'tcx, F>(
}

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);
})
}
Expand Down
14 changes: 3 additions & 11 deletions compiler/rustc_typeck/src/impl_wf_check/min_specialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::specialization_graph::Node;
use rustc_infer::traits::ObligationCause;
use rustc_middle::ty::subst::{GenericArg, InternalSubsts, SubstsRef};
use rustc_middle::ty::trait_def::TraitSpecializationKind;
use rustc_middle::ty::{self, TyCtxt, TypeVisitable};
Expand Down Expand Up @@ -145,15 +144,8 @@ fn get_impl_substs<'tcx>(
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 assumed_wf_types = tcx.assumed_wf_types(impl1_def_id);
let mut implied_bounds = FxHashSet::default();
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 assumed_wf_types =
ocx.assumed_wf_types(param_env, tcx.def_span(impl1_def_id), impl1_def_id);

let impl1_substs = InternalSubsts::identity_for_item(tcx, impl1_def_id.to_def_id());
let impl2_substs =
Expand All @@ -166,7 +158,7 @@ fn get_impl_substs<'tcx>(
}

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);
let Ok(impl2_substs) = infcx.fully_resolve(impl2_substs) else {
let span = tcx.def_span(impl1_def_id);
Expand Down

0 comments on commit 736288f

Please sign in to comment.