[syntax-errors] Alternative match patterns bind different names (#20682)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (macos) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / ty completion evaluation (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks walltime (medium|multithreaded) (push) Blocked by required conditions
CI / benchmarks walltime (small|large) (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->
This PR implements semantic syntax error where alternative patterns bind
different names

## Test Plan

<!-- How was it tested? -->
I have written inline tests as directed in #17412

---------

Signed-off-by: 11happy <soni5happy@gmail.com>
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
This commit is contained in:
Bhuminjay Soni 2025-10-18 03:05:48 +05:30 committed by GitHub
parent 8ca2b5555d
commit 7198e53182
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 1805 additions and 4 deletions

View file

@ -0,0 +1,13 @@
match x:
case [a] | [b]: ...
case [a] | []: ...
case (x, y) | (x,): ...
case [a, _] | [a, b]: ...
case (x, (y | z)): ...
case [a] | [b] | [c]: ...
case [] | [a]: ...
case [a] | [C(x)]: ...
case [[a] | [b]]: ...
case [C(a)] | [C(b)]: ...
case [C(D(a))] | [C(D(b))]: ...
case [(a, b)] | [(c, d)]: ...

View file

@ -0,0 +1,6 @@
match x:
case [a] | [a]: ...
case (x, y) | (x, y): ...
case (x, (y | y)): ...
case [a, _] | [a, _]: ...
case [a] | [C(a)]: ...

View file

@ -1137,6 +1137,9 @@ impl Display for SemanticSyntaxError {
}
SemanticSyntaxErrorKind::BreakOutsideLoop => f.write_str("`break` outside loop"),
SemanticSyntaxErrorKind::ContinueOutsideLoop => f.write_str("`continue` outside loop"),
SemanticSyntaxErrorKind::DifferentMatchPatternBindings => {
write!(f, "alternative patterns bind different names")
}
}
}
}
@ -1516,6 +1519,20 @@ pub enum SemanticSyntaxErrorKind {
/// Represents the use of a `continue` statement outside of a loop.
ContinueOutsideLoop,
/// Represents the use of alternative patterns in a `match` statement that bind different names.
///
/// Python requires all alternatives in an OR pattern (`|`) to bind the same set of names.
/// Using different names results in a `SyntaxError`.
///
/// ## Example:
///
/// ```python
/// match 5:
/// case [x] | [y]: # error
/// ...
/// ```
DifferentMatchPatternBindings,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, get_size2::GetSize)]
@ -1758,7 +1775,9 @@ impl<'a, Ctx: SemanticSyntaxContext> MatchPatternVisitor<'a, Ctx> {
self.insert(name);
}
}
Pattern::MatchOr(ast::PatternMatchOr { patterns, .. }) => {
Pattern::MatchOr(ast::PatternMatchOr {
patterns, range, ..
}) => {
// each of these patterns should be visited separately because patterns can only be
// duplicated within a single arm of the or pattern. For example, the case below is
// a valid pattern.
@ -1766,12 +1785,48 @@ impl<'a, Ctx: SemanticSyntaxContext> MatchPatternVisitor<'a, Ctx> {
// test_ok multiple_assignment_in_case_pattern
// match 2:
// case Class(x) | [x] | x: ...
let mut previous_names: Option<FxHashSet<&ast::name::Name>> = None;
for pattern in patterns {
let mut visitor = Self {
names: FxHashSet::default(),
ctx: self.ctx,
};
visitor.visit_pattern(pattern);
let Some(prev) = &previous_names else {
previous_names = Some(visitor.names);
continue;
};
if prev.symmetric_difference(&visitor.names).next().is_some() {
// test_err different_match_pattern_bindings
// match x:
// case [a] | [b]: ...
// case [a] | []: ...
// case (x, y) | (x,): ...
// case [a, _] | [a, b]: ...
// case (x, (y | z)): ...
// case [a] | [b] | [c]: ...
// case [] | [a]: ...
// case [a] | [C(x)]: ...
// case [[a] | [b]]: ...
// case [C(a)] | [C(b)]: ...
// case [C(D(a))] | [C(D(b))]: ...
// case [(a, b)] | [(c, d)]: ...
// test_ok different_match_pattern_bindings
// match x:
// case [a] | [a]: ...
// case (x, y) | (x, y): ...
// case (x, (y | y)): ...
// case [a, _] | [a, _]: ...
// case [a] | [C(a)]: ...
SemanticSyntaxChecker::add_error(
self.ctx,
SemanticSyntaxErrorKind::DifferentMatchPatternBindings,
*range,
);
break;
}
}
}
}

View file

@ -427,3 +427,12 @@ Module(
| ^^^ Syntax Error: name capture `var` makes remaining patterns unreachable
12 | case 2: ...
|
|
9 | case 2: ...
10 | match x:
11 | case enum.variant | var: ... # or pattern with irrefutable part
| ^^^^^^^^^^^^^^^^^^ Syntax Error: alternative patterns bind different names
12 | case 2: ...
|

View file

@ -613,3 +613,25 @@ Module(
| ^^ Syntax Error: Star pattern cannot be used here
24 | pass
|
## Semantic Syntax Errors
|
7 | case *foo:
8 | pass
9 | case *foo | 1:
| ^^^^^^^^ Syntax Error: alternative patterns bind different names
10 | pass
11 | case 1 | *foo:
|
|
9 | case *foo | 1:
10 | pass
11 | case 1 | *foo:
| ^^^^^^^^ Syntax Error: alternative patterns bind different names
12 | pass
13 | case Foo(*_):
|

View file

@ -0,0 +1,458 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/ok/different_match_pattern_bindings.py
---
## AST
```
Module(
ModModule {
node_index: NodeIndex(None),
range: 0..147,
body: [
Match(
StmtMatch {
node_index: NodeIndex(None),
range: 0..146,
subject: Name(
ExprName {
node_index: NodeIndex(None),
range: 6..7,
id: Name("x"),
ctx: Load,
},
),
cases: [
MatchCase {
range: 13..32,
node_index: NodeIndex(None),
pattern: MatchOr(
PatternMatchOr {
range: 18..27,
node_index: NodeIndex(None),
patterns: [
MatchSequence(
PatternMatchSequence {
range: 18..21,
node_index: NodeIndex(None),
patterns: [
MatchAs(
PatternMatchAs {
range: 19..20,
node_index: NodeIndex(None),
pattern: None,
name: Some(
Identifier {
id: Name("a"),
range: 19..20,
node_index: NodeIndex(None),
},
),
},
),
],
},
),
MatchSequence(
PatternMatchSequence {
range: 24..27,
node_index: NodeIndex(None),
patterns: [
MatchAs(
PatternMatchAs {
range: 25..26,
node_index: NodeIndex(None),
pattern: None,
name: Some(
Identifier {
id: Name("a"),
range: 25..26,
node_index: NodeIndex(None),
},
),
},
),
],
},
),
],
},
),
guard: None,
body: [
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 29..32,
value: EllipsisLiteral(
ExprEllipsisLiteral {
node_index: NodeIndex(None),
range: 29..32,
},
),
},
),
],
},
MatchCase {
range: 37..62,
node_index: NodeIndex(None),
pattern: MatchOr(
PatternMatchOr {
range: 42..57,
node_index: NodeIndex(None),
patterns: [
MatchSequence(
PatternMatchSequence {
range: 42..48,
node_index: NodeIndex(None),
patterns: [
MatchAs(
PatternMatchAs {
range: 43..44,
node_index: NodeIndex(None),
pattern: None,
name: Some(
Identifier {
id: Name("x"),
range: 43..44,
node_index: NodeIndex(None),
},
),
},
),
MatchAs(
PatternMatchAs {
range: 46..47,
node_index: NodeIndex(None),
pattern: None,
name: Some(
Identifier {
id: Name("y"),
range: 46..47,
node_index: NodeIndex(None),
},
),
},
),
],
},
),
MatchSequence(
PatternMatchSequence {
range: 51..57,
node_index: NodeIndex(None),
patterns: [
MatchAs(
PatternMatchAs {
range: 52..53,
node_index: NodeIndex(None),
pattern: None,
name: Some(
Identifier {
id: Name("x"),
range: 52..53,
node_index: NodeIndex(None),
},
),
},
),
MatchAs(
PatternMatchAs {
range: 55..56,
node_index: NodeIndex(None),
pattern: None,
name: Some(
Identifier {
id: Name("y"),
range: 55..56,
node_index: NodeIndex(None),
},
),
},
),
],
},
),
],
},
),
guard: None,
body: [
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 59..62,
value: EllipsisLiteral(
ExprEllipsisLiteral {
node_index: NodeIndex(None),
range: 59..62,
},
),
},
),
],
},
MatchCase {
range: 67..89,
node_index: NodeIndex(None),
pattern: MatchSequence(
PatternMatchSequence {
range: 72..84,
node_index: NodeIndex(None),
patterns: [
MatchAs(
PatternMatchAs {
range: 73..74,
node_index: NodeIndex(None),
pattern: None,
name: Some(
Identifier {
id: Name("x"),
range: 73..74,
node_index: NodeIndex(None),
},
),
},
),
MatchOr(
PatternMatchOr {
range: 77..82,
node_index: NodeIndex(None),
patterns: [
MatchAs(
PatternMatchAs {
range: 77..78,
node_index: NodeIndex(None),
pattern: None,
name: Some(
Identifier {
id: Name("y"),
range: 77..78,
node_index: NodeIndex(None),
},
),
},
),
MatchAs(
PatternMatchAs {
range: 81..82,
node_index: NodeIndex(None),
pattern: None,
name: Some(
Identifier {
id: Name("y"),
range: 81..82,
node_index: NodeIndex(None),
},
),
},
),
],
},
),
],
},
),
guard: None,
body: [
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 86..89,
value: EllipsisLiteral(
ExprEllipsisLiteral {
node_index: NodeIndex(None),
range: 86..89,
},
),
},
),
],
},
MatchCase {
range: 94..119,
node_index: NodeIndex(None),
pattern: MatchOr(
PatternMatchOr {
range: 99..114,
node_index: NodeIndex(None),
patterns: [
MatchSequence(
PatternMatchSequence {
range: 99..105,
node_index: NodeIndex(None),
patterns: [
MatchAs(
PatternMatchAs {
range: 100..101,
node_index: NodeIndex(None),
pattern: None,
name: Some(
Identifier {
id: Name("a"),
range: 100..101,
node_index: NodeIndex(None),
},
),
},
),
MatchAs(
PatternMatchAs {
range: 103..104,
node_index: NodeIndex(None),
pattern: None,
name: None,
},
),
],
},
),
MatchSequence(
PatternMatchSequence {
range: 108..114,
node_index: NodeIndex(None),
patterns: [
MatchAs(
PatternMatchAs {
range: 109..110,
node_index: NodeIndex(None),
pattern: None,
name: Some(
Identifier {
id: Name("a"),
range: 109..110,
node_index: NodeIndex(None),
},
),
},
),
MatchAs(
PatternMatchAs {
range: 112..113,
node_index: NodeIndex(None),
pattern: None,
name: None,
},
),
],
},
),
],
},
),
guard: None,
body: [
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 116..119,
value: EllipsisLiteral(
ExprEllipsisLiteral {
node_index: NodeIndex(None),
range: 116..119,
},
),
},
),
],
},
MatchCase {
range: 124..146,
node_index: NodeIndex(None),
pattern: MatchOr(
PatternMatchOr {
range: 129..141,
node_index: NodeIndex(None),
patterns: [
MatchSequence(
PatternMatchSequence {
range: 129..132,
node_index: NodeIndex(None),
patterns: [
MatchAs(
PatternMatchAs {
range: 130..131,
node_index: NodeIndex(None),
pattern: None,
name: Some(
Identifier {
id: Name("a"),
range: 130..131,
node_index: NodeIndex(None),
},
),
},
),
],
},
),
MatchSequence(
PatternMatchSequence {
range: 135..141,
node_index: NodeIndex(None),
patterns: [
MatchClass(
PatternMatchClass {
range: 136..140,
node_index: NodeIndex(None),
cls: Name(
ExprName {
node_index: NodeIndex(None),
range: 136..137,
id: Name("C"),
ctx: Load,
},
),
arguments: PatternArguments {
range: 137..140,
node_index: NodeIndex(None),
patterns: [
MatchAs(
PatternMatchAs {
range: 138..139,
node_index: NodeIndex(None),
pattern: None,
name: Some(
Identifier {
id: Name("a"),
range: 138..139,
node_index: NodeIndex(None),
},
),
},
),
],
keywords: [],
},
},
),
],
},
),
],
},
),
guard: None,
body: [
Expr(
StmtExpr {
node_index: NodeIndex(None),
range: 143..146,
value: EllipsisLiteral(
ExprEllipsisLiteral {
node_index: NodeIndex(None),
range: 143..146,
},
),
},
),
],
},
],
},
),
],
},
)
```