Commit a6d2bd93

a6d2bd938dedMerged

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

Description

fix(analyze,codegen): hash literal int/bigint values share sym_int_hash storage

Under promote mode {x:, sum: 18} (x is bigint LV, 18 is int literal) inferred as sym_poly_hash via the "mixed value types" branch, while scan_locals declared the LV slot as sym_int_hash from the initial all-int snapshot. set_var_type then widened the codegen-side tracked type to sym_poly_hash on the write, but the C declaration stayed sp_SymIntHash * -- read sites dispatched as sym_poly_hash and the cast (sp_SymPolyHash *)(lv) over a SymIntHash payload was UB.

Treat int and bigint as numerically compatible in the all_same check of both infer_hash_val_type (codegen) and infer_hash_val_type_raw (analyze). They lower to the same mrb_int slot storage with sp_bigint_to_int at set/get boundaries. The two-side parity prevents the analyze cache from disagreeing with the codegen literal emit.

Picks up hash_shorthand under promote.

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

Details

Commit
a6d2bd938ded
Parent
6f40f601bd2a

spinel_analyze.rbmodifiedView file

@@ -2624,14 +2624,18 @@
26242624 eid = elems[0]
26252625 if @nd_type[eid] == "AssocNode"
26262626 first_vt = infer_type(@nd_expression[eid])
2627- # Check if all values have the same type
2627+ # Same int/bigint normalization as the codegen-side
2628+ # infer_hash_val_type: both lower to mrb_int hash storage with
2629+ # sp_bigint_to_int at the boundaries.
2630+ first_vt_norm = (first_vt == "bigint") ? "int" : first_vt
26282631 all_same = 1
26292632 k = 1
26302633 while k < elems.length
26312634 eid2 = elems[k]
26322635 if @nd_type[eid2] == "AssocNode"
26332636 vt2 = infer_type(@nd_expression[eid2])
2634- if vt2 != first_vt
2637+ vt2_norm = (vt2 == "bigint") ? "int" : vt2
2638+ if vt2_norm != first_vt_norm
26352639 all_same = 0
26362640 end
26372641 end
@@ -2677,6 +2681,15 @@
26772681 if all_sym_keys == 1 && (first_vt == "int" || first_vt == "bool" || first_vt == "nil")
26782682 return "sym_int_hash"
26792683 end
2684+ # promote-mode bigint values map onto sym_int_hash storage by
2685+ # unboxing each bigint via sp_bigint_to_int at the codegen
2686+ # set/get sites (mirrors the codegen-side infer_hash_val_type).
2687+ # Keeping the analyzer/codegen inferences aligned avoids a
2688+ # set_var_type widening to sym_poly_hash that would mis-cast
2689+ # the SymIntHash slot at read sites.
2690+ if all_sym_keys == 1 && first_vt == "bigint"
2691+ return "sym_int_hash"
2692+ end
26802693 # Symbol values get sym_poly_hash storage so dig / lookup
26812694 # preserves the sp_TAG_SYM tag (sym_int_hash would
26822695 # round-trip through mrb_int and lose the symbol-ness).