Skip to content

Commit

Permalink
Fix range of unparenthesized tuple subject in match statement (#8101)
Browse files Browse the repository at this point in the history
## Summary

This was just a bug in the parser ranges, probably since it was
initially implemented. Given `match n % 3, n % 5: ...`, the "subject"
(i.e., the tuple of two binary operators) was using the entire range of
the `match` statement.

Closes #8091.

## Test Plan

`cargo test`
  • Loading branch information
charliermarsh authored Oct 22, 2023
1 parent 95702e4 commit d6a4283
Show file tree
Hide file tree
Showing 7 changed files with 1,746 additions and 1,352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,17 @@ def foo():
# comment
a, b,):
pass

# Tuple subject.
match n % 3, n % 5:
case 0, 0:
# n is divisible by both 3 and 5
print("FizzBuzz")
case 0, _:
# n is divisible by 3, but not 5
print("Fizz")
case _, 0:
# n is divisible by 5, but not 3
print("Buzz")
case _:
print(n)
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,20 @@ match pattern:
# comment
a, b,):
pass
# Tuple subject.
match n % 3, n % 5:
case 0, 0:
# n is divisible by both 3 and 5
print("FizzBuzz")
case 0, _:
# n is divisible by 3, but not 5
print("Fizz")
case _, 0:
# n is divisible by 5, but not 3
print("Buzz")
case _:
print(n)
```

## Output
Expand Down Expand Up @@ -1182,6 +1196,20 @@ match pattern:
b,
):
pass
# Tuple subject.
match n % 3, n % 5:
case 0, 0:
# n is divisible by both 3 and 5
print("FizzBuzz")
case 0, _:
# n is divisible by 3, but not 5
print("Fizz")
case _, 0:
# n is divisible by 5, but not 3
print("Buzz")
case _:
print(n)
```


Expand Down
9 changes: 9 additions & 0 deletions crates/ruff_python_parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,15 @@ match x:
match x:
case (0,):
y = 0
match x,:
case z:
pass
match x, y:
case z:
pass
match x, y,:
case z:
pass
"#,
"<test>",
)
Expand Down
14 changes: 10 additions & 4 deletions crates/ruff_python_parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ MatchStatement: ast::Stmt = {
}
)
},
<location:@L> "match" <subject:TestOrStarNamedExpr> "," ":" "\n" Indent <cases:MatchCase+> Dedent => {
<location:@L> "match" <tuple_location:@L> <subject:TestOrStarNamedExpr> "," <tuple_end_location:@R> ":" "\n" Indent <cases:MatchCase+> Dedent => {
let end_location = cases
.last()
.unwrap()
Expand All @@ -476,13 +476,19 @@ MatchStatement: ast::Stmt = {
.end();
ast::Stmt::Match(
ast::StmtMatch {
subject: Box::new(subject.into()),
subject: Box::new(ast::Expr::Tuple(
ast::ExprTuple {
elts: vec![subject.into()],
ctx: ast::ExprContext::Load,
range: (tuple_location..tuple_end_location).into()
},
)),
cases,
range: (location..end_location).into()
}
)
},
<location:@L> "match" <elts:TwoOrMore<TestOrStarNamedExpr, ",">> ","? ":" "\n" Indent <cases:MatchCase+> Dedent => {
<location:@L> "match" <tuple_location:@L> <elts:TwoOrMore<TestOrStarNamedExpr, ",">> ","? <tuple_end_location:@R> ":" "\n" Indent <cases:MatchCase+> Dedent => {
let end_location = cases
.last()
.unwrap()
Expand All @@ -497,7 +503,7 @@ MatchStatement: ast::Stmt = {
ast::ExprTuple {
elts,
ctx: ast::ExprContext::Load,
range: (location..end_location).into()
range: (tuple_location..tuple_end_location).into()
},
)),
cases,
Expand Down
Loading

0 comments on commit d6a4283

Please sign in to comment.