Skip to content

Error recovery after errors during macro expansion is usually futile #116180

@Noratrieb

Description

@Noratrieb

Code

// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=13d58799822d542a2c53296f715ac44c
macro_rules! define {
    (enum $name:ident) => {
        struct $name {}
    };
}

define! {
    struct Hello
}

fn main() {
    Hello;
}

Current output

error: no rules expected the token `struct`
 --> src/main.rs:8:5
  |
1 | macro_rules! define {
  | ------------------- when calling this macro
...
8 |     struct Hello
  |     ^^^^^^ no rules expected this token in macro call
  |
note: while trying to match `enum`
 --> src/main.rs:2:6
  |
2 |     (enum $name:ident) => {
  |      ^^^^

error[E0425]: cannot find value `Hello` in this scope
  --> src/main.rs:12:5
   |
12 |     Hello;
   |     ^^^^^ not found in this scope

Desired output

error: no rules expected the token `struct`
 --> src/main.rs:8:5
  |
1 | macro_rules! define {
  | ------------------- when calling this macro
...
8 |     struct Hello
  |     ^^^^^^ no rules expected this token in macro call
  |
note: while trying to match `enum`
 --> src/main.rs:2:6
  |
2 |     (enum $name:ident) => {
  |      ^^^^

Rationale and extra context

This is a concrete example, but the problem is very general. When macro expansion goes wrong, anything might have not happened as a result, for example defining types and other things. In my experience, this frequently causes cascading useless errors, mostly but not exclusively from name resolution. This makes it very hard to work with the output of cargo check when dealing with macro expansion errors in my experience.

We should, when there are errors during macro expansion, abort compilation after macro expansion without any further recovery. It's impossible to know which errors are actually useful and in many cases there are a lot of useless ones.

With errors during macro expansion I mean any errors that stop macros from being expanded, for example matching mismatches or macros not being found.

Note that errors before macro expansion, like parser errors, tend to be fairly recoverable and we should still do recovery on them, this is specifically about the case where the error count increases during macro expansion.

Other cases

No response

Anything else?

cc @rust-lang/wg-diagnostics

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions