Commit d5413c7f

d5413c7f577dMerged

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

Description

fix(analyze,codegen): narrow poly-recv dispatch return type to scalar when reachable arms agree

Followup to #531. The cls_id-switch dispatch's result temp widened to sp_RbVal even when every reachable arm returned the same scalar. poly_dispatch_return_type iterated all user classes that define the method and unioned their return types, but it did not consult the same arm-suppression logic the emit loop uses; unreachable arms whose return types diverged from the rest still poisoned the union.

The test fixture for #531 (test/poly_recv_dispatch_narrow_module.rb) already exercised this: M::Server#run takes int and returns int, while the Worker family takes string and returns string. At the @w.run(item) dispatch site, the emit loop pruned Server's arm via the arm_incompat check (its int param can't accept the string arg), so only Worker arms were emitted. But the result temp was sp_RbVal because Server's int return still entered the union.

This change adds two filters to poly_dispatch_return_type, applied symmetrically in analyze and codegen so both sides agree on the dispatch site's return type:

  1. Param-incompat suppression. Skip arms whose param types cannot accept the dispatch site's arg base types. Mirrors the arm_incompat loop in compile_poly_method_call.

  2. Observed-class narrow set. When the receiver reads an ivar whose observed type set is all obj_X (no poly, no primitives), restrict the candidate classes to that set. This handles unrelated classes whose methods happen to share the dispatched name but can never appear at the receiver.

Both filters require the call site to pass nid and the arg base types into poly_dispatch_return_type; the existing callers are updated.

Known gap: codegen's observed-class narrow uses @cls_ivar_observed_types directly, which equals "poly" when the ivar is written from a poly-typed param even though analyze's source walk (recover_concrete_classes_for_ivar) recovers a concrete class set. So a dispatch site whose observation list is "poly" still admits unrelated classes through. Porting the source-walk path into codegen is a separate change.

test/poly_dispatch_narrow_return_type.rb is a new fixture that discriminates the bug via a scalar-typed consumer of the dispatch result (s + "."); the existing #531 fixture only asserted stdout shape, which the pre-fix poly-boxed form also produced.

Closes #549

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

Details

Commit
d5413c7f577d
Parent
10da3e9a08d1

spinel_analyze.rbmodifiedView file

@@ -5379,8 +5379,21 @@
53795379 end
53805380 # Scan every user class that defines this method. If they all
53815381 # agree on the return type, the call has that concrete type.
5382- # If they disagree, the call is genuinely polymorphic.
5383- return poly_dispatch_return_type(mname)
5382+ # If they disagree, the call is genuinely polymorphic. The
5383+ # narrow set + arg_types parameters let the scan skip arms
5384+ # that the dispatch site source-level can't reach (mirrors
5385+ # codegen-side narrow). Issue #549.
5386+ args_id_pdrt = @nd_arguments[nid]
5387+ arg_types_pdrt = "".split(",")
5388+ if args_id_pdrt >= 0
5389+ a_ids_pdrt = get_args(args_id_pdrt)
5390+ kp_pdrt = 0
5391+ while kp_pdrt < a_ids_pdrt.length
5392+ arg_types_pdrt.push(infer_type(a_ids_pdrt[kp_pdrt]))
5393+ kp_pdrt = kp_pdrt + 1
5394+ end
5395+ end
5396+ return poly_dispatch_return_type(mname, nid, arg_types_pdrt)
53845397 end
53855398 # Method call on int (possible IntArray element storing object pointers)
53865399 # when recv is a LocalVariableReadNode whose
@@ -22641,7 +22654,10 @@
2264122654 out
2264222655 end
2264322656
22644- def poly_dispatch_return_type(mname)
22657+ def poly_dispatch_return_type(mname, nid = -1, arg_types = nil)
22658+ if arg_types == nil
22659+ arg_types = "".split(",")
22660+ end
2264522661 if mname == "[]"
2264622662 @needs_rb_value = 1
2264722663 return "poly"
@@ -22658,6 +22674,15 @@
2265822674 @needs_rb_value = 1
2265922675 return "poly"
2266022676 end
22677+ # Source-level narrow set from ivar observations: when the
22678+ # receiver reads from an ivar whose observed type set is all
22679+ # obj_X (no poly / no primitives), the dispatch can only ever
22680+ # land on those classes. Issue #549. Mirrors the codegen-side
22681+ # narrow in compile_poly_method_call so both sides agree on
22682+ # the return type — otherwise the analyze-side stays poly and
22683+ # the codegen-side narrows the temp, producing an
22684+ # sp_RbVal-vs-scalar mismatch on the wrapping return path.
22685+ narrow_set = poly_dispatch_narrow_class_set(nid)
2266122686 # Setters: mname ends with "=" and at least one class has an
2266222687 # attr_writer for the bare name. Return type is the ivar type
2266322688 # (Ruby returns the rhs from `x = v`); without this, the result
@@ -22670,26 +22695,145 @@
2267022695 common = ""
2267122696 ci = 0
2267222697 while ci < @cls_names.length
22673- rt = ""
22674- if cls_find_method_direct(ci, mname) >= 0
22675- rt = cls_method_return(ci, mname)
22676- elsif cls_has_attr_reader(ci, mname) == 1
22698+ if narrow_set != "" && poly_dispatch_class_in_set(narrow_set, ci) == 0
22699+ ci = ci + 1
22700+ else
22701+ rt = ""
22702+ if cls_find_method_direct(ci, mname) >= 0
22703+ # Skip arms whose param types can't accept the dispatch
22704+ # site's arg types -- mirrors the arm_incompat check in
22705+ # the emit loop. Issue #549.
22706+ if poly_dispatch_arm_param_compat(ci, mname, arg_types) == 0
22707+ rt = ""
22708+ else
22709+ rt = cls_method_return(ci, mname)
22710+ end
22711+ elsif cls_has_attr_reader(ci, mname) == 1
2267722712 # An attr_reader returns the ivar type. .
22678- rt = cls_ivar_type(ci, "@" + mname)
22679- elsif setter_bname != "" && cls_has_attr_writer(ci, setter_bname) == 1
22713+ rt = cls_ivar_type(ci, "@" + mname)
22714+ elsif setter_bname != "" && cls_has_attr_writer(ci, setter_bname) == 1
2268022715 # An attr_writer setter returns the ivar's type.
22681- rt = cls_ivar_type(ci, "@" + setter_bname)
22716+ rt = cls_ivar_type(ci, "@" + setter_bname)
22717+ end
22718+ if rt != ""
22719+ if common == ""
22720+ common = rt
22721+ elsif common != rt
22722+ return "poly"
22723+ end
22724+ end
22725+ ci = ci + 1
22726+ end
22727+ end
22728+ common == "" ? "int" : common
22729+ end
22730+
22731+ # Returns the narrow class-index set for a poly-recv dispatch
22732+ # site. Mirrors the codegen-side. Issue #549.
22733+ def poly_dispatch_narrow_class_set(nid)
22734+ if nid < 0
22735+ return ""
22736+ end
22737+ if @current_class_idx < 0
22738+ return ""
22739+ end
22740+ recv_id = @nd_receiver[nid]
22741+ if recv_id < 0
22742+ return ""
22743+ end
22744+ if @nd_type[recv_id] != "InstanceVariableReadNode"
22745+ return ""
22746+ end
22747+ iname = @nd_name[recv_id]
22748+ obs = cls_ivar_observed_types_for(@current_class_idx, iname)
22749+ if obs == ""
22750+ return ""
22751+ end
22752+ out = ""
22753+ parts = obs.split(",")
22754+ k = 0
22755+ while k < parts.length
22756+ t = parts[k]
22757+ if t == ""
22758+ # blank slot - uninformative
22759+ elsif is_obj_type(t) == 1
22760+ cname = t[4, t.length - 4]
22761+ cidx = find_class_idx(cname)
22762+ if cidx >= 0
22763+ s_idx = cidx.to_s
22764+ if out == ""
22765+ out = s_idx
22766+ else
22767+ already = 0
22768+ seen = out.split(",")
22769+ sk = 0
22770+ while sk < seen.length
22771+ if seen[sk] == s_idx
22772+ already = 1
22773+ end
22774+ sk = sk + 1
22775+ end
22776+ if already == 0
22777+ out = out + "," + s_idx
22778+ end
22779+ end
22780+ end
22781+ else
22782+ # Any non-obj observation poisons the narrow set.
22783+ return ""
22784+ end
22785+ k = k + 1
22786+ end
22787+ out
22788+ end
22789+
22790+ def poly_dispatch_class_in_set(set, ci)
22791+ if set == ""
22792+ return 1
22793+ end
22794+ target = ci.to_s
22795+ parts = set.split(",")
22796+ k = 0
22797+ while k < parts.length
22798+ if parts[k] == target
22799+ return 1
22800+ end
22801+ k = k + 1
22802+ end
22803+ 0
22804+ end
22805+
22806+ def poly_dispatch_arm_param_compat(ci, mname, arg_types)
22807+ midx = cls_find_method_direct(ci, mname)
22808+ owner_idx = ci
22809+ if midx < 0
22810+ owner_name = find_method_owner(ci, mname)
22811+ if owner_name != ""
22812+ owner_idx = find_class_idx(owner_name)
22813+ if owner_idx >= 0
22814+ midx = cls_find_method_direct(owner_idx, mname)
22815+ end
2268222816 end
22683- if rt != ""
22684- if common == ""
22685- common = rt
22686- elsif common != rt
22687- return "poly"
22817+ end
22818+ if midx < 0
22819+ return 1
22820+ end
22821+ arm_ptypes = cls_meth_ptypes_get(owner_idx, midx)
22822+ pk = 0
22823+ while pk < arm_ptypes.length && pk < arg_types.length
22824+ at_b = base_type(arg_types[pk])
22825+ pt_b = base_type(arm_ptypes[pk])
22826+ if at_b != "" && pt_b != "" && at_b != "poly" && pt_b != "poly" && at_b != pt_b
22827+ if (at_b == "int" && pt_b == "symbol") || (at_b == "symbol" && pt_b == "int") ||
22828+ (at_b == "int" && pt_b == "bool") || (at_b == "bool" && pt_b == "int")
22829+ # compatible
22830+ else
22831+ return 0
2268822832 end
2268922833 end
22690- ci = ci + 1
22834+ pk = pk + 1
2269122835 end
22692- common == "" ? "int" : common
22836+ 1
2269322837 end
2269422838
2269522839 # Runtime tag-check for `<poly>.is_a?(<klass>)` / `kind_of?` /