Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix range of unparenthesized tuple subject in match statement #8101

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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