Grammar: Don't allow inner attrs in certain block expressions#2269
Open
fmease wants to merge 1 commit into
Open
Grammar: Don't allow inner attrs in certain block expressions#2269fmease wants to merge 1 commit into
fmease wants to merge 1 commit into
Conversation
fmease
commented
May 11, 2026
| Statements? | ||
| `}` | ||
| BlockExpressionNoInnerAttributes -> |
Member
Author
There was a problem hiding this comment.
Naming inspired by TypeNoBounds.
5389944 to
255b7aa
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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) {}andwhile 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 forlet 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
However, back in version 1.0 inner attributes weren't permitted in most expressions with blocks like in
whileexpressions 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. ↩