Skip to content

Commit f0fe6d6

Browse files
authored
Fix syntax error false positive on nested alternative patterns (#21104)
## Summary Fixes #21101 by storing the child visitor's names in the parent visitor. This makes sure that `visitor.names` on line 1818 isn't empty after we visit a nested OR pattern. ## Test Plan New inline test cases derived from the issue, [playground](https://play.ruff.rs/7b6439ac-ee8f-4593-9a3e-c2aa34a595d0)
1 parent 10bda3d commit f0fe6d6

File tree

3 files changed

+512
-0
lines changed

3 files changed

+512
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
match ruff:
2+
case {"lint": {"select": x} | {"extend-select": x}} | {"select": x}:
3+
...
4+
match 42:
5+
case [[x] | [x]] | x: ...
6+
match 42:
7+
case [[x | x] | [x]] | x: ...

crates/ruff_python_parser/src/semantic_errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,13 +1841,23 @@ impl<'a, Ctx: SemanticSyntaxContext> MatchPatternVisitor<'a, Ctx> {
18411841
// case (x, (y | y)): ...
18421842
// case [a, _] | [a, _]: ...
18431843
// case [a] | [C(a)]: ...
1844+
1845+
// test_ok nested_alternative_patterns
1846+
// match ruff:
1847+
// case {"lint": {"select": x} | {"extend-select": x}} | {"select": x}:
1848+
// ...
1849+
// match 42:
1850+
// case [[x] | [x]] | x: ...
1851+
// match 42:
1852+
// case [[x | x] | [x]] | x: ...
18441853
SemanticSyntaxChecker::add_error(
18451854
self.ctx,
18461855
SemanticSyntaxErrorKind::DifferentMatchPatternBindings,
18471856
*range,
18481857
);
18491858
break;
18501859
}
1860+
self.names = visitor.names;
18511861
}
18521862
}
18531863
}

0 commit comments

Comments
 (0)