You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Attribute macro can replace item with expression. #144716
The macro invocation is in a statement position, so it can produce multiple statements, including expression statements.
So it's not necessarily an item.
Expression statements do not produce values though, right? If the macro expanded to an expression statement (as opposed to a non-statement expression), I would not have been able to print the value "weird".
Yes, I believe the reference ought to mention the special case of the without-semicolon "block statement", whose value is not necessarily always discarded. Do you want to open an issue about it?
IMO, this shouldn't work. It appears to violate the tree structure of the language in a way that macros usually can't (e.g. foo(a, b!(), c) always has 3 arguments), and thus surprisingly reduces the ability to read Rust without knowing everything about every involved macro expansion.
Activity
danielhenrymantilla commentedon Jul 31, 2025
This might be an issue with the reference rather than a bug in implementation?
Noratrieb commentedon Jul 31, 2025
@petrochenkov about your opinion
petrochenkov commentedon Jul 31, 2025
The macro invocation is in a statement position, so it can produce multiple statements, including expression statements.
So it's not necessarily an item.
theemathas commentedon Jul 31, 2025
Expression statements do not produce values though, right? If the macro expanded to an expression statement (as opposed to a non-statement expression), I would not have been able to print the value
"weird".danielhenrymantilla commentedon Jul 31, 2025
They can when within braces:
theemathas commentedon Jul 31, 2025
And..... the reference says that the result of an expression statement is always ignored.
This function compiles, and returns 42:
Based on the grammar in the reference, I believe that the
{ 42 }must be an expression statement. However, the result of it is clearly not ignored.Either the implementation or the reference is also wrong here.
danielhenrymantilla commentedon Jul 31, 2025
Yes, I believe the reference ought to mention the special case of the without-semicolon "block statement", whose value is not necessarily always discarded. Do you want to open an issue about it?
kpreid commentedon Jul 31, 2025
IMO, this shouldn't work. It appears to violate the tree structure of the language in a way that macros usually can't (e.g.
foo(a, b!(), c)always has 3 arguments), and thus surprisingly reduces the ability to read Rust without knowing everything about every involved macro expansion.petrochenkov commentedon Jul 31, 2025
What about
foo(a, #[my_cfg(false)] b, c), how many arguments does it have?