Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,8 @@ impl SemanticSyntaxContext for Checker<'_> {
| SemanticSyntaxErrorKind::DuplicateMatchClassAttribute(_)
| SemanticSyntaxErrorKind::InvalidStarExpression
| SemanticSyntaxErrorKind::AsyncComprehensionInSyncComprehension(_)
| SemanticSyntaxErrorKind::DuplicateParameter(_) => {
| SemanticSyntaxErrorKind::DuplicateParameter(_)
| SemanticSyntaxErrorKind::NonlocalDeclarationAtModuleLevel => {
if self.settings.preview.is_enabled() {
self.semantic_errors.borrow_mut().push(error);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nonlocal x
nonlocal x, y
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
nonlocal
def _():
nonlocal
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
nonlocal x + 1
def _():
nonlocal x + 1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
nonlocal ,
nonlocal x,
nonlocal x, y,
def _():
nonlocal ,
nonlocal x,
nonlocal x, y,
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def _():
nonlocal x
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nonlocal x
nonlocal x, y, z
def _():
nonlocal x
nonlocal x, y, z
18 changes: 11 additions & 7 deletions crates/ruff_python_parser/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,29 +897,33 @@ 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,
);

if names.is_empty() {
// test_err nonlocal_stmt_empty
// nonlocal
// def _():
// nonlocal
self.add_error(
ParseErrorType::EmptyNonlocalNames,
self.current_token_range(),
);
}

// 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,
Expand Down
22 changes: 22 additions & 0 deletions crates/ruff_python_parser/src/semantic_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,22 @@ impl SemanticSyntaxChecker {
AwaitOutsideAsyncFunctionKind::AsyncWith,
);
}
Stmt::Nonlocal(ast::StmtNonlocal { range, .. }) => {
// test_ok nonlocal_declaration_at_module_level
// def _():
// nonlocal x

// test_err nonlocal_declaration_at_module_level
// nonlocal x
// nonlocal x, y
if ctx.in_module_scope() {
Self::add_error(
ctx,
SemanticSyntaxErrorKind::NonlocalDeclarationAtModuleLevel,
*range,
);
}
}
_ => {}
}

Expand Down Expand Up @@ -933,6 +949,9 @@ impl Display for SemanticSyntaxError {
SemanticSyntaxErrorKind::DuplicateParameter(name) => {
write!(f, r#"Duplicate parameter "{name}""#)
}
SemanticSyntaxErrorKind::NonlocalDeclarationAtModuleLevel => {
write!(f, "nonlocal declaration not allowed at module level")
}
}
}
}
Expand Down Expand Up @@ -1254,6 +1273,9 @@ pub enum SemanticSyntaxErrorKind {
/// lambda x, x: ...
/// ```
DuplicateParameter(String),

/// Represents a nonlocal declaration at module level
NonlocalDeclarationAtModuleLevel,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_parser/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ impl SemanticSyntaxContext for SemanticSyntaxCheckerVisitor<'_> {
}

fn in_module_scope(&self) -> bool {
true
self.scopes.len() == 1
}

fn in_function_scope(&self) -> bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/nonlocal_declaration_at_module_level.py
---
## AST

```
Module(
ModModule {
range: 0..25,
body: [
Nonlocal(
StmtNonlocal {
range: 0..10,
names: [
Identifier {
id: Name("x"),
range: 9..10,
},
],
},
),
Nonlocal(
StmtNonlocal {
range: 11..24,
names: [
Identifier {
id: Name("x"),
range: 20..21,
},
Identifier {
id: Name("y"),
range: 23..24,
},
],
},
),
],
},
)
```
## Semantic Syntax Errors

|
1 | nonlocal x
| ^^^^^^^^^^ Syntax Error: nonlocal declaration not allowed at module level
2 | nonlocal x, y
|


|
1 | nonlocal x
2 | nonlocal x, y
| ^^^^^^^^^^^^^ Syntax Error: nonlocal declaration not allowed at module level
|
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/nonlocal_stmt_empty.py
snapshot_kind: text
---
## AST

```
Module(
ModModule {
range: 0..9,
range: 0..22,
body: [
Nonlocal(
StmtNonlocal {
range: 0..8,
names: [],
FunctionDef(
StmtFunctionDef {
range: 0..21,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("_"),
range: 4..5,
},
type_params: None,
parameters: Parameters {
range: 5..7,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Nonlocal(
StmtNonlocal {
range: 13..21,
names: [],
},
),
],
},
),
],
Expand All @@ -23,6 +45,7 @@ Module(
## Errors

|
1 | nonlocal
| ^ Syntax Error: Nonlocal statement must have at least one name
1 | def _():
2 | nonlocal
| ^ Syntax Error: Nonlocal statement must have at least one name
|
Original file line number Diff line number Diff line change
@@ -1,52 +1,75 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/nonlocal_stmt_expression.py
snapshot_kind: text
---
## AST

```
Module(
ModModule {
range: 0..15,
range: 0..28,
body: [
Nonlocal(
StmtNonlocal {
range: 0..10,
names: [
Identifier {
id: Name("x"),
range: 9..10,
},
FunctionDef(
StmtFunctionDef {
range: 0..27,
is_async: false,
decorator_list: [],
name: Identifier {
id: Name("_"),
range: 4..5,
},
type_params: None,
parameters: Parameters {
range: 5..7,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Nonlocal(
StmtNonlocal {
range: 13..23,
names: [
Identifier {
id: Name("x"),
range: 22..23,
},
],
},
),
Expr(
StmtExpr {
range: 24..27,
value: UnaryOp(
ExprUnaryOp {
range: 24..27,
op: UAdd,
operand: NumberLiteral(
ExprNumberLiteral {
range: 26..27,
value: Int(
1,
),
},
),
},
),
},
),
],
},
),
Expr(
StmtExpr {
range: 11..14,
value: UnaryOp(
ExprUnaryOp {
range: 11..14,
op: UAdd,
operand: NumberLiteral(
ExprNumberLiteral {
range: 13..14,
value: Int(
1,
),
},
),
},
),
},
),
],
},
)
```
## Errors

|
1 | nonlocal x + 1
| ^ Syntax Error: Simple statements must be separated by newlines or semicolons
1 | def _():
2 | nonlocal x + 1
| ^ Syntax Error: Simple statements must be separated by newlines or semicolons
|
Loading
Loading