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 a1235512355
# concrete element resets the flag to "".1235612356
@scan_empty_flags = "".split(",")12357+
# Parallel to `names`: "1" if the FIRST write to this local was an12358+
# empty `{}` literal — promote_empty_hash_local_writes uses this to12359+
# 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
end1235812363
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
end1237212378
end1237312379
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
end1238812395
end1238912396
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
end1241412422
end1241512423
end@@ -12438,6 +12446,14 @@
1243812446
else1243912447
@scan_empty_flags.push("")1244012448
end12449+
# Track empty-hash literal so a later []= write can promote12450+
# the local's hash variant from the str_int_hash default to12451+
# whatever key/value types the first []= pins.12452+
if is_empty_hash_literal(@nd_expression[nid]) == 112453+
@scan_empty_hash_flags.push("1")12454+
else12455+
@scan_empty_hash_flags.push("")12456+
end1244112457
end1244212458
else1244312459
if not_in(lname, params) == 1@@ -12454,6 +12470,18 @@
1245412470
ei = ei + 11245512471
end1245612472
end12473+
# Same shape for empty-hash flag: a non-empty-hash overwrite12474+
# commits the local to whatever concrete hash type was12475+
# assigned, so a later []= shouldn't trigger promotion.12476+
if is_empty_hash_literal(@nd_expression[nid]) == 012477+
ehi = 012478+
while ehi < names.length12479+
if names[ehi] == lname && ehi < @scan_empty_hash_flags.length12480+
@scan_empty_hash_flags[ehi] = ""12481+
end12482+
ehi = ehi + 112483+
end12484+
end1245712485
ki = 01245812486
while ki < names.length1245912487
if names[ki] == lname@@ -12818,7 +12846,15 @@
1281812846
end1281912847
end1282012848
end12821-
# Detect hash value type from h["key"] = val12849+
# `local[k] = v` on a local declared as the empty-hash default12850+
# (str_int_hash from `local = {}`) — promote based on the actual12851+
# key/value types so the C declaration picks the matching12852+
# sp_*Hash struct. Mirrors the ivar-side promotion in12853+
# scan_writer_calls. Only fires when @scan_empty_hash_flags12854+
# confirms every prior write to this local was an empty-hash12855+
# literal — concretely-typed hashes (`h = {"a" => 1}`) keep12856+
# their declared type even when later []= writes mix value12857+
# 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 >= 01282912865
aargs = get_args(args_id)1283012866
if aargs.length >= 212831-
val_type = infer_type(aargs[1])12832-
if val_type == "string"12833-
ki = 012834-
while ki < names.length12835-
if names[ki] == hname12836-
if types[ki] == "str_int_hash"12837-
types[ki] = "str_str_hash"12838-
@needs_str_str_hash = 112867+
ki = 012868+
while ki < names.length12869+
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] = promoted12876+
@scan_empty_hash_flags[ki] = ""12877+
if promoted == "str_str_hash"12878+
@needs_str_str_hash = 112879+
elsif promoted == "int_str_hash"12880+
@needs_int_str_hash = 112881+
@needs_int_array = 112882+
elsif promoted == "sym_int_hash"12883+
@needs_sym_int_hash = 112884+
elsif promoted == "sym_str_hash"12885+
@needs_sym_str_hash = 112886+
elsif promoted == "str_poly_hash"12887+
@needs_str_poly_hash = 112888+
@needs_rb_value = 112889+
elsif promoted == "sym_poly_hash"12890+
@needs_sym_poly_hash = 112891+
@needs_rb_value = 112892+
end1283912893
end1284012894
end12841-
ki = ki + 11284212895
end12896+
ki = ki + 11284312897
end1284412898
end1284512899
end@@ -19842,15 +19896,39 @@
1984219896
end1984319897
end1984419898
end19845-
# Empty hash literal: create the correct hash type19846-
if vt == "str_str_hash"19899+
# Empty hash literal: create the correct hash type. The local's19900+
# type was promoted by scan_locals' empty-hash promotion (str_int_hash19901+
# default refined based on first []= site's key/value types).19902+
# Without these arms the literal `{}` would always emit19903+
# sp_StrIntHash_new() and the assignment would mismatch the19904+
# 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 == 019851-
@needs_str_str_hash = 11985219910
@needs_gc = 119853-
emit(" " + vref + " = sp_StrStrHash_new();")19911+
if vt == "str_str_hash"19912+
@needs_str_str_hash = 119913+
emit(" " + vref + " = sp_StrStrHash_new();")19914+
elsif vt == "int_str_hash"19915+
@needs_int_str_hash = 119916+
emit(" " + vref + " = sp_IntStrHash_new();")19917+
elsif vt == "sym_int_hash"19918+
@needs_sym_int_hash = 119919+
emit(" " + vref + " = sp_SymIntHash_new();")19920+
elsif vt == "sym_str_hash"19921+
@needs_sym_str_hash = 119922+
emit(" " + vref + " = sp_SymStrHash_new();")19923+
elsif vt == "str_poly_hash"19924+
@needs_str_poly_hash = 119925+
@needs_rb_value = 119926+
emit(" " + vref + " = sp_StrPolyHash_new();")19927+
elsif vt == "sym_poly_hash"19928+
@needs_sym_poly_hash = 119929+
@needs_rb_value = 119930+
emit(" " + vref + " = sp_SymPolyHash_new();")19931+
end1985419932
return1985519933
end1985619934
endtest/empty_hash_promote.rbaddedView file
Loading diff…