Skip to content

Commit 7ebef61

Browse files
committed
Limit match range to end of last statement
1 parent b61f4d7 commit 7ebef61

4 files changed

+109
-85
lines changed

parser/python.lalrpop

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,15 @@ CompoundStatement: ast::Stmt = {
359359
};
360360

361361
MatchStatement: ast::Stmt = {
362-
<location:@L> "match" <subject:TestOrStarNamedExpr> ":" "\n" Indent <cases:MatchCase+> Dedent <end_location:@R> => {
362+
<location:@L> "match" <subject:TestOrStarNamedExpr> ":" "\n" Indent <cases:MatchCase+> Dedent => {
363+
let end_location = cases
364+
.last()
365+
.unwrap()
366+
.body
367+
.last()
368+
.unwrap()
369+
.end_location
370+
.unwrap();
363371
ast::Stmt {
364372
location,
365373
end_location: Some(end_location),
@@ -370,7 +378,15 @@ MatchStatement: ast::Stmt = {
370378
}
371379
}
372380
},
373-
<location:@L> "match" <subject:TestOrStarNamedExpr> "," ":" "\n" Indent <cases:MatchCase+> Dedent <end_location:@R> => {
381+
<location:@L> "match" <subject:TestOrStarNamedExpr> "," ":" "\n" Indent <cases:MatchCase+> Dedent => {
382+
let end_location = cases
383+
.last()
384+
.unwrap()
385+
.body
386+
.last()
387+
.unwrap()
388+
.end_location
389+
.unwrap();
374390
ast::Stmt {
375391
location,
376392
end_location: Some(end_location),
@@ -381,7 +397,15 @@ MatchStatement: ast::Stmt = {
381397
}
382398
}
383399
},
384-
<location:@L> "match" <subject:TestOrStarNamedExpr> "," <subjects:OneOrMore<TestOrStarNamedExpr>> ","? ":" "\n" Indent <cases:MatchCase+> Dedent <end_location:@R> => {
400+
<location:@L> "match" <subject:TestOrStarNamedExpr> "," <subjects:OneOrMore<TestOrStarNamedExpr>> ","? ":" "\n" Indent <cases:MatchCase+> Dedent => {
401+
let end_location = cases
402+
.last()
403+
.unwrap()
404+
.body
405+
.last()
406+
.unwrap()
407+
.end_location
408+
.unwrap();
385409
let mut subjects = subjects;
386410
subjects.insert(0, subject);
387411
ast::Stmt {

parser/src/snapshots/rustpython_parser__parser__tests__match.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ expression: parse_ast
1010
},
1111
end_location: Some(
1212
Location {
13-
row: 7,
14-
column: 0,
13+
row: 6,
14+
column: 19,
1515
},
1616
),
1717
custom: (),
@@ -178,8 +178,8 @@ expression: parse_ast
178178
},
179179
end_location: Some(
180180
Location {
181-
row: 12,
182-
column: 0,
181+
row: 11,
182+
column: 20,
183183
},
184184
),
185185
custom: (),
@@ -453,8 +453,8 @@ expression: parse_ast
453453
},
454454
end_location: Some(
455455
Location {
456-
row: 15,
457-
column: 0,
456+
row: 14,
457+
column: 13,
458458
},
459459
),
460460
custom: (),
@@ -631,8 +631,8 @@ expression: parse_ast
631631
},
632632
end_location: Some(
633633
Location {
634-
row: 18,
635-
column: 0,
634+
row: 17,
635+
column: 13,
636636
},
637637
),
638638
custom: (),
@@ -809,8 +809,8 @@ expression: parse_ast
809809
},
810810
end_location: Some(
811811
Location {
812-
row: 21,
813-
column: 0,
812+
row: 20,
813+
column: 13,
814814
},
815815
),
816816
custom: (),

parser/src/snapshots/rustpython_parser__parser__tests__match_as_identifier.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,8 +1538,8 @@ expression: parse_ast
15381538
},
15391539
end_location: Some(
15401540
Location {
1541-
row: 21,
1542-
column: 0,
1541+
row: 20,
1542+
column: 12,
15431543
},
15441544
),
15451545
custom: (),

0 commit comments

Comments
 (0)