Commit ee5c05b7

ee5c05b72d03Merged

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

Description

Merge pull request #138 from ryanseys/rs-fix-empty-hash-promote

fix(codegen): promote empty-hash locals based on first []= site

Details

Commit
ee5c05b72d03
Parent
9453d99fb18d
Merge parent
d54986345b5b

spinel_codegen.rbmodifiedView file

@@ -12354,6 +12354,11 @@
1235412354 # until first `push` (issue #58). A subsequent write with a
1235512355 # concrete element resets the flag to "".
1235612356 @scan_empty_flags = "".split(",")
12357+ # Parallel to `names`: "1" if the FIRST write to this local was an
12358+ # empty `{}` literal — promote_empty_hash_local_writes uses this to
12359+ # decide whether to refine str_int_hash (the empty-hash default)
12360+ # to a more specific variant on first []= write.
12361+ @scan_empty_hash_flags = "".split(",")
1235712362 end
1235812363 if @nd_type[nid] == "MultiWriteNode"
1235912364 targets = parse_id_list(@nd_targets[nid])
@@ -12368,6 +12373,7 @@
1236812373 types.push(multi_write_target_type(val_id2, ti2))
1236912374 @scan_literal_flags.push("")
1237012375 @scan_empty_flags.push("")
12376+ @scan_empty_hash_flags.push("")
1237112377 end
1237212378 end
1237312379 end
@@ -12384,6 +12390,7 @@
1238412390 types.push(splat_rest_type(val_id2))
1238512391 @scan_literal_flags.push("")
1238612392 @scan_empty_flags.push("")
12393+ @scan_empty_hash_flags.push("")
1238712394 end
1238812395 end
1238912396 end
@@ -12410,6 +12417,7 @@
1241012417 types.push(multi_write_target_type(val_id2, t_idx2))
1241112418 @scan_literal_flags.push("")
1241212419 @scan_empty_flags.push("")
12420+ @scan_empty_hash_flags.push("")
1241312421 end
1241412422 end
1241512423 end
@@ -12438,6 +12446,14 @@
1243812446 else
1243912447 @scan_empty_flags.push("")
1244012448 end
12449+ # Track empty-hash literal so a later []= write can promote
12450+ # the local's hash variant from the str_int_hash default to
12451+ # whatever key/value types the first []= pins.
12452+ if is_empty_hash_literal(@nd_expression[nid]) == 1
12453+ @scan_empty_hash_flags.push("1")
12454+ else
12455+ @scan_empty_hash_flags.push("")
12456+ end
1244112457 end
1244212458 else
1244312459 if not_in(lname, params) == 1
@@ -12454,6 +12470,18 @@
1245412470 ei = ei + 1
1245512471 end
1245612472 end
12473+ # Same shape for empty-hash flag: a non-empty-hash overwrite
12474+ # commits the local to whatever concrete hash type was
12475+ # assigned, so a later []= shouldn't trigger promotion.
12476+ if is_empty_hash_literal(@nd_expression[nid]) == 0
12477+ ehi = 0
12478+ while ehi < names.length
12479+ if names[ehi] == lname && ehi < @scan_empty_hash_flags.length
12480+ @scan_empty_hash_flags[ehi] = ""
12481+ end
12482+ ehi = ehi + 1
12483+ end
12484+ end
1245712485 ki = 0
1245812486 while ki < names.length
1245912487 if names[ki] == lname
@@ -12818,7 +12846,15 @@
1281812846 end
1281912847 end
1282012848 end
12821- # Detect hash value type from h["key"] = val
12849+ # `local[k] = v` on a local declared as the empty-hash default
12850+ # (str_int_hash from `local = {}`) — promote based on the actual
12851+ # key/value types so the C declaration picks the matching
12852+ # sp_*Hash struct. Mirrors the ivar-side promotion in
12853+ # scan_writer_calls. Only fires when @scan_empty_hash_flags
12854+ # confirms every prior write to this local was an empty-hash
12855+ # literal — concretely-typed hashes (`h = {"a" => 1}`) keep
12856+ # their declared type even when later []= writes mix value
12857+ # types.
1282212858 if @nd_type[nid] == "CallNode"
1282312859 if @nd_name[nid] == "[]="
1282412860 recv = @nd_receiver[nid]
@@ -12828,18 +12864,36 @@
1282812864 if args_id >= 0
1282912865 aargs = get_args(args_id)
1283012866 if aargs.length >= 2
12831- val_type = infer_type(aargs[1])
12832- if val_type == "string"
12833- ki = 0
12834- while ki < names.length
12835- if names[ki] == hname
12836- if types[ki] == "str_int_hash"
12837- types[ki] = "str_str_hash"
12838- @needs_str_str_hash = 1
12867+ ki = 0
12868+ while ki < names.length
12869+ if names[ki] == hname && types[ki] == "str_int_hash"
12870+ if ki < @scan_empty_hash_flags.length && @scan_empty_hash_flags[ki] == "1"
12871+ key_type = infer_type(aargs[0])
12872+ val_type = infer_type(aargs[aargs.length - 1])
12873+ promoted = promote_empty_hash_for(key_type, val_type)
12874+ if promoted != "" && promoted != "str_int_hash"
12875+ types[ki] = promoted
12876+ @scan_empty_hash_flags[ki] = ""
12877+ if promoted == "str_str_hash"
12878+ @needs_str_str_hash = 1
12879+ elsif promoted == "int_str_hash"
12880+ @needs_int_str_hash = 1
12881+ @needs_int_array = 1
12882+ elsif promoted == "sym_int_hash"
12883+ @needs_sym_int_hash = 1
12884+ elsif promoted == "sym_str_hash"
12885+ @needs_sym_str_hash = 1
12886+ elsif promoted == "str_poly_hash"
12887+ @needs_str_poly_hash = 1
12888+ @needs_rb_value = 1
12889+ elsif promoted == "sym_poly_hash"
12890+ @needs_sym_poly_hash = 1
12891+ @needs_rb_value = 1
12892+ end
1283912893 end
1284012894 end
12841- ki = ki + 1
1284212895 end
12896+ ki = ki + 1
1284312897 end
1284412898 end
1284512899 end
@@ -19842,15 +19896,39 @@
1984219896 end
1984319897 end
1984419898 end
19845- # Empty hash literal: create the correct hash type
19846- if vt == "str_str_hash"
19899+ # Empty hash literal: create the correct hash type. The local's
19900+ # type was promoted by scan_locals' empty-hash promotion (str_int_hash
19901+ # default refined based on first []= site's key/value types).
19902+ # Without these arms the literal `{}` would always emit
19903+ # sp_StrIntHash_new() and the assignment would mismatch the
19904+ # promoted local's struct type.
19905+ if vt == "str_str_hash" || vt == "int_str_hash" || vt == "sym_int_hash" || vt == "sym_str_hash" || vt == "str_poly_hash" || vt == "sym_poly_hash"
1984719906 expr_id2 = @nd_expression[nid]
1984819907 if expr_id2 >= 0 && @nd_type[expr_id2] == "HashNode"
1984919908 elems2 = parse_id_list(@nd_elements[expr_id2])
1985019909 if elems2.length == 0
19851- @needs_str_str_hash = 1
1985219910 @needs_gc = 1
19853- emit(" " + vref + " = sp_StrStrHash_new();")
19911+ if vt == "str_str_hash"
19912+ @needs_str_str_hash = 1
19913+ emit(" " + vref + " = sp_StrStrHash_new();")
19914+ elsif vt == "int_str_hash"
19915+ @needs_int_str_hash = 1
19916+ emit(" " + vref + " = sp_IntStrHash_new();")
19917+ elsif vt == "sym_int_hash"
19918+ @needs_sym_int_hash = 1
19919+ emit(" " + vref + " = sp_SymIntHash_new();")
19920+ elsif vt == "sym_str_hash"
19921+ @needs_sym_str_hash = 1
19922+ emit(" " + vref + " = sp_SymStrHash_new();")
19923+ elsif vt == "str_poly_hash"
19924+ @needs_str_poly_hash = 1
19925+ @needs_rb_value = 1
19926+ emit(" " + vref + " = sp_StrPolyHash_new();")
19927+ elsif vt == "sym_poly_hash"
19928+ @needs_sym_poly_hash = 1
19929+ @needs_rb_value = 1
19930+ emit(" " + vref + " = sp_SymPolyHash_new();")
19931+ end
1985419932 return
1985519933 end
1985619934 end