Skip to content

Commit 15f8847

Browse files
authored
Rollup merge of #140144 - nnethercote:fix-140098, r=petrochenkov
Handle another negated literal in `eat_token_lit`. Extends the change from #139653, which was on expressions, to literals. Fixes #140098. r? ``@petrochenkov``
2 parents 9471811 + 6be270b commit 15f8847

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

Diff for: compiler/rustc_parse/src/parser/expr.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,17 @@ impl<'a> Parser<'a> {
21462146
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
21472147
/// `Lit::from_token` (excluding unary negation).
21482148
fn eat_token_lit(&mut self) -> Option<token::Lit> {
2149+
let check_expr = |expr: P<Expr>| {
2150+
if let ast::ExprKind::Lit(token_lit) = expr.kind {
2151+
Some(token_lit)
2152+
} else if let ast::ExprKind::Unary(UnOp::Neg, inner) = &expr.kind
2153+
&& let ast::Expr { kind: ast::ExprKind::Lit(_), .. } = **inner
2154+
{
2155+
None
2156+
} else {
2157+
panic!("unexpected reparsed expr/literal: {:?}", expr.kind);
2158+
}
2159+
};
21492160
match self.token.uninterpolate().kind {
21502161
token::Ident(name, IdentIsRaw::No) if name.is_bool_lit() => {
21512162
self.bump();
@@ -2159,26 +2170,15 @@ impl<'a> Parser<'a> {
21592170
let lit = self
21602171
.eat_metavar_seq(MetaVarKind::Literal, |this| this.parse_literal_maybe_minus())
21612172
.expect("metavar seq literal");
2162-
let ast::ExprKind::Lit(token_lit) = lit.kind else {
2163-
panic!("didn't reparse a literal");
2164-
};
2165-
Some(token_lit)
2173+
check_expr(lit)
21662174
}
21672175
token::OpenInvisible(InvisibleOrigin::MetaVar(
21682176
mv_kind @ MetaVarKind::Expr { can_begin_literal_maybe_minus: true, .. },
21692177
)) => {
21702178
let expr = self
21712179
.eat_metavar_seq(mv_kind, |this| this.parse_expr())
21722180
.expect("metavar seq expr");
2173-
if let ast::ExprKind::Lit(token_lit) = expr.kind {
2174-
Some(token_lit)
2175-
} else if let ast::ExprKind::Unary(UnOp::Neg, inner) = &expr.kind
2176-
&& let ast::Expr { kind: ast::ExprKind::Lit(_), .. } = **inner
2177-
{
2178-
None
2179-
} else {
2180-
panic!("unexpected reparsed expr: {:?}", expr.kind);
2181-
}
2181+
check_expr(expr)
21822182
}
21832183
_ => None,
21842184
}

Diff for: tests/ui/macros/reparse-expr-issue-139495.rs

+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
}

Diff for: tests/ui/macros/reparse-expr-issue-139495.stderr

+18-7
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)