Skip to content

Commit 356d9b7

Browse files
Rollup merge of rust-lang#137183 - compiler-errors:dead-regionck-code, r=lcnr
Prune dead regionck code We never encounter `ObligationCauseCode`s that correspond to region obligations that originate from "within" a body, since we don't do HIR regionck anymore on bodies. So prune some dead code.
2 parents 0402a04 + b2b2e53 commit 356d9b7

File tree

7 files changed

+9
-241
lines changed

7 files changed

+9
-241
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

-10
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,6 @@ impl<'tcx> ObligationCause<'tcx> {
144144
}
145145
}
146146

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-
155147
/// A compact form of `ObligationCauseCode`.
156148
#[derive(Clone, PartialEq, Eq, Default, HashStable)]
157149
#[derive(TypeVisitable, TypeFoldable, TyEncodable, TyDecodable)]
@@ -360,8 +352,6 @@ pub enum ObligationCauseCode<'tcx> {
360352
/// Method receiver
361353
MethodReceiver,
362354

363-
UnifyReceiver(Box<UnifyReceiverContext<'tcx>>),
364-
365355
/// `return` with no expression
366356
ReturnNoExpression,
367357

compiler/rustc_trait_selection/messages.ftl

-8
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,6 @@ trait_selection_mismatched_static_lifetime = incompatible lifetime on type
225225
trait_selection_missing_options_for_on_unimplemented_attr = missing options for `on_unimplemented` attribute
226226
.help = at least one of the `message`, `note` and `label` options are expected
227227
228-
trait_selection_more_targeted = {$has_param_name ->
229-
[true] `{$param_name}`
230-
*[false] `fn` parameter
231-
} has {$has_lifetime ->
232-
[true] lifetime `{$lifetime}`
233-
*[false] an anonymous lifetime `'_`
234-
} but calling `{$ident}` introduces an implicit `'static` lifetime requirement
235-
236228
trait_selection_msl_introduces_static = introduces a `'static` lifetime requirement
237229
trait_selection_msl_unmet_req = because this has an unmet lifetime requirement
238230

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
3333
};
3434
// If we added a "points at argument expression" obligation, we remove it here, we care
3535
// about the original obligation only.
36-
let code = match cause.code() {
37-
ObligationCauseCode::FunctionArg { parent_code, .. } => &*parent_code,
38-
code => code,
39-
};
40-
let ObligationCauseCode::MatchImpl(parent, impl_def_id) = code else {
36+
let ObligationCauseCode::MatchImpl(parent, impl_def_id) = cause.code() else {
4137
return None;
4238
};
4339
let (ObligationCauseCode::WhereClause(_, binding_span)

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

+5-187
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
//! Error Reporting for static impl Traits.
22
33
use rustc_data_structures::fx::FxIndexSet;
4-
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan, Subdiagnostic};
4+
use rustc_errors::{Applicability, Diag, ErrorGuaranteed};
55
use rustc_hir::def_id::DefId;
66
use rustc_hir::intravisit::{Visitor, VisitorExt, walk_ty};
77
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, 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;
19-
use crate::errors::{
20-
ButCallingIntroduces, ButNeedsToSatisfy, DynTraitConstraintSuggestion, MoreTargeted,
21-
ReqIntroducedLocations,
22-
};
23-
use crate::infer::{RegionResolutionError, SubregionOrigin, TypeTrace};
24-
use crate::traits::{ObligationCauseCode, UnifyReceiverContext};
17+
use crate::errors::ButNeedsToSatisfy;
18+
use crate::infer::{RegionResolutionError, SubregionOrigin};
2519

2620
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
2721
/// Print the error message for lifetime errors when the return type is a static `impl Trait`,
@@ -39,52 +33,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
3933
sup_r,
4034
spans,
4135
) 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-
}
8836
_ => return None,
8937
};
9038
debug!(
@@ -140,39 +88,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
14088
None
14189
};
14290

