Commit 13f741ed

13f741ed400bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): poly-recv user-class dispatch consults ivar narrow set

compile_poly_method_call's SP_TAG_OBJ user-class dispatch loop iterated every class in @cls_names and emitted an arm for any class that defined the method by name. The companion helper poly_dispatch_narrow_class_set already exists in compiler_helpers (and is consulted by poly_dispatch_return_type for the result-type union) but the emit loop missed the matching skip.

Effect: when the receiver is an ivar read whose observed types are entirely concrete obj_ entries, dispatch arms for unrelated classes that happen to share the method name are now dropped. The result-type union shrinks correspondingly, so a non-overlapping return type on one of those arms no longer widens the result temp to sp_RbVal and trips downstream type-strict consumers.

Standalone this patch fixes the dispatch when ivar observations are sharp; the canonical #575 repro still needs a follow-up because @worker's observations widen to "poly" before reaching here (the ivar's writer is a method param whose type is the unified union of callsite arg types — that union loses the concrete obj_X classes). The analyzer-side forward-propagation pass that recovers the concrete set is the next commit.

Refs #575. 586 tests pass / bootstrap fixpoint clean.

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

Details

Commit
13f741ed400b
Parent
aa800fe35c0b

spinel_codegen.rbmodifiedView file

@@ -20843,9 +20843,26 @@
2084320843 emit(" if (" + recv_tmp + ".tag == SP_TAG_STR) " + tmp + " = " + sinc_rhs + ";")
2084420844 end
2084520845 emit(" if (" + recv_tmp + ".tag == SP_TAG_OBJ) {")
20846- # User-class dispatch
20846+ # User-class dispatch. Narrow the candidate set when the recv
20847+ # is an ivar whose observed types are entirely concrete obj_<C>
20848+ # entries — the runtime value can only be an instance of one
20849+ # of those classes, so dispatch arms for unrelated classes
20850+ # that happen to share the method name are unreachable.
20851+ # Without this filter, Issue #575 surfaces: a same-named method
20852+ # on a class never assigned to the ivar (e.g. an Unrelated#run
20853+ # with zero args returning int when the ivar only ever sees
20854+ # Worker#run(item) returning string) widens the dispatch
20855+ # result temp to sp_RbVal and trips downstream type-strict
20856+ # consumers (sp_file_write etc.). The return-type union in
20857+ # poly_dispatch_return_type already consults this set; the
20858+ # emit loop just needed the matching skip.
20859+ dispatch_narrow_set = poly_dispatch_narrow_class_set(nid)
2084720860 i = 0
2084820861 while i < @cls_names.length
20862+ if dispatch_narrow_set != "" && poly_dispatch_class_in_set(dispatch_narrow_set, i) == 0
20863+ i = i + 1
20864+ next
20865+ end
2084920866 cname = @cls_names[i]
2085020867 midx = cls_find_method_direct(i, mname)
2085120868 owner_idx = i