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:

  1. 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_methods reuses 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.
  2. 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 new skip_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

@@ -23103,6 +23103,34 @@
2310323103 if nt == "CallNode" && @nd_receiver[nid] < 0 && @nd_name[nid] == "new"
2310423104 skip_cache = 1
2310523105 end
23106+ # Bare class-method call inside an inherited class-method body
23107+ # resolves to the *calling* subclass's override at emit time. The
23108+ # body's AST id is shared across the propagated subclass copies
23109+ # (see `propagate_inherited_class_methods`), so walk_and_cache
23110+ # sees the same node id once per class and the last writer wins.
23111+ # When a subclass overrides a class method that the inherited
23112+ # body calls (e.g. Base.last calls `all`, Sub overrides `all` to
23113+ # return a different type), the cache must NOT freeze the parent's
23114+ # signature — codegen needs to re-resolve under the subclass's
23115+ # @current_class_idx. Issue #523, sibling to #516. Triggered only
23116+ # for bare calls that resolve to a cmeth on the current class
23117+ # (find_method_idx < 0 rules out true top-level methods, which
23118+ # are stable across walks).
23119+ if nt == "CallNode" && @nd_receiver[nid] < 0 && @nd_name[nid] != "new" && @current_class_idx >= 0
23120+ mn_check = @nd_name[nid]
23121+ if find_method_idx(mn_check) < 0
23122+ cmnames_check = @cls_cmeth_names[@current_class_idx].split(";")
23123+ ccheck = 0
23124+ while ccheck < cmnames_check.length
23125+ if cmnames_check[ccheck] == mn_check
23126+ skip_cache = 1
23127+ ccheck = cmnames_check.length
23128+ else
23129+ ccheck = ccheck + 1
23130+ end
23131+ end
23132+ end
23133+ end
2310623134 # `lambda.call(...)` — the return type comes from
2310723135 # @lambda_var_ret_types which is built at codegen time
2310823136 # multi-arg lambda). Caching at analyze would freeze the type as
@@ -23117,6 +23145,17 @@
2311723145 end
2311823146 end
2311923147 if skip_cache == 0
23148+ # Invalidate any prior cache before recompute. Inherited cmeth
23149+ # bodies are walked once per class that holds a propagated copy
23150+ # (Base.last's body is also walked under Sub's @current_class_idx,
23151+ # etc.). infer_type's own cache-hit short-circuit would otherwise
23152+ # return the first walker's answer and silently swallow the
23153+ # second walker's recompute under the subclass context. Issue
23154+ # #523 made this user-visible: Sub.last calls bare `all`, the
23155+ # node's cached type was frozen at Base.all's int_array, and
23156+ # codegen emitted `sp_IntArray *` for a slot now receiving
23157+ # `sp_PtrArray *`.
23158+ @nd_inferred_type[nid] = ""
2312023159 @nd_inferred_type[nid] = infer_type(nid)
2312123160 end
2312223161 end