Commit d7c9c49c
d7c9c49cbafcMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Resolve expression-form hash []= (Hash.new default block, x = (h[k]=v))
Ruby's (h[k] = v) mutates the hash and evaluates to v, but Spinel left
expression-form hash []= unresolved -- it raised "undefined method '[]='"
at runtime. This broke Hash.new { |h, k| h[k] = 0 } (the default block's
value-producing final expression) and x = (h[k] = v).
Add compile_hash_set_expr, which emits the set as ordinary statements (a
value temp, then sp_*Hash_set) and returns the assigned-value temp -- no
GCC statement expression, per the portability constraint. Wire it into the
Hash.new default-proc body and the local-variable-write RHS. Type the
expression in the analyzer with a []= arm mirroring the attr-writer arm,
so x = (h[k] = v) gives x the value's type rather than the int default.
The helper falls back to the existing path when an int-/string-valued hash
receives an incompatible value -- the separate empty-{}-not-promoted shape
(e.g. an array stored into an un-promoted str_int_hash) that the statement
form mishandles too, left out of scope here.
Fixes #1231
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- d7c9c49cbafc
- Parent
- 46ab95f836ac
spinel_analyze.rbmodifiedView file
end end # `hash[k] = v` (hash element write) — same Ruby semantics: the # assignment expression evaluates to v. Mirrors the attr-writer arm # above so `x = (h[k] = v)` types `x` from the value, not the int # default. int-/string-valued variants store a fixed type; poly-valued # variants carry the value's natural type through. if mname == "[]=" && recv >= 0 rt_haw = base_type(infer_type(recv)) if is_hash_type(rt_haw) == 1 args_id_haw = @nd_arguments[nid] if args_id_haw >= 0 arg_ids_haw = get_args(args_id_haw) if arg_ids_haw.length >= 2 if rt_haw == "str_int_hash" || rt_haw == "sym_int_hash" || rt_haw == "int_int_hash" return "int" end if rt_haw == "str_str_hash" || rt_haw == "sym_str_hash" || rt_haw == "int_str_hash" return "string" end return infer_type(arg_ids_haw[arg_ids_haw.length - 1]) end end end end # User-defined top-level method (bare call): take precedence over # name-based builtin inference so `def minmax(a,b); ... end; minmax(1,2)` # binds to the user def instead of Array#minmax's tuple return.spinel_codegen.rbmodifiedView file
Loading diff…
test/hash_index_assign_expression.rbaddedView file
Loading diff…
test/hash_new_default_block_assign.rbaddedView file
Loading diff…