Skip to content

Commit 9bb23b0

Browse files
authored
Expect indented case block instead of match stmt (#11033)
## Summary This PR adds a new `Clause::Case` and uses it to parse the body of a `case` block. Earlier, it was using `Match` which would give an incorrect error message like: ``` | 1 | match subject: 2 | case 1: 3 | case 2: ... | ^^^^ Syntax Error: Expected an indented block after `match` statement | ``` ## Test Plan Add test case and update the snapshot.
1 parent 06c248a commit 9bb23b0

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
match subject:
2+
case 1:
3+
case 2: ...

crates/ruff_python_parser/src/parser/statement.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -2492,7 +2492,12 @@ impl<'src> Parser<'src> {
24922492
};
24932493

24942494
self.expect(TokenKind::Colon);
2495-
let body = self.parse_body(Clause::Match);
2495+
2496+
// test_err case_expect_indented_block
2497+
// match subject:
2498+
// case 1:
2499+
// case 2: ...
2500+
let body = self.parse_body(Clause::Case);
24962501

24972502
ast::MatchCase {
24982503
pattern,
@@ -3363,7 +3368,7 @@ enum Clause {
33633368
Class,
33643369
While,
33653370
FunctionDef,
3366-
Match,
3371+
Case,
33673372
Try,
33683373
Except,
33693374
Finally,
@@ -3380,7 +3385,7 @@ impl Display for Clause {
33803385
Clause::Class => write!(f, "`class` definition"),
33813386
Clause::While => write!(f, "`while` statement"),
33823387
Clause::FunctionDef => write!(f, "function definition"),
3383-
Clause::Match => write!(f, "`match` statement"),
3388+
Clause::Case => write!(f, "`case` block"),
33843389
Clause::Try => write!(f, "`try` statement"),
33853390
Clause::Except => write!(f, "`except` clause"),
33863391
Clause::Finally => write!(f, "`finally` clause"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
source: crates/ruff_python_parser/tests/fixtures.rs
3+
input_file: crates/ruff_python_parser/resources/inline/err/case_expect_indented_block.py
4+
---
5+
## AST
6+
7+
```
8+
Module(
9+
ModModule {
10+
range: 0..43,
11+
body: [
12+
Match(
13+
StmtMatch {
14+
range: 0..42,
15+
subject: Name(
16+
ExprName {
17+
range: 6..13,
18+
id: "subject",
19+
ctx: Load,
20+
},
21+
),
22+
cases: [
23+
MatchCase {
24+
range: 19..26,
25+
pattern: MatchValue(
26+
PatternMatchValue {
27+
range: 24..25,
28+
value: NumberLiteral(
29+
ExprNumberLiteral {
30+
range: 24..25,
31+
value: Int(
32+
1,
33+
),
34+
},
35+
),
36+
},
37+
),
38+
guard: None,
39+
body: [],
40+
},
41+
MatchCase {
42+
range: 31..42,
43+
pattern: MatchValue(
44+
PatternMatchValue {
45+
range: 36..37,
46+
value: NumberLiteral(
47+
ExprNumberLiteral {
48+
range: 36..37,
49+
value: Int(
50+
2,
51+
),
52+
},
53+
),
54+
},
55+
),
56+
guard: None,
57+
body: [
58+
Expr(
59+
StmtExpr {
60+
range: 39..42,
61+
value: EllipsisLiteral(
62+
ExprEllipsisLiteral {
63+
range: 39..42,
64+
},
65+
),
66+
},
67+
),
68+
],
69+
},
70+
],
71+
},
72+
),
73+
],
74+
},
75+
)
76+
```
77+
## Errors
78+
79+
|
80+
1 | match subject:
81+
2 | case 1:
82+
3 | case 2: ...
83+
| ^^^^ Syntax Error: Expected an indented block after `case` block
84+
|

0 commit comments

Comments
 (0)