Skip to content

Commit dad9ce9

Browse files
committed
Surface yet more trait obligations
1 parent 4a02504 commit dad9ce9

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
@@ -624,6 +624,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
624624
outlived_fr: RegionVid,
625625
) {
626626
let tcx = self.infcx.tcx;
627+
debug!(?code);
627628
let ObligationCauseCode::MethodCallConstraint(ty, call_span) = code else {
628629
return;
629630
};
@@ -638,10 +639,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
638639
) else {
639640
return;
640641
};
642+
debug!(?instance);
641643
let def_id = instance.def_id();
642644
let mut parent = tcx.parent(def_id);
643-
match tcx.def_kind(parent) {
644-
hir::def::DefKind::Impl { .. } => {}
645+
debug!(?def_id, ?parent);
646+
let trait_preds = match tcx.def_kind(parent) {
647+
hir::def::DefKind::Impl { .. } => &[],
645648
hir::def::DefKind::Trait => {
646649
let Some(ty) = args.get(0).and_then(|arg| arg.as_type()) else {
647650
return;
@@ -653,14 +656,18 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
653656
if let [def_id] = impls[..] {
654657
// The method we have is on the trait, but for `parent` we want to analyze the
655658
// relevant impl instead.
659+
let preds = tcx.predicates_of(parent).predicates;
656660
parent = def_id;
661+
preds
657662
} else {
658663
return;
659-
};
664+
}
660665
}
661666
_ => return,
662-
}
667+
};
668+
debug!(?def_id, ?parent);
663669
let ty = tcx.type_of(parent).instantiate_identity();
670+
debug!(?ty);
664671
if self.to_error_region(outlived_fr) != Some(tcx.lifetimes.re_static) {
665672
return;
666673
}
@@ -679,23 +686,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
679686
// ```
680687
let mut predicates: Vec<Span> = traits::elaborate(
681688
tcx,
682-
tcx.predicates_of(def_id)
683-
.predicates
684-
.iter()
685-
.map(|(p, sp)| (p.as_predicate(), *sp))
686-
.chain(
687-
tcx.predicates_of(parent)
688-
.predicates
689-
.iter()
690-
.map(|(p, sp)| (p.as_predicate(), *sp)),
691-
),
689+
tcx.predicates_of(def_id).predicates.iter().map(|(p, sp)| (p.as_predicate(), *sp)),
692690
)
691+
.chain(traits::elaborate(
692+
tcx,
693+
tcx.predicates_of(parent).predicates.iter().map(|(p, sp)| (p.as_predicate(), *sp)),
694+
))
695+
.chain(traits::elaborate(tcx, trait_preds.iter().map(|(p, sp)| (p.as_predicate(), *sp))))
693696
.filter_map(|(pred, pred_span)| {
694697
if let ty::PredicateKind::Clause(clause) = pred.kind().skip_binder()
695698
&& let ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(pred_ty, r)) = clause
696-
// Look for `'static` bounds
697699
&& r.kind() == ty::ReStatic
698-
// We only want bounds on `Self`
699700
&& (self.infcx.can_eq(self.param_env, ty, pred_ty)
700701
|| matches!(
701702
pred_ty.kind(),
@@ -707,6 +708,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
707708
}
708709
})
709710
.collect();
711+
debug!(?predicates);
710712

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

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)