mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Core: Say what is unsupported instead of dropping it silently
Three review notes from the globals PR, all about being explicit: - Augmented assignment on a struct field handles one level of dereference. Say so where the target is unpacked, and give `obj.inner.field += v` its own message instead of an ast.dump. - The ast.Global branch of process_stmt is a no-op for a reason (declarations are collected before anything is lowered, because the binding is function-wide). Spell that out, and log the names at debug level rather than leaving a bare pass. - process_stmt had no else, so any statement the compiler does not handle was dropped without a word, which makes a program mean something other than what it says. Warn, with the line number. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@ -233,6 +233,10 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
|
||||
elif isinstance(stmt.target, ast.Attribute) and isinstance(
|
||||
stmt.target.value, ast.Name
|
||||
):
|
||||
# NOTE: one level of struct dereference only (`obj.field += v`). A
|
||||
# nested target (`obj.inner.field += v`) has an Attribute, not a Name,
|
||||
# as its value and is rejected below; supporting it means walking the
|
||||
# chain of gep()s, which plain assignment does not do either.
|
||||
var_name, field_name = stmt.target.value.id, stmt.target.attr
|
||||
if var_name not in local_sym_tab:
|
||||
raise SyntaxError(
|
||||
@ -250,6 +254,12 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
|
||||
raise SyntaxError(f"Field '{field_name}' not found in struct '{metadata}'")
|
||||
slot = struct_info.gep(builder, local_sym_tab[var_name].var, field_name)
|
||||
slot_type = struct_info.field_type(field_name)
|
||||
elif isinstance(stmt.target, ast.Attribute):
|
||||
raise SyntaxError(
|
||||
f"augmented assignment to a nested struct field "
|
||||
f"('...{stmt.target.attr}') is not supported yet; only one level "
|
||||
f"of dereference (obj.field) works so far"
|
||||
)
|
||||
else:
|
||||
raise SyntaxError(
|
||||
f"Unsupported augmented-assignment target: {ast.dump(stmt.target)}"
|
||||
@ -389,14 +399,25 @@ def process_stmt(
|
||||
elif isinstance(stmt, ast.AugAssign):
|
||||
handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab)
|
||||
elif isinstance(stmt, ast.Global):
|
||||
# Declarations were collected by process_func_body; nothing to emit.
|
||||
pass
|
||||
# Nothing to emit: `global x` binds a name for the whole function body,
|
||||
# so process_func_body collects every declaration before the first
|
||||
# statement is lowered (and before allocation, which would otherwise
|
||||
# give a declared name a stack slot). The branch exists so that a valid
|
||||
# declaration does not reach the unsupported-statement warning below.
|
||||
logger.debug(f"global declaration of {', '.join(stmt.names)} already bound")
|
||||
elif isinstance(stmt, ast.If):
|
||||
handle_if(func, compilation_context, builder, stmt, local_sym_tab)
|
||||
elif isinstance(stmt, ast.Return):
|
||||
did_return = handle_return(
|
||||
builder, stmt, local_sym_tab, ret_type, compilation_context
|
||||
)
|
||||
else:
|
||||
# Silently dropping a statement makes the program mean something other
|
||||
# than what it says, so say so.
|
||||
logger.warning(
|
||||
f"Unsupported statement on line {getattr(stmt, 'lineno', '?')}, "
|
||||
f"ignored: {type(stmt).__name__}"
|
||||
)
|
||||
return did_return
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user