Skip to content

Commit 57efe6f

Browse files
committed
Limit match range to end of last statement
1 parent 6d71f75 commit 57efe6f

4 files changed

+109
-85
lines changed

compiler/parser/python.lalrpop

+27-3
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 {

compiler/parser/src/snapshots/rustpython_parser__parser__tests__match.snap

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/parser/src/snapshots/rustpython_parser__parser__tests__match_as_identifier.snap

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)