From 82c30c4f156abb2563f7a944aef1cea3d21ee635 Mon Sep 17 00:00:00 2001 From: Zimri Leisher Date: Sat, 5 Sep 2026 14:02:57 -0400 Subject: [PATCH] Reject == and != between void expressions (#196) --- src/fpy/semantics.py | 5 +++-- test/fpy/test_functions.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/fpy/semantics.py b/src/fpy/semantics.py index c8f19a6..1bea28a 100644 --- a/src/fpy/semantics.py +++ b/src/fpy/semantics.py @@ -1741,10 +1741,11 @@ def pick_intermediate_type( if op in BOOLEAN_OPERATORS: return BOOL - # for == and !=, non-numeric same-type comparisons are valid + # for == and !=, non-numeric same-type comparisons are valid, as long + # as the operands have a value at all if op in (BinaryStackOp.EQUAL, BinaryStackOp.NOT_EQUAL): if len(arg_types) == 2 and arg_types[0] == arg_types[1]: - if not arg_types[0].is_numerical: + if not arg_types[0].is_numerical and arg_types[0] != NOTHING: # non-numeric equality (struct, array, enum, time) return arg_types[0] diff --git a/test/fpy/test_functions.py b/test/fpy/test_functions.py index 7dfa4fb..6eb993b 100644 --- a/test/fpy/test_functions.py +++ b/test/fpy/test_functions.py @@ -313,6 +313,30 @@ def noop(): """ assert_compile_failure(fprime_test_api, seq) + def test_compare_void_function_results_equal(self, fprime_test_api): + """A void call has no value, so it cannot be an operand of ==.""" + seq = """ +def f(): + pass + +b: bool = f() == f() +""" + assert_compile_failure(fprime_test_api, seq, match="undefined for Nothing") + + def test_compare_void_function_results_not_equal(self, fprime_test_api): + """A void call has no value, so it cannot be an operand of !=.""" + seq = """ +def f(): + pass + +def g(): + pass + +if f() != g(): + exit(1) +""" + assert_compile_failure(fprime_test_api, seq, match="undefined for Nothing") + def test_call_embedded_in_bare_expression(self, fprime_test_api): """A bare expression statement whose top-level node isn't itself side-effecting (an == comparison) still embeds a call that is; the