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
cur == "int_str_hash" || cur == "sym_int_hash" || cur == "sym_str_hash") if promotable && ki < @scan_empty_hash_flags.length && @scan_empty_hash_flags[ki] == "1" if promotable key_type = scan_locals_arg_type(aargs[0], names, types, params) val_type = scan_locals_arg_type(aargs[aargs.length - 1], names, types, params) started_empty = (ki < @scan_empty_hash_flags.length && @scan_empty_hash_flags[ki] == "1") promoted = "" if started_empty == 1 # Block params (e.g. `|k, v|` in `each`) aren't # yet `declare_var`'d when scan_locals walks # the block body, so a bare `infer_type(v)` # `types[]`. Prefer that local types array when # the argument is a LocalVariableReadNode we # recorded; fall back to infer_type otherwise. key_type = scan_locals_arg_type(aargs[0], names, types, params) val_type = scan_locals_arg_type(aargs[aargs.length - 1], names, types, params) promoted = promote_empty_hash_for(key_type, val_type) promoted = promote_empty_hash_for(key_type, val_type) else # Non-empty literal init followed by a `[]=` write whose # value (or key) type doesn't fit the current variant's # slot. CRuby treats the hash as having mixed value # types; widen to `<key>_poly_hash` so the C-side store # accepts the new value via sp_box_*. Without this, a # `m = {"a" => "1"}; m["k"] = 42` emit fell through to # `sp_StrStrHash_set(m, "k", 42LL)` which gcc rejects # (mrb_int passed where const char * expected). Issue # #589 minimal repro. # # Naming: infer_type uses long names ("string", "symbol", # "int") so the fit-check compares against those, not # against hash_key_part's short forms. cur_kt_l = "" cur_vt_l = "" if cur == "str_int_hash" cur_kt_l = "string" cur_vt_l = "int" elsif cur == "str_str_hash" cur_kt_l = "string" cur_vt_l = "string" elsif cur == "int_str_hash" cur_kt_l = "int" cur_vt_l = "string" elsif cur == "sym_int_hash" cur_kt_l = "symbol" cur_vt_l = "int" elsif cur == "sym_str_hash" cur_kt_l = "symbol" cur_vt_l = "string" end val_fits = (val_type == cur_vt_l) || (cur_vt_l == "int" && (val_type == "nil" || val_type == "bool")) || val_type == "" || val_type == cur_vt_l key_fits = (key_type == cur_kt_l) || key_type == "" if val_fits == 0 || key_fits == 0 if cur_kt_l == "string" promoted = "str_poly_hash" elsif cur_kt_l == "symbol" promoted = "sym_poly_hash" elsif cur_kt_l == "int" # int-keyed -> poly_poly is the only "poly value" # variant spinel offers (no int_poly_hash). Widen # the key side too. promoted = "poly_poly_hash" end end end if promoted != "" && promoted != cur types[ki] = promoted # Clear the flag only when reaching the terminalspinel_codegen.rbmodifiedView file
Loading diff…
test/local_hash_cross_type_widen.rbaddedView file
Loading diff…
test/local_hash_cross_type_widen.rb.expectedaddedView file
Loading diff…