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:
-
Empty-
{}/ empty-[]promotion gated on `@current_class_idx= 0
. Module class methods (def self.Xon a module) leave that idx at -1, so theivtlookup 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 "". -
LHS hardcoded to
self_arrow + sanitize_ivar(iname)—self->iv_slots. For module class methods that's a danglingself.ivar_lhsalready knows to route@Xinside<Mod>_cls_*tocst_<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
if @current_class_idx >= 0 ivt = cls_ivar_type(@current_class_idx, iname) end # Module class method (`def self.X` on a module): the ivar # `@X` resolves to a module-level constant `cst_<Mod>_<X>`, # not an instance ivar. Look up the const's stored type so the # empty-`{}` / empty-`[]` promotion below picks the right # `sp_*_new()` for the slot's actual hash/array variant. # Without this, `def self.reset_slots!; @slots = {}; end` on a # module whose @slots constant is sym_poly_hash emits # sp_StrIntHash_new() (the empty-hash default), failing C # compile at the cst write. if ivt == "" && @current_method_name != "" mci = 0 while mci < @module_names.length mod_n = @module_names[mci] if mod_n != "" && @current_method_name.start_with?(mod_n + "_cls_") const_n = mod_n + "_" + iname[1, iname.length - 1] const_idx = find_const_idx(const_n) if const_idx >= 0 ivt = @const_types[const_idx] break end end mci = mci + 1 end end if is_empty_hash_literal(expr_id) == 1 && ivt != "" && ivt != "str_int_hash" ctor = "" if ivt == "str_str_hash" end if ctor != "" @needs_gc = 1 emit(" " + self_arrow + sanitize_ivar(iname) + " = " + ctor + ";") emit(" " + ivar_lhs(iname) + " = " + ctor + ";") return end end ctor = empty_array_new_for_type(ivt) if ctor != "" @needs_gc = 1 emit(" " + self_arrow + sanitize_ivar(iname) + " = " + ctor + ";") emit(" " + ivar_lhs(iname) + " = " + ctor + ";") return end endtest/module_ivar_empty_hash_reset.rbaddedView file
Loading diff…
test/module_ivar_empty_hash_reset.rb.expectedaddedView file
Loading diff…