Commit a8e07e43
a8e07e4326b4Merged
Yusuke Endoh <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified
Description
fix(codegen): collection-element chained ||= lowers to a value-producing temp for sym/str_poly_hash
The IndexOrWriteNode expression-form (x = h[k] ||= v,
parenthesised chains like (@h[k] ||= []) << v, ternary arms
…) only handled poly_poly_hash receivers — every other typed
hash fell through to the default catch-all and silently emitted
literal 0 as the chain receiver. Subsequent calls in the chain
(<<, [], etc.) then landed on an int instead of the
just-stored or pre-existing value, and the C compiler rejected
e.g. sp_poly_shl(0, …) because the runtime helper expects a
boxed sp_RbVal recv.
Add the same get-then-set lowering for sym_poly_hash and
str_poly_hash. The shape mirrors the existing
compile_poly_poly_index_or_assign_to_temp: stash the
hash / key / get-result in three temps, branch on
tag == SP_TAG_NIL to set the rhs and overwrite the result.
The two typed variants need a different key C type
(sp_sym / const char *) but reuse the same value-poly
machinery, so factor them into one helper
(compile_typed_poly_hash_index_or_assign_to_temp) and dispatch
in both the expression-form (compile_expr) and the
statement-form (compile_index_or_assign).
Details
- Commit
- a8e07e4326b4
- Parent
- 31a59e30af5e
spinel_codegen.rbmodifiedView file
end if t == "IndexOrWriteNode" # `recv[k] ||= v` used as an expression (`x = recv[k] ||= v`, # method-body return, ternary arm, …). Lower the get-then-set # into a stmt-level emit and return the temp holding the # resulting value. Only the poly_poly_hash receiver shape needs # this expression form so far — typed hashes were always lowered # as stmts for backward compatibility, so falling through to the # default catch-all is fine for them. # method-body return, parenthesized chain like # `(@h[k] ||= []) << v`). Lower the get-then-set into a # stmt-level emit and return the temp holding the resulting # value (existing on hit, freshly-stored rhs on miss). Without # this branch the expression-form falls through to the default # catch-all and silently emits `0` as the value, collapsing # any subsequent chain. recv_iow = @nd_receiver[nid] args_iow = @nd_arguments[nid] if recv_iow >= 0 && args_iow >= 0 tg = compile_poly_poly_index_or_assign_to_temp(nid, rc_iow, arg_ids_iow) return tg end if rt_iow == "sym_poly_hash" || rt_iow == "str_poly_hash" rc_iow = compile_expr_gc_rooted(recv_iow) arg_ids_iow = get_args(args_iow) tg = compile_typed_poly_hash_index_or_assign_to_temp(nid, rc_iow, arg_ids_iow, rt_iow) return tg end end end if t == "ConstantReadNode" compile_poly_poly_index_or_assign_to_temp(nid, rc, arg_ids) return end if rt == "sym_poly_hash" || rt == "str_poly_hash" compile_typed_poly_hash_index_or_assign_to_temp(nid, rc, arg_ids, rt) return end end # Emit the get-then-set body for `recv[k] ||= v` against a tg end # Same shape as `compile_poly_poly_index_or_assign_to_temp` for # sym_poly_hash / str_poly_hash receivers — the keys are bare # `sp_sym` / `const char *` (not boxed sp_RbVal) but the value # slot is poly so the get-then-set still pivots on the # `tag == SP_TAG_NIL` miss probe. def compile_typed_poly_hash_index_or_assign_to_temp(nid, rc, arg_ids, recv_type) tt = new_temp tk = new_temp tg = new_temp if recv_type == "sym_poly_hash" hash_c = "sp_SymPolyHash" key_t = "sp_sym" key_expr = compile_expr(arg_ids[0]) else hash_c = "sp_StrPolyHash" key_t = "const char *" key_expr = compile_expr_as_string(arg_ids[0]) end emit(" " + hash_c + " *" + tt + " = " + rc + ";") emit(" " + key_t + " " + tk + " = " + key_expr + ";") emit(" sp_RbVal " + tg + " = " + hash_c + "_get(" + tt + ", " + tk + ");") val_boxed = box_expr_to_poly(@nd_expression[nid]) emit(" if (" + tg + ".tag == SP_TAG_NIL) {") emit(" " + tg + " = " + val_boxed + ";") emit(" " + hash_c + "_set(" + tt + ", " + tk + ", " + tg + ");") emit(" }") tg end # Return a C expression that evaluates to the inspected form of `val` # (a value of inferred Ruby type `at`), following Ruby's Object#inspect # contract. Returns "" when `at` has no inspect implementation yet, sotest/chained_or_assign_collection_element.rbaddedView file
Loading diff…
test/chained_or_assign_collection_element.rb.expectedaddedView file
Loading diff…
test/chained_or_assign_collection_str_keys.rbaddedView file
Loading diff…
test/chained_or_assign_collection_str_keys.rb.expectedaddedView file
Loading diff…