Commit d5498634

d54986345b5bMerged

Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

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

h = {}; h[1] = "one" failed to compile because the local was declared as the empty-hash default (sp_StrIntHash) but the int-keyed []= needed sp_IntStrHash. The existing partial promotion at scan_locals' []= branch only handled the str→str case (any str_int_hash + string-valued []= => str_str_hash, ignoring the key type), which incorrectly typed h3 = {}; h3[1] = "one" as sp_StrStrHash and h5 = {}; h5[:a] = "x" as sp_StrStrHash.

Three coordinated changes:

  1. scan_locals tracking. A new @scan_empty_hash_flags ivar runs parallel to @scan_empty_flags / @scan_literal_flags. Set to "1" when a LocalVariableWriteNode binds an empty {} literal; cleared when the same local is reassigned with a non-empty hash.

  2. Key-aware promotion in scan_locals. The existing []=-on-local branch (~line 12796) is rewritten to consult the new flag and route through promote_empty_hash_for(key_type, value_type). Concrete-typed hashes (e.g. h = {"a" => 1}) keep their declared type even on a later mismatched []= — only {}-initialized locals get promoted. The full set of promotions covered now: str_int_hash → str_str_hash, int_str_hash, sym_int_hash, sym_str_hash, str_poly_hash, sym_poly_hash.

  3. Empty-hash LVW codegen. compile_stmt's LVW branch had a matching arm for vt == "str_str_hash" that emits sp_StrStrHash_new() when the value is {}. Extend to all six promoted hash variants so the lv_h = sp_<Type>Hash_new() constructor matches the C declaration.

Test exercises all five hash variants:

  • str → int (str_int_hash; matches default, no promotion needed)
  • str → str (str_str_hash)
  • int → str (int_str_hash)
  • sym → int (sym_int_hash)
  • sym → str (sym_str_hash)

Each builds via h = {} then []= writes, and verifies has-key/get/ length round-trip cleanly.

Out of scope: nested empty-hash promotions (h = {}; h[k] = {}; h[k][k2] = ...); poly-valued empty hashes where the first []= writes one type and a later []= writes another (would need a second-pass promotion to widen str_str → str_poly).

Bootstrap: gen2.c == gen3.c. Tests: 219 pass.

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

Details

Commit
d54986345b5b
Parent
02005f26deba

spinel_codegen.rbmodifiedView file

