Skip to content

Commit e7497a8

Browse files
committed
Auto merge of rust-lang#69793 - estebank:predicate-obligations-4, r=nikomatsakis
Maintain chain of derived obligations When evaluating the derived obligations from super traits, maintain a reference to the original obligation in order to give more actionable context in the output. Continuation (and built on) rust-lang#69745, subset of rust-lang#69709. r? @eddyb
2 parents a0e52b1 + d9a5419 commit e7497a8

File tree

55 files changed

+287
-313
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+287
-313
lines changed

src/librustc_middle/traits/mod.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ pub enum ObligationCauseCode<'tcx> {
191191

192192
ImplDerivedObligation(DerivedObligationCause<'tcx>),
193193

194+
DerivedObligation(DerivedObligationCause<'tcx>),
195+
194196
/// Error derived when matching traits/impls; see ObligationCause for more details
195197
CompareImplMethodObligation {
196198
item_name: ast::Name,
@@ -257,28 +259,22 @@ pub enum ObligationCauseCode<'tcx> {
257259

258260
/// #[feature(trivial_bounds)] is not enabled
259261
TrivialBound,
260-
261-
AssocTypeBound(Box<AssocTypeBoundData>),
262262
}
263263

264264
impl ObligationCauseCode<'_> {
265265
// Return the base obligation, ignoring derived obligations.
266266
pub fn peel_derives(&self) -> &Self {
267267
let mut base_cause = self;
268-
while let BuiltinDerivedObligation(cause) | ImplDerivedObligation(cause) = base_cause {
268+
while let BuiltinDerivedObligation(cause)
269+
| ImplDerivedObligation(cause)
270+
| DerivedObligation(cause) = base_cause
271+
{
269272
base_cause = &cause.parent_code;
270273
}
271274
base_cause
272275
}
273276
}
274277

275-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
276-
pub struct AssocTypeBoundData {
277-
pub impl_span: Option<Span>,
278-
pub original: Span,
279-
pub bounds: Vec<Span>,
280-
}
281-
282278
// `ObligationCauseCode` is used a lot. Make sure it doesn't unintentionally get bigger.
283279
#[cfg(target_arch = "x86_64")]
284280
static_assert_size!(ObligationCauseCode<'_>, 32);

src/librustc_middle/traits/structural_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
456456
super::ImplDerivedObligation(ref cause) => {
457457
tcx.lift(cause).map(super::ImplDerivedObligation)
458458
}
459+
super::DerivedObligation(ref cause) => tcx.lift(cause).map(super::DerivedObligation),
459460
super::CompareImplMethodObligation {
460461
item_name,
461462
impl_item_def_id,
@@ -501,7 +502,6 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
501502
super::MethodReceiver => Some(super::MethodReceiver),
502503
super::BlockTailExpression(id) => Some(super::BlockTailExpression(id)),
503504
super::TrivialBound => Some(super::TrivialBound),
504-
super::AssocTypeBound(ref data) => Some(super::AssocTypeBound(data.clone())),
505505
}
506506
}
507507
}

src/librustc_trait_selection/traits/error_reporting/on_unimplemented.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
134134

135135
match obligation.cause.code {
136136
ObligationCauseCode::BuiltinDerivedObligation(..)
137-
| ObligationCauseCode::ImplDerivedObligation(..) => {}
137+
| ObligationCauseCode::ImplDerivedObligation(..)
138+
| ObligationCauseCode::DerivedObligation(..) => {}
138139
_ => {
139140
// this is a "direct", user-specified, rather than derived,
140141
// obligation.

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11351135
while let Some(code) = next_code {
11361136
debug!("maybe_note_obligation_cause_for_async_await: code={:?}", code);
11371137
match code {
1138-
ObligationCauseCode::BuiltinDerivedObligation(derived_obligation)
1138+
ObligationCauseCode::DerivedObligation(derived_obligation)
1139+
| ObligationCauseCode::BuiltinDerivedObligation(derived_obligation)
11391140
| ObligationCauseCode::ImplDerivedObligation(derived_obligation) => {
11401141
let ty = derived_obligation.parent_trait_ref.self_ty();
11411142
debug!(
@@ -1531,14 +1532,14 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
15311532
let item_name = tcx.def_path_str(item_def_id);
15321533
let msg = format!("required by this bound in `{}`", item_name);
15331534
if let Some(ident) = tcx.opt_item_name(item_def_id) {
1534-
let sm = self.tcx.sess.source_map();
1535+
let sm = tcx.sess.source_map();
15351536
let same_line =
15361537
match (sm.lookup_line(ident.span.hi()), sm.lookup_line(span.lo())) {
15371538
(Ok(l), Ok(r)) => l.line == r.line,
15381539
_ => true,
15391540
};
15401541
if !ident.span.overlaps(span) && !same_line {
1541-
err.span_label(ident.span, "");
1542+
err.span_label(ident.span, "required by a bound in this");
15421543
}
15431544
}
15441545
if span != DUMMY_SP {
@@ -1661,6 +1662,16 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
16611662
obligated_types,
16621663
);
16631664
}
1665+
ObligationCauseCode::DerivedObligation(ref data) => {
1666+
let parent_trait_ref = self.resolve_vars_if_possible(&data.parent_trait_ref);
1667+
let parent_predicate = parent_trait_ref.without_const().to_predicate();
1668+
self.note_obligation_cause_code(
1669+
err,
1670+
&parent_predicate,
1671+
&data.parent_code,
1672+
obligated_types,
1673+
);
1674+
}
16641675
ObligationCauseCode::CompareImplMethodObligation { .. } => {
16651676
err.note(&format!(
16661677
"the requirement `{}` appears on the impl method \
@@ -1684,15 +1695,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
16841695
err.help("add `#![feature(trivial_bounds)]` to the crate attributes to enable");
16851696
}
16861697
}
1687-
ObligationCauseCode::AssocTypeBound(ref data) => {
1688-
err.span_label(data.original, "associated type defined here");
1689-
if let Some(sp) = data.impl_span {
1690-
err.span_label(sp, "in this `impl` item");
1691-
}
1692-
for sp in &data.bounds {
1693-
err.span_label(*sp, "restricted in this bound");
1694-
}
1695-
}
16961698
}
16971699
}
16981700

0 commit comments

Comments
 (0)