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
if rt == "int_str_hash" @needs_int_str_hash = 1 end # Poly receiver -- compile_each_block emits a cls_id dispatch # table covering every hash variant. Flag all 8 hash typedefs # so the runtime emit pass produces them (the dispatch arm # references them by name unconditionally). Issue #603 -- # without this, an each() on a sp_RbVal-bound variable hit the # arm but the casts (sp_StrIntHash *, etc.) failed at C compile # because the typedef wasn't emitted. if rt == "poly" @needs_str_int_hash = 1 @needs_str_str_hash = 1 @needs_int_str_hash = 1 @needs_sym_int_hash = 1 @needs_sym_str_hash = 1 @needs_str_poly_hash = 1 @needs_sym_poly_hash = 1 @needs_poly_poly_hash = 1 @needs_rb_value = 1 @needs_poly_array = 1 @needs_gc = 1 end end end endspinel_codegen.rbmodifiedView file
Loading diff…
test/case_when_poly_scrutinee_bound_to_local.rbaddedView file
Loading diff…
test/case_when_poly_scrutinee_bound_to_local.rb.expectedaddedView file
Loading diff…
test/poly_recv_hash_each_dispatch.rbaddedView file
Loading diff…
test/poly_recv_hash_each_dispatch.rb.expectedaddedView file
Loading diff…