mirror of
https://github.com/astral-sh/ruff
synced 2026-01-11 00:24:13 -05:00
[syntax-errors] nonlocal declaration at module level (#17559)
## Summary Part of #17412 Add a new compile-time syntax error for detecting `nonlocal` declarations at a module level. ## Test Plan - Added new inline tests for the syntax error - Updated existing tests for `nonlocal` statement parsing to be inside a function scope Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
538393d1f3
commit
cf59cee928
@@ -897,12 +897,14 @@ impl<'src> Parser<'src> {
|
||||
self.bump(TokenKind::Nonlocal);
|
||||
|
||||
// test_err nonlocal_stmt_trailing_comma
|
||||
// nonlocal ,
|
||||
// nonlocal x,
|
||||
// nonlocal x, y,
|
||||
// def _():
|
||||
// nonlocal ,
|
||||
// nonlocal x,
|
||||
// nonlocal x, y,
|
||||
|
||||
// test_err nonlocal_stmt_expression
|
||||
// nonlocal x + 1
|
||||
// def _():
|
||||
// nonlocal x + 1
|
||||
let names = self.parse_comma_separated_list_into_vec(
|
||||
RecoveryContextKind::Identifiers,
|
||||
Parser::parse_identifier,
|
||||
@@ -910,7 +912,8 @@ impl<'src> Parser<'src> {
|
||||
|
||||
if names.is_empty() {
|
||||
// test_err nonlocal_stmt_empty
|
||||
// nonlocal
|
||||
// def _():
|
||||
// nonlocal
|
||||
self.add_error(
|
||||
ParseErrorType::EmptyNonlocalNames,
|
||||
self.current_token_range(),
|
||||
@@ -918,8 +921,9 @@ impl<'src> Parser<'src> {
|
||||
}
|
||||
|
||||
// test_ok nonlocal_stmt
|
||||
// nonlocal x
|
||||
// nonlocal x, y, z
|
||||
// def _():
|
||||
// nonlocal x
|
||||
// nonlocal x, y, z
|
||||
ast::StmtNonlocal {
|
||||
range: self.node_range(start),
|
||||
names,
|
||||
|
||||
Reference in New Issue
Block a user