Skip to content

Commit 2a75eb8

Browse files
committed
Always look at the trait bounds (not just when looking at a trait DefId)
1 parent 6192d40 commit 2a75eb8

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
640640
let mut parent = tcx.parent(def_id);
641641
debug!(?def_id, ?parent);
642642
let trait_preds = match tcx.def_kind(parent) {
643-
hir::def::DefKind::Impl { .. } => &[],
643+
hir::def::DefKind::Impl { .. } => {
644+
tcx.trait_id_of_impl(parent).map_or(&[][..], |id| tcx.predicates_of(id).predicates)
645+
}
644646
hir::def::DefKind::Trait => {
645647
let Some(ty) = args.get(0).and_then(|arg| arg.as_type()) else {
646648
return;

tests/ui/lifetimes/static-impl-obligation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ mod w {
272272

273273
}
274274
fn convert<'a>(x: &'a &'a u32) {
275-
let y: &dyn Foo = x; //~ ERROR lifetime may not live long enough
276-
y.hello();
275+
let y: &dyn Foo = x;
276+
y.hello(); //~ ERROR lifetime may not live long enough
277277
}
278278
}
279279
fn main() {}

tests/ui/lifetimes/static-impl-obligation.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,20 @@ LL | impl Trait for dyn Foo + '_ {
438438
| ++++
439439

440440
error: lifetime may not live long enough
441-
--> $DIR/static-impl-obligation.rs:275:27
441+
--> $DIR/static-impl-obligation.rs:276:9
442442
|
443443
LL | fn convert<'a>(x: &'a &'a u32) {
444444
| -- lifetime `'a` defined here
445445
LL | let y: &dyn Foo = x;
446-
| ^ cast requires that `'a` must outlive `'static`
446+
| - cast requires that `'a` must outlive `'static`
447+
LL | y.hello();
448+
| ^^^^^^^^^ calling this method introduces a `'static` lifetime requirement
449+
|
450+
note: the `impl` on `dyn w::Foo` has a `'static` lifetime requirement
451+
--> $DIR/static-impl-obligation.rs:268:29
452+
|
453+
LL | trait Trait where Self: 'static { fn hello(&self) {} }
454+
| ^^^^^^^
447455

448456
error: aborting due to 25 previous errors
449457

0 commit comments

Comments
 (0)