bad diagnostic when macro tries to use stmt as expr

7dbd011
Opened by Alex Burka at 2023-01-08 21:09:40

Given this macro:

macro_rules! foo {
    ($s:stmt) => { $s }
}

When called in expression context, for example println!("{}", foo!(42)), the error message refers to "unexpected token 42;", thereby conjuring a semicolon out of the ether. We aren't JS so we shouldn't conjure semicolons out of the ether :)

  1. So if we follow the trace; https://github.com/rust-lang/rust/blob/master/src/libsyntax/parse/parser.rs#L398 https://github.com/rust-lang/rust/blob/master/src/libsyntax/parse/parser.rs#L387 https://github.com/rust-lang/rust/blob/master/src/libsyntax/print/pprust.rs#L249

    I think the "error" comes from these lines.

    Guillaume Gomez at 2016-01-05 12:45:39

  2. @GuillaumeGomez Hmm, I guess it must be that the "statement" 42 ends up as essentially Literal(Integer(42), Some(intern(";")))?

    Alex Burka at 2016-01-06 05:58:51

  3. So we still produce a semicolon, but the error is much better (though still far from ideal):

    error: expected expression, found `42;`
     --> test.rs:2:20
      |
    2 |     ($s:stmt) => { $s }
      |                    ^^ expected expression
    ...
    6 |     println!("{}", foo!(42));
      |                    -------- in this macro invocation
    

    Mark Rousskov at 2017-07-24 19:39:53

  4. The current output provides an incorrect suggestion:

    error: expected expression, found `42;`
     --> src/main.rs:2:20
      |
    2 |     ($s:stmt) => { $s }
      |                    ^^ expected expression
    ...
    6 |     println!("{}", foo!(42));
      |                    -------- in this macro invocation
      |
      = note: the macro call doesn't expand to an expression, but it can expand to a statement
      = note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
    help: add `;` to interpret the expansion as a statement
      |
    6 |     println!("{}", foo!(42););
      |                            +
    

    Esteban Kuber at 2022-06-08 21:44:11

  5. Triage: no change.

    Esteban Kuber at 2023-01-08 21:09:31