Skip to content

Commit a714cee

Browse files
Remove UnifyReceiver cause code
1 parent 9f48ded commit a714cee

File tree

4 files changed

+12
-126
lines changed

4 files changed

+12
-126
lines changed

compiler/rustc_hir_typeck/src/method/confirm.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hir_analysis::hir_ty_lowering::{
1111
};
1212
use rustc_infer::infer::{self, DefineOpaqueTypes, InferOk};
1313
use rustc_lint::builtin::SUPERTRAIT_ITEM_SHADOWING_USAGE;
14-
use rustc_middle::traits::{ObligationCauseCode, UnifyReceiverContext};
14+
use rustc_middle::traits::ObligationCauseCode;
1515
use rustc_middle::ty::adjustment::{
1616
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCoercion,
1717
};
@@ -136,7 +136,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
136136
"confirm: self_ty={:?} method_sig_rcvr={:?} method_sig={:?} method_predicates={:?}",
137137
self_ty, method_sig_rcvr, method_sig, method_predicates
138138
);
139-
self.unify_receivers(self_ty, method_sig_rcvr, pick, all_args);
139+
self.unify_receivers(self_ty, method_sig_rcvr, pick);
140140

141141
let (method_sig, method_predicates) =
142142
self.normalize(self.span, (method_sig, method_predicates));
@@ -525,20 +525,12 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
525525
self_ty: Ty<'tcx>,
526526
method_self_ty: Ty<'tcx>,
527527
pick: &probe::Pick<'tcx>,
528-
args: GenericArgsRef<'tcx>,
529528
) {
530529
debug!(
531530
"unify_receivers: self_ty={:?} method_self_ty={:?} span={:?} pick={:?}",
532531
self_ty, method_self_ty, self.span, pick
533532
);
534-
let cause = self.cause(
535-
self.self_expr.span,
536-
ObligationCauseCode::UnifyReceiver(Box::new(UnifyReceiverContext {
537-
assoc_item: pick.item,
538-
param_env: self.param_env,
539-
args,
540-
})),
541-
);
533+
let cause = self.cause(self.self_expr.span, ObligationCauseCode::Misc);
542534
match self.at(&cause, self.param_env).sup(DefineOpaqueTypes::Yes, method_self_ty, self_ty) {
543535
Ok(InferOk { obligations, value: () }) => {
544536
self.register_predicates(obligations);

compiler/rustc_middle/src/traits/mod.rs

-11
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,6 @@ impl<'tcx> ObligationCause<'tcx> {
143143
}
144144
}
145145
}
146-
147-
#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
148-
#[derive(TypeVisitable, TypeFoldable)]
149-
pub struct UnifyReceiverContext<'tcx> {
150-
pub assoc_item: ty::AssocItem,
151-
pub param_env: ty::ParamEnv<'tcx>,
152-
pub args: GenericArgsRef<'tcx>,
153-
}
154-
155146
#[derive(Clone, PartialEq, Eq, Default, HashStable)]
156147
#[derive(TypeVisitable, TypeFoldable, TyEncodable, TyDecodable)]
157148
pub struct InternedObligationCauseCode<'tcx> {
@@ -359,8 +350,6 @@ pub enum ObligationCauseCode<'tcx> {
359350
/// Method receiver
360351
MethodReceiver,
361352

