Skip to content

Commit fa3eebb

Browse files
committed
Modify bounds_span to ignore bounds coming from a derive macro
1 parent e70105f commit fa3eebb

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

compiler/rustc_hir/src/hir.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_index::vec::IndexVec;
1717
use rustc_macros::HashStable_Generic;
1818
use rustc_span::source_map::Spanned;
1919
use rustc_span::symbol::{kw, sym, Ident, Symbol};
20-
use rustc_span::{def_id::LocalDefId, BytePos, MultiSpan, Span, DUMMY_SP};
20+
use rustc_span::{def_id::LocalDefId, BytePos, ExpnKind, MacroKind, MultiSpan, Span, DUMMY_SP};
2121
use rustc_target::asm::InlineAsmRegOrRegClass;
2222
use rustc_target::spec::abi::Abi;
2323

@@ -527,8 +527,17 @@ pub struct GenericParam<'hir> {
527527
impl GenericParam<'hir> {
528528
pub fn bounds_span(&self) -> Option<Span> {
529529
self.bounds.iter().fold(None, |span, bound| {
530-
let span = span.map(|s| s.to(bound.span())).unwrap_or_else(|| bound.span());
531-
Some(span)
530+
if let ExpnKind::Macro(MacroKind::Derive, _) =
531+
bound.span().ctxt().outer_expn_data().kind
532+
{
533+
// We ignore bounds that come from exclusively from a `#[derive(_)]`, because we
534+
// can't really point at them, and we sometimes use this method to get a span
535+
// appropriate for suggestions.
536+
span
537+
} else {
538+
let span = span.map(|s| s.to(bound.span())).unwrap_or_else(|| bound.span());
539+
Some(span)
540+
}
532541
})
533542
}
534543
}

src/test/ui/suggestions/derive-macro-missing-bounds.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ LL | impl<T: Debug + Trait> Debug for Inner<T> {
3131
= note: required because of the requirements on the impl of `Debug` for `&c::Inner<T>`
3232
= note: required for the cast to the object type `dyn Debug`
3333
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
34-
help: consider further restricting this bound
34+
help: consider restricting type parameter `T`
3535
|
36-
LL | #[derive(Debug + c::Trait)]
37-
| ++++++++++
36+
LL | struct Outer<T: c::Trait>(Inner<T>);
37+
| ++++++++++
3838

3939
error[E0277]: the trait bound `T: d::Trait` is not satisfied
4040
--> $DIR/derive-macro-missing-bounds.rs:56:21
@@ -53,10 +53,10 @@ LL | impl<T> Debug for Inner<T> where T: Debug, T: Trait {
5353
= note: required because of the requirements on the impl of `Debug` for `&d::Inner<T>`
5454
= note: required for the cast to the object type `dyn Debug`
5555
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
56-
help: consider further restricting this bound
56+
help: consider restricting type parameter `T`
5757
|
58-
LL | #[derive(Debug + d::Trait)]
59-
| ++++++++++
58+
LL | struct Outer<T: d::Trait>(Inner<T>);
59+
| ++++++++++
6060

6161
error[E0277]: the trait bound `T: e::Trait` is not satisfied
6262
--> $DIR/derive-macro-missing-bounds.rs:71:21
@@ -75,10 +75,10 @@ LL | impl<T> Debug for Inner<T> where T: Debug + Trait {
7575
= note: required because of the requirements on the impl of `Debug` for `&e::Inner<T>`
7676
= note: required for the cast to the object type `dyn Debug`
7777
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
78-
help: consider further restricting this bound
78+
help: consider restricting type parameter `T`
7979
|
80-
LL | #[derive(Debug + e::Trait)]
81-
| ++++++++++
80+
LL | struct Outer<T: e::Trait>(Inner<T>);
81+
| ++++++++++
8282

8383
error[E0277]: the trait bound `T: f::Trait` is not satisfied
8484
--> $DIR/derive-macro-missing-bounds.rs:86:21
@@ -97,10 +97,10 @@ LL | impl<T: Debug> Debug for Inner<T> where T: Trait {
9797
= note: required because of the requirements on the impl of `Debug` for `&f::Inner<T>`
9898
= note: required for the cast to the object type `dyn Debug`
9999
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
100-
help: consider further restricting this bound
100+
help: consider restricting type parameter `T`
101101
|
102-
LL | #[derive(Debug + f::Trait)]
103-
| ++++++++++
102+
LL | struct Outer<T: f::Trait>(Inner<T>);
103+
| ++++++++++
104104

105105
error: aborting due to 5 previous errors
106106

0 commit comments

Comments
 (0)