Commit 6165d11f

6165d11f070aMerged

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

Description

fix(codegen): module ivar empty-{} / empty-[] honors const slot type

Real-blog archive: ActionView::ViewHelpers.reset_slots! failed C compile.

module ActionView::ViewHelpers @slots = {} # const slot promoted to sym_poly_hash # by @slots[sym_key] = ... writes elsewhere.

def self.reset_slots!
  @slots = {}                # ← codegen emitted
                             # `self->iv_slots = sp_StrIntHash_new()`
                             # — wrong constructor AND dangling
                             # `self` (module class methods have
                             # no instance).
end

end

Two bugs in the InstanceVariableWriteNode emit path:

  1. Empty-{} / empty-[] promotion gated on `@current_class_idx

    = 0. Module class methods (def self.Xon a module) leave that idx at -1, so theivt lookup fell through to the default empty-hash ctor (sp_StrIntHash_new()) regardless of the slot's actual variant. Look up the const's stored type via @module_names/find_const_idx` when the class-idx path returns "".

  2. LHS hardcoded to self_arrow + sanitize_ivar(iname) — self->iv_slots. For module class methods that's a dangling self. ivar_lhs already knows to route @X inside <Mod>_cls_* to cst_<Mod>_<X>; use it.

Both the hash arm (line 20977) and the array arm (line 20985) shared the same hardcoded LHS, so both are switched to ivar_lhs.

Real-blog archive: errors 56 → 55. Tests 409 pass (+1 regression covering module-level hash reset to {} plus the array variant @list = []), bench 52, optcarrot checksum 59662.

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

Details

Commit
6165d11f070a
Parent
95572f7ccfd3

spinel_codegen.rbmodifiedView file

@@ -20925,6 +20925,30 @@
2092520925 if @current_class_idx >= 0
2092620926 ivt = cls_ivar_type(@current_class_idx, iname)
2092720927 end
20928+ # Module class method (`def self.X` on a module): the ivar
20929+ # `@X` resolves to a module-level constant `cst_<Mod>_<X>`,
20930+ # not an instance ivar. Look up the const's stored type so the
20931+ # empty-`{}` / empty-`[]` promotion below picks the right
20932+ # `sp_*_new()` for the slot's actual hash/array variant.
20933+ # Without this, `def self.reset_slots!; @slots = {}; end` on a
20934+ # module whose @slots constant is sym_poly_hash emits
20935+ # sp_StrIntHash_new() (the empty-hash default), failing C
20936+ # compile at the cst write.
20937+ if ivt == "" && @current_method_name != ""
20938+ mci = 0
20939+ while mci < @module_names.length
20940+ mod_n = @module_names[mci]
20941+ if mod_n != "" && @current_method_name.start_with?(mod_n + "_cls_")
20942+ const_n = mod_n + "_" + iname[1, iname.length - 1]
20943+ const_idx = find_const_idx(const_n)
20944+ if const_idx >= 0
20945+ ivt = @const_types[const_idx]
20946+ break
20947+ end
20948+ end
20949+ mci = mci + 1
20950+ end
20951+ end
2092820952 if is_empty_hash_literal(expr_id) == 1 && ivt != "" && ivt != "str_int_hash"
2092920953 ctor = ""
2093020954 if ivt == "str_str_hash"
@@ -20950,7 +20974,7 @@
2095020974 end
2095120975 if ctor != ""
2095220976 @needs_gc = 1
20953- emit(" " + self_arrow + sanitize_ivar(iname) + " = " + ctor + ";")
20977+ emit(" " + ivar_lhs(iname) + " = " + ctor + ";")
2095420978 return
2095520979 end
2095620980 end
@@ -20958,7 +20982,7 @@
2095820982 ctor = empty_array_new_for_type(ivt)
2095920983 if ctor != ""
2096020984 @needs_gc = 1
20961- emit(" " + self_arrow + sanitize_ivar(iname) + " = " + ctor + ";")
20985+ emit(" " + ivar_lhs(iname) + " = " + ctor + ";")
2096220986 return
2096320987 end
2096420988 end