362-
UnifyReceiver(Box<UnifyReceiverContext<'tcx>>),
363-
364353
/// `return` with no expression
365354
ReturnNoExpression,
366355

compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/static_impl_trait.rs

+9-103
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@ use rustc_hir::{
88
self as hir, AmbigArg, GenericBound, GenericParam, GenericParamKind, Item, ItemKind, Lifetime,
99
LifetimeName, LifetimeParamKind, MissingLifetimeKind, Node, TyKind,
1010
};
11-
use rustc_middle::ty::{
12-
self, AssocItemContainer, StaticLifetimeVisitor, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor,
13-
};
11+
use rustc_middle::ty::{self, StaticLifetimeVisitor, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor};
1412
use rustc_span::def_id::LocalDefId;
1513
use rustc_span::{Ident, Span};
1614
use tracing::debug;
1715

1816
use crate::error_reporting::infer::nice_region_error::NiceRegionError;
1917
use crate::errors::{
20-
ButCallingIntroduces, ButNeedsToSatisfy, DynTraitConstraintSuggestion, MoreTargeted,
21-
ReqIntroducedLocations,
18+
ButNeedsToSatisfy, DynTraitConstraintSuggestion, MoreTargeted, ReqIntroducedLocations,
2219
};
2320
use crate::infer::{RegionResolutionError, SubregionOrigin, TypeTrace};
24-
use crate::traits::{ObligationCauseCode, UnifyReceiverContext};
21+
use crate::traits::ObligationCauseCode;
2522

2623
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
2724
/// 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> {
3936
sup_r,
4037
spans,
4138
) if sub_r.is_static() => (var_origin, sub_origin, sub_r, sup_origin, sup_r, spans),
42-
RegionResolutionError::ConcreteFailure(
43-
SubregionOrigin::Subtype(box TypeTrace { cause, .. }),
44-
sub_r,
45-
sup_r,
46-
) if sub_r.is_static() => {
47-
// This is for an implicit `'static` requirement coming from `impl dyn Trait {}`.
48-
if let ObligationCauseCode::UnifyReceiver(ctxt) = cause.code() {
49-
// This may have a closure and it would cause ICE
50-
// through `find_param_with_region` (#78262).
51-
let anon_reg_sup = tcx.is_suitable_region(self.generic_param_scope, *sup_r)?;
52-
let fn_returns = tcx.return_type_impl_or_dyn_traits(anon_reg_sup.scope);
53-
if fn_returns.is_empty() {
54-
return None;
55-
}
56-
57-
let param = self.find_param_with_region(*sup_r, *sub_r)?;
58-
let simple_ident = param.param.pat.simple_ident();
59-
60-
let (has_impl_path, impl_path) = match ctxt.assoc_item.container {
61-
AssocItemContainer::Trait => {
62-
let id = ctxt.assoc_item.container_id(tcx);
63-
(true, tcx.def_path_str(id))
64-
}
65-
AssocItemContainer::Impl => (false, String::new()),
66-
};
67-
68-
let mut err = self.tcx().dcx().create_err(ButCallingIntroduces {
69-
param_ty_span: param.param_ty_span,
70-
cause_span: cause.span,
71-
has_param_name: simple_ident.is_some(),
72-
param_name: simple_ident.map(|x| x.to_string()).unwrap_or_default(),
73-
has_lifetime: sup_r.has_name(),
74-
lifetime: sup_r.to_string(),
75-
assoc_item: ctxt.assoc_item.name,
76-
has_impl_path,
77-
impl_path,
78-
});
79-
if self.find_impl_on_dyn_trait(&mut err, param.param_ty, ctxt) {
80-
let reported = err.emit();
81-
return Some(reported);
82-
} else {
83-
err.cancel()
84-
}
85-
}
86-
return None;
87-
}
8839
_ => return None,
8940
};
9041
debug!(
@@ -198,25 +149,14 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
198149
let fn_returns = tcx.return_type_impl_or_dyn_traits(anon_reg_sup.scope);
199150

200151
let mut override_error_code = None;
201-
if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sup_origin
202-
&& let ObligationCauseCode::UnifyReceiver(ctxt) = cause.code()
203-
// Handle case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a
204-
// `'static` lifetime when called as a method on a binding: `bar.qux()`.
205-
&& self.find_impl_on_dyn_trait(&mut err, param.param_ty, ctxt)
206-
{
207-
override_error_code = Some(ctxt.assoc_item.name);
208-
}
209152

210153
if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sub_origin
211154
&& let code = match cause.code() {
212155
ObligationCauseCode::MatchImpl(parent, ..) => parent.code(),
213156
_ => cause.code(),
214157
}
215-
&& let (
216-
&ObligationCauseCode::WhereClause(item_def_id, _)
217-
| &ObligationCauseCode::WhereClauseInExpr(item_def_id, ..),
218-
None,
219-
) = (code, override_error_code)
158+
&& let &ObligationCauseCode::WhereClause(item_def_id, _)
159+
| &ObligationCauseCode::WhereClauseInExpr(item_def_id, ..) = code
220160
{
221161
// Same case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a `'static`
222162
// lifetime as above, but called using a fully-qualified path to the method:
@@ -230,7 +170,9 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
230170
override_error_code = Some(ident.name);
231171
}
232172
}
233-
if let (Some(ident), true) = (override_error_code, fn_returns.is_empty()) {
173+
if let Some(ident) = override_error_code
174+
&& fn_returns.is_empty()
175+
{
234176
// Provide a more targeted error code and description.
235177
let retarget_subdiag = MoreTargeted { ident };
236178
retarget_subdiag.add_to_diag(&mut err);
@@ -495,8 +437,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
495437
kind: ItemKind::Impl(hir::Impl { self_ty, .. }), ..
496438
}) = tcx.hir_node_by_def_id(impl_did)
497439
&& trait_objects.iter().all(|did| {
498-
// FIXME: we should check `self_ty` against the receiver
499-
// type in the `UnifyReceiver` context, but for now, use
440+
// FIXME: we should check `self_ty`, but for now, use
500441
// this imperfect proxy. This will fail if there are
501442
// multiple `impl`s for the same trait like
502443
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
@@ -517,41 +458,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
517458
}
518459
}
519460

