about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2025-02-17 17:36:35 +0000
committerMichael Goulet <michael@errs.io>2025-02-22 00:02:11 +0000
commit681c95c55c449a20c0abe8aadc55b0677cabaa25 (patch)
tree04fcc7e51aa3f4a0ac9b2b8a7bf41a41ce6dc6ae /compiler
parent794c12416b2138064af1f2746646973fafd9419d (diff)
downloadrust-681c95c55c449a20c0abe8aadc55b0677cabaa25.tar.gz
rust-681c95c55c449a20c0abe8aadc55b0677cabaa25.zip
Remove UnifyReceiver cause code
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_hir_typeck/src/method/confirm.rs14
-rw-r--r--compiler/rustc_middle/src/traits/mod.rs10
-rw-r--r--compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs112
-rw-r--r--compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs1
4 files changed, 12 insertions, 125 deletions
diff --git a/compiler/rustc_hir_typeck/src/method/confirm.rs b/compiler/rustc_hir_typeck/src/method/confirm.rs
index 8fb425ff0c9..3b107fbf173 100644
--- a/compiler/rustc_hir_typeck/src/method/confirm.rs
+++ b/compiler/rustc_hir_typeck/src/method/confirm.rs
@@ -11,7 +11,7 @@ use rustc_hir_analysis::hir_ty_lowering::{
 };
 use rustc_infer::infer::{self, DefineOpaqueTypes, InferOk};
 use rustc_lint::builtin::SUPERTRAIT_ITEM_SHADOWING_USAGE;
-use rustc_middle::traits::{ObligationCauseCode, UnifyReceiverContext};
+use rustc_middle::traits::ObligationCauseCode;
 use rustc_middle::ty::adjustment::{
     Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCoercion,
 };
@@ -136,7 +136,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
             "confirm: self_ty={:?} method_sig_rcvr={:?} method_sig={:?} method_predicates={:?}",
             self_ty, method_sig_rcvr, method_sig, method_predicates
         );
-        self.unify_receivers(self_ty, method_sig_rcvr, pick, all_args);
+        self.unify_receivers(self_ty, method_sig_rcvr, pick);
 
         let (method_sig, method_predicates) =
             self.normalize(self.span, (method_sig, method_predicates));
@@ -525,20 +525,12 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
         self_ty: Ty<'tcx>,
         method_self_ty: Ty<'tcx>,
         pick: &probe::Pick<'tcx>,
