Skip to content

Commit 970cb15

Browse files
committed
Use ast node instead of boolean
1 parent 9c9f439 commit 970cb15

5 files changed

Lines changed: 22 additions & 24 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4010,6 +4010,7 @@ dependencies = [
40104010
name = "rustc_feature"
40114011
version = "0.0.0"
40124012
dependencies = [
4013+
"rustc_ast",
40134014
"rustc_data_structures",
40144015
"rustc_hir",
40154016
"rustc_span",

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ pub fn parse_cfg_attr(
329329
Err(e) => {
330330
let suggestions = CFG_ATTR_TEMPLATE.suggestions(
331331
AttrSuggestionStyle::Attribute(cfg_attr.style),
332-
matches!(cfg_attr.get_normal_item().unsafety, rustc_ast::Safety::Unsafe(_)),
332+
cfg_attr.get_normal_item().unsafety,
333333
sym::cfg_attr,
334334
);
335335
e.with_span_suggestions(
@@ -365,7 +365,7 @@ pub fn parse_cfg_attr(
365365
suggestions: session_diagnostics::AttributeParseErrorSuggestions::CreatedByTemplate(
366366
CFG_ATTR_TEMPLATE.suggestions(
367367
AttrSuggestionStyle::Attribute(cfg_attr.style),
368-
matches!(cfg_attr.get_normal_item().unsafety, rustc_ast::Safety::Unsafe(_)),
368+
cfg_attr.get_normal_item().unsafety,
369369
sym::cfg_attr,
370370
),
371371
),

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ pub struct AcceptContext<'f, 'sess> {
358358
pub(crate) template: &'f AttributeTemplate,
359359

360360
/// The safety attribute (if any) applied to the attribute.
361-
pub(crate) attr_safety: rustc_ast::Safety,
361+
pub(crate) attr_safety: Safety,
362362

363363
/// The name of the attribute we're currently accepting.
364364
pub(crate) attr_path: AttrPath,
@@ -876,11 +876,7 @@ impl<'a, 'f, 'sess: 'f> AttributeDiagnosticContext<'a, 'f, 'sess> {
876876
ParsedDescription::Macro => AttrSuggestionStyle::Macro,
877877
};
878878

879-
self.template.suggestions(
880-
style,
881-
matches!(self.attr_safety, rustc_ast::Safety::Unsafe(_)),
882-
&self.attr_path,
883-
)
879+
self.template.suggestions(style, self.attr_safety, &self.attr_path)
884880
}
885881
}
886882

@@ -1071,11 +1067,7 @@ impl<'a, 'f, 'sess: 'f> AttributeDiagnosticContext<'a, 'f, 'sess> {
10711067
ParsedDescription::Macro => AttrSuggestionStyle::Macro,
10721068
};
10731069

1074-
self.template.suggestions(
1075-
style,
1076-
matches!(self.attr_safety, Safety::Unsafe(_)),
1077-
&self.attr_path,
1078-
)
1070+
self.template.suggestions(style, self.attr_safety, &self.attr_path)
10791071
}
10801072
/// Error that a string literal was expected.
10811073
/// You can optionally give the literal you did find (which you found not to be a string literal)

compiler/rustc_feature/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
# tidy-alphabetical-start
8+
rustc_ast = { path = "../rustc_ast" }
89
rustc_data_structures = { path = "../rustc_data_structures" }
910
rustc_hir = { path = "../rustc_hir" }
1011
rustc_span = { path = "../rustc_span" }

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::sync::LazyLock;
44

55
use AttributeGate::*;
6+
use rustc_ast::ast::Safety;
67
use rustc_data_structures::fx::FxHashMap;
78
use rustc_hir::AttrStyle;
89
use rustc_span::{Symbol, sym};
@@ -118,7 +119,7 @@ impl AttributeTemplate {
118119
pub fn suggestions(
119120
&self,
120121
style: AttrSuggestionStyle,
121-
wrap_with_unsafe: bool,
122+
safety: Safety,
122123
name: impl std::fmt::Display,
123124
) -> Vec<String> {
124125
let (start, macro_call, end) = match style {
@@ -130,29 +131,32 @@ impl AttributeTemplate {
130131

131132
let mut suggestions = vec![];
132133

133-
let (maybe_unsafe_start, maybe_unsafe_end) =
134-
if wrap_with_unsafe { ("unsafe(", ")") } else { ("", "") };
134+
let (safety_start, safety_end) = match safety {
135+
Safety::Unsafe(_) => ("unsafe(", ")"),
136+
_ => ("", ""),
137+
};
135138

136139
if self.word {
137140
debug_assert!(macro_call.is_empty(), "Macro suggestions use list style");
138-
suggestions.push(format!("{start}{maybe_unsafe_start}{name}{maybe_unsafe_end}{end}"));
141+
suggestions.push(format!("{start}{safety_start}{name}{safety_end}{end}"));
139142
}
140143
if let Some(descr) = self.list {
141144
for descr in descr {
142145
suggestions.push(format!(
143-
"{start}{maybe_unsafe_start}{name}{macro_call}({descr}){maybe_unsafe_end}{end}"
146+
"{start}{safety_start}{name}{macro_call}({descr}){safety_end}{end}"
144147
));
145148
}
146149
}
147-
suggestions.extend(self.one_of.iter().map(|&word| {
148-
format!("{start}{maybe_unsafe_start}{name}({word}){maybe_unsafe_end}{end}")
149-
}));
150+
suggestions.extend(
151+
self.one_of
152+
.iter()
153+
.map(|&word| format!("{start}{safety_start}{name}({word}){safety_end}{end}")),
154+
);
150155
if let Some(descr) = self.name_value_str {
151156
debug_assert!(macro_call.is_empty(), "Macro suggestions use list style");
152157
for descr in descr {
153-
suggestions.push(format!(
154-
"{start}{maybe_unsafe_start}{name} = \"{descr}\"{maybe_unsafe_end}{end}"
155-
));
158+
suggestions
159+
.push(format!("{start}{safety_start}{name} = \"{descr}\"{safety_end}{end}"));
156160
}
157161
}
158162
suggestions.sort();

0 commit comments

Comments
 (0)