Skip to content

Error when an arg to #[allow_internal_unstable] is a key-value#155787

Closed
scrabsha wants to merge 2 commits intorust-lang:mainfrom
scrabsha:push-nxxrssyvwswk
Closed

Error when an arg to #[allow_internal_unstable] is a key-value#155787
scrabsha wants to merge 2 commits intorust-lang:mainfrom
scrabsha:push-nxxrssyvwswk

Conversation

@scrabsha
Copy link
Copy Markdown
Contributor

@scrabsha scrabsha commented Apr 25, 2026

It looks like we don't check that the features passed to #[allow_internal_unstable] are "just features" (as in, with no arguments). This means the following compiles:

#![feature(allow_internal_unstable)]

#[allow_internal_unstable(cat = "meow")]
macro_rules! foo {
    () => {};
}
Playground link

This PR makes it an error to pass anything other than an identifier as argument to #[allow_internal_unstable]. I believe it is ok to make it an error (and technically breaking some code) because allow_internal_unstable is an internal feature, which (quoting rustc's output) is internal to the compiler or standard library. Happy to change to a warning if my assumption is wrong though :)

Discovered while writing the followup of #155696.

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 25, 2026
if let Some(ident) = param.meta_item().and_then(|i| i.path().word()) {
res.push(ident.name);
} else {
let fail = || {
Copy link
Copy Markdown
Contributor

@mejrs mejrs Apr 25, 2026

Choose a reason for hiding this comment

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

You could do something like this:

macro or_malformed($($code:tt)*) {{
let Some(ret) = (||{
Some($($code)*)
})() else {
malformed!()
};
ret
}}
macro duplicate($name: ident, $($first_span:tt)*) {{
let first_span = $($first_span)*;
cx.emit_dyn_lint(
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
move |dcx, level| IgnoredDiagnosticOption {
first_span,
later_span: span,
option_name: $name,
}.into_diag(dcx, level),
span,
);
}}
let item: &MetaItemParser = or_malformed!(item.meta_item()?);
let name = or_malformed!(item.ident()?).name;

View changes since the review

Copy link
Copy Markdown
Contributor Author

@scrabsha scrabsha Apr 25, 2026

Choose a reason for hiding this comment

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

(||{ Some($($code)*) })()

... wow

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

... wow

A poor man's try block 😢

@JonathanBrouwer
Copy link
Copy Markdown
Contributor

This problem is already fixed by my pr: #155193
(Which has been in the crater queue for like half a month at this point, lmao)

@scrabsha
Copy link
Copy Markdown
Contributor Author

oof, you have been more thorough than me hehe. closing my PR then - good luck for 155193 😭

@scrabsha scrabsha closed this Apr 25, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 25, 2026
@scrabsha scrabsha deleted the push-nxxrssyvwswk branch April 25, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants