Commit 10746b57

10746b571fe3Merged

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

Description

codegen: Hash#[] op= for string-valued and poly-valued variants

compile_index_op_assign only handled sym_int / str_int / int_str / int_array / float_array — the four poly / str-value variants silently no-op'd, so `h[:a] += "!"` and `h["a"] += 10` left the hash unchanged. Add per-variant arms:

  • sym_str_hash / str_str_hash: `+= str` lowers to sp_str_concat on the existing value.

  • sym_poly_hash / str_poly_hash: `+= int` unboxes the sp_RbVal, runs the op, re-boxes via sp_box_int. Other op-arg shapes still fall through (poly compound is wider than any single inline emit).

Fixes #834

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

Details

Commit
10746b571fe3
Parent
26187bf304e4

spinel_codegen.rbmodifiedView file

@@ -36419,6 +36419,47 @@
3641936419 ", sp_SymIntHash_get(" + tt + ", " + ti + ") " + op + " (" + val + ")); }")
3642036420 return
3642136421 end
36422+ # sym_str_hash / str_str_hash with `+= str` — string concat
36423+ # (the only sensible compound op for string-valued hashes).
36424+ if rt == "sym_str_hash" && op == "+"
36425+ tt = new_temp
36426+ ti = new_temp
36427+ emit(" { sp_SymStrHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx +
36428+ "; sp_SymStrHash_set(" + tt + ", " + ti +
36429+ ", sp_str_concat(sp_SymStrHash_get(" + tt + ", " + ti + "), " + val + ")); }")
36430+ return
36431+ end
36432+ if rt == "str_str_hash" && op == "+"
36433+ tt = new_temp
36434+ ti = new_temp
36435+ idx_s2 = compile_expr_as_string(arg_ids[0])
36436+ emit(" { sp_StrStrHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s2 +
36437+ "; sp_StrStrHash_set(" + tt + ", " + ti +
36438+ ", sp_str_concat(sp_StrStrHash_get(" + tt + ", " + ti + "), " + val + ")); }")
36439+ return
36440+ end
36441+ # sym_poly_hash / str_poly_hash with `+= int` — unbox the
36442+ # current sp_RbVal, run the op, re-box. Only int-valued slots
36443+ # are supported here; other op-arg shapes fall through.
36444+ if rt == "sym_poly_hash" && op == "+" && infer_type(@nd_expression[nid]) == "int"
36445+ @needs_rb_value = 1
36446+ tt = new_temp
36447+ ti = new_temp
36448+ emit(" { sp_SymPolyHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx +
36449+ "; sp_SymPolyHash_set(" + tt + ", " + ti +
36450+ ", sp_box_int(sp_SymPolyHash_get(" + tt + ", " + ti + ").v.i " + op + " (" + val + "))); }")
36451+ return
36452+ end
36453+ if rt == "str_poly_hash" && op == "+" && infer_type(@nd_expression[nid]) == "int"
36454+ @needs_rb_value = 1
36455+ tt = new_temp
36456+ ti = new_temp
36457+ idx_s3 = compile_expr_as_string(arg_ids[0])
36458+ emit(" { sp_StrPolyHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s3 +
36459+ "; sp_StrPolyHash_set(" + tt + ", " + ti +
36460+ ", sp_box_int(sp_StrPolyHash_get(" + tt + ", " + ti + ").v.i " + op + " (" + val + "))); }")
36461+ return
36462+ end
3642236463 # Poly recv + symbol idx: surfaces in `arr[i][:k] += v` (and the
3642336464 # each-block analog `f[:k] += v` where f is destructured from a
3642436465 # poly_array of boxed hashes — block param type is widened to