-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Description
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