Skip to content

Grammar: Don't allow inner attrs in certain block expressions#2269

Open
fmease wants to merge 1 commit into
rust-lang:masterfrom
fmease:block-expr-no-inner-attrs
Open

Grammar: Don't allow inner attrs in certain block expressions#2269
fmease wants to merge 1 commit into
rust-lang:masterfrom
fmease:block-expr-no-inner-attrs

Conversation

@fmease
Copy link
Copy Markdown
Member

@fmease fmease commented May 11, 2026

rustc doesn't syntactically permit inner attributes (1) in the consequent & alternate blocks of if-expressions, (2) in the alternate block of let-statements (let/else) and (3) in blocks matched by macro metavariables of type block.

Intuitively that makes sense since inner attributes generally target the "owner" of the surrounding block (e.g., the overarching function item or while-expression), neither "the block itself" nor the sequence of statements in said block (so fn f(_: Undef) { #![cfg(false)] } = #[cfg(false)] fn f(_: Undef) {} and while undef { #![cfg(false)] } = #[cfg(false)] while undef {}) which would be very surprising for if-exprs and let/else stmts I feel like and straight up ill-defined in the MBE case.

Concretely, it would mean that if undef { /*…*/ } else if undef { /*…*/ } else { #![cfg(false)] } would be equivalent to #[cfg(false)] if …; similarly for let self::undef = /*…*/ else { #![cfg(false)] }; / #[cfg(false)] let …;.

I'm not sure if that's the "official" reasoning, I've yet to find that out. As far as I can tell, inner attributes have been illegal in then/else blocks since version 1.0 at least1.

Fixes #2212.

Footnotes

  1. However, back in version 1.0 inner attributes weren't permitted in most expressions with blocks like in while expressions or "normal" block exprs either (but they were already allowed in the block of function items) which were all lifted at some point except for if-expressions, of course.

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label May 11, 2026
Statements?
`}`
BlockExpressionNoInnerAttributes ->
Copy link
Copy Markdown
Member Author

@fmease fmease May 11, 2026

Choose a reason for hiding this comment

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

Naming inspired by TypeNoBounds.

View changes since the review

@fmease fmease force-pushed the block-expr-no-inner-attrs branch from 5389944 to 255b7aa Compare May 12, 2026 01:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: The marked PR is awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documentation of the block macro metavariable syntax is confusing.

2 participants