mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
Core: Reject a name used before its global declaration, as Python does
`global x` covers the whole function body, so a body that reads x above the declaration has two incompatible readings of that line. Python rejects it with "name 'x' is used prior to global declaration"; say the same thing rather than silently picking the global. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@ -461,11 +461,23 @@ def process_func_body(
|
||||
# flagged, so every read and write resolves it through the one table with
|
||||
# no separate lookup order to get wrong. Python's rules apply: a parameter
|
||||
# cannot be declared global, and the name must be a @bpfglobal.
|
||||
first_use = {}
|
||||
for node in ast.walk(func_node):
|
||||
if isinstance(node, ast.Name):
|
||||
first_use[node.id] = min(first_use.get(node.id, node.lineno), node.lineno)
|
||||
|
||||
for node in ast.walk(func_node):
|
||||
if isinstance(node, ast.Global):
|
||||
for gname in node.names:
|
||||
if gname in local_sym_tab:
|
||||
raise SyntaxError(f"name '{gname}' is parameter and global")
|
||||
# Python reads the declaration as covering the whole body, and
|
||||
# rejects a body that used the name before saying so, because
|
||||
# the two readings of the earlier line disagree.
|
||||
if first_use.get(gname, node.lineno) < node.lineno:
|
||||
raise SyntaxError(
|
||||
f"name '{gname}' is used prior to global declaration"
|
||||
)
|
||||
if gname not in compilation_context.bpf_globals:
|
||||
raise SyntaxError(
|
||||
f"'global {gname}' in '{func_node.name}': no @bpfglobal "
|
||||
|
||||
Reference in New Issue
Block a user