Auto merge of #87668 - estebank:tweak-bound-output, r=oli-obk

Use note for pointing at bound introducing requirement

Modify output for pointing where a trait bound obligation is introduced in an E0277 from using a span label to using a note in order to always preserve order of the output:

Before:
```
error[E0277]: `<<Self as Case1>::A as Iterator>::Item` doesn't implement `Debug`
  --> $DIR/bounds-on-assoc-in-trait.rs:18:28
   |
LL |     type A: Iterator<Item: Debug>;
   |                            ^^^^^ `<<Self as Case1>::A as Iterator>::Item` cannot be formatted using `{:?}` because it doesn't implement `Debug`
   |
  ::: $SRC_DIR/core/src/fmt/mod.rs:LL:COL
   |
LL | pub trait Debug {
   | --------------- required by this bound in `Debug`
   |
   = help: the trait `Debug` is not implemented for `<<Self as Case1>::A as Iterator>::Item`
```
After:
```
error[E0277]: `<<Self as Case1>::A as Iterator>::Item` doesn't implement `Debug`
  --> $DIR/bounds-on-assoc-in-trait.rs:18:28
   |
LL |     type A: Iterator<Item: Debug>;
   |                            ^^^^^ `<<Self as Case1>::A as Iterator>::Item` cannot be formatted using `{:?}` because it doesn't implement `Debug`
   |
   = help: the trait `Debug` is not implemented for `<<Self as Case1>::A as Iterator>::Item`
note: required by a bound in `Debug`
  --> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
   |
LL | pub trait Debug {
   | ^^^^^^^^^^^^^^^ required by this bound in `Debug`
```
This commit is contained in:
bors 2021-08-17 06:59:49 +00:00
commit aa8f27bf4d
333 changed files with 3171 additions and 1929 deletions

View file

@ -1962,7 +1962,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
}
ObligationCauseCode::BindingObligation(item_def_id, span) => {
let item_name = tcx.def_path_str(item_def_id);
let msg = format!("required by this bound in `{}`", item_name);
let mut multispan = MultiSpan::from(span);
if let Some(ident) = tcx.opt_item_name(item_def_id) {
let sm = tcx.sess.source_map();
let same_line =
@ -1971,16 +1971,17 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
_ => true,
};
if !ident.span.overlaps(span) && !same_line {
err.span_label(ident.span, "required by a bound in this");
multispan
.push_span_label(ident.span, "required by a bound in this".to_string());
}
}
let descr = format!("required by a bound in `{}`", item_name);
if span != DUMMY_SP {
err.span_label(span, &msg);
let msg = format!("required by this bound in `{}`", item_name);
multispan.push_span_label(span, msg);
err.span_note(multispan, &descr);
} else {
err.span_note(
tcx.def_span(item_def_id),
&format!("required by a bound in `{}`", item_name),
);
err.span_note(tcx.def_span(item_def_id), &descr);
}
}
ObligationCauseCode::ObjectCastObligation(object_ty) => {