Skip to content

Commit edf5995

Browse files
authored
Merge pull request #4552 from charliermarsh/charlie/loc
Limit match range to end of last statement
2 parents 6b2efdc + 57efe6f commit edf5995

4 files changed

+109
-85
lines changed

compiler/parser/python.lalrpop

+27-3
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,15 @@ CompoundStatement: ast::Stmt = {
358358
};
359359

360360
MatchStatement: ast::Stmt = {
361-
<location:@L> "match" <subject:TestOrStarNamedExpr> ":" "\n" Indent <cases:MatchCase+> Dedent <end_location:@R> => {
361+
<location:@L> "match" <subject:TestOrStarNamedExpr> ":" "\n" Indent <cases:MatchCase+> Dedent => {
362+
let end_location = cases
363+
.last()
364+
.unwrap()
365+
.body
366+
.last()
367+
.unwrap()
368+
.end_location
369+
.unwrap();
362370
ast::Stmt {
363371
location,
364372
end_location: Some(end_location),
@@ -369,7 +377,15 @@ MatchStatement: ast::Stmt = {
369377
}
370378
}
371379
},
372-
<location:@L> "match" <subject:TestOrStarNamedExpr> "," ":" "\n" Indent <cases:MatchCase+> Dedent <end_location:@R> => {
380+
<location:@L> "match" <subject:TestOrStarNamedExpr> "," ":" "\n" Indent <cases:MatchCase+> Dedent => {
381+
let end_location = cases
382+
.last()
383+
.unwrap()
384+
.body
385+
.last()
386+
.unwrap()
387+
.end_location
388+
.unwrap();
373389
ast::Stmt {
374390
location,
375391
end_location: Some(end_location),
@@ -380,7 +396,15 @@ MatchStatement: ast::Stmt = {
380396
}
381397
}
382398
},
383-
<location:@L> "match" <subject:TestOrStarNamedExpr> "," <subjects:OneOrMore<TestOrStarNamedExpr>> ","? ":" "\n" Indent <cases:MatchCase+> Dedent <end_location:@R> => {
399+
<location:@L> "match" <subject:TestOrStarNamedExpr> "," <subjects:OneOrMore<TestOrStarNamedExpr>> ","? ":" "\n" Indent <cases:MatchCase+> Dedent => {
400+
let end_location = cases
401+
.last()
402+
.unwrap()
403+
.body
404+
.last()
405+
.unwrap()
406+
.end_location
407+
.unwrap();
384408
let mut subjects = subjects;
385409
subjects.insert(0, subject);
386410
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)