Skip to content

Commit 8bebbe5

Browse files
committed
Tweak logic to account for methods implemented on trait
1 parent c95b515 commit 8bebbe5

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,26 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
628628
return;
629629
};
630630
let def_id = instance.def_id();
631-
let parent = tcx.parent(def_id);
632-
let hir::def::DefKind::Impl { .. } = tcx.def_kind(parent) else {
633-
return;
634-
};
635-
let ty = tcx.type_of(parent).instantiate(tcx, instance.args);
631+
let mut parent = tcx.parent(def_id);
632+
match tcx.def_kind(parent) {
633+
hir::def::DefKind::Impl { .. } => {}
634+
hir::def::DefKind::Trait => {
635+
let Some(ty) = args.get(0).and_then(|arg| arg.as_type()) else {
636+
return;
637+
};
638+
let mut impls = vec![];
639+
tcx.for_each_relevant_impl(parent, ty, |id| { impls.push(id); });
640+
if let [def_id] = impls[..] {
641+
// The method we have is on the trait, but for `parent` we want to analyze the
642+
// relevant impl instead.
643+
parent = def_id;
644+
} else {
645+
return;
646+
};
647+
}
648+
_ => return,
649+
}
650+
let ty = tcx.type_of(parent).instantiate_identity();
636651
if self.to_error_region(outlived_fr) != Some(tcx.lifetimes.re_static) {
637652
return;
638653
}
@@ -713,9 +728,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
713728
..
714729
}),
715730
..
716-
})) = tcx.hir().get_if_local(parent)
717-
&& let Some(hir::Node::ImplItem(hir::ImplItem { .. })) = tcx.hir().get_if_local(def_id)
718-
{
731+
})) = tcx.hir().get_if_local(parent) {
719732
let suggestion = match lt.res {
720733
hir::LifetimeName::ImplicitObjectLifetimeDefault if predicates.is_empty() => {
721734
// `impl dyn Trait {}`

tests/ui/suggestions/impl-on-dyn-trait-with-implicit-static-bound.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ LL | fn use_self(&self) -> &() { panic!() }
6969
...
7070
LL | impl MyTrait for dyn ObjectTrait {}
7171
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
72+
help: consider relaxing the implicit `'static` requirement on the impl
73+
|
74+
LL | impl MyTrait for dyn ObjectTrait + '_ {}
75+
| ++++
7276

7377
error[E0521]: borrowed data escapes outside of function
7478
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:112:9
@@ -91,6 +95,10 @@ LL | fn use_self(&self) -> &() { panic!() }
9195
...
9296
LL | impl MyTrait for dyn ObjectTrait {}
9397
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
98+
help: consider relaxing the implicit `'static` requirement on the impl
99+
|
100+
LL | impl MyTrait for dyn ObjectTrait + '_ {}
101+
| ++++
94102

95103
error[E0521]: borrowed data escapes outside of function
96104
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:38:9

0 commit comments

Comments
 (0)