-        args: GenericArgsRef<'tcx>,
     ) {
         debug!(
             "unify_receivers: self_ty={:?} method_self_ty={:?} span={:?} pick={:?}",
             self_ty, method_self_ty, self.span, pick
         );
-        let cause = self.cause(
-            self.self_expr.span,
-            ObligationCauseCode::UnifyReceiver(Box::new(UnifyReceiverContext {
-                assoc_item: pick.item,
-                param_env: self.param_env,
-                args,
-            })),
-        );
+        let cause = self.cause(self.self_expr.span, ObligationCauseCode::Misc);
         match self.at(&cause, self.param_env).sup(DefineOpaqueTypes::Yes, method_self_ty, self_ty) {
             Ok(InferOk { obligations, value: () }) => {
                 self.register_predicates(obligations);
diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs
index 53bc9eb7e46..a2cc5221d00 100644
--- a/compiler/rustc_middle/src/traits/mod.rs
+++ b/compiler/rustc_middle/src/traits/mod.rs
@@ -144,14 +144,6 @@ impl<'tcx> ObligationCause<'tcx> {
     }
 }
 
-#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
-#[derive(TypeVisitable, TypeFoldable)]
-pub struct UnifyReceiverContext<'tcx> {
-    pub assoc_item: ty::AssocItem,
-    pub param_env: ty::ParamEnv<'tcx>,
-    pub args: GenericArgsRef<'tcx>,
-}
-
 /// A compact form of `ObligationCauseCode`.
 #[derive(Clone, PartialEq, Eq, Default, HashStable)]
 #[derive(TypeVisitable, TypeFoldable, TyEncodable, TyDecodable)]
@@ -360,8 +352,6 @@ pub enum ObligationCauseCode<'tcx> {
     /// Method receiver
     MethodReceiver,
 
-    UnifyReceiver(Box<UnifyReceiverContext<'tcx>>),
-
     /// `return` with no expression
     ReturnNoExpression,
 
diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs
index 3f15a79271d..b6ada8dfbf8 100644
--- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs
+++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs
@@ -8,20 +8,17 @@ use rustc_hir::{
     self as hir, AmbigArg, GenericBound, GenericParam, GenericParamKind, Item, ItemKind, Lifetime,
     LifetimeName, LifetimeParamKind, MissingLifetimeKind, Node, TyKind,
 };
-use rustc_middle::ty::{
-    self, AssocItemContainer, StaticLifetimeVisitor, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor,
-};
+use rustc_middle::ty::{self, StaticLifetimeVisitor, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor};
 use rustc_span::def_id::LocalDefId;
 use rustc_span::{Ident, Span};
 use tracing::debug;
 
 use crate::error_reporting::infer::nice_region_error::NiceRegionError;
 use crate::errors::{
-    ButCallingIntroduces, ButNeedsToSatisfy, DynTraitConstraintSuggestion, MoreTargeted,
-    ReqIntroducedLocations,
+    ButNeedsToSatisfy, DynTraitConstraintSuggestion, MoreTargeted, ReqIntroducedLocations,
 };
 use crate::infer::{RegionResolutionError, SubregionOrigin, TypeTrace};
-use crate::traits::{ObligationCauseCode, UnifyReceiverContext};
+use crate::traits::ObligationCauseCode;
 
 impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
     /// Print the error message for lifetime errors when the return type is a static `impl Trait`,
@@ -39,52 +36,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
                 sup_r,
                 spans,
             ) if sub_r.is_static() => (var_origin, sub_origin, sub_r, sup_origin, sup_r, spans),
-            RegionResolutionError::ConcreteFailure(
-                SubregionOrigin::Subtype(box TypeTrace { cause, .. }),
-                sub_r,
-                sup_r,
-            ) if sub_r.is_static() => {
-                // This is for an implicit `'static` requirement coming from `impl dyn Trait {}`.
-                if let ObligationCauseCode::UnifyReceiver(ctxt) = cause.code() {
-                    // This may have a closure and it would cause ICE
-                    // through `find_param_with_region` (#78262).
-                    let anon_reg_sup = tcx.is_suitable_region(self.generic_param_scope, *sup_r)?;
-                    let fn_returns = tcx.return_type_impl_or_dyn_traits(anon_reg_sup.scope);
-                    if fn_returns.is_empty() {
-                        return None;
-                    }
-
-                    let param = self.find_param_with_region(*sup_r, *sub_r)?;
-                    let simple_ident = param.param.pat.simple_ident();
-
-                    let (has_impl_path, impl_path) = match ctxt.assoc_item.container {
-                        AssocItemContainer::Trait => {
-                            let id = ctxt.assoc_item.container_id(tcx);
-                            (true, tcx.def_path_str(id))
-                        }
-                        AssocItemContainer::Impl => (false, String::new()),
-                    };
-
-                    let mut err = self.tcx().dcx().create_err(ButCallingIntroduces {
-                        param_ty_span: param.param_ty_span,
-                        cause_span: cause.span,
-                        has_param_name: simple_ident.is_some(),
-                        param_name: simple_ident.map(|x| x.to_string()).unwrap_or_default(),
-                        has_lifetime: sup_r.has_name(),
-                        lifetime: sup_r.to_string(),
-                        assoc_item: ctxt.assoc_item.name,
-                        has_impl_path,
-                        impl_path,
-                    });
-                    if self.find_impl_on_dyn_trait(&mut err, param.param_ty, ctxt) {
-                        let reported = err.emit();
-                        return Some(reported);
-                    } else {
-                        err.cancel()
-                    }
-                }
-                return None;
-            }
             _ => return None,
         };
         debug!(
@@ -198,25 +149,14 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
         let fn_returns = tcx.return_type_impl_or_dyn_traits(anon_reg_sup.scope);
 
         let mut override_error_code = None;
-        if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sup_origin
-            && let ObligationCauseCode::UnifyReceiver(ctxt) = cause.code()
-            // Handle case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a
-            // `'static` lifetime when called as a method on a binding: `bar.qux()`.
-            && self.find_impl_on_dyn_trait(&mut err, param.param_ty, ctxt)
-        {
-            override_error_code = Some(ctxt.assoc_item.name);
-        }
 
         if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sub_origin
             && let code = match cause.code() {
                 ObligationCauseCode::MatchImpl(parent, ..) => parent.code(),
                 _ => cause.code(),
             }
-            && let (
-                &ObligationCauseCode::WhereClause(item_def_id, _)
-                | &ObligationCauseCode::WhereClauseInExpr(item_def_id, ..),
-                None,
-            ) = (code, override_error_code)
+            && let &ObligationCauseCode::WhereClause(item_def_id, _)
+            | &ObligationCauseCode::WhereClauseInExpr(item_def_id, ..) = code
         {
             // Same case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a `'static`
             // lifetime as above, but called using a fully-qualified path to the method:
@@ -230,7 +170,9 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
                 override_error_code = Some(ident.name);
             }
         }
