Commit 91917fd2

91917fd23f4cMerged

Ori Pekelman <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): keep call's side effect when boxing nil-typed return

box_value_to_poly for at == "nil" returned the bare literal "sp_box_nil()", discarding the val argument. The poly dispatch loop calls this with val = "sp_<Subclass>_method(...)" — the actual method call. Discarding it means: when a subclass override's static return type is nil (e.g. its body is puts ... or hash[k] = v whose return is nil-typed), the cls_id arm emits a bare sp_box_nil() and the override never executes.

Equivalent for any base class with an empty body: empty body's inferred return is also nil, so even the base call is dropped.

Emit the val for its side effect, then yield nil:

return "((void)(" + val + "), sp_box_nil())"

The (void) cast suppresses the (benign) "expression result unused" warning when val is e.g. a function returning int.

Repro lives in test/poly_nil_return_dispatch.rb. Surfaced building the tep Sinatra-clone — Tep::Filter#before's empty base body made every user-supplied filter silently no-op until we added a placeholder return.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
91917fd23f4c
Parent
8964075ee5d1

spinel_codegen.rbmodifiedView file

@@ -29065,7 +29065,13 @@
2906529065 return "sp_box_bool(" + val + ")"
2906629066 end
2906729067 if at == "nil"
29068- return "sp_box_nil()"
29068+ # `val` may be a side-effecting call (e.g. an arm in a
29069+ # cls_id-dispatched table where the user-defined override
29070+ # returns nil because its body is a `puts` / similar). Drop
29071+ # the value but keep the side effect via the comma operator;
29072+ # otherwise the generated dispatch silently elides the call
29073+ # for any subclass whose method's static return type is nil.
29074+ return "((void)(" + val + "), sp_box_nil())"
2906929075 end
2907029076 if at == "symbol"
2907129077 return "sp_box_sym(" + val + ")"