Commit 874e0245
874e02451105Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): cls method body sees specialization-scope param type
def self.from_row(row) defined on a Base class and called from
multiple subclasses with different concrete types (CommentRow,
ArticleRow) gets specialized per-call-site into
sp_<C>_cls_from_row(sp_<RowType> lv_row). The body is shared
across specializations but the param's emitted type is per-spec.
Two coordinated emits broke the shape:
-
emit_clear_ptr_ivars_chainwalked own ivars after parent ivars, emitting a secondiv_X = NULLinit for an ivar that the parent chain had already cleared with the parent's declared type. The parent's type wins in the struct layout (emit_class_fieldsskips the child-side dup), so the child- side re-init'sNULLlands on asp_RbValslot — a C type error. Skip child-side inits when the ivar lives in the parent chain. -
infer_typeconsulted the@nd_inferred_typecache filled by analyze'swalk_and_cache, which sees only the Base-side unspecialized param type ("int" — the unconstrained fallback). In a Comment-specialized body,row.commenterthen returned int and the attr_writer's boxer dispatch pickedsp_box_inton aconst char *value. Add a CallNode arm that detectsLV(name).attr_reader_nameagainst a scope-declaredobj_<C>type and resolves the reader's slot type from that class's ivar table — same pattern as the existing is_a? narrow override.
Fixes #694
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 874e02451105
- Parent
- 09c88133b198
spinel_codegen.rbmodifiedView file
end end end # Specialization-scope override: when a class method body # (e.g. `def self.from_row(row)`) is reused across multiple # subclass specializations, the per-call-site specialization # narrows the `row` param to `obj_<CommentRow>` / `obj_<ArticleRow>` # etc. via declare_var in emit_class_level_method. But the # @nd_inferred_type cache was populated once during analyze with # the unspecialized union (often "int" — the unconstrained fallback), # so `row.commenter` reads back as int and the boxer dispatch # picks sp_box_int on a string field. Re-resolve `LV.attr_reader` # against the active scope when the scope's declared type is a # concrete obj_<C> that the analyze-time union missed. if @nd_type[nid] == "CallNode" cmt_recv_id = @nd_receiver[nid] if cmt_recv_id >= 0 && @nd_type[cmt_recv_id] == "LocalVariableReadNode" cmt_lvname = @nd_name[cmt_recv_id] cmt_scope_t = find_var_type(cmt_lvname) if is_obj_type(cmt_scope_t) == 1 cmt_cname = cmt_scope_t[4, cmt_scope_t.length - 4] cmt_ci = find_class_idx(cmt_cname) if cmt_ci >= 0 cmt_mname = @nd_name[nid] if cls_has_attr_reader(cmt_ci, cmt_mname) == 1 return cls_ivar_type(cmt_ci, "@" + cmt_mname) end end end end end # Cache lookup. analyze.rb's annotate_all_node_types fills # @nd_inferred_type for every reachable node OUTSIDE block bodies # (block-iteration scope is iterator-specific and dispatched at j = 0 while j < names.length if j < types.length kind = gc_trace_kind(types[j]) field = "self->" + sanitize_ivar(names[j]) if kind == "poly" emit_raw(" " + field + " = sp_box_nil();") elsif kind == "ptr" emit_raw(" " + field + " = NULL;") elsif kind == "value_type" emit_raw(" " + field + " = (" + c_type(types[j]) + "){0};") # Skip ivars already cleared by the parent chain walk above. # The struct field uses the parent's declared type (parent-fields- # first layout in emit_class_fields skips child-side dups), so the # parent's init is the one that matches the actual slot type. A # second init from the child side reads the child's narrower type # table and emits e.g. `NULL` for an sp_RbVal slot — a C type error. in_parent = 0 if @cls_parents[ci] != "" pi_dup = find_class_idx(@cls_parents[ci]) if pi_dup >= 0 && ivar_in_chain(pi_dup, names[j]) == 1 in_parent = 1 end end if in_parent == 0 kind = gc_trace_kind(types[j]) field = "self->" + sanitize_ivar(names[j]) if kind == "poly" emit_raw(" " + field + " = sp_box_nil();") elsif kind == "ptr" emit_raw(" " + field + " = NULL;") elsif kind == "value_type" emit_raw(" " + field + " = (" + c_type(types[j]) + "){0};") end end end j = j + 1test/cls_method_specialization_boxer.expectedaddedView file
Loading diff…
test/cls_method_specialization_boxer.rbaddedView file
Loading diff…