-        if let (Some(ident), true) = (override_error_code, fn_returns.is_empty()) {
+        if let Some(ident) = override_error_code
+            && fn_returns.is_empty()
+        {
             // Provide a more targeted error code and description.
             let retarget_subdiag = MoreTargeted { ident };
             retarget_subdiag.add_to_diag(&mut err);
@@ -495,8 +437,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
                         kind: ItemKind::Impl(hir::Impl { self_ty, .. }), ..
                     }) = tcx.hir_node_by_def_id(impl_did)
                         && trait_objects.iter().all(|did| {
-                            // FIXME: we should check `self_ty` against the receiver
-                            // type in the `UnifyReceiver` context, but for now, use
+                            // FIXME: we should check `self_ty`, but for now, use
                             // this imperfect proxy. This will fail if there are
                             // multiple `impl`s for the same trait like
                             // `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
@@ -517,41 +458,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
         }
     }
 
-    /// When we call a method coming from an `impl Foo for dyn Bar`, `dyn Bar` introduces a default
-    /// `'static` obligation. Suggest relaxing that implicit bound.
-    fn find_impl_on_dyn_trait(
-        &self,
-        err: &mut Diag<'_>,
-        ty: Ty<'_>,
-        ctxt: &UnifyReceiverContext<'tcx>,
-    ) -> bool {
-        let tcx = self.tcx();
-
-        // Find the method being called.
-        let Ok(Some(instance)) = ty::Instance::try_resolve(
-            tcx,
-            self.cx.typing_env(ctxt.param_env),
-            ctxt.assoc_item.def_id,
-            self.cx.resolve_vars_if_possible(ctxt.args),
-        ) else {
-            return false;
-        };
-
-        let mut v = TraitObjectVisitor(FxIndexSet::default());
-        v.visit_ty(ty);
-
-        // Get the `Ident` of the method being called and the corresponding `impl` (to point at
-        // `Bar` in `impl Foo for dyn Bar {}` and the definition of the method being called).
-        let Some((ident, self_ty)) =
-            NiceRegionError::get_impl_ident_and_self_ty_from_trait(tcx, instance.def_id(), &v.0)
-        else {
-            return false;
-        };
-
-        // Find the trait object types in the argument, so we point at *only* the trait object.
-        self.suggest_constrain_dyn_trait_in_impl(err, &v.0, ident, self_ty)
-    }
-
     fn suggest_constrain_dyn_trait_in_impl(
         &self,
         err: &mut Diag<'_>,
diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs
index 6eeb47a21f8..496a28cd39f 100644
--- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs
+++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs
@@ -2680,7 +2680,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
             | ObligationCauseCode::IntrinsicType
             | ObligationCauseCode::MethodReceiver
             | ObligationCauseCode::ReturnNoExpression
-            | ObligationCauseCode::UnifyReceiver(..)
             | ObligationCauseCode::Misc
             | ObligationCauseCode::WellFormed(..)
             | ObligationCauseCode::MatchImpl(..)