@@ -12304,6 +12304,11 @@
1230412304 # until first `push` (issue #58). A subsequent write with a
1230512305 # concrete element resets the flag to "".
1230612306 @scan_empty_flags = "".split(",")
12307+ # Parallel to `names`: "1" if the FIRST write to this local was an
12308+ # empty `{}` literal — promote_empty_hash_local_writes uses this to
12309+ # decide whether to refine str_int_hash (the empty-hash default)
12310+ # to a more specific variant on first []= write.
12311+ @scan_empty_hash_flags = "".split(",")
1230712312 end
1230812313 if @nd_type[nid] == "MultiWriteNode"
1230912314 targets = parse_id_list(@nd_targets[nid])
@@ -12318,6 +12323,7 @@
1231812323 types.push(multi_write_target_type(val_id2, ti2))
1231912324 @scan_literal_flags.push("")
1232012325 @scan_empty_flags.push("")
12326+ @scan_empty_hash_flags.push("")
1232112327 end
1232212328 end
1232312329 end
@@ -12334,6 +12340,7 @@
1233412340 types.push(splat_rest_type(val_id2))
1233512341 @scan_literal_flags.push("")
1233612342 @scan_empty_flags.push("")
12343+ @scan_empty_hash_flags.push("")
1233712344 end
1233812345 end
1233912346 end
@@ -12360,6 +12367,7 @@
1236012367 types.push(multi_write_target_type(val_id2, t_idx2))
1236112368 @scan_literal_flags.push("")
1236212369 @scan_empty_flags.push("")
12370+ @scan_empty_hash_flags.push("")
1236312371 end
1236412372 end
1236512373 end
@@ -12388,6 +12396,14 @@
1238812396 else
1238912397 @scan_empty_flags.push("")
1239012398 end
12399+ # Track empty-hash literal so a later []= write can promote
12400+ # the local's hash variant from the str_int_hash default to
12401+ # whatever key/value types the first []= pins.
12402+ if is_empty_hash_literal(@nd_expression[nid]) == 1
12403+ @scan_empty_hash_flags.push("1")
12404+ else
12405+ @scan_empty_hash_flags.push("")
12406+ end
1239112407 end
1239212408 else
1239312409 if not_in(lname, params) == 1
@@ -12404,6 +12420,18 @@
1240412420 ei = ei + 1
1240512421 end
1240612422 end
12423+ # Same shape for empty-hash flag: a non-empty-hash overwrite
12424+ # commits the local to whatever concrete hash type was
12425+ # assigned, so a later []= shouldn't trigger promotion.
12426+ if is_empty_hash_literal(@nd_expression[nid]) == 0
12427+ ehi = 0
12428+ while ehi < names.length
12429+ if names[ehi] == lname && ehi < @scan_empty_hash_flags.length
12430+ @scan_empty_hash_flags[ehi] = ""
12431+ end
12432+ ehi = ehi + 1
12433+ end
12434+ end
1240712435 ki = 0
1240812436 while ki < names.length
1240912437 if names[ki] == lname
@@ -12759,7 +12787,15 @@
1275912787 end
1276012788 end
1276112789 end
12762- # Detect hash value type from h["key"] = val
12790+ # `local[k] = v` on a local declared as the empty-hash default
12791+ # (str_int_hash from `local = {}`) — promote based on the actual
12792+ # key/value types so the C declaration picks the matching
12793+ # sp_*Hash struct. Mirrors the ivar-side promotion in
12794+ # scan_writer_calls. Only fires when @scan_empty_hash_flags
12795+ # confirms every prior write to this local was an empty-hash
12796+ # literal — concretely-typed hashes (`h = {"a" => 1}`) keep
12797+ # their declared type even when later []= writes mix value
12798+ # types.
1276312799 if @nd_type[nid] == "CallNode"
1276412800 if @nd_name[nid] == "[]="
1276512801 recv = @nd_receiver[nid]
@@ -12769,18 +12805,36 @@
1276912805 if args_id >= 0
1277012806 aargs = get_args(args_id)
1277112807 if aargs.length >= 2
12772- val_type = infer_type(aargs[1])
12773- if val_type == "string"
12774- ki = 0
12775- while ki < names.length
12776- if names[ki] == hname
12777- if types[ki] == "str_int_hash"
12778- types[ki] = "str_str_hash"
12779- @needs_str_str_hash = 1
12808+ ki = 0
12809+ while ki < names.length
12810+ if names[ki] == hname && types[ki] == "str_int_hash"
12811+ if ki < @scan_empty_hash_flags.length && @scan_empty_hash_flags[ki] == "1"
12812+ key_type = infer_type(aargs[0])
12813+ val_type = infer_type(aargs[aargs.length - 1])
12814+ promoted = promote_empty_hash_for(key_type, val_type)
12815+ if promoted != "" && promoted != "str_int_hash"
12816+ types[ki] = promoted
12817+ @scan_empty_hash_flags[ki] = ""
12818+ if promoted == "str_str_hash"
12819+ @needs_str_str_hash = 1
12820+ elsif promoted == "int_str_hash"
12821+ @needs_int_str_hash = 1
12822+ @needs_int_array = 1
12823+ elsif promoted == "sym_int_hash"
12824+ @needs_sym_int_hash = 1
12825+ elsif promoted == "sym_str_hash"
12826+ @needs_sym_str_hash = 1
12827+ elsif promoted == "str_poly_hash"
12828+ @needs_str_poly_hash = 1
12829+ @needs_rb_value = 1
12830+ elsif promoted == "sym_poly_hash"
12831+ @needs_sym_poly_hash = 1
12832+ @needs_rb_value = 1
12833+ end
1278012834 end
1278112835 end
12782- ki = ki + 1
1278312836 end
12837+ ki = ki + 1
1278412838 end
1278512839 end
1278612840 end
@@ -19757,15 +19811,39 @@
1975719811 end
1975819812 end
1975919813 end
19760- # Empty hash literal: create the correct hash type
19761- if vt == "str_str_hash"
19814+ # Empty hash literal: create the correct hash type. The local's
19815+ # type was promoted by scan_locals' empty-hash promotion (str_int_hash
19816+ # default refined based on first []= site's key/value types).
19817+ # Without these arms the literal `{}` would always emit
19818+ # sp_StrIntHash_new() and the assignment would mismatch the
19819+ # promoted local's struct type.
19820+ 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"
1976219821 expr_id2 = @nd_expression[nid]
1976319822 if expr_id2 >= 0 && @nd_type[expr_id2] == "HashNode"
1976419823 elems2 = parse_id_list(@nd_elements[expr_id2])
1976519824 if elems2.length == 0
19766- @needs_str_str_hash = 1
1976719825 @needs_gc = 1
19768- emit(" " + vref + " = sp_StrStrHash_new();")
19826+ if vt == "str_str_hash"
19827+ @needs_str_str_hash = 1
19828+ emit(" " + vref + " = sp_StrStrHash_new();")
19829+ elsif vt == "int_str_hash"
19830+ @needs_int_str_hash = 1
19831+ emit(" " + vref + " = sp_IntStrHash_new();")
19832+ elsif vt == "sym_int_hash"
19833+ @needs_sym_int_hash = 1
19834+ emit(" " + vref + " = sp_SymIntHash_new();")
19835+ elsif vt == "sym_str_hash"
19836+ @needs_sym_str_hash = 1
19837+ emit(" " + vref + " = sp_SymStrHash_new();")
19838+ elsif vt == "str_poly_hash"
19839+ @needs_str_poly_hash = 1
19840+ @needs_rb_value = 1
19841+ emit(" " + vref + " = sp_StrPolyHash_new();")
19842+ elsif vt == "sym_poly_hash"
19843+ @needs_sym_poly_hash = 1
19844+ @needs_rb_value = 1
19845+ emit(" " + vref + " = sp_SymPolyHash_new();")
19846+ end
1976919847 return
1977019848 end
1977119849 end