Skip to content

Wrong diagnostic for empty macro bodies #55037

@Bert-Proesmans

Description

@Bert-Proesmans

This issue was encountered while designing an API for my upcoming library and is related to empty (stub) macros.
The compiler gives a confusing error description about expecting an expression when the stub macro is called. This is a diagnostic issue.

Consider this case (playground link)

macro_rules! r {
    ($($x:tt)*) => {};
}
fn do<X>(x: X) { unimplemented!() }

fn main() { 
    do(r![
        do(Ok(12)),
        do(Err(12)),
    ]);
}

Attempting to compile this example returns the following error exactly

Compiling playground v0.0.1 (file:///playground)
error: expected expression, found `<eof>`
error: aborting due to previous error

Edit: Tested on the current stable and nightly compiler, 1.29.2 and 1.31 respectively.

This error is technically correct when you understand how the parser works, but really difficult to debug when you encounter it (at random) in a more complex example.
I would expect the error description to be more precise towards the macro context while also providing line numbers and code highlights. After all the compiler expects the macro to expand into an expression.

The above example can be made to compile by filling in the macro body. The macro unimplemented!() would be a semantically fitting option (in my opinion), but it causes unreachable code warnings. Returning () (empty tuple object) within the macro body works as well without these warnings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions