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:

  1. emit_clear_ptr_ivars_chain walked own ivars after parent ivars, emitting a second iv_X = NULL init 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_fields skips the child-side dup), so the child- side re-init's NULL lands on a sp_RbVal slot — a C type error. Skip child-side inits when the ivar lives in the parent chain.

  2. infer_type consulted the @nd_inferred_type cache filled by analyze's walk_and_cache, which sees only the Base-side unspecialized param type ("int" — the unconstrained fallback). In a Comment-specialized body, row.commenter then returned int and the attr_writer's boxer dispatch picked sp_box_int on a const char * value. Add a CallNode arm that detects LV(name).attr_reader_name against a scope-declared obj_<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

@@ -2815,6 +2815,34 @@
28152815 end
28162816 end
28172817 end
2818+ # Specialization-scope override: when a class method body
2819+ # (e.g. `def self.from_row(row)`) is reused across multiple
2820+ # subclass specializations, the per-call-site specialization
2821+ # narrows the `row` param to `obj_<CommentRow>` / `obj_<ArticleRow>`
2822+ # etc. via declare_var in emit_class_level_method. But the
2823+ # @nd_inferred_type cache was populated once during analyze with
2824+ # the unspecialized union (often "int" — the unconstrained fallback),
2825+ # so `row.commenter` reads back as int and the boxer dispatch
2826+ # picks sp_box_int on a string field. Re-resolve `LV.attr_reader`
2827+ # against the active scope when the scope's declared type is a
2828+ # concrete obj_<C> that the analyze-time union missed.
2829+ if @nd_type[nid] == "CallNode"
2830+ cmt_recv_id = @nd_receiver[nid]
2831+ if cmt_recv_id >= 0 && @nd_type[cmt_recv_id] == "LocalVariableReadNode"
2832+ cmt_lvname = @nd_name[cmt_recv_id]
2833+ cmt_scope_t = find_var_type(cmt_lvname)
2834+ if is_obj_type(cmt_scope_t) == 1
2835+ cmt_cname = cmt_scope_t[4, cmt_scope_t.length - 4]
2836+ cmt_ci = find_class_idx(cmt_cname)
2837+ if cmt_ci >= 0
2838+ cmt_mname = @nd_name[nid]
2839+ if cls_has_attr_reader(cmt_ci, cmt_mname) == 1
2840+ return cls_ivar_type(cmt_ci, "@" + cmt_mname)
2841+ end
2842+ end
2843+ end
2844+ end
2845+ end
28182846 # Cache lookup. analyze.rb's annotate_all_node_types fills
28192847 # @nd_inferred_type for every reachable node OUTSIDE block bodies
28202848 # (block-iteration scope is iterator-specific and dispatched at
@@ -9172,14 +9200,29 @@
91729200 j = 0
91739201 while j < names.length
91749202 if j < types.length
9175- kind = gc_trace_kind(types[j])
9176- field = "self->" + sanitize_ivar(names[j])
9177- if kind == "poly"
9178- emit_raw(" " + field + " = sp_box_nil();")
9179- elsif kind == "ptr"
9180- emit_raw(" " + field + " = NULL;")
9181- elsif kind == "value_type"
9182- emit_raw(" " + field + " = (" + c_type(types[j]) + "){0};")
9203+ # Skip ivars already cleared by the parent chain walk above.
9204+ # The struct field uses the parent's declared type (parent-fields-
9205+ # first layout in emit_class_fields skips child-side dups), so the
9206+ # parent's init is the one that matches the actual slot type. A
9207+ # second init from the child side reads the child's narrower type
9208+ # table and emits e.g. `NULL` for an sp_RbVal slot — a C type error.
9209+ in_parent = 0
9210+ if @cls_parents[ci] != ""
9211+ pi_dup = find_class_idx(@cls_parents[ci])
9212+ if pi_dup >= 0 && ivar_in_chain(pi_dup, names[j]) == 1
9213+ in_parent = 1
9214+ end
9215+ end
9216+ if in_parent == 0
9217+ kind = gc_trace_kind(types[j])
9218+ field = "self->" + sanitize_ivar(names[j])
9219+ if kind == "poly"
9220+ emit_raw(" " + field + " = sp_box_nil();")
9221+ elsif kind == "ptr"
9222+ emit_raw(" " + field + " = NULL;")
9223+ elsif kind == "value_type"
9224+ emit_raw(" " + field + " = (" + c_type(types[j]) + "){0};")
9225+ end
91839226 end
91849227 end
91859228 j = j + 1