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

@@ -3611,6 +3611,30 @@
36113611 end
36123612 end
36133613
3614+ # `hash[k] = v` (hash element write) — same Ruby semantics: the
3615+ # assignment expression evaluates to v. Mirrors the attr-writer arm
3616+ # above so `x = (h[k] = v)` types `x` from the value, not the int
3617+ # default. int-/string-valued variants store a fixed type; poly-valued
3618+ # variants carry the value's natural type through.
3619+ if mname == "[]=" && recv >= 0
3620+ rt_haw = base_type(infer_type(recv))
3621+ if is_hash_type(rt_haw) == 1
3622+ args_id_haw = @nd_arguments[nid]
3623+ if args_id_haw >= 0
3624+ arg_ids_haw = get_args(args_id_haw)
3625+ if arg_ids_haw.length >= 2
3626+ if rt_haw == "str_int_hash" || rt_haw == "sym_int_hash" || rt_haw == "int_int_hash"
3627+ return "int"
3628+ end
3629+ if rt_haw == "str_str_hash" || rt_haw == "sym_str_hash" || rt_haw == "int_str_hash"
3630+ return "string"
3631+ end
3632+ return infer_type(arg_ids_haw[arg_ids_haw.length - 1])
3633+ end
3634+ end
3635+ end
3636+ end
3637+
36143638 # User-defined top-level method (bare call): take precedence over
36153639 # name-based builtin inference so `def minmax(a,b); ... end; minmax(1,2)`
36163640 # binds to the user def instead of Array#minmax's tuple return.