520-
/// When we call a method coming from an `impl Foo for dyn Bar`, `dyn Bar` introduces a default
521-
/// `'static` obligation. Suggest relaxing that implicit bound.
522-
fn find_impl_on_dyn_trait(
523-
&self,
524-
err: &mut Diag<'_>,
525-
ty: Ty<'_>,
526-
ctxt: &UnifyReceiverContext<'tcx>,
527-
) -> bool {
528-
let tcx = self.tcx();
529-
530-
// Find the method being called.
531-
let Ok(Some(instance)) = ty::Instance::try_resolve(
532-
tcx,
533-
self.cx.typing_env(ctxt.param_env),
534-
ctxt.assoc_item.def_id,
535-
self.cx.resolve_vars_if_possible(ctxt.args),
536-
) else {
537-
return false;
538-
};
539-
540-
let mut v = TraitObjectVisitor(FxIndexSet::default());
541-
v.visit_ty(ty);
542-
543-
// Get the `Ident` of the method being called and the corresponding `impl` (to point at
544-
// `Bar` in `impl Foo for dyn Bar {}` and the definition of the method being called).
545-
let Some((ident, self_ty)) =
546-
NiceRegionError::get_impl_ident_and_self_ty_from_trait(tcx, instance.def_id(), &v.0)
547-
else {
548-
return false;
549-
};
550-
551-
// Find the trait object types in the argument, so we point at *only* the trait object.
552-
self.suggest_constrain_dyn_trait_in_impl(err, &v.0, ident, self_ty)
553-
}
554-
555461
fn suggest_constrain_dyn_trait_in_impl(
556462
&self,
557463
err: &mut Diag<'_>,

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2681,7 +2681,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
26812681
| ObligationCauseCode::IntrinsicType
26822682
| ObligationCauseCode::MethodReceiver
26832683
| ObligationCauseCode::ReturnNoExpression
2684-
| ObligationCauseCode::UnifyReceiver(..)
26852684
| ObligationCauseCode::Misc
26862685
| ObligationCauseCode::WellFormed(..)
26872686
| ObligationCauseCode::MatchImpl(..)

0 commit comments

Comments
 (0)