From 28536c2c072639ed3da519dcefc51b582a33c5fd Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 9 Oct 2026 03:13:50 +0530 Subject: [PATCH] Core: Refuse an expression statement whose calls would be dropped handle_expr lowered a statement only when it was a call, and dropped any other expression with an INFO log. So `print(a) if c else print(b)` compiled to nothing at all, and failing_tests/conditionals/oneline.py passed. An expression statement that contains a call is now a compile error; one without (a docstring, a bare name) is still dropped. oneline.py becomes an IR-level xfail. Co-Authored-By: Claude Opus 5.5 (1M context) --- pythonbpf/expr/expr_pass.py | 10 +++++++++- tests/test_config.toml | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index b9984b8..a4a746f 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -842,5 +842,13 @@ def handle_expr( call, local_sym_tab, ) + elif any(isinstance(node, ast.Call) for node in ast.walk(call)): + # A docstring or a bare name has no effect and can be dropped; an + # expression with a call in it (`f() if c else g()`, `a() or b()`) + # does, and dropping it would silently lose the call. + raise NotImplementedError( + f"Unsupported expression statement on line {expr.lineno}: " + f"{type(call).__name__}; write it as an if statement or assignment" + ) else: - logger.info("Unsupported expression type") + logger.debug(f"Dropping expression statement without effect: {ast.dump(call)}") diff --git a/tests/test_config.toml b/tests/test_config.toml index 10ab94b..c935312 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -17,6 +17,8 @@ "failing_tests/conditionals/struct_value.py" = {reason = "A struct value has no truth value (C rejects `if (s)`): a compile error naming the struct", level = "ir"} +"failing_tests/conditionals/oneline.py" = {reason = "A conditional expression used as a statement (`f() if c else g()`) is not lowered: a compile error rather than silently dropping both calls; write it as an if statement", level = "ir"} + "failing_tests/return_struct.py" = {reason = "Returning a struct value from an integer function is a type error (nothing to dereference to an integer)", level = "ir"} "failing_tests/license.py" = {reason = "Missing LICENSE global produces IR that llc rejects — should be caught earlier with a clear error message", level = "llc"}