Skip to content

Reject unexpanded attributes in attributes - #164712

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
fmease:early-attr-nested-attr
Nov 9, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
fmease:early-attr-nested-attr

Conversation

@fmease

@fmease fmease commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

View all comments

Reject "unexpanded" attributes on the "value" of key-value attributes. For "early"1 attributes, "unexpanded" means any attribute. For normal attribute, "unexpanded" means inert2.

For the early attributes crate_name, crate_type & recursion_limit the motivation for this change is the fact that the result of the expansion wouldn't get used and can't for that matter as the value needs to be known before macro expansion.

For the other kind of early attributes, namely attributes placed on macro calls, such contained attributes wouldn't even get name resolved due to the overarching attribute getting dropped early when the macro call, the target, gets expanded.

"Non-early", so normal, attributes didn't get parsed by the attribute parsers, so they weren't semantically validated at all. While it shouldn't be that hard to recursively validate nested attributes using the attribute parsers (e.g., #[doc = #[doc = #[repr(unknown)] ""] ""]), it's not worth the complexity. Moreover, it's not clear whether we can easily support such attributes in the rest of the compiler. For example, would our lint infra properly support #[doc = #[deny(rustdoc::invalid_html_tag)] "text"] out of the box? Let's not make life complicated and just forbid inert attributes in such contexts for now at least.

This is not a breaking change since such attributes are gated behind unstable feature stmt_expr_attributes (plus proc_macro_hygiene if you want to use an attribute macro).

Fixes #163330.

r? JonathanBrouwer or reassign

(No LLM was or will be used by me during the entire creation process of this PR)

Footnotes

  1. Attributes that "need to be known" very early during compilation. Those are #![crate_name], #![crate_type], #![recursion_limit] and any attributes places on macro calls. ↩

  2. Note that since PR https://github.com/rust-lang/rust/pull/159580 we also reject cfg on expressions in any context (despite not being inert -- a special case to prevent footguns). ↩

@rustbot

rustbot commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 21, 2026
@rustbot

rustbot commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

JonathanBrouwer is currently at their maximum review capacity.
They may take a while to respond.

@fmease
fmease force-pushed the early-attr-nested-attr branch from 70f1a26 to 64d8572 Compare October 22, 2026 08:47

@JonathanBrouwer JonathanBrouwer left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My gut instinct here is we should not allow attributes inside other attributes at all. This is not part of the stmt_expr_attributes RFC, so I think that this is possible is probably an accident. I don't think attributes on literals in attributes are useful, and I wouldn't be surprised if there are more bugs to be found here.

How do you feel about that?

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 22, 2026
@fmease

fmease commented Oct 22, 2026 •

Copy link
Copy Markdown
Member Author

This did cross my mind. However, for non-early non-inert attributes they should pretty much just work (tm). E.g., #[doc = #[replace::with("that")] "this"] works perfectly fine.

From a language design perspective, I don't see why we should forbid non-inert / macro attributes there since we already allow macro calls in such positions: #[doc = include_str!("...")], #[doc = replace::with!("this", "that")]. In my head, annotating a node with a macro attribute is just "syntactic sugar" for passing the "node" to a macro call instead (modulo some checks).

However, most inert attributes we should indeed probably forbid there before stabilizing stmt_expr_attributes. For example, we currently allow #[doc = #[repr] "..."], meaning we don't successfully validate these attributes yet.

@JonathanBrouwer

Copy link
Copy Markdown
Member

Allowing non-inert macros, while forbidding inert ones seems reasonable to me.
Could you change this PR to disallow all inert attributes?

@fmease
fmease force-pushed the early-attr-nested-attr branch from 64d8572 to 7c30ce2 Compare November 1, 2026 15:45
@rustbot

rustbot commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@fmease
fmease marked this pull request as draft November 1, 2026 15:45
@fmease fmease changed the title Reject attributes in early attributes [WIP] Reject unexpanded attributes in attributes Nov 1, 2026
@fmease
fmease force-pushed the early-attr-nested-attr branch from 7c30ce2 to b845654 Compare November 6, 2026 14:04
@fmease fmease changed the title [WIP] Reject unexpanded attributes in attributes Reject unexpanded attributes in attributes Nov 6, 2026
@fmease
fmease marked this pull request as ready for review November 6, 2026 14:29
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 6, 2026
@fmease

fmease commented Nov 6, 2026 •

Copy link
Copy Markdown
Member Author

Ready for review. Please see the updated PR description for a summary.

The diff has grown quite massively because of all the test cases I added, so fear not ^^!

[one] this attribute is
*[other] these attributes are
} not allowed in this context"
)]

@fmease fmease Nov 6, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I know, the diagnostic message is quite vague.

