From 1e236acd05c045b186195aa0a0b44657ac039978 Mon Sep 17 00:00:00 2001
From: Michael Goulet <michael@errs.io>
Date: Fri, 25 Nov 2022 18:40:52 +0000
Subject: [PATCH] Make ObligationCtxt::normalize take cause by borrow

---
 .../src/diagnostics/bound_region_errors.rs    |  2 +-
 .../src/transform/check_consts/check.rs       |  3 +--
 .../src/util/compare_types.rs                 |  4 ++--
 .../src/check/compare_method.rs               | 20 +++++++++----------
 .../rustc_hir_analysis/src/check/wfcheck.rs   |  2 +-
 compiler/rustc_hir_analysis/src/lib.rs        |  2 +-
 compiler/rustc_hir_typeck/src/inherited.rs    |  2 +-
 .../src/traits/engine.rs                      |  5 ++---
 .../src/traits/error_reporting/ambiguity.rs   |  4 ++--
 .../rustc_trait_selection/src/traits/mod.rs   |  6 +++---
 compiler/rustc_traits/src/type_op.rs          |  6 +++---
 11 files changed, 27 insertions(+), 29 deletions(-)

diff --git a/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs
index 009787cc94c..e05566dc2c7 100644
--- a/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs
+++ b/compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs
@@ -295,7 +295,7 @@ where
         // the former fails to normalize the `nll/relate_tys/impl-fn-ignore-binder-via-bottom.rs` test.
         // Check after #85499 lands to see if its fixes have erased this difference.
         let (param_env, value) = key.into_parts();
-        let _ = ocx.normalize(cause, param_env, value.value);
+        let _ = ocx.normalize(&cause, param_env, value.value);
 
         try_extract_error_from_fulfill_cx(&ocx, placeholder_region, error_region)
     }
diff --git a/compiler/rustc_const_eval/src/transform/check_consts/check.rs b/compiler/rustc_const_eval/src/transform/check_consts/check.rs
index d4cee305387..54213d55a2d 100644
--- a/compiler/rustc_const_eval/src/transform/check_consts/check.rs
+++ b/compiler/rustc_const_eval/src/transform/check_consts/check.rs
@@ -761,8 +761,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
                             hir_id,
                             ObligationCauseCode::ItemObligation(callee),
                         );
-                        let normalized_predicates =
-                            ocx.normalize(cause.clone(), param_env, predicates);
+                        let normalized_predicates = ocx.normalize(&cause, param_env, predicates);
                         ocx.register_obligations(traits::predicates_for_generics(
                             |_, _| cause.clone(),
                             self.param_env,
diff --git a/compiler/rustc_const_eval/src/util/compare_types.rs b/compiler/rustc_const_eval/src/util/compare_types.rs
index a9cb191cc59..be786569cde 100644
--- a/compiler/rustc_const_eval/src/util/compare_types.rs
+++ b/compiler/rustc_const_eval/src/util/compare_types.rs
@@ -46,8 +46,8 @@ pub fn is_subtype<'tcx>(
     let infcx = builder.build();
     let ocx = ObligationCtxt::new(&infcx);
     let cause = ObligationCause::dummy();
-    let src = ocx.normalize(cause.clone(), param_env, src);
-    let dest = ocx.normalize(cause.clone(), param_env, dest);
+    let src = ocx.normalize(&cause, param_env, src);
+    let dest = ocx.normalize(&cause, param_env, dest);
     match ocx.sub(&cause, param_env, src, dest) {
         Ok(()) => {}
         Err(_) => return false,
diff --git a/compiler/rustc_hir_analysis/src/check/compare_method.rs b/compiler/rustc_hir_analysis/src/check/compare_method.rs
index de386e2d135..ba58672e759 100644
--- a/compiler/rustc_hir_analysis/src/check/compare_method.rs
+++ b/compiler/rustc_hir_analysis/src/check/compare_method.rs
@@ -221,7 +221,7 @@ fn compare_predicate_entailment<'tcx>(
     let impl_m_own_bounds = impl_m_predicates.instantiate_own(tcx, impl_to_placeholder_substs);
     for (predicate, span) in iter::zip(impl_m_own_bounds.predicates, impl_m_own_bounds.spans) {
         let normalize_cause = traits::ObligationCause::misc(span, impl_m_hir_id);
-        let predicate = ocx.normalize(normalize_cause, param_env, predicate);
+        let predicate = ocx.normalize(&normalize_cause, param_env, predicate);
 
         let cause = ObligationCause::new(
             span,
@@ -260,7 +260,7 @@ fn compare_predicate_entailment<'tcx>(
     );
 
     let norm_cause = ObligationCause::misc(impl_m_span, impl_m_hir_id);
-    let impl_sig = ocx.normalize(norm_cause.clone(), param_env, impl_sig);
+    let impl_sig = ocx.normalize(&norm_cause, param_env, impl_sig);
     let impl_fty = tcx.mk_fn_ptr(ty::Binder::dummy(impl_sig));
     debug!("compare_impl_method: impl_fty={:?}", impl_fty);
 
@@ -271,7 +271,7 @@ fn compare_predicate_entailment<'tcx>(
     // we have to do this before normalization, since the normalized ty may
     // not contain the input parameters. See issue #87748.
     wf_tys.extend(trait_sig.inputs_and_output.iter());
-    let trait_sig = ocx.normalize(norm_cause, param_env, trait_sig);
+    let trait_sig = ocx.normalize(&norm_cause, param_env, trait_sig);
     // We also have to add the normalized trait signature
     // as we don't normalize during implied bounds computation.
     wf_tys.extend(trait_sig.inputs_and_output.iter());
@@ -366,7 +366,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
     // Normalize the impl signature with fresh variables for lifetime inference.
     let norm_cause = ObligationCause::misc(return_span, impl_m_hir_id);
     let impl_sig = ocx.normalize(
-        norm_cause.clone(),
+        &norm_cause,
         param_env,
         infcx.replace_bound_vars_with_fresh_vars(
             return_span,
@@ -387,7 +387,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
             tcx.bound_fn_sig(trait_m.def_id).subst(tcx, trait_to_placeholder_substs),
         )
         .fold_with(&mut collector);
-    let trait_sig = ocx.normalize(norm_cause.clone(), param_env, unnormalized_trait_sig);
+    let trait_sig = ocx.normalize(&norm_cause, param_env, unnormalized_trait_sig);
     let trait_return_ty = trait_sig.output();
 
     let wf_tys = FxIndexSet::from_iter(
@@ -592,7 +592,7 @@ impl<'tcx> TypeFolder<'tcx> for ImplTraitInTraitCollector<'_, 'tcx> {
             for (pred, pred_span) in self.tcx().bound_explicit_item_bounds(proj.item_def_id).subst_iter_copied(self.tcx(), proj.substs) {
                 let pred = pred.fold_with(self);
                 let pred = self.ocx.normalize(
-                    ObligationCause::misc(self.span, self.body_id),
+                    &ObligationCause::misc(self.span, self.body_id),
                     self.param_env,
                     pred,
                 );
@@ -1403,11 +1403,11 @@ pub(crate) fn raw_compare_const_impl<'tcx>(
     );
 
     // There is no "body" here, so just pass dummy id.
-    let impl_ty = ocx.normalize(cause.clone(), param_env, impl_ty);
+    let impl_ty = ocx.normalize(&cause, param_env, impl_ty);
 
     debug!("compare_const_impl: impl_ty={:?}", impl_ty);
 
-    let trait_ty = ocx.normalize(cause.clone(), param_env, trait_ty);
+    let trait_ty = ocx.normalize(&cause, param_env, trait_ty);
 
     debug!("compare_const_impl: trait_ty={:?}", trait_ty);
 
@@ -1556,7 +1556,7 @@ fn compare_type_predicate_entailment<'tcx>(
     for (span, predicate) in std::iter::zip(impl_ty_own_bounds.spans, impl_ty_own_bounds.predicates)
     {
         let cause = ObligationCause::misc(span, impl_ty_hir_id);
-        let predicate = ocx.normalize(cause, param_env, predicate);
+        let predicate = ocx.normalize(&cause, param_env, predicate);
 
         let cause = ObligationCause::new(
             span,
@@ -1778,7 +1778,7 @@ pub fn check_type_bounds<'tcx>(
 
     for mut obligation in util::elaborate_obligations(tcx, obligations) {
         let normalized_predicate =
-            ocx.normalize(normalize_cause.clone(), normalize_param_env, obligation.predicate);
+            ocx.normalize(&normalize_cause, normalize_param_env, obligation.predicate);
         debug!("compare_projection_bounds: normalized predicate = {:?}", normalized_predicate);
         obligation.predicate = normalized_predicate;
 
diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs
index 894c0ba3415..7daed74e9de 100644
--- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs
+++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs
@@ -60,7 +60,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
         T: TypeFoldable<'tcx>,
     {
         self.ocx.normalize(
-            ObligationCause::new(span, self.body_id, ObligationCauseCode::WellFormed(loc)),
+            &ObligationCause::new(span, self.body_id, ObligationCauseCode::WellFormed(loc)),
             self.param_env,
             value,
         )
diff --git a/compiler/rustc_hir_analysis/src/lib.rs b/compiler/rustc_hir_analysis/src/lib.rs
index 664d3a3a1db..2058832d5fd 100644
--- a/compiler/rustc_hir_analysis/src/lib.rs
+++ b/compiler/rustc_hir_analysis/src/lib.rs
@@ -332,7 +332,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
             ObligationCauseCode::MainFunctionType,
         );
         let ocx = traits::ObligationCtxt::new(&infcx);
-        let norm_return_ty = ocx.normalize(cause.clone(), param_env, return_ty);
+        let norm_return_ty = ocx.normalize(&cause, param_env, return_ty);
         ocx.register_bound(cause, param_env, norm_return_ty, term_did);
         let errors = ocx.select_all_or_error();
         if !errors.is_empty() {
diff --git a/compiler/rustc_hir_typeck/src/inherited.rs b/compiler/rustc_hir_typeck/src/inherited.rs
index e8a9e4980f6..869ad07c00d 100644
--- a/compiler/rustc_hir_typeck/src/inherited.rs
+++ b/compiler/rustc_hir_typeck/src/inherited.rs
@@ -100,7 +100,7 @@ impl<'tcx> Inherited<'tcx> {
                     infcx.probe(|_| {
                         let ocx = ObligationCtxt::new_in_snapshot(infcx);
                         let normalized_fn_sig = ocx.normalize(
-                            ObligationCause::dummy(),
+                            &ObligationCause::dummy(),
                             // FIXME(compiler-errors): This is probably not the right param-env...
                             infcx.tcx.param_env(def_id),
                             fn_sig,
diff --git a/compiler/rustc_trait_selection/src/traits/engine.rs b/compiler/rustc_trait_selection/src/traits/engine.rs
index 86c877347c9..64d01ddb09a 100644
--- a/compiler/rustc_trait_selection/src/traits/engine.rs
+++ b/compiler/rustc_trait_selection/src/traits/engine.rs
@@ -104,8 +104,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
 
     pub fn normalize<T: TypeFoldable<'tcx>>(
         &self,
-        // FIXME(compiler-errors): Make this borrow
-        cause: ObligationCause<'tcx>,
+        cause: &ObligationCause<'tcx>,
         param_env: ty::ParamEnv<'tcx>,
         value: T,
     ) -> T {
@@ -186,7 +185,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
             // sound and then uncomment this line again.
 
             // implied_bounds.insert(ty);
-            let normalized = self.normalize(cause.clone(), param_env, ty);
+            let normalized = self.normalize(&cause, param_env, ty);
             implied_bounds.insert(normalized);
         }
         implied_bounds
diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs
index 6a5744f5f76..752b53fbc3f 100644
--- a/compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs
+++ b/compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs
@@ -17,11 +17,11 @@ pub fn recompute_applicable_impls<'tcx>(
         let placeholder_obligation =
             infcx.replace_bound_vars_with_placeholders(obligation.predicate);
         let obligation_trait_ref =
-            ocx.normalize(dummy_cause.clone(), param_env, placeholder_obligation.trait_ref);
+            ocx.normalize(&dummy_cause, param_env, placeholder_obligation.trait_ref);
 
         let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id);
         let impl_trait_ref = tcx.bound_impl_trait_ref(impl_def_id).unwrap().subst(tcx, impl_substs);
-        let impl_trait_ref = ocx.normalize(ObligationCause::dummy(), param_env, impl_trait_ref);
+        let impl_trait_ref = ocx.normalize(&ObligationCause::dummy(), param_env, impl_trait_ref);
 
         if let Err(_) = ocx.eq(&dummy_cause, param_env, obligation_trait_ref, impl_trait_ref) {
             return false;
diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs
index c6efbd6b684..371367f0deb 100644
--- a/compiler/rustc_trait_selection/src/traits/mod.rs
+++ b/compiler/rustc_trait_selection/src/traits/mod.rs
@@ -56,8 +56,8 @@ pub use self::object_safety::astconv_object_safety_violations;
 pub use self::object_safety::is_vtable_safe_method;
 pub use self::object_safety::MethodViolationCode;
 pub use self::object_safety::ObjectSafetyViolation;
-pub use self::project::{NormalizeExt, normalize_projection_type};
 pub(crate) use self::project::{normalize, normalize_to};
+pub use self::project::{normalize_projection_type, NormalizeExt};
 pub use self::select::{EvaluationCache, SelectionCache, SelectionContext};
 pub use self::select::{EvaluationResult, IntercrateAmbiguityCause, OverflowError};
 pub use self::specialize::specialization_graph::FutureCompatOverlapError;
@@ -387,7 +387,7 @@ where
 {
     let ocx = ObligationCtxt::new(infcx);
     debug!(?value);
-    let normalized_value = ocx.normalize(cause, param_env, value);
+    let normalized_value = ocx.normalize(&cause, param_env, value);
     debug!(?normalized_value);
     debug!("select_all_or_error start");
     let errors = ocx.select_all_or_error();
@@ -454,7 +454,7 @@ pub fn impossible_predicates<'tcx>(
     let infcx = tcx.infer_ctxt().build();
     let param_env = ty::ParamEnv::reveal_all();
     let ocx = ObligationCtxt::new(&infcx);
-    let predicates = ocx.normalize(ObligationCause::dummy(), param_env, predicates);
+    let predicates = ocx.normalize(&ObligationCause::dummy(), param_env, predicates);
     for predicate in predicates {
         let obligation = Obligation::new(tcx, ObligationCause::dummy(), param_env, predicate);
         ocx.register_obligation(obligation);
diff --git a/compiler/rustc_traits/src/type_op.rs b/compiler/rustc_traits/src/type_op.rs
index 98141506298..7f964afde80 100644
--- a/compiler/rustc_traits/src/type_op.rs
+++ b/compiler/rustc_traits/src/type_op.rs
@@ -62,7 +62,7 @@ pub fn type_op_ascribe_user_type_with_span<'tcx>(
     let cause = ObligationCause::dummy_with_span(span);
 
     let ty = tcx.bound_type_of(def_id).subst(tcx, substs);
-    let ty = ocx.normalize(cause.clone(), param_env, ty);
+    let ty = ocx.normalize(&cause, param_env, ty);
     debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
 
     ocx.eq(&cause, param_env, mir_ty, ty)?;
@@ -85,14 +85,14 @@ pub fn type_op_ascribe_user_type_with_span<'tcx>(
             ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span),
         );
         let instantiated_predicate =
-            ocx.normalize(cause.clone(), param_env, instantiated_predicate);
+            ocx.normalize(&cause.clone(), param_env, instantiated_predicate);
 
         ocx.register_obligation(Obligation::new(tcx, cause, param_env, instantiated_predicate));
     }
 
     if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
         let impl_self_ty = tcx.bound_type_of(impl_def_id).subst(tcx, substs);
-        let impl_self_ty = ocx.normalize(cause.clone(), param_env, impl_self_ty);
+        let impl_self_ty = ocx.normalize(&cause, param_env, impl_self_ty);
 
         ocx.eq(&cause, param_env, self_ty, impl_self_ty)?;