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
end90329032
end90339033
end9034+
# Detect hash value type from h["key"] = val9035+
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 >= 09042+
aargs = get_args(args_id)9043+
if aargs.length >= 29044+
val_type = infer_type(aargs[1])9045+
if val_type == "string"9046+
ki = 09047+
while ki < names.length9048+
if names[ki] == hname9049+
if types[ki] == "str_int_hash"9050+
types[ki] = "str_str_hash"9051+
@needs_str_str_hash = 19052+
end9053+
end9054+
ki = ki + 19055+
end9056+
end9057+
end9058+
end9059+
end9060+
end9061+
end90349062
if @nd_type[nid] == "ForNode"90359063
tgt = @nd_target[nid]90369064
if tgt >= 0@@ -13252,6 +13280,19 @@
1325213280
end1325313281
end1325413282
end13283+
# Empty hash literal: create the correct hash type13284+
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 == 013289+
@needs_str_str_hash = 113290+
@needs_gc = 113291+
emit(" " + vref + " = sp_StrStrHash_new();")13292+
return13293+
end13294+
end13295+
end1325513296
if vt == "bigint"1325613297
rhs_t = infer_type(@nd_expression[nid])1325713298
val = compile_expr(@nd_expression[nid])