Skip to content

Commit ae0ecb8

Browse files
authored
Unrolled build for #148484
Rollup merge of #148484 - JonathanBrouwer:wip_attr_style, r=jdonszelmann Fix suggestion for the `cfg!` macro r? `@jdonszelmann`
2 parents 3d461af + f6cdd7e commit ae0ecb8

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};
@@ -323,8 +323,8 @@ pub fn parse_cfg_attr(
323323
}) {
324324
Ok(r) => return Some(r),
325325
Err(e) => {
326-
let suggestions =
327-
CFG_ATTR_TEMPLATE.suggestions(Some(cfg_attr.style), sym::cfg_attr);
326+
let suggestions = CFG_ATTR_TEMPLATE
327+
.suggestions(AttrSuggestionStyle::Attribute(cfg_attr.style), sym::cfg_attr);
328328
e.with_span_suggestions(
329329
cfg_attr.span,
330330
"must be of the form",
@@ -355,7 +355,8 @@ pub fn parse_cfg_attr(
355355
path: AttrPath::from_ast(&cfg_attr.get_normal_item().path),
356356
description: ParsedDescription::Attribute,
357357
reason,
358-
suggestions: CFG_ATTR_TEMPLATE.suggestions(Some(cfg_attr.style), sym::cfg_attr),
358+
suggestions: CFG_ATTR_TEMPLATE
359+
.suggestions(AttrSuggestionStyle::Attribute(cfg_attr.style), sym::cfg_attr),
359360
});
360361
}
361362
}

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};
@@ -637,9 +637,15 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
637637
}
638638

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

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
@@ -129,9 +129,10 @@ pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZero<u
129129

130130
pub use accepted::ACCEPTED_LANG_FEATURES;
131131
pub use builtin_attrs::{
132-
AttributeDuplicates, AttributeGate, AttributeSafety, AttributeTemplate, AttributeType,
133-
BUILTIN_ATTRIBUTE_MAP, BUILTIN_ATTRIBUTES, BuiltinAttribute, GatedCfg, encode_cross_crate,
134-
find_gated_cfg, is_builtin_attr_name, is_stable_diagnostic_attribute, is_valid_for_get_attr,
132+
AttrSuggestionStyle, AttributeDuplicates, AttributeGate, AttributeSafety, AttributeTemplate,
133+
AttributeType, BUILTIN_ATTRIBUTE_MAP, BUILTIN_ATTRIBUTES, BuiltinAttribute, GatedCfg,
134+
encode_cross_crate, find_gated_cfg, is_builtin_attr_name, is_stable_diagnostic_attribute,
135+
is_valid_for_get_attr,
135136
};
136137
pub use removed::REMOVED_LANG_FEATURES;
137138
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)