-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #100250 - cjgillot:recover-token-stream, r=Aaron1011
Manually cleanup token stream when macro expansion aborts. In case of syntax error in macro expansion, the expansion code can decide to stop processing anything. In that case, the token stream is malformed. This makes downstream users, like derive macros, ICE. In this case, this PR manually cleans up the token stream by closing all currently open delimiters. Fixes #96818. Fixes #80447. Fixes #81920. Fixes #91023.
- Loading branch information
Showing
4 changed files
with
84 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
macro_rules! values { | ||
($($token:ident($value:literal) $(as $inner:ty)? => $attr:meta,)*) => { | ||
#[derive(Debug)] | ||
pub enum TokenKind { | ||
$( | ||
#[$attr] | ||
$token $($inner)? = $value, | ||
)* | ||
} | ||
}; | ||
} | ||
//~^^^^^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found `(String)` | ||
//~| ERROR macro expansion ignores token `(String)` and any following | ||
|
||
values!(STRING(1) as (String) => cfg(test),); | ||
//~^ ERROR expected one of `!` or `::`, found `<eof>` | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `(String)` | ||
--> $DIR/syntax-error-recovery.rs:7:26 | ||
| | ||
LL | $token $($inner)? = $value, | ||
| ^^^^^^ expected one of `(`, `,`, `=`, `{`, or `}` | ||
... | ||
LL | values!(STRING(1) as (String) => cfg(test),); | ||
| -------------------------------------------- in this macro invocation | ||
| | ||
= note: this error originates in the macro `values` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: macro expansion ignores token `(String)` and any following | ||
--> $DIR/syntax-error-recovery.rs:7:26 | ||
| | ||
LL | $token $($inner)? = $value, | ||
| ^^^^^^ | ||
... | ||
LL | values!(STRING(1) as (String) => cfg(test),); | ||
| -------------------------------------------- caused by the macro expansion here | ||
| | ||
= note: the usage of `values!` is likely invalid in item context | ||
|
||
error: expected one of `!` or `::`, found `<eof>` | ||
--> $DIR/syntax-error-recovery.rs:15:9 | ||
| | ||
LL | values!(STRING(1) as (String) => cfg(test),); | ||
| ^^^^^^ expected one of `!` or `::` | ||
|
||
error: aborting due to 3 previous errors | ||
|