Commit 097825fd

097825fd48d1Merged

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

Description

fix(analyze,codegen): widen LV hash type on cross-type []= write after non-empty init

m = {"a" => "1"}; m["k"] = 42 lowered to sp_StrStrHash_set(m, "k", 42LL) -- a mrb_int passed where the StrStrHash setter expected const char *. The hash widening pass that promotes {}-initialized locals on first []= only fired when the LV started empty (@scan_empty_hash_flags[ki] == "1"), leaving non-empty literal inits stranded at their first-pass narrow variant.

Analyzer (scan_locals's CallNode []= arm): when the LV is bound to one of the narrow hash variants (str_int / str_str / int_str / sym_int / sym_str) and the write's value (or key) doesn't match the current variant's slot type, widen to the appropriate *_poly_hash:

str_hash -> str_poly_hash sym_hash -> sym_poly_hash int_str_hash -> poly_poly_hash (no int_poly_hash variant)

The fit-check compares against the long-form type names ("string", "symbol", "int") that infer_type returns, not the short forms hash_key_part uses, so a str_str_hash LV facing a mrb_int value (vt "string" vs val_type "int") correctly trips the widen. Matching same-type writes (str=>str followed by str=>str) keep the narrow variant -- no regression for single-shape hash usage.

Codegen (compile_stmt LocalVariableWriteNode HashNode arm): non-empty hash literal against a poly-widened LV builds the literal as the poly variant directly, boxing each value via box_expr_to_poly. Without this, the literal still emitted sp_StrStrHash_new + sp_StrStrHash_set and the result was assigned to a sp_StrPolyHash * LV (-Wincompatible-pointer-types).

Closes #589 (minimal repro). The dispatch_request merged = matched.path_params.dup; params.each { |k, v| merged[k.to_s] = v } silent-elision shape Sam noted requires a separate sibling fix (ivar.dup with cross-type writes from a block) that doesn't share the LV-write code path; out of scope for this commit.

make test: 566/566 pass. make optcarrot: checksum 59662 stable. Stage-2 bootstrap fixpoint clean.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
097825fd48d1
Parent
705afa1fb749

spinel_analyze.rbmodifiedView file

@@ -23019,7 +23019,12 @@
2301923019 cur == "int_str_hash" ||
2302023020 cur == "sym_int_hash" ||
2302123021 cur == "sym_str_hash")
23022- if promotable && ki < @scan_empty_hash_flags.length && @scan_empty_hash_flags[ki] == "1"
23022+ if promotable
23023+ key_type = scan_locals_arg_type(aargs[0], names, types, params)
23024+ val_type = scan_locals_arg_type(aargs[aargs.length - 1], names, types, params)
23025+ started_empty = (ki < @scan_empty_hash_flags.length && @scan_empty_hash_flags[ki] == "1")
23026+ promoted = ""
23027+ if started_empty == 1
2302323028 # Block params (e.g. `|k, v|` in `each`) aren't
2302423029 # yet `declare_var`'d when scan_locals walks
2302523030 # the block body, so a bare `infer_type(v)`
@@ -23028,9 +23033,54 @@
2302823033 # `types[]`. Prefer that local types array when
2302923034 # the argument is a LocalVariableReadNode we
2303023035 # recorded; fall back to infer_type otherwise.
23031- key_type = scan_locals_arg_type(aargs[0], names, types, params)
23032- val_type = scan_locals_arg_type(aargs[aargs.length - 1], names, types, params)
23033- promoted = promote_empty_hash_for(key_type, val_type)
23036+ promoted = promote_empty_hash_for(key_type, val_type)
23037+ else
23038+ # Non-empty literal init followed by a `[]=` write whose
23039+ # value (or key) type doesn't fit the current variant's
23040+ # slot. CRuby treats the hash as having mixed value
23041+ # types; widen to `<key>_poly_hash` so the C-side store
23042+ # accepts the new value via sp_box_*. Without this, a
23043+ # `m = {"a" => "1"}; m["k"] = 42` emit fell through to
23044+ # `sp_StrStrHash_set(m, "k", 42LL)` which gcc rejects
23045+ # (mrb_int passed where const char * expected). Issue
23046+ # #589 minimal repro.
23047+ #
23048+ # Naming: infer_type uses long names ("string", "symbol",
23049+ # "int") so the fit-check compares against those, not
23050+ # against hash_key_part's short forms.
23051+ cur_kt_l = ""
23052+ cur_vt_l = ""
23053+ if cur == "str_int_hash"
23054+ cur_kt_l = "string"
23055+ cur_vt_l = "int"
23056+ elsif cur == "str_str_hash"
23057+ cur_kt_l = "string"
23058+ cur_vt_l = "string"
23059+ elsif cur == "int_str_hash"
23060+ cur_kt_l = "int"
23061+ cur_vt_l = "string"
23062+ elsif cur == "sym_int_hash"
23063+ cur_kt_l = "symbol"
23064+ cur_vt_l = "int"
23065+ elsif cur == "sym_str_hash"
23066+ cur_kt_l = "symbol"
23067+ cur_vt_l = "string"
23068+ end
23069+ val_fits = (val_type == cur_vt_l) || (cur_vt_l == "int" && (val_type == "nil" || val_type == "bool")) || val_type == "" || val_type == cur_vt_l
23070+ key_fits = (key_type == cur_kt_l) || key_type == ""
23071+ if val_fits == 0 || key_fits == 0
23072+ if cur_kt_l == "string"
23073+ promoted = "str_poly_hash"
23074+ elsif cur_kt_l == "symbol"
23075+ promoted = "sym_poly_hash"
23076+ elsif cur_kt_l == "int"
23077+ # int-keyed -> poly_poly is the only "poly value"
23078+ # variant spinel offers (no int_poly_hash). Widen
23079+ # the key side too.
23080+ promoted = "poly_poly_hash"
23081+ end
23082+ end
23083+ end
2303423084 if promoted != "" && promoted != cur
2303523085 types[ki] = promoted
2303623086 # Clear the flag only when reaching the terminal