Commit d5b20a0d

d5b20a0db9dbMerged

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

Description

fix(analyze,codegen): per-(class, cmeth) scope tables for inherited bodies

Task b. `Comment.find` inherited the body of `Base.find`:

class Base
  def self.find(id)
    result = _adapter_find_by_id(id)
    result
  end
end

Each subclass overrides `_adapter_find_by_id` with a Self- returning version. scan_locals correctly picked up the per- subclass return type when it scanned with `@current_class_idx = ci` set, but the result was stored into `@nd_scope_names` keyed by the AST body id only — and the body id is shared across subclasses (propagate_inherited_class_methods reuses the parent's bid). Whichever subclass scanned last won; the others got the wrong LV C types.

Concretely `Comment.find` emitted

sp_Article * lv_result = NULL;
lv_result = sp_Comment_cls__adapter_find_by_id(lv_id);
return lv_result;

— signature returns sp_Comment * but the local is sp_Article *. Warning under -Wincompatible-pointer-types; silent miscompile if struct layouts diverge.

Fix: per-(class, cmeth_idx) scope tables.

  • analyze: `@cls_cmeth_scope_names` / `@cls_cmeth_scope_types` arrays parallel to @cls_cmeth_bodies; outer separator ";" indexes by cmj, inner separator "|" matches @nd_scope_names' format. The existing scan loop now stores results into the per-(ci, cmj) entry alongside the legacy per-bid @nd_scope_names write.

  • IR: two new ir_emit_sa entries serialize the per-(ci, cmj) tables across the analyze/codegen split.

  • codegen: emit_class_methods reads @cls_cmeth_scope_names[i] / [j] and passes the entry to emit_class_level_method. New declare_method_locals_from helper consumes the precomputed entry directly instead of going through @nd_scope_names[bid].

Closes #450 cluster's Comment.find shape; broader Self specialization (instance method bodies, deeper inheritance chains) follows the same pattern and can extend the same per-(class, method) infrastructure.

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

Details

Commit
d5b20a0db9db
Parent
dd5623bbe116

spinel_analyze.rbmodifiedView file

@@ -230,6 +230,18 @@
230230 @cls_cmeth_returns = "".split(",")
231231 @cls_cmeth_bodies = "".split(",")
232232 @cls_cmeth_defaults = "".split(",")
233+ # Per-(class, cmeth) local scope tables. @nd_scope_names is
234+ # keyed by AST body id only; an inherited class method's body
235+ # is shared across subclasses, so the per-bid table gets
236+ # overwritten by whichever subclass scans last. These per-
237+ # (class, cmj) tables preserve subclass-specific local type
238+ # info (e.g. Comment.find's `result : sp_Comment *` vs
239+ # Article.find's `result : sp_Article *`). Outer separator
240+ # ";" indexes by cmj (mirrors @cls_cmeth_bodies); inner
241+ # separator "|" matches @nd_scope_names' format so the codegen
242+ # consumer can split into the same (lnames, ltypes) pair.
243+ @cls_cmeth_scope_names = "".split(",")
244+ @cls_cmeth_scope_types = "".split(",")
233245 @cls_is_value_type = []
234246 # SRA (scalar replacement of aggregates) eligibility flag per class.
235247 # Classes marked here can have their non-escaping instances replaced
@@ -6809,6 +6821,8 @@
68096821 @cls_cmeth_returns.push("")
68106822 @cls_cmeth_bodies.push("")
68116823 @cls_cmeth_defaults.push("")
6824+ @cls_cmeth_scope_names.push("")
6825+ @cls_cmeth_scope_types.push("")
68126826 @cls_meth_has_yield.push("")
68136827
68146828 # Collect class body
@@ -9301,6 +9315,8 @@
93019315 @cls_cmeth_returns.push("")
93029316 @cls_cmeth_bodies.push("")
93039317 @cls_cmeth_defaults.push("")
9318+ @cls_cmeth_scope_names.push("")
9319+ @cls_cmeth_scope_types.push("")
93049320 @cls_meth_has_yield.push("0")
93059321 end
93069322
@@ -9331,6 +9347,8 @@
93319347 @cls_cmeth_returns.push("")
93329348 @cls_cmeth_bodies.push("")
93339349 @cls_cmeth_defaults.push("")
9350+ @cls_cmeth_scope_names.push("")
9351+ @cls_cmeth_scope_types.push("")
93349352 @cls_meth_has_yield.push("")
93359353
93369354 # Get field names from symbol args (skip keyword_init hash)
@@ -20546,6 +20564,8 @@
2054620564 buf = ir_emit_sa(buf, "@cls_cmeth_returns", @cls_cmeth_returns)
2054720565 buf = ir_emit_sa(buf, "@cls_cmeth_bodies", @cls_cmeth_bodies)
2054820566 buf = ir_emit_sa(buf, "@cls_cmeth_defaults", @cls_cmeth_defaults)
20567+ buf = ir_emit_sa(buf, "@cls_cmeth_scope_names", @cls_cmeth_scope_names)
20568+ buf = ir_emit_sa(buf, "@cls_cmeth_scope_types", @cls_cmeth_scope_types)
2054920569 buf = ir_emit_ia(buf, "@cls_is_value_type", @cls_is_value_type)
2055020570 buf = ir_emit_ia(buf, "@cls_is_sra", @cls_is_sra)
2055120571 buf = ir_emit_sa(buf, "@cls_meth_has_yield", @cls_meth_has_yield)
@@ -22051,9 +22071,20 @@
2205122071 cm_bodies = @cls_cmeth_bodies[ci].split(";")
2205222072 cm_names = @cls_cmeth_names[ci].split(";")
2205322073 saved_meth = @current_method_name
22074+ # Build per-(ci, cmj) scope tables alongside the per-bid
22075+ # @nd_scope_names entry. Inherited cmeths share their body id
22076+ # across subclasses, so the per-bid entry gets overwritten by
22077+ # whichever subclass scans last (effectively last-class-wins,
22078+ # leaving every other subclass with the wrong LV types — see
22079+ # Comment.find returning sp_Article *). Per-(ci, cmj) keeps
22080+ # each subclass's specialized result alive for codegen.
22081+ cms_per_ci = "".split(",")
22082+ cmt_per_ci = "".split(",")
2205422083 cbj = 0
2205522084 while cbj < cm_bodies.length
2205622085 cbid = cm_bodies[cbj].to_i
22086+ scope_n_entry = ""
22087+ scope_t_entry = ""
2205722088 if cbid >= 0
2205822089 if cbj < cm_names.length
2205922090 @current_method_name = @cls_names[ci] + "_cls_" + cm_names[cbj]
@@ -22075,10 +22106,16 @@
2207522106 refine_method_body_locals(cbid, cml, cmt, cpnames)
2207622107 @nd_scope_names[cbid] = cml.join("|")
2207722108 @nd_scope_types[cbid] = cmt.join("|")
22109+ scope_n_entry = cml.join("|")
22110+ scope_t_entry = cmt.join("|")
2207822111 pop_scope
2207922112 end
22113+ cms_per_ci.push(scope_n_entry)
22114+ cmt_per_ci.push(scope_t_entry)
2208022115 cbj = cbj + 1
2208122116 end
22117+ @cls_cmeth_scope_names[ci] = cms_per_ci.join(";")
22118+ @cls_cmeth_scope_types[ci] = cmt_per_ci.join(";")
2208222119 @current_method_name = saved_meth
2208322120 @current_class_idx = saved_ci
2208422121 ci = ci + 1