-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[syntax-errors] Irrefutable case pattern before final case #16905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Summary -- Detects irrefutable `match` cases before the final case using a modified version of the existing `Pattern::is_irrefutable` method from the AST crate. The modified method helps to retrieve a more precise diagnostic range to match what Python 3.13 shows in the REPL. Test Plan -- New inline tests, as well as some updates to existing tests that had irrefutable patterns before the last block.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, this looks good!
We could improve the error messages as I think there are only two cases where this can occur - name and wildcard pattern. CPython provides the following error messages for the above cases:
SyntaxError: name capture 'foo' makes remaining patterns unreachable
SyntaxError: wildcard makes remaining patterns unreachable
let Stmt::Match(ast::StmtMatch { cases, .. }) = stmt else { | ||
return; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we move the match expression in check_stmt
above now that we have multiple checks? That would also require updating the methods like irrefutable_match_case
to accept the relevant AST node which needs to be checked like in this case it would accept StmtMatch
which would also lead to a safer typed API.
Summary
Detects irrefutable
match
cases before the final case using a modified versionof the existing
Pattern::is_irrefutable
method from the AST crate. Themodified method helps to retrieve a more precise diagnostic range to match what
Python 3.13 shows in the REPL.
Test Plan
New inline tests, as well as some updates to existing tests that had irrefutable
patterns before the last block.