Skip to content

Commit f6cdd7e

Browse files
Fix suggestion for the cfg! macro
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent e5efc33 commit f6cdd7e

File tree

6 files changed

+46
-21
lines changed

6 files changed

+46
-21
lines changed

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_ast::token::Delimiter;
22
use rustc_ast::tokenstream::DelimSpan;
33
use rustc_ast::{AttrItem, Attribute, CRATE_NODE_ID, LitKind, NodeId, ast, token};
44
use rustc_errors::{Applicability, PResult};
5-
use rustc_feature::{AttributeTemplate, Features, template};
5+
use rustc_feature::{AttrSuggestionStyle, AttributeTemplate, Features, template};
66
use rustc_hir::attrs::CfgEntry;
77
use rustc_hir::{AttrPath, RustcVersion};
88
use rustc_parse::parser::{ForceCollect, Parser};
@@ -324,8 +324,8 @@ pub fn parse_cfg_attr(
324324
}) {
325325
Ok(r) => return Some(r),
326326
Err(e) => {
327-
let suggestions =
328-
CFG_ATTR_TEMPLATE.suggestions(Some(cfg_attr.style), sym::cfg_attr);
327+
let suggestions = CFG_ATTR_TEMPLATE
328+
.suggestions(AttrSuggestionStyle::Attribute(cfg_attr.style), sym::cfg_attr);
329329
e.with_span_suggestions(
330330
cfg_attr.span,
331331
"must be of the form",
@@ -356,7 +356,8 @@ pub fn parse_cfg_attr(
356356
path: AttrPath::from_ast(&cfg_attr.get_normal_item().path),
357357
description: ParsedDescription::Attribute,
358358
reason,
359-
suggestions: CFG_ATTR_TEMPLATE.suggestions(Some(cfg_attr.style), sym::cfg_attr),
359+
suggestions: CFG_ATTR_TEMPLATE
360+
.suggestions(AttrSuggestionStyle::Attribute(cfg_attr.style), sym::cfg_attr),
360361
});
361362
}
362363
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::sync::LazyLock;
66
use private::Sealed;
77
use rustc_ast::{AttrStyle, CRATE_NODE_ID, MetaItemLit, NodeId};
88
use rustc_errors::{Diag, Diagnostic, Level};
9-
use rustc_feature::AttributeTemplate;
9+
use rustc_feature::{AttrSuggestionStyle, AttributeTemplate};
1010
use rustc_hir::attrs::AttributeKind;
1111
use rustc_hir::lints::{AttributeLint, AttributeLintKind};
1212
use rustc_hir::{AttrPath, CRATE_HIR_ID, HirId};
@@ -638,9 +638,15 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
638638
}
639639

640640
pub(crate) fn suggestions(&self) -> Vec<String> {
641-
// If the outer and inner spans are equal, we are parsing an attribute from `cfg_attr`,
642-
// So don't display an attribute style in the suggestions
643-
let style = (self.attr_span != self.inner_span).then_some(self.attr_style);
641+
let style = match self.parsed_description {
642+
// If the outer and inner spans are equal, we are parsing an embedded attribute
643+
ParsedDescription::Attribute if self.attr_span == self.inner_span => {
644+
AttrSuggestionStyle::EmbeddedAttribute
645+
}
646+
ParsedDescription::Attribute => AttrSuggestionStyle::Attribute(self.attr_style),
647+
ParsedDescription::Macro => AttrSuggestionStyle::Macro,
648+
};
649+
644650
self.template.suggestions(style, &self.attr_path)
645651
}
646652
}

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,45 @@ pub struct AttributeTemplate {
132132
pub docs: Option<&'static str>,
133133
}
134134

