inline-const is not an expr in macros, but it should be · Issue #84155 · rust-lang/rust (original) (raw)

While writing a macro thingy that generates match blocks, I had a problem where it was previously using an expr type, but that didn't work[1], as it didn't accept the inline const as an expr, so I tried putting it as a pat instead, but that didn't work[2] as pats aren't accepted in exprs, even if they would be valid in text form.

[1] The subject of this bug report:

macro_rules! doggo { ($v:expr) => {4}; }

fn main() { doggo!(const { 2 }); }

Compile failure (the subject of this bug):

error: no rules expected the token `const`
 --> src/main.rs:8:12
  |
1 | macro_rules! doggo {
  | ------------------ when calling this macro
...
8 |     doggo!(const { 2 });
  |            ^^^^^ no rules expected this token in macro call

error: aborting due to previous error

error: could not compile `rusttest`

[2] The thing I tried, that I think is probably reasonable behaviour, although the error needs improvement:

#![feature(inline_const)]

macro_rules! doggo { ($v:pat) => { $v }; }

fn main() { let v = doggo!(const { 2 }); }

Error:

error: expected expression, found `const{ 2 }`
  --> src/main.rs:5:9
   |
5  |         $v
   |         ^^ expected expression
...
10 |     let v = doggo!(const { 2 });
   |             ------------------- in this macro invocation
   |
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

Meta

rustc --version --verbose:

rustc 1.53.0-nightly (d0695c908 2021-04-12)
binary: rustc
commit-hash: d0695c9081b16077d0aed368bccaf437d77ff497
commit-date: 2021-04-12
host: x86_64-unknown-linux-gnu
release: 1.53.0-nightly
LLVM version: 12.0.0