Fix range of unparenthesized tuple subject in match statement (#8101)

## 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 https://github.com/astral-sh/ruff/issues/8091.

## Test Plan

`cargo test`
This commit is contained in:
Charlie Marsh 2023-10-22 16:58:33 -07:00 committed by GitHub
parent 95702e408f
commit d6a4283003
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 2763 additions and 2369 deletions

View file

@ -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>",
)

View file

@ -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()
@ -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()
@ -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,

File diff suppressed because it is too large Load diff

View file

@ -445,4 +445,153 @@ expression: parse_ast
],
},
),
Match(
StmtMatch {
range: 298..332,
subject: Tuple(
ExprTuple {
range: 304..306,
elts: [
Name(
ExprName {
range: 304..305,
id: "x",
ctx: Load,
},
),
],
ctx: Load,
},
),
cases: [
MatchCase {
range: 312..332,
pattern: MatchAs(
PatternMatchAs {
range: 317..318,
pattern: None,
name: Some(
Identifier {
id: "z",
range: 317..318,
},
),
},
),
guard: None,
body: [
Pass(
StmtPass {
range: 328..332,
},
),
],
},
],
},
),
Match(
StmtMatch {
range: 333..369,
subject: Tuple(
ExprTuple {
range: 339..343,
elts: [
Name(
ExprName {
range: 339..340,
id: "x",
ctx: Load,
},
),
Name(
ExprName {
range: 342..343,
id: "y",
ctx: Load,
},
),
],
ctx: Load,
},
),
cases: [
MatchCase {
range: 349..369,
pattern: MatchAs(
PatternMatchAs {
range: 354..355,
pattern: None,
name: Some(
Identifier {
id: "z",
range: 354..355,
},
),
},
),
guard: None,
body: [
Pass(
StmtPass {
range: 365..369,
},
),
],
},
],
},
),
Match(
StmtMatch {
range: 370..407,
subject: Tuple(
ExprTuple {
range: 376..381,
elts: [
Name(
ExprName {
range: 376..377,
id: "x",
ctx: Load,
},
),
Name(
ExprName {
range: 379..380,
id: "y",
ctx: Load,
},
),
],
ctx: Load,
},
),
cases: [
MatchCase {
range: 387..407,
pattern: MatchAs(
PatternMatchAs {
range: 392..393,
pattern: None,
name: Some(
Identifier {
id: "z",
range: 392..393,
},
),
},
),
guard: None,
body: [
Pass(
StmtPass {
range: 403..407,
},
),
],
},
],
},
),
]

View file

@ -3449,10 +3449,18 @@ expression: parse_ast
Match(
StmtMatch {
range: 2661..2697,
subject: Name(
ExprName {
range: 2667..2668,
id: "x",
subject: Tuple(
ExprTuple {
range: 2667..2669,
elts: [
Name(
ExprName {
range: 2667..2668,
id: "x",
ctx: Load,
},
),
],
ctx: Load,
},
),
@ -3512,7 +3520,7 @@ expression: parse_ast
range: 2720..2760,
subject: Tuple(
ExprTuple {
range: 2720..2760,
range: 2726..2730,
elts: [
Name(
ExprName {
@ -3598,23 +3606,31 @@ expression: parse_ast
Match(
StmtMatch {
range: 2783..2829,
subject: NamedExpr(
ExprNamedExpr {
range: 2789..2795,
target: Name(
ExprName {
range: 2789..2790,
id: "w",
ctx: Store,
},
),
value: Name(
ExprName {
range: 2794..2795,
id: "x",
ctx: Load,
},
),
subject: Tuple(
ExprTuple {
range: 2789..2796,
elts: [
NamedExpr(
ExprNamedExpr {
range: 2789..2795,
target: Name(
ExprName {
range: 2789..2790,
id: "w",
ctx: Store,
},
),
value: Name(
ExprName {
range: 2794..2795,
id: "x",
ctx: Load,
},
),
},
),
],
ctx: Load,
},
),
cases: [