135+
pub enum AttrSuggestionStyle {
136+
/// The suggestion is styled for a normal attribute.
137+
/// The `AttrStyle` determines whether this is an inner or outer attribute.
138+
Attribute(AttrStyle),
139+
/// The suggestion is styled for an attribute embedded into another attribute.
140+
/// For example, attributes inside `#[cfg_attr(true, attr(...)]`.
141+
EmbeddedAttribute,
142+
/// The suggestion is styled for macros that are parsed with attribute parsers.
143+
/// For example, the `cfg!(predicate)` macro.
144+
Macro,
145+
}
146+
135147
impl AttributeTemplate {
136148
pub fn suggestions(
137149
&self,
138-
style: Option<AttrStyle>,
150+
style: AttrSuggestionStyle,
139151
name: impl std::fmt::Display,
140152
) -> Vec<String> {
141-
let mut suggestions = vec![];
142-
let (start, end) = match style {
143-
Some(AttrStyle::Outer) => ("#[", "]"),
144-
Some(AttrStyle::Inner) => ("#![", "]"),
145-
None => ("", ""),
153+
let (start, macro_call, end) = match style {
154+
AttrSuggestionStyle::Attribute(AttrStyle::Outer) => ("#[", "", "]"),
155+
AttrSuggestionStyle::Attribute(AttrStyle::Inner) => ("#![", "", "]"),
156+
AttrSuggestionStyle::Macro => ("", "!", ""),
157+
AttrSuggestionStyle::EmbeddedAttribute => ("", "", ""),
146158
};
159+
160+
let mut suggestions = vec![];
161+
147162
if self.word {
163+
debug_assert!(macro_call.is_empty(), "Macro suggestions use list style");
148164
suggestions.push(format!("{start}{name}{end}"));
149165
}
150166
if let Some(descr) = self.list {
151167
for descr in descr {
152-
suggestions.push(format!("{start}{name}({descr}){end}"));
168+
suggestions.push(format!("{start}{name}{macro_call}({descr}){end}"));
153169
}
154170
}
155171
suggestions.extend(self.one_of.iter().map(|&word| format!("{start}{name}({word}){end}")));
156172
if let Some(descr) = self.name_value_str {
173+
debug_assert!(macro_call.is_empty(), "Macro suggestions use list style");
157174
for descr in descr {
158175
suggestions.push(format!("{start}{name} = \"{descr}\"{end}"));
159176
}

compiler/rustc_feature/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZero<u
135135

136136
pub use accepted::ACCEPTED_LANG_FEATURES;
137137
pub use builtin_attrs::{
138-
AttributeDuplicates, AttributeGate, AttributeSafety, AttributeTemplate, AttributeType,
139-
BUILTIN_ATTRIBUTE_MAP, BUILTIN_ATTRIBUTES, BuiltinAttribute, GatedCfg, encode_cross_crate,
140-
find_gated_cfg, is_builtin_attr_name, is_stable_diagnostic_attribute, is_valid_for_get_attr,
138+
AttrSuggestionStyle, AttributeDuplicates, AttributeGate, AttributeSafety, AttributeTemplate,
139+
AttributeType, BUILTIN_ATTRIBUTE_MAP, BUILTIN_ATTRIBUTES, BuiltinAttribute, GatedCfg,
140+
encode_cross_crate, find_gated_cfg, is_builtin_attr_name, is_stable_diagnostic_attribute,
141+
is_valid_for_get_attr,
141142
};
142143
pub use removed::REMOVED_LANG_FEATURES;
143144
pub use unstable::{

tests/ui/macros/cfg.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | cfg!(123);
1111
| ^^^^^---^
1212
| | |
1313
| | expected a valid identifier here
14-
| help: must be of the form: `cfg(predicate)`
14+
| help: must be of the form: `cfg!(predicate)`
1515
|
1616
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
1717

@@ -22,7 +22,7 @@ LL | cfg!(foo = 123);
2222
| ^^^^^^^^^^^---^
2323
| | |
2424
| | expected a string literal here
25-
| help: must be of the form: `cfg(predicate)`
25+
| help: must be of the form: `cfg!(predicate)`
2626
|
2727
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
2828

tests/ui/span/E0805.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | if cfg!(not()) { }
55
| ^^^^^^^^--^
66
| | |
77
| | expected a single argument here
8-
| help: must be of the form: `cfg(predicate)`
8+
| help: must be of the form: `cfg!(predicate)`
99
|
1010
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
1111

0 commit comments

Comments
 (0)