From d905e766448eb5f249d97db95cb000c1d68dfc7a Mon Sep 17 00:00:00 2001 From: devteamaegis Date: Sun, 25 Oct 2026 23:22:23 -0400 Subject: [PATCH] [SymForce] Reject constant inputs in Codegen Codegen accepted inputs whose elements were constants or expressions rather than symbols, e.g. Values(a=sf.Rot3.from_storage([1, 2, 3, 4])), and generated a function with an argument that is never used. Raise a ValueError naming the offending input keys instead. Fixes #170 --- symforce/codegen/codegen.py | 11 +++++++++++ test/symforce_codegen_test.py | 17 ++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/symforce/codegen/codegen.py b/symforce/codegen/codegen.py index fd9be394..c36801e5 100644 --- a/symforce/codegen/codegen.py +++ b/symforce/codegen/codegen.py @@ -159,6 +159,17 @@ def __init__( self.inputs = inputs self.outputs = outputs + # Inputs must be symbols, not constants or expressions + non_symbolic_inputs = [ + key + for key, value in inputs.items_recursive() + if not all(isinstance(e, sf.Symbol) for e in ops.StorageOps.to_storage(value)) + ] + if non_symbolic_inputs: + raise ValueError( + f"Inputs must be symbolic, found constants or expressions in: {non_symbolic_inputs}" + ) + # All symbols in outputs must be present in inputs input_symbols_list = codegen_util.flat_symbols_from_values(inputs) input_symbols = set(input_symbols_list) diff --git a/test/symforce_codegen_test.py b/test/symforce_codegen_test.py index 6518924e..2edb12a0 100644 --- a/test/symforce_codegen_test.py +++ b/test/symforce_codegen_test.py @@ -925,14 +925,29 @@ def sparse_func(x: sf.V3) -> sf.M33: def test_invalid_codegen_raises(self) -> None: """ Tests: + Codegen inputs must be symbolic Codegen outputs must be a function of given inputs Codegen input/output names must be valid variable names Codegen namespace must be a valid identifier Codegen name must be a valid identifier """ - # Outputs have symbols not present in inputs x = sf.Symbol("x") y = sf.Symbol("y") + + # Inputs are constants + inputs = Values(a=sf.Rot3.from_storage([1, 2, 3, 4])) + outputs = Values(out=sf.Rot3.from_storage([1, 2, 3, 4])) + self.assertRaises(ValueError, codegen.Codegen, inputs, outputs, codegen.CppConfig(), "test") + + # Inputs contain an expression or a constant alongside symbols + inputs = Values(a=x, b=x * y) + outputs = Values(out=x) + self.assertRaises(ValueError, codegen.Codegen, inputs, outputs, codegen.CppConfig(), "test") + inputs = Values(a=sf.V2(x, 2)) + outputs = Values(out=x) + self.assertRaises(ValueError, codegen.Codegen, inputs, outputs, codegen.CppConfig(), "test") + + # Outputs have symbols not present in inputs inputs = Values(input=x) outputs = Values(output=x + y) self.assertRaises(ValueError, codegen.Codegen, inputs, outputs, codegen.CppConfig(), "test")