mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-20 12:35:40 +00:00
[syntax-errors] Alternative match patterns bind different names (#20682)
Some checks are pending
CI / pre-commit (push) Waiting to run
CI / python package (push) Waiting to run
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 / 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
Some checks are pending
CI / pre-commit (push) Waiting to run
CI / python package (push) Waiting to run
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 / 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:
parent
8ca2b5555d
commit
7198e53182
9 changed files with 1805 additions and 4 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue