check start location for target and global

Signed-off-by: 11happy <soni5happy@gmail.com>
This commit is contained in:
11happy 2025-12-14 13:45:05 +05:30
parent d233adf92b
commit bce4e2ca2b
2 changed files with 19 additions and 6 deletions

View File

@ -44,3 +44,13 @@ invalid-syntax: annotated name `d` can't be global
21 |
22 | e: int = 1
|
invalid-syntax: annotated name `g` can't be global
--> resources/test/fixtures/semantic_errors/annotated_global.py:29:1
|
27 | f: int = 1 # okay
28 |
29 | g: int = 1
| ^
30 | global g # error
|

View File

@ -300,7 +300,9 @@ impl SemanticSyntaxChecker {
visitor.visit_expr(annotation);
}
if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
if ctx.global(id.as_str()).is_some() && ctx.in_function_scope() {
if let Some(global_stmt) = ctx.global(id.as_str()) {
let global_start = global_stmt.start();
if ctx.in_function_scope() || target.start() < global_start {
Self::add_error(
ctx,
SemanticSyntaxErrorKind::AnnotatedGlobal(id.to_string()),
@ -309,6 +311,7 @@ impl SemanticSyntaxChecker {
}
}
}
}
Stmt::FunctionDef(ast::StmtFunctionDef {
type_params,
parameters,