Macro Invocations Behave Questionable Regarding Semicolons #70432
Labels
A-frontend
Area: Compiler frontend (errors, parsing and HIR)
A-macros
Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
A-parser
Area: The parsing of Rust source code to an AST
C-bug
Category: This is a bug.
T-lang
Relevant to the language team, which will review and decide on the PR/issue.
Calling Statement Macros with “return values”?
Macros can be used both for expressions and for sequences of statements. The latter being useful to create new definitions (e.g. one or even multiple
let
clause(s)).I’m observing that the general rule that expressions ending in a “Block” of type
()
don’t need a semicolon when used as a statement translates in a weird way to even macros with multiple statements. For examplecan’t be used as
s!{x}
. Howevers!{x};
does work (as doess!(x);
). I’m questioning how sensible it is to support these kind of macros since they have some kind of “return value”, whose type even is relevant for if they need a trailing;
, while the “return value” cannot actually in any way (that I can think of) be retrieved.A way nicer approach at resolving the non-working
s!{x}
is of course to change the macro:Ignored Semicolon
One (or at least I) would expect the macro
m1
below to produce just a statement (judging from the semicolon), i.e. be illegal in expression context. However it somehow does pass as an expression, totally ignoring the semicolon. In the return position of a function it does produce()
though [when called likem1!{}
– on the other handm1!()
would still produce the integer].(Playground)
Output:
Errors:
The text was updated successfully, but these errors were encountered: