Commit 78de785d

78de785dc7bfMerged

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

Description

fix(codegen): heterogeneous Hash rebuild via each-loop now compiles

out = {}; src.each { |k, v| out[k] = v } failed to compile when src's value types were heterogeneous (e.g. {name: "alice", age: 30}). out was typed sp_StrIntHash from the empty-hash default and never escalated, so the C-level out[k] = v call passed sp_RbVal where mrb_int was expected.

Three places needed a fix:

  1. scan_locals's []= empty-hash promotion only re-fired on str_int_hash. Widen to all non-poly hash variants so a previously promoted sym_int_hash can re-promote to sym_poly_hash on a later poly write.

  2. infer_type(v) for a block param falls back to "int" because block params aren't declare_var'd while scan_locals walks the block body. Add scan_locals_arg_type that prefers scan_locals's collected types[] for LocalVariableReadNodes, falling back to infer_type otherwise.

  3. infer_all_returns and declare_method_locals merge the 1st pass (scan_locals_first_type) with the 2nd pass (full scan_locals) types. Both merges only widened nil/poly transitions; add a hash variant -> poly_hash escalation so build's return type and the surrounding scope reflect the upgraded local type. The class instance method path was using scan_locals_first_type for the 2nd pass; switch to scan_locals so the empty-hash promotion runs there too.

After these, build returns sp_SymPolyHash*, the @h ivar is typed sp_SymPolyHash, and the original Sam Ruby reproducer compiles cleanly.

Closes #133

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

Details

Commit
78de785dc7bf
Parent
c64c82b343ed

spinel_codegen.rbmodifiedView file

@@ -7166,6 +7166,29 @@
71667166 ""
71677167 end
71687168
7169+ # Issue #133: block-param-aware lookup used by scan_locals's
7170+ # empty-hash promotion. When `nid` reads a local variable that
7171+ # scan_locals itself has just collected (e.g. a block param like
7172+ # `|k, v|`), prefer the type recorded in `types[]` over
7173+ # `infer_type` — the block param hasn't been `declare_var`'d in
7174+ # scope yet, so a bare infer_type falls back to "int" and breaks
7175+ # the promotion of the surrounding `out[k] = v` write.
7176+ def scan_locals_arg_type(nid, names, types, params)
7177+ if nid >= 0 && @nd_type[nid] == "LocalVariableReadNode"
7178+ lname = @nd_name[nid]
7179+ k = 0
7180+ while k < names.length
7181+ if names[k] == lname
7182+ if k < types.length
7183+ return types[k]
7184+ end
7185+ end
7186+ k = k + 1
7187+ end
7188+ end
7189+ infer_type(nid)
7190+ end
7191+
71697192 # Does class `ci` provide `mname` as a reader, writer, or method?
71707193 # Walks parent classes for inherited members.
71717194 def class_has_method(ci, mname)
@@ -7827,6 +7850,12 @@
78277850 ltypes[mk] = ltypes2[lk]
78287851 set_var_type(lnames[mk], ltypes2[lk])
78297852 end
7853+ elsif (ltypes2[lk] == "str_poly_hash" || ltypes2[lk] == "sym_poly_hash") && ltypes[mk] != ltypes2[lk]
7854+ # Issue #133: 2nd pass discovered the iterated value is
7855+ # poly (each-rebuild over a poly_hash whose source type
7856+ # wasn't known on 1st pass). Upgrade.
7857+ ltypes[mk] = ltypes2[lk]
7858+ set_var_type(lnames[mk], ltypes2[lk])
78307859 end
78317860 end
78327861 mk = mk + 1
@@ -7931,14 +7960,33 @@
79317960 declare_var(rlnames[rlk], rltypes[rlk])
79327961 rlk = rlk + 1
79337962 end
7934- # Second pass with locals in scope
7963+ # Second pass: full scan_locals so the empty-hash promotion
7964+ # path (issue #133) sees the in-scope param/local types and
7965+ # promotes `out = {}` correctly when the iterated value is
7966+ # poly. The full scanner also marks polymorphic locals if it
7967+ # encounters disagreeing concrete-literal writes; that's a
7968+ # superset of scan_locals_first_type's "never-poly" rule but
7969+ # produces strictly more accurate types for return inference.
79357970 rlnames2 = "".split(",")
79367971 rltypes2 = "".split(",")
7937- scan_locals_first_type(bid, rlnames2, rltypes2, pnames)
7972+ scan_locals(bid, rlnames2, rltypes2, pnames)
79387973 rlk2 = 0
79397974 while rlk2 < rlnames2.length
7940- if rltypes2[rlk2] != "int"
7941- set_var_type(rlnames2[rlk2], rltypes2[rlk2])
7975+ cur = ""
7976+ mk2 = 0
7977+ while mk2 < rlnames.length
7978+ if rlnames[mk2] == rlnames2[rlk2]
7979+ cur = rltypes[mk2]
7980+ end
7981+ mk2 = mk2 + 1
7982+ end
7983+ if rltypes2[rlk2] != "int" && rltypes2[rlk2] != cur
7984+ # Same merge widening as the top-level path: hash variants
7985+ # escalate to poly_hash, int/nil → concrete, etc.
7986+ if cur == "int" || cur == "nil" ||
7987+ ((rltypes2[rlk2] == "str_poly_hash" || rltypes2[rlk2] == "sym_poly_hash") && cur != rltypes2[rlk2])
7988+ set_var_type(rlnames2[rlk2], rltypes2[rlk2])
7989+ end
79427990 end
79437991 rlk2 = rlk2 + 1
79447992 end
@@ -12488,6 +12536,13 @@
1248812536 ltypes[k] = ltypes2[j] + "?"
1248912537 end
1249012538 set_var_type(lnames[k], ltypes[k])
12539+ elsif (ltypes2[j] == "str_poly_hash" || ltypes2[j] == "sym_poly_hash") && ltypes[k] != ltypes2[j]
12540+ # Issue #133: empty-hash promotion in 2nd pass discovered the
12541+ # iterated value type is poly (each-rebuild over a poly_hash
12542+ # whose source type wasn't known on 1st pass). Upgrade the
12543+ # 1st pass's intermediate non-poly variant.
12544+ ltypes[k] = ltypes2[j]
12545+ set_var_type(lnames[k], ltypes2[j])
1249112546 end
1249212547 end
1249312548 k = k + 1
@@ -13094,14 +13149,41 @@
1309413149 if aargs.length >= 2
1309513150 ki = 0
1309613151 while ki < names.length
13097- if names[ki] == hname && types[ki] == "str_int_hash"
13098- if ki < @scan_empty_hash_flags.length && @scan_empty_hash_flags[ki] == "1"
13099- key_type = infer_type(aargs[0])
13100- val_type = infer_type(aargs[aargs.length - 1])
13152+ if names[ki] == hname
13153+ cur = types[ki]
13154+ # An empty-hash-tracked local is still "promotable"
13155+ # while it sits on str_int_hash (the default empty
13156+ # shape) OR on any non-poly hash variant we previously
13157+ # promoted it to from str_int_hash. Issue #133: a
13158+ # later pass discovers the iterated value type is
13159+ # actually poly (each-rebuild over a poly_hash whose
13160+ # source type wasn't known at first scan), and we
13161+ # need to escalate sym_int_hash → sym_poly_hash etc.
13162+ promotable = (cur == "str_int_hash" ||
13163+ cur == "str_str_hash" ||
13164+ cur == "int_str_hash" ||
13165+ cur == "sym_int_hash" ||
13166+ cur == "sym_str_hash")
13167+ if promotable && ki < @scan_empty_hash_flags.length && @scan_empty_hash_flags[ki] == "1"
13168+ # Issue #133: block params (e.g. `|k, v|` in
13169+ # `each`) aren't yet `declare_var`'d when scan_locals
13170+ # walks the block body, so a bare `infer_type(v)`
13171+ # falls back to "int" even though scan_locals has
13172+ # already collected v's actual type into `types[]`.
13173+ # Prefer scan_locals's local types array when the
13174+ # argument is a LocalVariableReadNode we've recorded;
13175+ # fall back to infer_type for everything else.
13176+ key_type = scan_locals_arg_type(aargs[0], names, types, params)
13177+ val_type = scan_locals_arg_type(aargs[aargs.length - 1], names, types, params)
1310113178 promoted = promote_empty_hash_for(key_type, val_type)
13102- if promoted != "" && promoted != "str_int_hash"
13179+ if promoted != "" && promoted != cur
1310313180 types[ki] = promoted
13104- @scan_empty_hash_flags[ki] = ""
13181+ # Clear the flag only when reaching the terminal
13182+ # poly_hash variant — int/str variants still need
13183+ # to be re-promotable on a later poly write.
13184+ if promoted == "str_poly_hash" || promoted == "sym_poly_hash"
13185+ @scan_empty_hash_flags[ki] = ""
13186+ end
1310513187 if promoted == "str_str_hash"
1310613188 @needs_str_str_hash = 1
1310713189 elsif promoted == "int_str_hash"