If I could determine whether we're "not early", I could specialize this message to say "inert attributes aren't allowed in this context" but then again, I haven't found any preexisting (user-facing) diagnostics that used the term "inert"... but maybe it would be fine to use that jargon...

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is a good compromise between not throwing jargon at users and being clear what is wrong

@fmease fmease added the F-stmt_expr_attributes `#![feature(stmt_expr_attributes)]` label Nov 6, 2026
@fmease
fmease force-pushed the early-attr-nested-attr branch 2 times, most recently from 677552e to b400fb1 Compare November 6, 2026 18:12
@rust-log-analyzer

This comment has been minimized.

@fmease
fmease force-pushed the early-attr-nested-attr branch from b400fb1 to 88cbcf7 Compare November 6, 2026 20:57
@fmease

fmease commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Whoops, downgrade_to_delayed_bug != delay_as_bug. Fixed, we should now be all gucci :)

@JonathanBrouwer

Copy link
Copy Markdown
Member

Just a check, probably not significant but attribute parsing has continued to surprise me perf-wise
@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 9, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 9, 2026
Reject unexpanded attributes in attributes

@JonathanBrouwer JonathanBrouwer left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@bors r+ rollup
This makes sense, thanks!

View changes since this review

@rust-bors

rust-bors Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 88cbcf7 has been approved by JonathanBrouwer

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 9, 2026
@rust-bors

rust-bors Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 9f946e8 (9f946e852f8d1c174198120252b841dbe1c4f41e)
Base parent: fd4400d (fd4400dce238d263ff4239ac9eb902cb9547daf2)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (9f946e8): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (secondary 3.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.0% [2.3%, 3.6%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results (secondary 2.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.4% [2.4%, 2.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 485.883s -> 482.884s (-0.62%)
Artifact size: 406.18 MiB -> 406.25 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 9, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 9, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - #165161 (`rustc_builtin_macros` cleanup, part 9)
 - #165663 (Avoid repeated gated spans snapshots during macro matching)
 - #165637 (library: std: process: Make Command::status platform specific)
 - #163571 ([rustdoc] Render `Deref` methods on aliased items)
 - #164712 (Reject unexpanded attributes in attributes)
 - #165516 (Point at array length instead of whole array type)
 - #165674 (Add regression test for issue 118449)
 - #165676 (Make `Proxy` weak reference as it seems that it is not dropped at all)
 - #165695 (Add regression test for unsafe diagnostic attributes)
@rust-bors
rust-bors Bot merged commit d76eff8 into rust-lang:main Nov 9, 2026
15 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 9, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 9, 2026
Rollup merge of #164712 - fmease:early-attr-nested-attr, r=JonathanBrouwer

Reject unexpanded attributes in attributes

Reject "unexpanded" attributes on the "value" of key-value attributes. For "early"[^1] attributes, "unexpanded" means any attribute. For normal attribute, "unexpanded" means inert[^2].

For the early attributes `crate_name`, `crate_type` & `recursion_limit` the motivation for this change is the fact that the result of the expansion wouldn't get used and can't for that matter as the value needs to be known before macro expansion.

For the other kind of early attributes, namely attributes placed on macro calls, such contained attributes wouldn't even get name resolved due to the overarching attribute getting dropped early when the macro call, the target, gets expanded.

"Non-early", so normal, attributes didn't get parsed by the attribute parsers, so they weren't semantically validated at all. While it shouldn't be that hard to recursively validate nested attributes using the attribute parsers (e.g., `#[doc = #[doc = #[repr(unknown)] ""] ""]`), it's not worth the complexity. Moreover, it's not clear whether we can easily support such attributes in the rest of the compiler. For example, would our lint infra properly support `#[doc = #[deny(rustdoc::invalid_html_tag)] "text"]` out of the box? Let's not make life complicated and just forbid inert attributes in such contexts for now at least.

This is not a breaking change since such attributes are gated behind unstable feature `stmt_expr_attributes` (plus `proc_macro_hygiene` if you want to use an attribute macro).

Fixes #163330.

r? JonathanBrouwer or reassign

<sub>(No LLM was or will be used by me during the entire creation process of this PR)</sub>

[^1]: Attributes that "need to be known" very early during compilation. Those are `#![crate_name]`, `#![crate_type]`, `#![recursion_limit]` and any attributes places on macro calls.
[^2]: Note that since PR #159580 we also reject `cfg` on expressions in *any* context (despite not being inert -- a special case to prevent footguns).
@fmease
fmease deleted the early-attr-nested-attr branch November 9, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) F-stmt_expr_attributes `#![feature(stmt_expr_attributes)]` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

"Early" attrs crate_name, recursion_limit & crate_type can be set to #[attribute] "string literal" causing them to misbehave in various ways

5 participants