Skip to content

Commit 6be270b

Browse files
committed
Handle another negated literal in eat_token_lit.
Extends the change from rust-lang#139653, which was on expressions, to literals. Fixes rust-lang#140098.
1 parent b8c54d6 commit 6be270b

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

compiler/rustc_parse/src/parser/expr.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,17 @@ impl<'a> Parser<'a> {
21482148
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
21492149
/// `Lit::from_token` (excluding unary negation).
21502150
fn eat_token_lit(&mut self) -> Option<token::Lit> {
2151+
let check_expr = |expr: P<Expr>| {
2152+
if let ast::ExprKind::Lit(token_lit) = expr.kind {
2153+
Some(token_lit)
2154+
} else if let ast::ExprKind::Unary(UnOp::Neg, inner) = &expr.kind
2155+
&& let ast::Expr { kind: ast::ExprKind::Lit(_), .. } = **inner
2156+
{
2157+
None
2158+
} else {
2159+
panic!("unexpected reparsed expr/literal: {:?}", expr.kind);
2160+
}
2161+
};
21512162
match self.token.uninterpolate().kind {
21522163
token::Ident(name, IdentIsRaw::No) if name.is_bool_lit() => {
21532164
self.bump();
@@ -2163,26 +2174,15 @@ impl<'a> Parser<'a> {
21632174
let lit = self
21642175
.eat_metavar_seq(MetaVarKind::Literal, |this| this.parse_literal_maybe_minus())
21652176
.expect("metavar seq literal");
2166-
let ast::ExprKind::Lit(token_lit) = lit.kind else {
2167-
panic!("didn't reparse a literal");
2168-
};
2169-
Some(token_lit)
2177+
check_expr(lit)
21702178
}
21712179
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
21722180
mv_kind @ MetaVarKind::Expr { can_begin_literal_maybe_minus: true, .. },
21732181
))) => {
21742182
let expr = self
21752183
.eat_metavar_seq(mv_kind, |this| this.parse_expr())
21762184
.expect("metavar seq expr");
2177-
if let ast::ExprKind::Lit(token_lit) = expr.kind {
2178-
Some(token_lit)
2179-
} else if let ast::ExprKind::Unary(UnOp::Neg, inner) = &expr.kind
2180-
&& let ast::Expr { kind: ast::ExprKind::Lit(_), .. } = **inner
2181-
{
2182-
None
2183-
} else {
2184-
panic!("unexpected reparsed expr: {:?}", expr.kind);
2185-
}
2185+
check_expr(expr)
21862186
}
21872187
_ => None,
21882188
}
+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
macro_rules! m {
2-
($abi : expr) => { extern $abi } //~ ERROR expected expression, found keyword `extern`
1+
macro_rules! m1 {
2+
($abi: literal) => { extern $abi } //~ ERROR expected expression, found keyword `extern`
3+
}
4+
5+
macro_rules! m2 {
6+
($abi: expr) => { extern $abi } //~ ERROR expected expression, found keyword `extern`
37
}
48

59
fn main() {
6-
m!(-2)
10+
m1!(-2)
11+
}
12+
13+
fn f() {
14+
m2!(-2)
715
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
error: expected expression, found keyword `extern`
2-
--> $DIR/reparse-expr-issue-139495.rs:2:22
2+
--> $DIR/reparse-expr-issue-139495.rs:2:24
33
|
4-
LL | ($abi : expr) => { extern $abi }
5-
| ^^^^^^ expected expression
4+
LL | ($abi: literal) => { extern $abi }
5+
| ^^^^^^ expected expression
66
...
7-
LL | m!(-2)
8-
| ------ in this macro invocation
7+
LL | m1!(-2)
8+
| ------- in this macro invocation
99
|
10-
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
10+
= note: this error originates in the macro `m1` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

12-
error: aborting due to 1 previous error
12+
error: expected expression, found keyword `extern`
13+
--> $DIR/reparse-expr-issue-139495.rs:6:21
14+
|
15+
LL | ($abi: expr) => { extern $abi }
16+
| ^^^^^^ expected expression
17+
...
18+
LL | m2!(-2)
19+
| ------- in this macro invocation
20+
|
21+
= note: this error originates in the macro `m2` (in Nightly builds, run with -Z macro-backtrace for more info)
22+
23+
error: aborting due to 2 previous errors
1324

0 commit comments

Comments
 (0)