]> git.lizzy.rs Git - rust.git/commitdiff
dedup some code
authorlcnr <rust@lcnr.de>
Wed, 17 Aug 2022 16:14:25 +0000 (18:14 +0200)
committerlcnr <rust@lcnr.de>
Wed, 17 Aug 2022 16:14:25 +0000 (18:14 +0200)
compiler/rustc_trait_selection/src/traits/engine.rs
compiler/rustc_typeck/src/check/compare_method.rs
compiler/rustc_typeck/src/check/wfcheck.rs
compiler/rustc_typeck/src/impl_wf_check/min_specialization.rs

index 6c177f6388704a943548eb824a97e5df6ab2b46d..136b94321459ee062840443e9534b30dc101f835 100644 (file)
@@ -3,7 +3,8 @@
 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 _,
@@ -12,6 +13,7 @@
 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>;
@@ -109,4 +111,23 @@ pub fn equate_types(
     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
+    }
 }
index 0716b3bc6708b5c4a408ffb3f1252001d7cd5f9f..e42f18db1f5c5673a674ef0feaa55b3c7ac24325 100644 (file)
@@ -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(
@@ -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,
index 2ac183b504e0560d012fcfe44836d3f2c2be54a9..4814aea7afb9e6322e1ec1065948c2e11736011d 100644 (file)
@@ -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 };
 
@@ -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);
     })
 }
index cc8453bf1d09c26576c13838202109bcf2bc214d..97346f0f834c88337da2a8e7b25171be3e463d03 100644 (file)
@@ -74,7 +74,6 @@
 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};
@@ -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 =
@@ -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);