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:
-
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.
-
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
end # Scan every user class that defines this method. If they all # agree on the return type, the call has that concrete type. # If they disagree, the call is genuinely polymorphic. return poly_dispatch_return_type(mname) # If they disagree, the call is genuinely polymorphic. The # narrow set + arg_types parameters let the scan skip arms # that the dispatch site source-level can't reach (mirrors # codegen-side narrow). Issue #549. args_id_pdrt = @nd_arguments[nid] arg_types_pdrt = "".split(",") if args_id_pdrt >= 0 a_ids_pdrt = get_args(args_id_pdrt) kp_pdrt = 0 while kp_pdrt < a_ids_pdrt.length arg_types_pdrt.push(infer_type(a_ids_pdrt[kp_pdrt])) kp_pdrt = kp_pdrt + 1 end end return poly_dispatch_return_type(mname, nid, arg_types_pdrt) end # Method call on int (possible IntArray element storing object pointers) # when recv is a LocalVariableReadNode whose out end def poly_dispatch_return_type(mname) def poly_dispatch_return_type(mname, nid = -1, arg_types = nil) if arg_types == nil arg_types = "".split(",") end if mname == "[]" @needs_rb_value = 1 return "poly" @needs_rb_value = 1 return "poly" end # Source-level narrow set from ivar observations: when the # receiver reads from an ivar whose observed type set is all # obj_X (no poly / no primitives), the dispatch can only ever # land on those classes. Issue #549. Mirrors the codegen-side # narrow in compile_poly_method_call so both sides agree on # the return type — otherwise the analyze-side stays poly and # the codegen-side narrows the temp, producing an # sp_RbVal-vs-scalar mismatch on the wrapping return path. narrow_set = poly_dispatch_narrow_class_set(nid) # Setters: mname ends with "=" and at least one class has an # attr_writer for the bare name. Return type is the ivar type # (Ruby returns the rhs from `x = v`); without this, the result common = "" ci = 0 while ci < @cls_names.length rt = "" if cls_find_method_direct(ci, mname) >= 0 rt = cls_method_return(ci, mname) elsif cls_has_attr_reader(ci, mname) == 1 if narrow_set != "" && poly_dispatch_class_in_set(narrow_set, ci) == 0 ci = ci + 1 else rt = "" if cls_find_method_direct(ci, mname) >= 0 # Skip arms whose param types can't accept the dispatch # site's arg types -- mirrors the arm_incompat check in # the emit loop. Issue #549. if poly_dispatch_arm_param_compat(ci, mname, arg_types) == 0 rt = "" else rt = cls_method_return(ci, mname) end elsif cls_has_attr_reader(ci, mname) == 1 # An attr_reader returns the ivar type. . rt = cls_ivar_type(ci, "@" + mname) elsif setter_bname != "" && cls_has_attr_writer(ci, setter_bname) == 1 rt = cls_ivar_type(ci, "@" + mname) elsif setter_bname != "" && cls_has_attr_writer(ci, setter_bname) == 1 # An attr_writer setter returns the ivar's type. rt = cls_ivar_type(ci, "@" + setter_bname) rt = cls_ivar_type(ci, "@" + setter_bname) end if rt != "" if common == "" common = rt elsif common != rt return "poly" end end ci = ci + 1 end end common == "" ? "int" : common end # Returns the narrow class-index set for a poly-recv dispatch # site. Mirrors the codegen-side. Issue #549. def poly_dispatch_narrow_class_set(nid) if nid < 0 return "" end if @current_class_idx < 0 return "" end recv_id = @nd_receiver[nid] if recv_id < 0 return "" end if @nd_type[recv_id] != "InstanceVariableReadNode" return "" end iname = @nd_name[recv_id] obs = cls_ivar_observed_types_for(@current_class_idx, iname) if obs == "" return "" end out = "" parts = obs.split(",") k = 0 while k < parts.length t = parts[k] if t == "" # blank slot - uninformative elsif is_obj_type(t) == 1 cname = t[4, t.length - 4] cidx = find_class_idx(cname) if cidx >= 0 s_idx = cidx.to_s if out == "" out = s_idx else already = 0 seen = out.split(",") sk = 0 while sk < seen.length if seen[sk] == s_idx already = 1 end sk = sk + 1 end if already == 0 out = out + "," + s_idx end end end else # Any non-obj observation poisons the narrow set. return "" end k = k + 1 end out end def poly_dispatch_class_in_set(set, ci) if set == "" return 1 end target = ci.to_s parts = set.split(",") k = 0 while k < parts.length if parts[k] == target return 1 end k = k + 1 end 0 end def poly_dispatch_arm_param_compat(ci, mname, arg_types) midx = cls_find_method_direct(ci, mname) owner_idx = ci if midx < 0 owner_name = find_method_owner(ci, mname) if owner_name != "" owner_idx = find_class_idx(owner_name) if owner_idx >= 0 midx = cls_find_method_direct(owner_idx, mname) end end if rt != "" if common == "" common = rt elsif common != rt return "poly" end if midx < 0 return 1 end arm_ptypes = cls_meth_ptypes_get(owner_idx, midx) pk = 0 while pk < arm_ptypes.length && pk < arg_types.length at_b = base_type(arg_types[pk]) pt_b = base_type(arm_ptypes[pk]) if at_b != "" && pt_b != "" && at_b != "poly" && pt_b != "poly" && at_b != pt_b if (at_b == "int" && pt_b == "symbol") || (at_b == "symbol" && pt_b == "int") || (at_b == "int" && pt_b == "bool") || (at_b == "bool" && pt_b == "int") # compatible else return 0 end end ci = ci + 1 pk = pk + 1 end common == "" ? "int" : common 1 end # Runtime tag-check for `<poly>.is_a?(<klass>)` / `kind_of?` /spinel_codegen.rbmodifiedView file
Loading diff…
test/poly_dispatch_narrow_return_type.rbaddedView file
Loading diff…
test/poly_dispatch_narrow_return_type.rb.expectedaddedView file
Loading diff…