Skip to content

Commit 3d6b09e

Browse files
committed
Keep obligation chain when elaborating obligations
1 parent f6e6d2a commit 3d6b09e

File tree

6 files changed

+59
-14
lines changed

6 files changed

+59
-14
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
692692
"auto trait is invoked with no method error, but no error reported?",
693693
);
694694
}
695-
Some(_) => unreachable!(),
695+
Some(Node::Item(hir::Item {
696+
ident, kind: hir::ItemKind::Trait(..), ..
697+
})) => {
698+
skip_list.insert(p);
699+
let entry = spanned_predicates.entry(ident.span);
700+
let entry = entry.or_insert_with(|| {
701+
(FxHashSet::default(), FxHashSet::default(), Vec::new())
702+
});
703+
entry.0.insert(cause.span);
704+
entry.1.insert((ident.span, ""));
705+
entry.1.insert((cause.span, "unsatisfied trait bound introduced here"));
706+
entry.2.push(p);
707+
}
708+
Some(node) => unreachable!("encountered `{node:?}`"),
696709
None => (),
697710
}
698711
}

compiler/rustc_infer/src/traits/util.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use smallvec::smallvec;
22

33
use crate::infer::outlives::components::{push_outlives_components, Component};
4-
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
4+
use crate::traits::{self, Obligation, ObligationCause, PredicateObligation};
55
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
66
use rustc_middle::ty::{self, ToPredicate, TyCtxt};
77
use rustc_span::symbol::Ident;
@@ -145,16 +145,28 @@ impl<'tcx> Elaborator<'tcx> {
145145
// Get predicates declared on the trait.
146146
let predicates = tcx.super_predicates_of(data.def_id());
147147

148-
let obligations = predicates.predicates.iter().map(|&(mut pred, _)| {
148+
let obligations = predicates.predicates.iter().map(|&(mut pred, span)| {
149149
// when parent predicate is non-const, elaborate it to non-const predicates.
150150
if data.constness == ty::BoundConstness::NotConst {
151151
pred = pred.without_const(tcx);
152152
}
153153

154+
let cause = obligation.cause.clone().derived_cause(
155+
bound_predicate.rebind(data),
156+
|derived| {
157+
traits::ImplDerivedObligation(Box::new(
158+
traits::ImplDerivedObligationCause {
159+
derived,
160+
impl_def_id: data.def_id(),
161+
span,
162+
},
163+
))
164+
},
165+
);
154166
predicate_obligation(
155167
pred.subst_supertrait(tcx, &bound_predicate.rebind(data.trait_ref)),
156168
obligation.param_env,
157-
obligation.cause.clone(),
169+
cause,
158170
)
159171
});
160172
debug!(?data, ?obligations, "super_predicates");

tests/ui/associated-types/issue-43784-associated-type.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0277]: the trait bound `T: Copy` is not satisfied
44
LL | type Assoc = T;
55
| ^ the trait `Copy` is not implemented for `T`
66
|
7+
note: required for `<T as Complete>::Assoc` to implement `Partial<T>`
8+
--> $DIR/issue-43784-associated-type.rs:1:11
9+
|
10+
LL | pub trait Partial<X: ?Sized>: Copy {
11+
| ^^^^^^^
712
note: required by a bound in `Complete::Assoc`
813
--> $DIR/issue-43784-associated-type.rs:5:17
914
|

tests/ui/derives/issue-91550.stderr

+19-10
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ LL | struct Object<T>(T);
3636
LL | foo.use_eq();
3737
| ^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
3838
|
39-
note: the following trait bounds were not satisfied:
40-
`NoDerives: Eq`
41-
`NoDerives: PartialEq`
39+
note: trait bound `NoDerives: Eq` was not satisfied
4240
--> $DIR/issue-91550.rs:15:9
4341
|
4442
LL | impl<T: Eq> Object<T> {
4543
| ^^ ---------
4644
| |
4745
| unsatisfied trait bound introduced here
46+
= note: the following trait bounds were not satisfied:
47+
`NoDerives: PartialEq`
48+
which is required by `NoDerives: Eq`
4849
help: consider annotating `NoDerives` with `#[derive(Eq, PartialEq)]`
4950
|
5051
LL | #[derive(Eq, PartialEq)]
@@ -67,17 +68,20 @@ LL | struct Object<T>(T);
6768
LL | foo.use_ord();
6869
| ^^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
6970
|
70-
note: the following trait bounds were not satisfied:
71-
`NoDerives: Eq`
72-
`NoDerives: Ord`
73-
`NoDerives: PartialEq`
74-
`NoDerives: PartialOrd`
71+
note: trait bound `NoDerives: Ord` was not satisfied
7572
--> $DIR/issue-91550.rs:18:9
7673
|
7774
LL | impl<T: Ord> Object<T> {
7875
| ^^^ ---------
7976
| |
8077
| unsatisfied trait bound introduced here
78+
= note: the following trait bounds were not satisfied:
79+
`NoDerives: PartialOrd`
80+
which is required by `NoDerives: Ord`
81+
`NoDerives: PartialEq`
82+
which is required by `NoDerives: Ord`
83+
`NoDerives: Eq`
84+
which is required by `NoDerives: Ord`
8185
help: consider annotating `NoDerives` with `#[derive(Eq, Ord, PartialEq, PartialOrd)]`
8286
|
8387
LL | #[derive(Eq, Ord, PartialEq, PartialOrd)]
@@ -101,9 +105,7 @@ LL | foo.use_ord_and_partial_ord();
101105
| ^^^^^^^^^^^^^^^^^^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
102106
|
103107
note: the following trait bounds were not satisfied:
104-
`NoDerives: Eq`
105108
`NoDerives: Ord`
106-
`NoDerives: PartialEq`
107109
`NoDerives: PartialOrd`
108110
--> $DIR/issue-91550.rs:21:9
109111
|
@@ -112,6 +114,13 @@ LL | impl<T: Ord + PartialOrd> Object<T> {
112114
| | |
113115
| | unsatisfied trait bound introduced here
114116
| unsatisfied trait bound introduced here
117+
= note: the following trait bounds were not satisfied:
118+
`NoDerives: PartialEq`
119+
which is required by `NoDerives: Ord`
120+
`NoDerives: Eq`
121+
which is required by `NoDerives: Ord`
122+
`NoDerives: PartialEq`
123+
which is required by `NoDerives: PartialOrd`
115124
help: consider annotating `NoDerives` with `#[derive(Eq, Ord, PartialEq, PartialOrd)]`
116125
|
117126
LL | #[derive(Eq, Ord, PartialEq, PartialOrd)]

tests/ui/generic-associated-types/issue-74824.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ LL | type Copy<T>: Copy = Box<T>;
1717
| ^^^^^^ the trait `Clone` is not implemented for `T`
1818
|
1919
= note: required for `Box<T>` to implement `Clone`
20+
= note: required for `<Self as UnsafeCopy>::Copy<T>` to implement `Copy`
2021
note: required by a bound in `UnsafeCopy::Copy`
2122
--> $DIR/issue-74824.rs:6:19
2223
|

tests/ui/traits/issue-43784-supertrait.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0277]: the trait bound `T: Copy` is not satisfied
44
LL | impl<T> Complete for T {}
55
| ^ the trait `Copy` is not implemented for `T`
66
|
7+
note: required for `T` to implement `Partial`
8+
--> $DIR/issue-43784-supertrait.rs:1:11
9+
|
10+
LL | pub trait Partial: Copy {
11+
| ^^^^^^^
712
note: required by a bound in `Complete`
813
--> $DIR/issue-43784-supertrait.rs:4:21
914
|

0 commit comments

Comments
 (0)