Commit c734bbad

c734bbad3ac0Merged

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

Description

fix(codegen): promote empty-{} ivar from string-valued []= writes

An ivar initialized as {} had its tracked type pinned to the empty-hash default (str_int_hash), and the codegen's []= site fed const char * values into sp_StrIntHash_set — the C compile warned and rejected the call.

The fix has three pieces, mirroring how empty [] arrays were already handled:

  1. scan_writer_calls recognises @h[k] = v against an ivar still typed str_int_hash and promotes the slot via a new promote_empty_hash_for(kt, vt) helper. The promotion uses a new replace_ivar_type (the existing update_ivar_type would widen the str_int_hash → str_str_hash transition to poly). The @needs_*_hash flag for the promoted type is set the same pass so emit_features picks up the runtime helpers before they are referenced.

  2. The same scanner now skips empty {} / [] literal writes when re-seeding the ivar type. Without this, the iterative inference loop kept widening the promoted type back to poly because each pass saw old=str_str_hash, new=str_int_hash.

  3. Both compile_stmt's InstanceVariableWriteNode arm and the inline ivar walker in emit_constructor route an empty {} against a promoted ivar to the matching sp_*Hash_new() ctor (str_str / int_str / sym_int / sym_str). Without this the slot type and the initializer type disagreed.

test/empty_hash_ivar_string_value.rb covers the issue's exact reproducer plus an int-keyed variant. The sym-keyed promotion path is wired but not part of the make-test surface because the runtime helper for sp_SymIntHash trips a pre-existing -Walloc-size-larger-than warning unrelated to this fix.

Closes #64

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

Details

Commit
c734bbad3ac0
Parent
3fbc04216ea2

spinel_codegen.rbmodifiedView file

@@ -3430,6 +3430,20 @@
34303430 # resolution. This helper distinguishes `[]` from `[1, 2, 3]` so the
34313431 # promotion machinery can know "writes haven't fixed the element type
34323432 # yet, so a later push can still pick it".
3433+ def is_empty_hash_literal(nid)
3434+ if nid < 0
3435+ return 0
3436+ end
3437+ if @nd_type[nid] != "HashNode"
3438+ return 0
3439+ end
3440+ elems = parse_id_list(@nd_elements[nid])
3441+ if elems.length == 0
3442+ return 1
3443+ end
3444+ 0
3445+ end
3446+
34333447 def is_empty_array_literal(nid)
34343448 if nid < 0
34353449 return 0
@@ -4549,6 +4563,26 @@
45494563 end
45504564 end
45514565
4566+ # Direct, unconditional ivar type replacement. Bypasses the
4567+ # widening logic in update_ivar_type — used when the caller has
4568+ # already determined the new type is correct (e.g. promoting an
4569+ # empty-hash default to a concrete hash type from a `[]=` write).
4570+ def replace_ivar_type(ci, iname, new_type)
4571+ names = @cls_ivar_names[ci].split(";")
4572+ types = @cls_ivar_types[ci].split(";")
4573+ k = 0
4574+ while k < names.length
4575+ if names[k] == iname
4576+ if k < types.length
4577+ types[k] = new_type
4578+ @cls_ivar_types[ci] = types.join(";")
4579+ end
4580+ return
4581+ end
4582+ k = k + 1
4583+ end
4584+ end
4585+
45524586 def update_ivar_type(ci, iname, new_type)
45534587 names = @cls_ivar_names[ci].split(";")
45544588 types = @cls_ivar_types[ci].split(";")
@@ -6015,6 +6049,38 @@
60156049 ""
60166050 end
60176051
6052+ # Pick the concrete hash type for an ivar that was initialized as
6053+ # the empty-hash default (`str_int_hash`) and is later written via
6054+ # `@h[k] = v`. Returns "" when the (key, value) pair has no
6055+ # matching concrete container — the caller leaves the ivar type
6056+ # alone in that case.
6057+ def promote_empty_hash_for(kt, vt)
6058+ if kt == "string"
6059+ if vt == "string"
6060+ return "str_str_hash"
6061+ end
6062+ if vt == "int" || vt == "bool" || vt == "nil"
6063+ return "str_int_hash"
6064+ end
6065+ return "str_poly_hash"
6066+ end
6067+ if kt == "symbol"
6068+ if vt == "string"
6069+ return "sym_str_hash"
6070+ end
6071+ if vt == "int" || vt == "bool" || vt == "nil"
6072+ return "sym_int_hash"
6073+ end
6074+ return "sym_poly_hash"
6075+ end
6076+ if kt == "int"
6077+ if vt == "string"
6078+ return "int_str_hash"
6079+ end
6080+ end
6081+ ""
6082+ end
6083+
60186084 # Does class `ci` provide `mname` as a reader, writer, or method?
60196085 # Walks parent classes for inherited members.
60206086 def class_has_method(ci, mname)
@@ -6166,9 +6232,17 @@
61666232 if @nd_type[nid] == "InstanceVariableWriteNode"
61676233 if @current_class_idx >= 0
61686234 iname = @nd_name[nid]
6169- at = infer_type(@nd_expression[nid])
6170- if at != "int" && at != "nil"
6171- update_ivar_type(@current_class_idx, iname, at)
6235+ expr_id = @nd_expression[nid]
6236+ # Empty `{}` / `[]` literal: don't reset the ivar's tracked
6237+ # type to the default (`str_int_hash` / `int_array`), since a
6238+ # later `[]=` write may have already promoted the slot to a
6239+ # more specific type. Reseeding from the empty-default would
6240+ # widen the promoted type to poly on the next iteration.
6241+ if is_empty_hash_literal(expr_id) == 0 && is_empty_array_literal(expr_id) == 0
6242+ at = infer_type(expr_id)
6243+ if at != "int" && at != "nil"
6244+ update_ivar_type(@current_class_idx, iname, at)
6245+ end
61726246 end
61736247 end
61746248 end
@@ -6207,6 +6281,44 @@
62076281 end
62086282 end
62096283 end
6284+ # `@h[k] = v` against an ivar still typed as the empty-hash
6285+ # default (str_int_hash) — promote based on the actual key/value
6286+ # types so the codegen picks the matching `sp_*Hash_set` (issue
6287+ # #64). Only the empty-default → another concrete hash type
6288+ # transition; richer mismatches stay where they are.
6289+ if mname == "[]=" && @current_class_idx >= 0 && recv >= 0 && @nd_type[recv] == "InstanceVariableReadNode"
6290+ iname = @nd_name[recv]
6291+ cur_t = cls_ivar_type(@current_class_idx, iname)
6292+ if cur_t == "str_int_hash"
6293+ args_id = @nd_arguments[nid]
6294+ if args_id >= 0
6295+ ai = get_args(args_id)
6296+ if ai.length >= 2
6297+ kt = infer_type(ai[0])
6298+ vt = infer_type(ai[ai.length - 1])
6299+ promoted = promote_empty_hash_for(kt, vt)
6300+ if promoted != "" && promoted != cur_t
6301+ # Direct assign: update_ivar_type would widen the
6302+ # existing-vs-new mismatch to `poly`, but we know this
6303+ # transition is just refining the empty-hash default.
6304+ replace_ivar_type(@current_class_idx, iname, promoted)
6305+ # Mark the runtime feature as needed before emit_features
6306+ # runs, so the corresponding `sp_*Hash_*` helpers are
6307+ # emitted into the generated C.
6308+ if promoted == "str_str_hash"
6309+ @needs_str_str_hash = 1
6310+ elsif promoted == "int_str_hash"
6311+ @needs_int_str_hash = 1
6312+ elsif promoted == "sym_int_hash"
6313+ @needs_sym_int_hash = 1
6314+ elsif promoted == "sym_str_hash"
6315+ @needs_sym_str_hash = 1
6316+ end
6317+ end
6318+ end
6319+ end
6320+ end
6321+ end
62106322 end
62116323 # Recurse
62126324 if @nd_body[nid] >= 0
@@ -10713,9 +10825,36 @@
1071310825 end
1071410826 end
1071510827 if @nd_type[sid] == "InstanceVariableWriteNode"
10716- ivar = sanitize_ivar(@nd_name[sid])
10717- val = compile_expr(@nd_expression[sid])
10718- emit_raw(" " + self_arrow + ivar + " = " + val + ";")
10828+ ivar_name = @nd_name[sid]
10829+ ivar = sanitize_ivar(ivar_name)
10830+ expr_id_iv = @nd_expression[sid]
10831+ # Match the special-case in compile_stmt: an empty `{}`
10832+ # assigned to an ivar promoted by scan_writer_calls needs
10833+ # the matching `sp_*Hash_new()` constructor (issue #64).
10834+ ivt = cls_ivar_type(@current_class_idx, ivar_name)
10835+ iv_ctor = ""
10836+ if is_empty_hash_literal(expr_id_iv) == 1 && ivt != "" && ivt != "str_int_hash"
10837+ if ivt == "str_str_hash"
10838+ @needs_str_str_hash = 1
10839+ iv_ctor = "sp_StrStrHash_new()"
10840+ elsif ivt == "int_str_hash"
10841+ @needs_int_str_hash = 1
10842+ iv_ctor = "sp_IntStrHash_new()"
10843+ elsif ivt == "sym_int_hash"
10844+ @needs_sym_int_hash = 1
10845+ iv_ctor = "sp_SymIntHash_new()"
10846+ elsif ivt == "sym_str_hash"
10847+ @needs_sym_str_hash = 1
10848+ iv_ctor = "sp_SymStrHash_new()"
10849+ end
10850+ end
10851+ if iv_ctor != ""
10852+ @needs_gc = 1
10853+ emit_raw(" " + self_arrow + ivar + " = " + iv_ctor + ";")
10854+ else
10855+ val = compile_expr(expr_id_iv)
10856+ emit_raw(" " + self_arrow + ivar + " = " + val + ";")
10857+ end
1071910858 else
1072010859 if @nd_type[sid] != "SuperNode"
1072110860 # Compile other statements (e.g., method calls like @arr[0] = val)
@@ -18492,7 +18631,39 @@
1849218631 return
1849318632 end
1849418633 if t == "InstanceVariableWriteNode"
18495- val = compile_expr(@nd_expression[nid])
18634+ iname = @nd_name[nid]
18635+ expr_id = @nd_expression[nid]
18636+ # Empty `{}` literal assigned to an ivar that scan_writer_calls
18637+ # has promoted to a non-default hash type. compile_hash_literal
18638+ # always returns `sp_StrIntHash_new()` for empty `{}`, so without
18639+ # this special-case the ivar slot's type and the initializer's
18640+ # type disagree (issue #64).
18641+ ivt = ""
18642+ if @current_class_idx >= 0
18643+ ivt = cls_ivar_type(@current_class_idx, iname)
18644+ end
18645+ if is_empty_hash_literal(expr_id) == 1 && ivt != "" && ivt != "str_int_hash"
18646+ ctor = ""
18647+ if ivt == "str_str_hash"
18648+ @needs_str_str_hash = 1
18649+ ctor = "sp_StrStrHash_new()"
18650+ elsif ivt == "int_str_hash"
18651+ @needs_int_str_hash = 1
18652+ ctor = "sp_IntStrHash_new()"
18653+ elsif ivt == "sym_int_hash"
18654+ @needs_sym_int_hash = 1
18655+ ctor = "sp_SymIntHash_new()"
18656+ elsif ivt == "sym_str_hash"
18657+ @needs_sym_str_hash = 1
18658+ ctor = "sp_SymStrHash_new()"
18659+ end
18660+ if ctor != ""
18661+ @needs_gc = 1
18662+ emit(" " + self_arrow + sanitize_ivar(iname) + " = " + ctor + ";")
18663+ return
18664+ end
18665+ end
18666+ val = compile_expr(expr_id)
1849618667 # Check if we're in a module class method
1849718668 mod_ivar = 0
1849818669 mi3 = 0
@@ -18500,7 +18671,6 @@
1850018671 mmod = @module_names[mi3]
1850118672 if mmod != ""
1850218673 if @current_method_name.start_with?(mmod + "_cls_")
18503- iname = @nd_name[nid]
1850418674 cname3 = mmod + "_" + iname[1, iname.length - 1]
1850518675 ci3 = find_const_idx(cname3)
1850618676 if ci3 >= 0
@@ -18512,7 +18682,7 @@
1851218682 mi3 = mi3 + 1
1851318683 end
1851418684 if mod_ivar == 0
18515- emit(" " + self_arrow + sanitize_ivar(@nd_name[nid]) + " = " + val + ";")
18685+ emit(" " + self_arrow + sanitize_ivar(iname) + " = " + val + ";")
1851618686 end
1851718687 return
1851818688 end