diff --git a/compiler/rustc_attr_parsing/src/attributes/cfg.rs b/compiler/rustc_attr_parsing/src/attributes/cfg.rs index 9f0c98668924..dab9e7666c23 100644 --- a/compiler/rustc_attr_parsing/src/attributes/cfg.rs +++ b/compiler/rustc_attr_parsing/src/attributes/cfg.rs @@ -389,6 +389,7 @@ fn parse_cfg_attr_internal<'a>( let cfg_predicate = AttributeParser::parse_single_args( sess, attribute.span, + attribute.get_normal_item().span(), attribute.style, AttrPath { segments: attribute diff --git a/compiler/rustc_attr_parsing/src/context.rs b/compiler/rustc_attr_parsing/src/context.rs index 7b14e24a218b..6694dac8bb25 100644 --- a/compiler/rustc_attr_parsing/src/context.rs +++ b/compiler/rustc_attr_parsing/src/context.rs @@ -337,8 +337,16 @@ pub struct Early { /// Gives [`AttributeParser`]s enough information to create errors, for example. pub struct AcceptContext<'f, 'sess, S: Stage> { pub(crate) shared: SharedContext<'f, 'sess, S>, - /// The span of the attribute currently being parsed + + /// The outer span of the attribute currently being parsed + /// #[attribute(...)] + /// ^^^^^^^^^^^^^^^^^ outer span + /// For attributes in `cfg_attr`, the outer span and inner spans are equal. pub(crate) attr_span: Span, + /// The inner span of the attribute currently being parsed + /// #[attribute(...)] + /// ^^^^^^^^^^^^^^ inner span + pub(crate) inner_span: Span, /// Whether it is an inner or outer attribute pub(crate) attr_style: AttrStyle, @@ -607,7 +615,10 @@ pub(crate) fn warn_empty_attribute(&mut self, span: Span) { } pub(crate) fn suggestions(&self) -> Vec { - self.template.suggestions(Some(self.attr_style), &self.attr_path) + // If the outer and inner spans are equal, we are parsing an attribute from `cfg_attr`, + // So don't display an attribute style in the suggestions + let style = (self.attr_span != self.inner_span).then_some(self.attr_style); + self.template.suggestions(style, &self.attr_path) } } diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index b8ef11c26d80..953b0ebfaf04 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -142,6 +142,7 @@ pub fn parse_single( Self::parse_single_args( sess, attr.span, + normal_attr.item.span(), attr.style, path.get_attribute_path(), target_span, @@ -159,6 +160,7 @@ pub fn parse_single( pub fn parse_single_args( sess: &'sess Session, attr_span: Span, + inner_span: Span, attr_style: AttrStyle, attr_path: AttrPath, target_span: Span, @@ -186,6 +188,7 @@ pub fn parse_single_args( }, }, attr_span, + inner_span, attr_style, template, attr_path, @@ -305,6 +308,7 @@ pub fn parse_attribute_list( emit_lint: &mut emit_lint, }, attr_span: lower_span(attr.span), + inner_span: lower_span(attr.get_normal_item().span()), attr_style: attr.style, template: &accept.template, attr_path: path.get_attribute_path(), diff --git a/tests/ui/conditional-compilation/cfg_attr-attr-syntax-validation.stderr b/tests/ui/conditional-compilation/cfg_attr-attr-syntax-validation.stderr index 2b7a7da3e33d..fd03fa62864a 100644 --- a/tests/ui/conditional-compilation/cfg_attr-attr-syntax-validation.stderr +++ b/tests/ui/conditional-compilation/cfg_attr-attr-syntax-validation.stderr @@ -113,7 +113,7 @@ error[E0539]: malformed `link_section` attribute input --> $DIR/cfg_attr-attr-syntax-validation.rs:44:18 | LL | #[cfg_attr(true, link_section)] - | ^^^^^^^^^^^^ help: must be of the form: `#[link_section = "name"]` + | ^^^^^^^^^^^^ help: must be of the form: `link_section = "name"` | = note: for more information, visit @@ -129,14 +129,12 @@ LL | #[cfg_attr(true, inline())] help: try changing it to one of the following valid forms of the attribute | LL - #[cfg_attr(true, inline())] -LL + #[cfg_attr(true, #[inline(always)])] - | -LL - #[cfg_attr(true, inline())] -LL + #[cfg_attr(true, #[inline(never)])] - | -LL - #[cfg_attr(true, inline())] -LL + #[cfg_attr(true, #[inline])] +LL + #[cfg_attr(true, inline)] | +LL | #[cfg_attr(true, inline(always))] + | ++++++ +LL | #[cfg_attr(true, inline(never))] + | +++++ warning: `#[link_section]` attribute cannot be used on structs --> $DIR/cfg_attr-attr-syntax-validation.rs:44:18