Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4010,6 +4010,7 @@ dependencies = [
name = "rustc_feature"
version = "0.0.0"
dependencies = [
"rustc_ast",
"rustc_data_structures",
"rustc_hir",
"rustc_span",
Expand Down
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),
cfg_attr.get_normal_item().unsafety,
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),
cfg_attr.get_normal_item().unsafety,
sym::cfg_attr,
),
),
});
}
Expand Down
9 changes: 6 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: Safety,

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

self.template.suggestions(style, &self.attr_path)
self.template.suggestions(style, self.attr_safety, &self.attr_path)
}
}

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

self.template.suggestions(style, &self.attr_path)
self.template.suggestions(style, self.attr_safety, &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
1 change: 1 addition & 0 deletions compiler/rustc_feature/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2024"

[dependencies]
# tidy-alphabetical-start
rustc_ast = { path = "../rustc_ast" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_hir = { path = "../rustc_hir" }
rustc_span = { path = "../rustc_span" }
Expand Down
22 changes: 18 additions & 4 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::sync::LazyLock;

use AttributeGate::*;
use rustc_ast::ast::Safety;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::AttrStyle;
use rustc_span::{Symbol, sym};
Expand Down Expand Up @@ -118,6 +119,7 @@ impl AttributeTemplate {
pub fn suggestions(
&self,
style: AttrSuggestionStyle,
safety: Safety,
name: impl std::fmt::Display,
) -> Vec<String> {
let (start, macro_call, end) = match style {
Expand All @@ -129,20 +131,32 @@ impl AttributeTemplate {

let mut suggestions = vec![];

let (safety_start, safety_end) = match safety {
Safety::Unsafe(_) => ("unsafe(", ")"),
_ => ("", ""),
};

if self.word {
debug_assert!(macro_call.is_empty(), "Macro suggestions use list style");
suggestions.push(format!("{start}{name}{end}"));
suggestions.push(format!("{start}{safety_start}{name}{safety_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}{safety_start}{name}{macro_call}({descr}){safety_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}{safety_start}{name}({word}){safety_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}{safety_start}{name} = \"{descr}\"{safety_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