$crate in a macro pattern behaves oddly #99037
Labels
A-macros
Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
C-bug
Category: This is a bug.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
It is my opinion that this should not have been allowed in the first place, and should be forward-compat deprecated for eventual removal.
I tried this code:
I expected to see this happen:
A compiler error. Likely,
Instead, this happened:
The macro compiles without any errors. Calling such a macro is difficult:
The following illustrates what is going on here:
All of these calls work. What is happening is that
$crate
even in macro patterns is getting glued into a single identifier token. Thus, in the macro pattern, it can only be fulfilled by another glued$crate
token, which is only possible do in the expansion of a macro.I think it is better to forbid this usage, as it is strongly inconsistent with the behavior of other keywords in macro binders (they work like any other identifier and are currently not reserved in this position), and
$crate
is taught asWhile both of these are subtly wrong (
$crate
can be observed to "expand" into a single identifier), they agree that$crate
is semantically a "reserved binder" which expands to the crate that the containingmacro_rules!
is defined in.The current behavior of
$crate
in macro pattern position is as a compound token (only producible with macros) is incompatible with this understanding of$crate
, which is otherwise (mostly) correct. The behavior of$crate
in pattern position should by this definition be to$crate
is a reserved keyword name that cannot be used as a custom binder.Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: