Commit c4fcd37e
c4fcd37e5799Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified
Description
Merge pull request #377 from mame/pr/chained-or-assign-collection
fix(codegen): collection-element chained ||= lowers to a value-producing temp for sym/str_poly_hash
Details
- Commit
- c4fcd37e5799
- Parent
- c3b4e3229e31
- Merge parent
- a8e07e4326b4
spinel_codegen.rbmodifiedView file
@@ -21189,12 +21189,13 @@
2118921189
end2119021190
if t == "IndexOrWriteNode"2119121191
# `recv[k] ||= v` used as an expression (`x = recv[k] ||= v`,21192-
# method-body return, ternary arm, …). Lower the get-then-set21193-
# into a stmt-level emit and return the temp holding the21194-
# resulting value. Only the poly_poly_hash receiver shape needs21195-
# this expression form so far — typed hashes were always lowered21196-
# as stmts for backward compatibility, so falling through to the21197-
# default catch-all is fine for them.21192+
# method-body return, parenthesized chain like21193+
# `(@h[k] ||= []) << v`). Lower the get-then-set into a21194+
# stmt-level emit and return the temp holding the resulting21195+
# value (existing on hit, freshly-stored rhs on miss). Without21196+
# this branch the expression-form falls through to the default21197+
# catch-all and silently emits `0` as the value, collapsing21198+
# any subsequent chain.2119821199
recv_iow = @nd_receiver[nid]2119921200
args_iow = @nd_arguments[nid]2120021201
if recv_iow >= 0 && args_iow >= 0@@ -21205,6 +21206,12 @@
2120521206
tg = compile_poly_poly_index_or_assign_to_temp(nid, rc_iow, arg_ids_iow)2120621207
return tg2120721208
end21209+
if rt_iow == "sym_poly_hash" || rt_iow == "str_poly_hash"21210+
rc_iow = compile_expr_gc_rooted(recv_iow)21211+
arg_ids_iow = get_args(args_iow)21212+
tg = compile_typed_poly_hash_index_or_assign_to_temp(nid, rc_iow, arg_ids_iow, rt_iow)21213+
return tg21214+
end2120821215
end2120921216
end2121021217
if t == "ConstantReadNode"@@ -35246,6 +35253,10 @@
3524635253
compile_poly_poly_index_or_assign_to_temp(nid, rc, arg_ids)3524735254
return3524835255
end35256+
if rt == "sym_poly_hash" || rt == "str_poly_hash"35257+
compile_typed_poly_hash_index_or_assign_to_temp(nid, rc, arg_ids, rt)35258+
return35259+
end3524935260
end3525035261
3525135262
# Emit the get-then-set body for `recv[k] ||= v` against a@@ -35268,6 +35279,35 @@
3526835279
tg3526935280
end3527035281
35282+
# Same shape as `compile_poly_poly_index_or_assign_to_temp` for35283+
# sym_poly_hash / str_poly_hash receivers — the keys are bare35284+
# `sp_sym` / `const char *` (not boxed sp_RbVal) but the value35285+
# slot is poly so the get-then-set still pivots on the35286+
# `tag == SP_TAG_NIL` miss probe.35287+
def compile_typed_poly_hash_index_or_assign_to_temp(nid, rc, arg_ids, recv_type)35288+
tt = new_temp35289+
tk = new_temp35290+
tg = new_temp35291+
if recv_type == "sym_poly_hash"35292+
hash_c = "sp_SymPolyHash"35293+
key_t = "sp_sym"35294+
key_expr = compile_expr(arg_ids[0])35295+
else35296+
hash_c = "sp_StrPolyHash"35297+
key_t = "const char *"35298+
key_expr = compile_expr_as_string(arg_ids[0])35299+
end35300+
emit(" " + hash_c + " *" + tt + " = " + rc + ";")35301+
emit(" " + key_t + " " + tk + " = " + key_expr + ";")35302+
emit(" sp_RbVal " + tg + " = " + hash_c + "_get(" + tt + ", " + tk + ");")35303+
val_boxed = box_expr_to_poly(@nd_expression[nid])35304+
emit(" if (" + tg + ".tag == SP_TAG_NIL) {")35305+
emit(" " + tg + " = " + val_boxed + ";")35306+
emit(" " + hash_c + "_set(" + tt + ", " + tk + ", " + tg + ");")35307+
emit(" }")35308+
tg35309+
end35310+
3527135311
# Return a C expression that evaluates to the inspected form of `val`3527235312
# (a value of inferred Ruby type `at`), following Ruby's Object#inspect3527335313
# 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…