Skip to content

Commit 6192d40

Browse files
committed
Surface yet more trait obligations
1 parent 6b4f02f commit 6192d40

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
620620
outlived_fr: RegionVid,
621621
) {
622622
let tcx = self.infcx.tcx;
623+
debug!(?code);
623624
let ObligationCauseCode::MethodCallConstraint(ty, call_span) = code else {
624625
return;
625626
};
@@ -634,10 +635,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
634635
) else {
635636
return;
636637
};
638+
debug!(?instance);
637639
let def_id = instance.def_id();
638640
let mut parent = tcx.parent(def_id);
639-
match tcx.def_kind(parent) {
640-
hir::def::DefKind::Impl { .. } => {}
641+
debug!(?def_id, ?parent);
642+
let trait_preds = match tcx.def_kind(parent) {
643+
hir::def::DefKind::Impl { .. } => &[],
641644
hir::def::DefKind::Trait => {
642645
let Some(ty) = args.get(0).and_then(|arg| arg.as_type()) else {
643646
return;
@@ -649,14 +652,18 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
649652
if let [def_id] = impls[..] {
650653
// The method we have is on the trait, but for `parent` we want to analyze the
651654
// relevant impl instead.
655+
let preds = tcx.predicates_of(parent).predicates;
652656
parent = def_id;
657+
preds
653658
} else {
654659
return;
655-
};
660+
}
656661
}
657662
_ => return,
658-
}
663+
};
664+
debug!(?def_id, ?parent);
659665
let ty = tcx.type_of(parent).instantiate_identity();
666+
debug!(?ty);
660667
if self.to_error_region(outlived_fr) != Some(tcx.lifetimes.re_static) {
661668
return;
662669
}
@@ -675,23 +682,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
675682
// ```
676683
let mut predicates: Vec<Span> = traits::elaborate(
677684
tcx,
678-
tcx.predicates_of(def_id)
679-
.predicates
680-
.iter()
681-
.map(|(p, sp)| (p.as_predicate(), *sp))
682-
.chain(
683-
tcx.predicates_of(parent)
684-
.predicates
685-
.iter()
686-
.map(|(p, sp)| (p.as_predicate(), *sp)),
687-
),
685+
tcx.predicates_of(def_id).predicates.iter().map(|(p, sp)| (p.as_predicate(), *sp)),
688686
)
687+
.chain(traits::elaborate(
688+
tcx,
689+
tcx.predicates_of(parent).predicates.iter().map(|(p, sp)| (p.as_predicate(), *sp)),
690+
))
691+
.chain(traits::elaborate(tcx, trait_preds.iter().map(|(p, sp)| (p.as_predicate(), *sp))))
689692
.filter_map(|(pred, pred_span)| {
690693
if let ty::PredicateKind::Clause(clause) = pred.kind().skip_binder()
691694
&& let ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(pred_ty, r)) = clause
692-
// Look for `'static` bounds
693695
&& r.kind() == ty::ReStatic
694-
// We only want bounds on `Self`
695696
&& (self.infcx.can_eq(self.param_env, ty, pred_ty)
696697
|| matches!(
697698
pred_ty.kind(),
@@ -703,6 +704,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
703704
}
704705
})
705706
.collect();
707+
debug!(?predicates);
706708

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

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

+7-3
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)