Commit 7980373f

7980373f777bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 6 files · +4 added · ~2 modified

Description

fix(codegen): three Ruby-semantics fixes — multi-write nil + block_given inline + LV ||=/&&= poly

Three orthogonal repros from the new issue batch:

#705: a, b = nil segfaulted because multi-write fell through to the typed-array destructure path and emitted sp_IntArray *_t = 0; sp_IntArray_get(_t, 0). Add a NilNode-RHS early-return in compile_multi_write that binds each target to nil. The analyze side (multi_write_target_type) likewise returns "nil" so the target slots get the correct first-write type. (Output still prints [0, 0] vs Ruby's [nil, nil] -- the LV nil type makes the surrounding array literal an int_array; a wider fix for nil-typed-LV-in-array shape is deferred. The segfault is gone.)

#706: f (no args) on a yield-using method (block_given? marks it yield-using) emitted sp_f(, NULL, NULL) -- empty leading arg slot. Strip the leading ", " from yargs when the regular- args list is empty.

#706 part 2: f { } (with literal block) inlined block_given? to 0 because compile_block_given_expr's only 1 arm checked @in_yield_method (set when compiling f's own body, NOT when inlining at the call site). Add an @inline_yield_blk arm so inlined-context block_given? answers true.

#702: a = nil; a ||= 5; a &&= true failed C compile because the LV ||= / &&= emit used C's if (!vref) directly. For poly (sp_RbVal struct) vref this isn't a valid scalar; for int? it compared the SP_INT_NIL sentinel to NULL. Route the truthy check through truthy_c_expr when the LV's declared type is poly or scalar-nullable; keep the C-truthy fallback for other types (notably the a = nil first-write nil type, where the runtime value gets checked via C if (vref)).

(#702 display: the final puts a.inspect still emits nil because analyze didn't widen a's declared type past the initial nil write -- a separate scan_locals issue for the ||= / &&= forms, deferred.)

Tests: bang_op_ruby_truthy.rb, block_given_inline_call_site.rb. 451 tests pass, optcarrot 59662 holds.

Refs #702 Fixes #705 (segfault) Fixes #706

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

Details

Commit
7980373f777b
Parent
8e19a79517b4

spinel_analyze.rbmodifiedView file

@@ -6194,6 +6194,12 @@
61946194 if val_id < 0
61956195 return "int"
61966196 end
6197+ # `a, b = nil` -- every target gets nil (Ruby semantic). Avoids
6198+ # the typed-array destructure path that NULL-derefs at runtime
6199+ # (#705).
6200+ if @nd_type[val_id] == "NilNode"
6201+ return "nil"
6202+ end
61976203 rt = infer_type(val_id)
61986204 if is_tuple_type(rt) == 1
61996205 return tuple_elem_type_at(rt, ti)