Commit ae3afb5c

ae3afb5c9a33Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

fix: infer hash value type from subsequent []= assignments

Empty hash literals {} now correctly infer str_str_hash when followed by string value assignments like h["key"] = "val". Previously always defaulted to str_int_hash regardless of usage. scan_locals detects the []= pattern and upgrades the type, and compile_stmt generates the matching sp_StrStrHash_new() initializer.

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

Details

Commit
ae3afb5c9a33
Parent
e5e4e991d525

spinel_codegen.rbmodifiedView file

@@ -9031,6 +9031,34 @@
90319031 end
90329032 end
90339033 end
9034+ # Detect hash value type from h["key"] = val
9035+ if @nd_type[nid] == "CallNode"
9036+ if @nd_name[nid] == "[]="
9037+ recv = @nd_receiver[nid]
9038+ if recv >= 0 && @nd_type[recv] == "LocalVariableReadNode"
9039+ hname = @nd_name[recv]
9040+ args_id = @nd_arguments[nid]
9041+ if args_id >= 0
9042+ aargs = get_args(args_id)
9043+ if aargs.length >= 2
9044+ val_type = infer_type(aargs[1])
9045+ if val_type == "string"
9046+ ki = 0
9047+ while ki < names.length
9048+ if names[ki] == hname
9049+ if types[ki] == "str_int_hash"
9050+ types[ki] = "str_str_hash"
9051+ @needs_str_str_hash = 1
9052+ end
9053+ end
9054+ ki = ki + 1
9055+ end
9056+ end
9057+ end
9058+ end
9059+ end
9060+ end
9061+ end
90349062 if @nd_type[nid] == "ForNode"
90359063 tgt = @nd_target[nid]
90369064 if tgt >= 0
@@ -13252,6 +13280,19 @@
1325213280 end
1325313281 end
1325413282 end
13283+ # Empty hash literal: create the correct hash type
13284+ if vt == "str_str_hash"
13285+ expr_id2 = @nd_expression[nid]
13286+ if expr_id2 >= 0 && @nd_type[expr_id2] == "HashNode"
13287+ elems2 = parse_id_list(@nd_elements[expr_id2])
13288+ if elems2.length == 0
13289+ @needs_str_str_hash = 1
13290+ @needs_gc = 1
13291+ emit(" " + vref + " = sp_StrStrHash_new();")
13292+ return
13293+ end
13294+ end
13295+ end
1325513296 if vt == "bigint"
1325613297 rhs_t = infer_type(@nd_expression[nid])
1325713298 val = compile_expr(@nd_expression[nid])