Skip to content

Commit d25c8a8

Browse files
committed
Handle a negated literal in eat_token_lit.
Fixes #139495.
1 parent d4f880f commit d25c8a8

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

compiler/rustc_parse/src/parser/expr.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -2166,10 +2166,15 @@ impl<'a> Parser<'a> {
21662166
let expr = self
21672167
.eat_metavar_seq(mv_kind, |this| this.parse_expr())
21682168
.expect("metavar seq expr");
2169-
let ast::ExprKind::Lit(token_lit) = expr.kind else {
2170-
panic!("didn't reparse an expr");
2171-
};
2172-
Some(token_lit)
2169+
if let ast::ExprKind::Lit(token_lit) = expr.kind {
2170+
Some(token_lit)
2171+
} else if let ast::ExprKind::Unary(UnOp::Neg, inner) = &expr.kind
2172+
&& let ast::Expr { kind: ast::ExprKind::Lit(_), .. } = **inner
2173+
{
2174+
None
2175+
} else {
2176+
panic!("unexpected reparsed expr: {:?}", expr.kind);
2177+
}
21732178
}
21742179
_ => None,
21752180
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
macro_rules! m {
2+
($abi : expr) => { extern $abi } //~ ERROR expected expression, found keyword `extern`
3+
}
4+
5+
fn main() {
6+
m!(-2)
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: expected expression, found keyword `extern`
2+
--> $DIR/reparse-expr-issue-139495.rs:2:22
3+
|
4+
LL | ($abi : expr) => { extern $abi }
5+
| ^^^^^^ expected expression
6+
...
7+
LL | m!(-2)
8+
| ------ in this macro invocation
9+
|
10+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error: aborting due to 1 previous error
13+

0 commit comments

Comments
 (0)