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 | 21 |
22 | e: int = 1 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,12 +300,15 @@ impl SemanticSyntaxChecker {
visitor.visit_expr(annotation); visitor.visit_expr(annotation);
} }
if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() { 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()) {
Self::add_error( let global_start = global_stmt.start();
ctx, if ctx.in_function_scope() || target.start() < global_start {
SemanticSyntaxErrorKind::AnnotatedGlobal(id.to_string()), Self::add_error(
target.range(), ctx,
); SemanticSyntaxErrorKind::AnnotatedGlobal(id.to_string()),
target.range(),
);
}
} }
} }
} }