Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions compiler/rustc_attr_parsing/src/attributes/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,11 @@ pub fn parse_cfg_attr(
}) {
Ok(r) => return Some(r),
Err(e) => {
let suggestions = CFG_ATTR_TEMPLATE
.suggestions(AttrSuggestionStyle::Attribute(cfg_attr.style), sym::cfg_attr);
let suggestions = CFG_ATTR_TEMPLATE.suggestions(
AttrSuggestionStyle::Attribute(cfg_attr.style),
matches!(cfg_attr.get_normal_item().unsafety, rustc_ast::Safety::Unsafe(_)),
sym::cfg_attr,
);
e.with_span_suggestions(
cfg_attr.span,
"must be of the form",
Expand Down Expand Up @@ -360,8 +363,11 @@ pub fn parse_cfg_attr(
description: ParsedDescription::Attribute,
reason,
suggestions: session_diagnostics::AttributeParseErrorSuggestions::CreatedByTemplate(
CFG_ATTR_TEMPLATE
.suggestions(AttrSuggestionStyle::Attribute(cfg_attr.style), sym::cfg_attr),
CFG_ATTR_TEMPLATE.suggestions(
AttrSuggestionStyle::Attribute(cfg_attr.style),
matches!(cfg_attr.get_normal_item().unsafety, rustc_ast::Safety::Unsafe(_)),
sym::cfg_attr,
),
),
});
}
Expand Down
17 changes: 14 additions & 3 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::mem;
use std::ops::{Deref, DerefMut};
use std::sync::LazyLock;

use rustc_ast::{AttrStyle, MetaItemLit};
use rustc_ast::{AttrStyle, MetaItemLit, Safety};
use rustc_data_structures::sync::{DynSend, DynSync};
use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level, MultiSpan};
use rustc_feature::{AttrSuggestionStyle, AttributeTemplate};
Expand Down Expand Up @@ -357,6 +357,9 @@ pub struct AcceptContext<'f, 'sess> {
/// Used in reporting errors to give a hint to users what the attribute *should* look like.
pub(crate) template: &'f AttributeTemplate,

/// The safety attribute (if any) applied to the attribute.
pub(crate) attr_safety: rustc_ast::Safety,

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

self.template.suggestions(style, &self.attr_path)
self.template.suggestions(
style,
matches!(self.attr_safety, rustc_ast::Safety::Unsafe(_)),
&self.attr_path,
)
}
}

Expand Down Expand Up @@ -1064,7 +1071,11 @@ impl<'a, 'f, 'sess: 'f> AttributeDiagnosticContext<'a, 'f, 'sess> {
ParsedDescription::Macro => AttrSuggestionStyle::Macro,
};

self.template.suggestions(style, &self.attr_path)
self.template.suggestions(
style,
matches!(self.attr_safety, Safety::Unsafe(_)),
&self.attr_path,
)
}
/// Error that a string literal was expected.
/// You can optionally give the literal you did find (which you found not to be a string literal)
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_attr_parsing/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ impl<'sess> AttributeParser<'sess> {
attr_style,
parsed_description,
template,
attr_safety: attr_safety.unwrap_or(Safety::Default),
attr_path,
};
parse_fn(&mut cx, args)
Expand Down Expand Up @@ -404,6 +405,7 @@ impl<'sess> AttributeParser<'sess> {
attr_style: attr.style,
parsed_description: ParsedDescription::Attribute,
template: &accept.template,
attr_safety: n.item.unsafety,
attr_path: attr_path.clone(),
};

Expand Down
18 changes: 14 additions & 4 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl AttributeTemplate {
pub fn suggestions(
&self,
style: AttrSuggestionStyle,
wrap_with_unsafe: bool,
Copy link
Copy Markdown
Contributor

@JonathanBrouwer JonathanBrouwer May 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to pass the Safety in here, instead of a bool

View changes since the review

name: impl std::fmt::Display,
) -> Vec<String> {
let (start, macro_call, end) = match style {
Expand All @@ -129,20 +130,29 @@ impl AttributeTemplate {

let mut suggestions = vec![];

let (maybe_unsafe_start, maybe_unsafe_end) =
if wrap_with_unsafe { ("unsafe(", ")") } else { ("", "") };

if self.word {
debug_assert!(macro_call.is_empty(), "Macro suggestions use list style");
suggestions.push(format!("{start}{name}{end}"));
suggestions.push(format!("{start}{maybe_unsafe_start}{name}{maybe_unsafe_end}{end}"));
}
if let Some(descr) = self.list {
for descr in descr {
suggestions.push(format!("{start}{name}{macro_call}({descr}){end}"));
suggestions.push(format!(
"{start}{maybe_unsafe_start}{name}{macro_call}({descr}){maybe_unsafe_end}{end}"
));
}
}
suggestions.extend(self.one_of.iter().map(|&word| format!("{start}{name}({word}){end}")));
suggestions.extend(self.one_of.iter().map(|&word| {
format!("{start}{maybe_unsafe_start}{name}({word}){maybe_unsafe_end}{end}")
}));
if let Some(descr) = self.name_value_str {
debug_assert!(macro_call.is_empty(), "Macro suggestions use list style");
for descr in descr {
suggestions.push(format!("{start}{name} = \"{descr}\"{end}"));
suggestions.push(format!(
"{start}{maybe_unsafe_start}{name} = \"{descr}\"{maybe_unsafe_end}{end}"
));
}
}
suggestions.sort();
Expand Down
11 changes: 5 additions & 6 deletions tests/ui/attributes/malformed-attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,8 @@ LL | #[unsafe(export_name)]
|
help: must be of the form
|
LL - #[unsafe(export_name)]
LL + #[export_name = "name"]
|
LL | #[unsafe(export_name = "name")]
| ++++++++

error: `rustc_allow_const_fn_unstable` expects a list of feature names
--> $DIR/malformed-attrs.rs:31:1
Expand Down Expand Up @@ -330,7 +329,7 @@ LL | #[unsafe(naked())]
help: must be of the form
|
LL - #[unsafe(naked())]
LL + #[naked]
LL + #[unsafe(naked)]
|

error[E0565]: malformed `track_caller` attribute input
Expand Down Expand Up @@ -651,7 +650,7 @@ LL | #[unsafe(ffi_pure = 1)]
help: must be of the form
|
LL - #[unsafe(ffi_pure = 1)]
LL + #[ffi_pure]
LL + #[unsafe(ffi_pure)]
|

error[E0539]: malformed `link_ordinal` attribute input
Expand All @@ -677,7 +676,7 @@ LL | #[unsafe(ffi_const = 1)]
help: must be of the form
|
LL - #[unsafe(ffi_const = 1)]
LL + #[ffi_const]
LL + #[unsafe(ffi_const)]
|

error[E0539]: malformed `linkage` attribute input
Expand Down
Loading