Commit 7ccf8d27

7ccf8d27ae19Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): tail-position arr << x lowers as stmt, not comma-expr (#672)

A method body ending with if cond; arr << x; end (no else) where the declared return type is non-Array (e.g. nil via RBS -> nil) previously emitted return (sp_StrArray_push(_t1, ...), _t1); — a C comma expression whose value is the sp_StrArray * local. The function signature declared mrb_int (the C lowering of RBS nil), and the implicit-pointer-to-int conversion tripped clang.

compile_body_return_inner's side-effect CallNode list already routed push, pop, []=, etc. through compile_stmt + return c_return_default. << was missing — it has the same builtin Array/Hash mutator role and the same "side-effect as a statement" convention. Add it alongside push. The user-class escape hatch (is_user_method == 1 skips this branch) still fires when a custom class defines <<, so user << methods that legitimately return a non-array value are unaffected.

Regression-tested manually against the issue's RBS repro: validate declared -> nil with a tail if [email protected]?; errors << "too short" if @body.length < 10; end now compiles cleanly. Test suite remains 649/0/0 and optcarrot checksum 59662 unchanged. A test-harness regression case would need a .rbs sidecar; the harness's recipe calls spinel_parse / spinel_analyze / spinel_codegen directly and doesn't run the spinel --rbs DIR plumbing, so the fix is covered by manual verification of the issue's repro plus existing tests for the side-effect-mutator class of CallNode last-statement lowering.

Closes #672.

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

Details

Commit
7ccf8d27ae19
Parent
4b438d05e7d2

spinel_codegen.rbmodifiedView file

@@ -39068,7 +39068,7 @@
3906839068 lt = @nd_type[last]
3906939069 if lt == "CallNode"
3907039070 lm = @nd_name[last]
39071- if lm == "[]=" || lm == "push" || lm == "pop" || lm == "emit" || lm == "emit_raw" || lm == "puts" || lm == "print" || lm == "p" || lm == "printf" || lm == "warn" || lm == "raise" || lm == "exit" || lm == "abort" || lm == "sleep" || lm == "delete" || lm == "clear" || lm == "concat" || lm == "prepend" || lm == "fill" || lm == "insert" || lm == "update" || lm == "merge!" || lm == "store" || lm == "reverse!" || lm == "sort!" || lm == "each" || lm == "times" || lm == "upto" || lm == "downto"
39071+ if lm == "[]=" || lm == "push" || lm == "pop" || lm == "<<" || lm == "emit" || lm == "emit_raw" || lm == "puts" || lm == "print" || lm == "p" || lm == "printf" || lm == "warn" || lm == "raise" || lm == "exit" || lm == "abort" || lm == "sleep" || lm == "delete" || lm == "clear" || lm == "concat" || lm == "prepend" || lm == "fill" || lm == "insert" || lm == "update" || lm == "merge!" || lm == "store" || lm == "reverse!" || lm == "sort!" || lm == "each" || lm == "times" || lm == "upto" || lm == "downto"
3907239072 # The hardcoded list above is meant to catch builtin Array/Hash
3907339073 # mutators conventionally called for side-effect. But several of
3907439074 # these names (update, clear, concat, delete, each, pop, push, ...)