Skip to content

Commit 6fde87c

Browse files
committed
Surface yet more trait obligations
1 parent d89dba1 commit 6fde87c

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
623623
outlived_fr: RegionVid,
624624
) {
625625
let tcx = self.infcx.tcx;
626+
debug!(?code);
626627
let ObligationCauseCode::MethodCallConstraint(ty, call_span) = code else {
627628
return;
628629
};
@@ -637,10 +638,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
637638
) else {
638639
return;
639640
};
641+
debug!(?instance);
640642
let def_id = instance.def_id();
641643
let mut parent = tcx.parent(def_id);
642-
match tcx.def_kind(parent) {
643-
hir::def::DefKind::Impl { .. } => {}
644+
debug!(?def_id, ?parent);
645+
let trait_preds = match tcx.def_kind(parent) {
646+
hir::def::DefKind::Impl { .. } => &[],
644647
hir::def::DefKind::Trait => {
645648
let Some(ty) = args.get(0).and_then(|arg| arg.as_type()) else {
646649
return;
@@ -652,14 +655,18 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
652655
if let [def_id] = impls[..] {
653656
// The method we have is on the trait, but for `parent` we want to analyze the
654657
// relevant impl instead.
658+
let preds = tcx.predicates_of(parent).predicates;
655659
parent = def_id;
660+
preds
656661
} else {
657662
return;
658-
};
663+
}
659664
}
660665
_ => return,
661-
}
666+
};
667+
debug!(?def_id, ?parent);
662668
let ty = tcx.type_of(parent).instantiate_identity();
669+
debug!(?ty);
663670
if self.to_error_region(outlived_fr) != Some(tcx.lifetimes.re_static) {
664671
return;
665672
}
@@ -678,23 +685,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
678685
// ```
679686
let mut predicates: Vec<Span> = traits::elaborate(
680687
tcx,
681-
tcx.predicates_of(def_id)
682-
.predicates
683-
.iter()
684-
.map(|(p, sp)| (p.as_predicate(), *sp))
685-
.chain(
686-
tcx.predicates_of(parent)
687-
.predicates
688-
.iter()
689-
.map(|(p, sp)| (p.as_predicate(), *sp)),
690-
),
688+
tcx.predicates_of(def_id).predicates.iter().map(|(p, sp)| (p.as_predicate(), *sp)),
691689
)
690+
.chain(traits::elaborate(
691+
tcx,
692+
tcx.predicates_of(parent).predicates.iter().map(|(p, sp)| (p.as_predicate(), *sp)),
693+
))
694+
.chain(traits::elaborate(tcx, trait_preds.iter().map(|(p, sp)| (p.as_predicate(), *sp))))
692695
.filter_map(|(pred, pred_span)| {
693696
if let ty::PredicateKind::Clause(clause) = pred.kind().skip_binder()
694697
&& let ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(pred_ty, r)) = clause
695-
// Look for `'static` bounds
696698
&& r.kind() == ty::ReStatic
697-
// We only want bounds on `Self`
698699
&& (self.infcx.can_eq(self.param_env, ty, pred_ty)
699700
|| matches!(
700701
pred_ty.kind(),
@@ -706,6 +707,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
706707
}
707708
})
708709
.collect();
710+
debug!(?predicates);
709711

710712
// Look at the receiver for `&'static self`, which introduces a `'static` obligation.
711713
// ```

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ LL | val.use_self()
2929
| `val` escapes the function body here
3030
| argument requires that `'a` must outlive `'static`
3131
|
32-
note: the `impl` on `dyn t::ObjectTrait` has a `'static` lifetime requirement
33-
--> $DIR/static-impl-obligation.rs:216:47
32+
note: the `impl` on `dyn t::ObjectTrait` has `'static` lifetime requirements
33+
--> $DIR/static-impl-obligation.rs:215:31
3434
|
35+
LL | trait MyTrait where Self: 'static {
36+
| ^^^^^^^
3537
LL | fn use_self(&self) -> &() where Self: 'static { panic!() }
3638
| ^^^^^^^
3739

@@ -76,8 +78,10 @@ LL | fn use_self(&'static self) -> &() { panic!() }
7678
LL | impl MyTrait for dyn ObjectTrait {}
7779
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
7880
note: the `impl` on `(dyn v::ObjectTrait + 'static)` has `'static` lifetime requirements
79-
--> $DIR/static-impl-obligation.rs:252:21
81+
--> $DIR/static-impl-obligation.rs:251:31
8082
|
83+
LL | trait MyTrait where Self: 'static {
84+
| ^^^^^^^
8185
LL | fn use_self(&'static self) -> &() { panic!() }
8286
| ^^^^^^^^^^^^^
8387
...

0 commit comments

Comments
 (0)