mirror of
https://github.com/astral-sh/ruff
synced 2026-01-22 14:00:51 -05:00
[parser] Fix panic when parsing IPython escape command expressions (#21480)
## Summary Fixes a panic when parsing IPython escape commands with `Help` kind (`?`) in expression contexts. The parser now reports an error instead of panicking. Fixes #21465. ## Problem The parser panicked with `unreachable!()` in `parse_ipython_escape_command_expression` when encountering escape commands with `Help` kind (`?`) in expression contexts, where only `Magic` (`%`) and `Shell` (`!`) are allowed. ## Approach Replaced the `unreachable!()` panic with error handling that adds a `ParseErrorType::OtherError` and continues parsing, returning a valid AST node with the error attached. ## Test Plan Added `test_ipython_escape_command_in_with_statement` and `test_ipython_help_escape_command_as_expression` to verify the fix. --------- Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
This commit is contained in:
@@ -1115,7 +1115,27 @@ impl RecoveryContextKind {
|
||||
TokenKind::Colon => Some(ListTerminatorKind::ErrorRecovery),
|
||||
_ => None,
|
||||
},
|
||||
WithItemKind::Unparenthesized | WithItemKind::ParenthesizedExpression => p
|
||||
// test_err ipython_help_escape_command_error_recovery_1
|
||||
// # parse_options: {"mode": "ipython"}
|
||||
// with (a, ?b)
|
||||
// ?
|
||||
|
||||
// test_err ipython_help_escape_command_error_recovery_2
|
||||
// # parse_options: {"mode": "ipython"}
|
||||
// with (a, ?b
|
||||
// ?
|
||||
|
||||
// test_err ipython_help_escape_command_error_recovery_3
|
||||
// # parse_options: {"mode": "ipython"}
|
||||
// with a, ?b
|
||||
// ?
|
||||
// x = 1
|
||||
WithItemKind::Unparenthesized => matches!(
|
||||
p.current_token_kind(),
|
||||
TokenKind::Colon | TokenKind::Newline
|
||||
)
|
||||
.then_some(ListTerminatorKind::Regular),
|
||||
WithItemKind::ParenthesizedExpression => p
|
||||
.at(TokenKind::Colon)
|
||||
.then_some(ListTerminatorKind::Regular),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user