Commit 37805c2f

37805c2fe020Merged

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

Description

fix(codegen,analyze): Hash#each on poly recv + case/when scrutinee temp type

Two related dispatch-table fixes from Sam Ruby's poly-recv work.

#603 -- Hash#each on a poly-typed receiver (sp_RbVal) silently iterated zero times. compile_each_block's rt == "poly" arm emitted a cls_id-keyed length / per-iteration table covering Range and the six Array variants but no Hash variants. _t2_len stayed 0 for every hash kind and the block never ran.

Adds 8 hash arms to the poly-each emit. Length reads h->len; per-iteration packs the (key, value) pair as a 2-element PolyArray so the existing array-splat logic at the block-param boundary delivers |k, v| without further code. 1-arg blocks receive the boxed PolyArray, matching CRuby's hash-each 2-element-array yield to single-arg blocks.

The dispatch references all 8 hash typedefs unconditionally, so the runtime emit pass has to produce them before the dispatch arm reaches the C compiler. scan_for_container_typedef_usage (codegen pre-pass) gains an each arm that flags every hash @needs_* when any .each { ... } call is present in the AST. The @needs_poly_poly_hash -> @needs_method implication is re-checked after the pre-scan so sp_method_obj_hash_dispatch's sp_Method reference resolves.

A sibling arm in spinel_analyze's scan_features's mname == "each" flags the same set when the receiver's static type is poly, so the IR carries the flags even when codegen pre-scan is bypassed.

#604 -- r = case v when X then ... end emitted two C temps: a result temp (typed from the branch values) and a scrutinee temp used inside sp_class_for_poly(...). The scrutinee temp's declaration fell through to the mrb_int default when the predicate's type wasn't string / obj-class, so a poly scrutinee landed as mrb_int _t = lv_v; -- a struct-to-int conversion that the C compiler rejected.

Adds a pred_type == "poly" arm to compile_expr's CaseNode emit and routes the remaining non-int / non-string pred types through c_type(pred_type) instead of the mrb_int default. Sibling change to compile_case_stmt: concrete range / time / float / bool scrutinee temps now use the right C type too (the prior mrb_int fallback masked the same shape for tail-position case statements).

make test: 579/579 pass (+2 fixtures). make optcarrot: checksum 59662 stable. Stage-2 bootstrap fixpoint clean.

Closes #603 Closes #604

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
37805c2fe020
Parent
397889d88953

spinel_analyze.rbmodifiedView file

@@ -18384,6 +18384,26 @@
1838418384 if rt == "int_str_hash"
1838518385 @needs_int_str_hash = 1
1838618386 end
18387+ # Poly receiver -- compile_each_block emits a cls_id dispatch
18388+ # table covering every hash variant. Flag all 8 hash typedefs
18389+ # so the runtime emit pass produces them (the dispatch arm
18390+ # references them by name unconditionally). Issue #603 --
18391+ # without this, an each() on a sp_RbVal-bound variable hit the
18392+ # arm but the casts (sp_StrIntHash *, etc.) failed at C compile
18393+ # because the typedef wasn't emitted.
18394+ if rt == "poly"
18395+ @needs_str_int_hash = 1
18396+ @needs_str_str_hash = 1
18397+ @needs_int_str_hash = 1
18398+ @needs_sym_int_hash = 1
18399+ @needs_sym_str_hash = 1
18400+ @needs_str_poly_hash = 1
18401+ @needs_sym_poly_hash = 1
18402+ @needs_poly_poly_hash = 1
18403+ @needs_rb_value = 1
18404+ @needs_poly_array = 1
18405+ @needs_gc = 1
18406+ end
1838718407 end
1838818408 end
1838918409 end