Commit 992c82ea
992c82eada39Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(analyze,codegen): inherited cmethod body resolves bare calls per subclass
Sibling to #516. When a subclass inherits a class method whose
body calls another class method via bare dispatch (self.last
calls bare all; Sub overrides all with a different return
type), spinel monomorphized the call correctly
(sp_Sub_cls_all() got invoked) but the surrounding type
inference still used the parent's signature -- the local slot
for the returned array was typed sp_IntArray * from Base.all's
empty [] literal while sp_Sub_cls_all() actually returned
sp_PtrArray *. The subsequent [-1] then dispatched via
Integer#[idx] bit-extract instead of array indexing.
Two layered causes:
- Analyze's walk_and_cache writes a per-AST-node-id type cache.
Inherited cmeth bodies share their AST node ids across
subclass copies (
propagate_inherited_class_methodsreuses the parent's body id). The first walker populated the cache; later walkers' recomputes under a different @current_class_idx were short-circuited by infer_type's own cache hit at line 2134, so the second walker silently inherited the first walker's class-context answer. - Codegen's infer_type for CallNode had no fallback path for bare cmeth calls -- the cache-miss arm defaulted to "int" -- so even when analyze deliberately skipped the cache for those nodes, codegen still mistyped them.
Fix three pieces:
- analyze/walk_and_cache: invalidate the per-node cache slot before the recompute call so the second walker actually overwrites instead of being short-circuited.
- analyze/walk_and_cache: extend skip_cache to bare CallNodes
resolving to a sibling cmeth on @current_class_idx, mirroring
the existing
newskip_cache and the same reasoning (the resolution must follow emit-time @current_class_idx). - codegen/infer_type: cache-miss arm now mirrors analyze's @cls_cmeth_returns lookup so the dynamic resolution finds the subclass's override at emit time.
Known limitation: the cache is single-valued per node id, so a
shared body emitted under two distinct @current_class_idx values
(both Base and Sub live + emitted) would have only one cache
view. The skip_cache + codegen dynamic resolution mostly hides
this since each emit recomputes from the cmeth table, but
non-bare-cmeth nodes (the [-1] bracket call itself) still rely
on the cached recv type. Real-blog's @cls_cmeth_live filtering
keeps only the leaf subclass live, so this case doesn't bite in
practice yet -- proper fix needs per-(ci, node-id) caching.
Closes #523
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 992c82eada39
- Parent
- 25c6cc357a4a
spinel_analyze.rbmodifiedView file
if nt == "CallNode" && @nd_receiver[nid] < 0 && @nd_name[nid] == "new" skip_cache = 1 end # Bare class-method call inside an inherited class-method body # resolves to the *calling* subclass's override at emit time. The # body's AST id is shared across the propagated subclass copies # (see `propagate_inherited_class_methods`), so walk_and_cache # sees the same node id once per class and the last writer wins. # When a subclass overrides a class method that the inherited # body calls (e.g. Base.last calls `all`, Sub overrides `all` to # return a different type), the cache must NOT freeze the parent's # signature — codegen needs to re-resolve under the subclass's # @current_class_idx. Issue #523, sibling to #516. Triggered only # for bare calls that resolve to a cmeth on the current class # (find_method_idx < 0 rules out true top-level methods, which # are stable across walks). if nt == "CallNode" && @nd_receiver[nid] < 0 && @nd_name[nid] != "new" && @current_class_idx >= 0 mn_check = @nd_name[nid] if find_method_idx(mn_check) < 0 cmnames_check = @cls_cmeth_names[@current_class_idx].split(";") ccheck = 0 while ccheck < cmnames_check.length if cmnames_check[ccheck] == mn_check skip_cache = 1 ccheck = cmnames_check.length else ccheck = ccheck + 1 end end end end # `lambda.call(...)` — the return type comes from # @lambda_var_ret_types which is built at codegen time # multi-arg lambda). Caching at analyze would freeze the type as end end if skip_cache == 0 # Invalidate any prior cache before recompute. Inherited cmeth # bodies are walked once per class that holds a propagated copy # (Base.last's body is also walked under Sub's @current_class_idx, # etc.). infer_type's own cache-hit short-circuit would otherwise # return the first walker's answer and silently swallow the # second walker's recompute under the subclass context. Issue # #523 made this user-visible: Sub.last calls bare `all`, the # node's cached type was frozen at Base.all's int_array, and # codegen emitted `sp_IntArray *` for a slot now receiving # `sp_PtrArray *`. @nd_inferred_type[nid] = "" @nd_inferred_type[nid] = infer_type(nid) end endspinel_codegen.rbmodifiedView file
Loading diff…
test/inherited_cmethod_subclass_dispatch.rbaddedView file
Loading diff…
test/inherited_cmethod_subclass_dispatch.rb.expectedaddedView file
Loading diff…