143-
let mut subdiag = None;
144-
145-
if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = sub_origin {
146-
if let ObligationCauseCode::ReturnValue(hir_id)
147-
| ObligationCauseCode::BlockTailExpression(hir_id, ..) = cause.code()
148-
{
149-
let parent_id = tcx.hir_get_parent_item(*hir_id);
150-
if let Some(fn_decl) = tcx.hir_fn_decl_by_hir_id(parent_id.into()) {
151-
let mut span: MultiSpan = fn_decl.output.span().into();
152-
let mut spans = Vec::new();
153-
let mut add_label = true;
154-
if let hir::FnRetTy::Return(ty) = fn_decl.output {
155-
let mut v = StaticLifetimeVisitor(vec![], tcx.hir());
156-
v.visit_ty_unambig(ty);
157-
if !v.0.is_empty() {
158-
span = v.0.clone().into();
159-
spans = v.0;
160-
add_label = false;
161-
}
162-
}
163-
let fn_decl_span = fn_decl.output.span();
164-
165-
subdiag = Some(ReqIntroducedLocations {
166-
span,
167-
spans,
168-
fn_decl_span,
169-
cause_span: cause.span,
170-
add_label,
171-
});
172-
}
173-
}
174-
}
175-
17691
let diag = ButNeedsToSatisfy {
17792
sp,
17893
influencer_point,
@@ -183,7 +98,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
18398
require_span_as_note: require_as_note.then_some(require_span),
18499
// We don't need a note, it's already at the end, it can be shown as a `span_label`.
185100
require_span_as_label: (!require_as_note).then_some(require_span),
186-
req_introduces_loc: subdiag,
187101

188102
has_lifetime: sup_r.has_name(),
189103
lifetime: lifetime_name.clone(),
@@ -197,45 +111,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
197111

198112
let fn_returns = tcx.return_type_impl_or_dyn_traits(anon_reg_sup.scope);
199113

200-
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-
}
209-
210-
if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sub_origin
211-
&& let code = match cause.code() {
212-
ObligationCauseCode::MatchImpl(parent, ..) => parent.code(),
213-
_ => cause.code(),
214-
}
215-
&& let (
216-
&ObligationCauseCode::WhereClause(item_def_id, _)
217-
| &ObligationCauseCode::WhereClauseInExpr(item_def_id, ..),
218-
None,
219-
) = (code, override_error_code)
220-
{
221-
// Same case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a `'static`
222-
// lifetime as above, but called using a fully-qualified path to the method:
223-
// `Foo::qux(bar)`.
224-
let mut v = TraitObjectVisitor(FxIndexSet::default());
225-
v.visit_ty(param.param_ty);
226-
if let Some((ident, self_ty)) =
227-
NiceRegionError::get_impl_ident_and_self_ty_from_trait(tcx, item_def_id, &v.0)
228-
&& self.suggest_constrain_dyn_trait_in_impl(&mut err, &v.0, ident, self_ty)
229-
{
230-
override_error_code = Some(ident.name);
231-
}
232-
}
233-
if let (Some(ident), true) = (override_error_code, fn_returns.is_empty()) {
234-
// Provide a more targeted error code and description.
235-
let retarget_subdiag = MoreTargeted { ident };
236-
retarget_subdiag.add_to_diag(&mut err);
237-
}
238-
239114
let arg = match param.param.pat.simple_ident() {
240115
Some(simple_ident) => format!("argument `{simple_ident}`"),
241116
None => "the argument".to_string(),
@@ -495,8 +370,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
495370
kind: ItemKind::Impl(hir::Impl { self_ty, .. }), ..
496371
}) = tcx.hir_node_by_def_id(impl_did)
497372
&& 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
373+
// FIXME: we should check `self_ty`, but for now, use
500374
// this imperfect proxy. This will fail if there are
501375
// multiple `impl`s for the same trait like
502376
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
@@ -516,62 +390,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
516390
_ => None,
517391
}
518392
}
519-
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-
555-
fn suggest_constrain_dyn_trait_in_impl(
556-
&self,
557-
err: &mut Diag<'_>,
558-
found_dids: &FxIndexSet<DefId>,
559-
ident: Ident,
560-
self_ty: &hir::Ty<'_>,
561-
) -> bool {
562-
let mut suggested = false;
563-
for found_did in found_dids {
564-
let mut traits = vec![];
565-
let mut hir_v = HirTraitObjectVisitor(&mut traits, *found_did);
566-
hir_v.visit_ty_unambig(self_ty);
567-
for &span in &traits {
568-
let subdiag = DynTraitConstraintSuggestion { span, ident };
569-
subdiag.add_to_diag(err);
570-
suggested = true;
571-
}
572-
}
573-
suggested
574-
}
575393
}
576394

577395
/// Collect all the trait objects in a type that could have received an implicit `'static` lifetime.

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

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

compiler/rustc_trait_selection/src/errors.rs

-19
Original file line numberDiff line numberDiff line change
@@ -1119,22 +1119,6 @@ impl Subdiagnostic for ReqIntroducedLocations {
11191119
}
11201120
}
11211121

1122-
pub struct MoreTargeted {
1123-
pub ident: Symbol,
1124-
}
1125-
1126-
impl Subdiagnostic for MoreTargeted {
1127-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
1128-
self,
1129-
diag: &mut Diag<'_, G>,
1130-
_f: &F,
1131-
) {
1132-
diag.code(E0772);
1133-
diag.primary_message(fluent::trait_selection_more_targeted);
1134-
diag.arg("ident", self.ident);
1135-
}
1136-
}
1137-
11381122
#[derive(Diagnostic)]
11391123
#[diag(trait_selection_but_needs_to_satisfy, code = E0759)]
11401124
pub struct ButNeedsToSatisfy {
@@ -1151,9 +1135,6 @@ pub struct ButNeedsToSatisfy {
11511135
#[note(trait_selection_introduced_by_bound)]
11521136
pub bound: Option<Span>,
11531137

1154-
#[subdiagnostic]
1155-
pub req_introduces_loc: Option<ReqIntroducedLocations>,
1156-
11571138
pub has_param_name: bool,
11581139
pub param_name: String,
11591140
pub spans_empty: bool,

0 commit comments

Comments
 (0)