From 2d850f457f37b4861043d73299dfaaa60e3a2f1f Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Wed, 8 Oct 2025 02:22:41 +0530 Subject: [PATCH] Add _normalize_types to handle mismatched ints, move type_mismatch test to passing --- pythonbpf/expr_pass.py | 19 +++++++++++++++++++ .../conditionals/type_mismatch.py | 0 2 files changed, 19 insertions(+) rename tests/{failing_tests => passing_tests}/conditionals/type_mismatch.py (100%) diff --git a/pythonbpf/expr_pass.py b/pythonbpf/expr_pass.py index 0bccc5b..fa22c2e 100644 --- a/pythonbpf/expr_pass.py +++ b/pythonbpf/expr_pass.py @@ -129,10 +129,29 @@ def _handle_ctypes_call( return val +def _normalize_types(builder, lhs, rhs): + """Normalize types for comparison.""" + + if isinstance(lhs.type, ir.IntType) and isinstance(rhs.type, ir.IntType): + if lhs.type.width < rhs.type.width: + lhs = builder.sext(lhs, rhs.type) + else: + rhs = builder.sext(rhs, lhs.type) + return lhs, rhs + + logger.error(f"Type mismatch: {lhs.type} vs {rhs.type}") + return None, None + + def _handle_comparator(builder, op, lhs, rhs): """Handle comparison operations.""" # NOTE: For now assume same types + if lhs.type != rhs.type: + lhs, rhs = _normalize_types(builder, lhs, rhs) + + if lhs is None or rhs is None: + return None comparison_ops = { ast.Eq: "==", diff --git a/tests/failing_tests/conditionals/type_mismatch.py b/tests/passing_tests/conditionals/type_mismatch.py similarity index 100% rename from tests/failing_tests/conditionals/type_mismatch.py rename to tests/passing_tests/conditionals/type_mismatch.py