Skip to content

Commit af44b3e

Browse files
committed
Surface yet more trait obligations
1 parent 6f0a72c commit af44b3e

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
@@ -614,6 +614,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
614614
outlived_fr: RegionVid,
615615
) {
616616
let tcx = self.infcx.tcx;
617+
debug!(?code);
617618
let ObligationCauseCode::MethodCallConstraint(ty, call_span) = code else {
618619
return;
619620
};
@@ -628,10 +629,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
628629
) else {
629630
return;
630631
};
632+
debug!(?instance);
631633
let def_id = instance.def_id();
632634
let mut parent = tcx.parent(def_id);
633-
match tcx.def_kind(parent) {
634-
hir::def::DefKind::Impl { .. } => {}
635+
debug!(?def_id, ?parent);
636+
let trait_preds = match tcx.def_kind(parent) {
637+
hir::def::DefKind::Impl { .. } => &[],
635638
hir::def::DefKind::Trait => {
636639
let Some(ty) = args.get(0).and_then(|arg| arg.as_type()) else {
637640
return;
@@ -643,14 +646,18 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
643646
if let [def_id] = impls[..] {
644647
// The method we have is on the trait, but for `parent` we want to analyze the
645648
// relevant impl instead.
649+
let preds = tcx.predicates_of(parent).predicates;
646650
parent = def_id;
651+
preds
647652
} else {
648653
return;
649-
};
654+
}
650655
}
651656
_ => return,
652-
}
657+
};
658+
debug!(?def_id, ?parent);
653659
let ty = tcx.type_of(parent).instantiate_identity();
660+
debug!(?ty);
654661
if self.to_error_region(outlived_fr) != Some(tcx.lifetimes.re_static) {
655662
return;
656663
}
@@ -669,23 +676,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
669676
// ```
670677
let mut predicates: Vec<Span> = traits::elaborate(
671678
tcx,
672-
tcx.predicates_of(def_id)
673-
.predicates
674-
.iter()
675-
.map(|(p, sp)| (p.as_predicate(), *sp))
676-
.chain(
677-
tcx.predicates_of(parent)
678-
.predicates
679-
.iter()
680-
.map(|(p, sp)| (p.as_predicate(), *sp)),
681-
),
679+
tcx.predicates_of(def_id).predicates.iter().map(|(p, sp)| (p.as_predicate(), *sp)),
682680
)
681+
.chain(traits::elaborate(
682+
tcx,
683+
tcx.predicates_of(parent).predicates.iter().map(|(p, sp)| (p.as_predicate(), *sp)),
684+
))
685+
.chain(traits::elaborate(tcx, trait_preds.iter().map(|(p, sp)| (p.as_predicate(), *sp))))
683686
.filter_map(|(pred, pred_span)| {
684687
if let ty::PredicateKind::Clause(clause) = pred.kind().skip_binder()
685688
&& let ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(pred_ty, r)) = clause
686-
// Look for `'static` bounds
687689
&& r.kind() == ty::ReStatic
688-
// We only want bounds on `Self`
689690
&& (self.infcx.can_eq(self.param_env, ty, pred_ty)
690691
|| matches!(
691692
pred_ty.kind(),
@@ -697,6 +698,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
697698
}
698699
})
699700
.collect();
701+
debug!(?predicates);
700702

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

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)