From fa3eebb26e103a8f0fa81b378e8c8525a80baf32 Mon Sep 17 00:00:00 2001 From: Esteban Kuber Date: Wed, 17 Nov 2021 23:35:38 +0000 Subject: [PATCH] Modify `bounds_span` to ignore bounds coming from a `derive` macro --- compiler/rustc_hir/src/hir.rs | 15 +++++++++--- .../derive-macro-missing-bounds.stderr | 24 +++++++++---------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 69f770265dda..d5132ad5bdf2 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -17,7 +17,7 @@ use rustc_index::vec::IndexVec; use rustc_macros::HashStable_Generic; use rustc_span::source_map::Spanned; use rustc_span::symbol::{kw, sym, Ident, Symbol}; -use rustc_span::{def_id::LocalDefId, BytePos, MultiSpan, Span, DUMMY_SP}; +use rustc_span::{def_id::LocalDefId, BytePos, ExpnKind, MacroKind, MultiSpan, Span, DUMMY_SP}; use rustc_target::asm::InlineAsmRegOrRegClass; use rustc_target::spec::abi::Abi; @@ -527,8 +527,17 @@ pub struct GenericParam<'hir> { impl GenericParam<'hir> { pub fn bounds_span(&self) -> Option { self.bounds.iter().fold(None, |span, bound| { - let span = span.map(|s| s.to(bound.span())).unwrap_or_else(|| bound.span()); - Some(span) + if let ExpnKind::Macro(MacroKind::Derive, _) = + bound.span().ctxt().outer_expn_data().kind + { + // We ignore bounds that come from exclusively from a `#[derive(_)]`, because we + // can't really point at them, and we sometimes use this method to get a span + // appropriate for suggestions. + span + } else { + let span = span.map(|s| s.to(bound.span())).unwrap_or_else(|| bound.span()); + Some(span) + } }) } } diff --git a/src/test/ui/suggestions/derive-macro-missing-bounds.stderr b/src/test/ui/suggestions/derive-macro-missing-bounds.stderr index 396781059309..7a4f7e209c19 100644 --- a/src/test/ui/suggestions/derive-macro-missing-bounds.stderr +++ b/src/test/ui/suggestions/derive-macro-missing-bounds.stderr @@ -31,10 +31,10 @@ LL | impl Debug for Inner { = note: required because of the requirements on the impl of `Debug` for `&c::Inner` = note: required for the cast to the object type `dyn Debug` = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info) -help: consider further restricting this bound +help: consider restricting type parameter `T` | -LL | #[derive(Debug + c::Trait)] - | ++++++++++ +LL | struct Outer(Inner); + | ++++++++++ error[E0277]: the trait bound `T: d::Trait` is not satisfied --> $DIR/derive-macro-missing-bounds.rs:56:21 @@ -53,10 +53,10 @@ LL | impl Debug for Inner where T: Debug, T: Trait { = note: required because of the requirements on the impl of `Debug` for `&d::Inner` = note: required for the cast to the object type `dyn Debug` = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info) -help: consider further restricting this bound +help: consider restricting type parameter `T` | -LL | #[derive(Debug + d::Trait)] - | ++++++++++ +LL | struct Outer(Inner); + | ++++++++++ error[E0277]: the trait bound `T: e::Trait` is not satisfied --> $DIR/derive-macro-missing-bounds.rs:71:21 @@ -75,10 +75,10 @@ LL | impl Debug for Inner where T: Debug + Trait { = note: required because of the requirements on the impl of `Debug` for `&e::Inner` = note: required for the cast to the object type `dyn Debug` = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info) -help: consider further restricting this bound +help: consider restricting type parameter `T` | -LL | #[derive(Debug + e::Trait)] - | ++++++++++ +LL | struct Outer(Inner); + | ++++++++++ error[E0277]: the trait bound `T: f::Trait` is not satisfied --> $DIR/derive-macro-missing-bounds.rs:86:21 @@ -97,10 +97,10 @@ LL | impl Debug for Inner where T: Trait { = note: required because of the requirements on the impl of `Debug` for `&f::Inner` = note: required for the cast to the object type `dyn Debug` = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info) -help: consider further restricting this bound +help: consider restricting type parameter `T` | -LL | #[derive(Debug + f::Trait)] - | ++++++++++ +LL | struct Outer(Inner); + | ++++++++++ error: aborting due to 5 previous errors