Skip to content

Commit

Permalink
Better error recovery for match patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Mar 20, 2024
1 parent 1cf7954 commit e1f1cc8
Show file tree
Hide file tree
Showing 11 changed files with 3,042 additions and 764 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Invalid keyword pattern in class argument
match subject:
case Foo(x as y = 1):
pass
case Foo(x | y = 1):
pass
case Foo([x, y] = 1):
pass
case Foo({False: 0} = 1):
pass
case Foo(1=1):
pass
case Foo(Bar()=1):
pass
# Positional pattern cannot follow keyword pattern
# case Foo(x, y=1, z):
# pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
match invalid_lhs_pattern:
case Foo() + 1j:
pass
case x + 2j:
pass
case _ + 3j:
pass
case (1 | 2) + 4j:
pass
case [1, 2] + 5j:
pass
case {True: 1} + 6j:
pass
case 1j + 2j:
pass

match invalid_rhs_pattern:
case 1 + Foo():
pass
case 2 + x:
pass
case 3 + _:
pass
case 4 + (1 | 2):
pass
case 5 + [1, 2]:
pass
case 6 + {True: 1}:
pass
case 1 + 2:
pass

match invalid_lhs_rhs_pattern:
case Foo() + Bar():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Starred expression is not allowed as a mapping pattern key
match subject:
case {*key}:
pass
case {*key: 1}:
pass
case {*key 1}:
pass
case {*key, None: 1}:
pass

# Double starred pattern cannot follow any other pattern
match subject:
case {**rest, None: 1}:
pass
case {**rest1, **rest2, None: 1}:
pass
3 changes: 3 additions & 0 deletions crates/ruff_python_parser/resources/valid/statement/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
z = 0

match x:
# F-strings aren't allowed as patterns but it's a soft syntax error in Python.
case f"{y}":
pass
match {"test": 1}:
Expand Down Expand Up @@ -240,6 +241,8 @@

# PatternMatchAs
match x:
case a:
...
case a as b:
...
case 1 | 2 as two:
Expand Down
7 changes: 6 additions & 1 deletion crates/ruff_python_parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod expression;
mod helpers;
mod pattern;
mod progress;
mod recovery;
mod statement;
#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -949,7 +950,11 @@ impl RecoveryContextKind {
| RecoveryContextKind::TupleElements(_) => p.at_expr(),
RecoveryContextKind::DictElements => p.at(TokenKind::DoubleStar) || p.at_expr(),
RecoveryContextKind::SequenceMatchPattern(_) => p.at_pattern_start(),
RecoveryContextKind::MatchPatternMapping => p.at_mapping_pattern_start(),
RecoveryContextKind::MatchPatternMapping => {
// A star pattern is invalid as a mapping key and is here only for
// better error recovery.
p.at(TokenKind::Star) || p.at_mapping_pattern_start()
}
RecoveryContextKind::MatchPatternClassArguments => p.at_pattern_start(),
RecoveryContextKind::Arguments => p.at_expr(),
RecoveryContextKind::DeleteTargets => p.at_expr(),
Expand Down
Loading

0 comments on commit e1f1cc8

Please sign in to comment.