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
@cls_cmeth_returns = "".split(",") @cls_cmeth_bodies = "".split(",") @cls_cmeth_defaults = "".split(",") # Per-(class, cmeth) local scope tables. @nd_scope_names is # keyed by AST body id only; an inherited class method's body # is shared across subclasses, so the per-bid table gets # overwritten by whichever subclass scans last. These per- # (class, cmj) tables preserve subclass-specific local type # info (e.g. Comment.find's `result : sp_Comment *` vs # Article.find's `result : sp_Article *`). Outer separator # ";" indexes by cmj (mirrors @cls_cmeth_bodies); inner # separator "|" matches @nd_scope_names' format so the codegen # consumer can split into the same (lnames, ltypes) pair. @cls_cmeth_scope_names = "".split(",") @cls_cmeth_scope_types = "".split(",") @cls_is_value_type = [] # SRA (scalar replacement of aggregates) eligibility flag per class. # Classes marked here can have their non-escaping instances replaced @cls_cmeth_returns.push("") @cls_cmeth_bodies.push("") @cls_cmeth_defaults.push("") @cls_cmeth_scope_names.push("") @cls_cmeth_scope_types.push("") @cls_meth_has_yield.push("") # Collect class body @cls_cmeth_returns.push("") @cls_cmeth_bodies.push("") @cls_cmeth_defaults.push("") @cls_cmeth_scope_names.push("") @cls_cmeth_scope_types.push("") @cls_meth_has_yield.push("0") end @cls_cmeth_returns.push("") @cls_cmeth_bodies.push("") @cls_cmeth_defaults.push("") @cls_cmeth_scope_names.push("") @cls_cmeth_scope_types.push("") @cls_meth_has_yield.push("") # Get field names from symbol args (skip keyword_init hash) buf = ir_emit_sa(buf, "@cls_cmeth_returns", @cls_cmeth_returns) buf = ir_emit_sa(buf, "@cls_cmeth_bodies", @cls_cmeth_bodies) buf = ir_emit_sa(buf, "@cls_cmeth_defaults", @cls_cmeth_defaults) buf = ir_emit_sa(buf, "@cls_cmeth_scope_names", @cls_cmeth_scope_names) buf = ir_emit_sa(buf, "@cls_cmeth_scope_types", @cls_cmeth_scope_types) buf = ir_emit_ia(buf, "@cls_is_value_type", @cls_is_value_type) buf = ir_emit_ia(buf, "@cls_is_sra", @cls_is_sra) buf = ir_emit_sa(buf, "@cls_meth_has_yield", @cls_meth_has_yield) cm_bodies = @cls_cmeth_bodies[ci].split(";") cm_names = @cls_cmeth_names[ci].split(";") saved_meth = @current_method_name # Build per-(ci, cmj) scope tables alongside the per-bid # @nd_scope_names entry. Inherited cmeths share their body id # across subclasses, so the per-bid entry gets overwritten by # whichever subclass scans last (effectively last-class-wins, # leaving every other subclass with the wrong LV types — see # Comment.find returning sp_Article *). Per-(ci, cmj) keeps # each subclass's specialized result alive for codegen. cms_per_ci = "".split(",") cmt_per_ci = "".split(",") cbj = 0 while cbj < cm_bodies.length cbid = cm_bodies[cbj].to_i scope_n_entry = "" scope_t_entry = "" if cbid >= 0 if cbj < cm_names.length @current_method_name = @cls_names[ci] + "_cls_" + cm_names[cbj] refine_method_body_locals(cbid, cml, cmt, cpnames) @nd_scope_names[cbid] = cml.join("|") @nd_scope_types[cbid] = cmt.join("|") scope_n_entry = cml.join("|") scope_t_entry = cmt.join("|") pop_scope end cms_per_ci.push(scope_n_entry) cmt_per_ci.push(scope_t_entry) cbj = cbj + 1 end @cls_cmeth_scope_names[ci] = cms_per_ci.join(";") @cls_cmeth_scope_types[ci] = cmt_per_ci.join(";") @current_method_name = saved_meth @current_class_idx = saved_ci ci = ci + 1spinel_codegen.rbmodifiedView file
Loading diff…
test/inherited_cmeth_self_returning_local.rbaddedView file
Loading diff…