Skip to content

Commit 24502dd

Browse files
Improve suggestions for cfg_attr
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent 9735524 commit 24502dd

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ fn parse_cfg_attr_internal<'a>(
389389
let cfg_predicate = AttributeParser::parse_single_args(
390390
sess,
391391
attribute.span,
392+
attribute.get_normal_item().span(),
392393
attribute.style,
393394
AttrPath {
394395
segments: attribute

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,16 @@ pub struct Late;
337337
/// Gives [`AttributeParser`]s enough information to create errors, for example.
338338
pub struct AcceptContext<'f, 'sess, S: Stage> {
339339
pub(crate) shared: SharedContext<'f, 'sess, S>,
340-
/// The span of the attribute currently being parsed
340+
341+
/// The outer span of the attribute currently being parsed
342+
/// #[attribute(...)]
343+
/// ^^^^^^^^^^^^^^^^^ outer span
344+
/// For attributes in `cfg_attr`, the outer span and inner spans are equal.
341345
pub(crate) attr_span: Span,
346+
/// The inner span of the attribute currently being parsed
347+
/// #[attribute(...)]
348+
/// ^^^^^^^^^^^^^^ inner span
349+
pub(crate) inner_span: Span,
342350

343351
/// Whether it is an inner or outer attribute
344352
pub(crate) attr_style: AttrStyle,
@@ -607,7 +615,10 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
607615
}
608616

609617
pub(crate) fn suggestions(&self) -> Vec<String> {
610-
self.template.suggestions(Some(self.attr_style), &self.attr_path)
618+
// If the outer and inner spans are equal, we are parsing an attribute from `cfg_attr`,
619+
// So don't display an attribute style in the suggestions
620+
let style = (self.attr_span != self.inner_span).then_some(self.attr_style);
621+
self.template.suggestions(style, &self.attr_path)
611622
}
612623
}
613624

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ impl<'sess> AttributeParser<'sess, Early> {
142142
Self::parse_single_args(
143143
sess,
144144
attr.span,
145+
normal_attr.item.span(),
145146
attr.style,
146147
path.get_attribute_path(),
147148
target_span,
@@ -159,6 +160,7 @@ impl<'sess> AttributeParser<'sess, Early> {
159160
pub fn parse_single_args<T, I>(
160161
sess: &'sess Session,
161162
attr_span: Span,
163+
inner_span: Span,
162164
attr_style: AttrStyle,
163165
attr_path: AttrPath,
164166
target_span: Span,
@@ -186,6 +188,7 @@ impl<'sess> AttributeParser<'sess, Early> {
186188
},
187189
},
188190
attr_span,
191+
inner_span,
189192
attr_style,
190193
template,
191194
attr_path,
@@ -305,6 +308,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
305308
emit_lint: &mut emit_lint,
306309
},
307310
attr_span: lower_span(attr.span),
311+
inner_span: lower_span(attr.get_normal_item().span()),
308312
attr_style: attr.style,
309313
template: &accept.template,
310314
attr_path: path.get_attribute_path(),

tests/ui/conditional-compilation/cfg_attr-attr-syntax-validation.stderr

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ error[E0539]: malformed `link_section` attribute input
113113
--> $DIR/cfg_attr-attr-syntax-validation.rs:44:18
114114
|
115115
LL | #[cfg_attr(true, link_section)]
116-
| ^^^^^^^^^^^^ help: must be of the form: `#[link_section = "name"]`
116+
| ^^^^^^^^^^^^ help: must be of the form: `link_section = "name"`
117117
|
118118
= note: for more information, visit <https://doc.rust-lang.org/reference/abi.html#the-link_section-attribute>
119119

@@ -129,14 +129,12 @@ LL | #[cfg_attr(true, inline())]
129129
help: try changing it to one of the following valid forms of the attribute
130130
|
131131
LL - #[cfg_attr(true, inline())]
132-
LL + #[cfg_attr(true, #[inline(always)])]
133-
|
134-
LL - #[cfg_attr(true, inline())]
135-
LL + #[cfg_attr(true, #[inline(never)])]
136-
|
137-
LL - #[cfg_attr(true, inline())]
138-
LL + #[cfg_attr(true, #[inline])]
132+
LL + #[cfg_attr(true, inline)]
139133
|
134+
LL | #[cfg_attr(true, inline(always))]
135+
| ++++++
136+
LL | #[cfg_attr(true, inline(never))]
137+
| +++++
140138

141139
warning: `#[link_section]` attribute cannot be used on structs
142140
--> $DIR/cfg_attr-attr-syntax-validation.rs:44:18

0 commit comments

Comments
 (0)