Commit cadd330e
cadd330ea4b9Merged
Sam Ruby <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): suppress poly-recv user-class arms with incompatible param types
After the parent commit (697ad2d) added Hash arms to poly-recv .fetch
dispatch, real-blog still failed C compile at 8 sites: the user-class
arms (ActionDispatch::Flash#fetch, Session#fetch) emitted call-site
String args into mrb_int (sym) slots — Flash/Session#fetch's key
param is committed to mrb_int by sym-only direct callers elsewhere,
and the dispatch site's String "title" can't be cast.
At runtime in any source-level-valid path the receiver is a Hash
(here narrowed by sub = raw_sub if raw_sub.is_a?(Hash) else {}),
never Flash/Session, so the arm is unreachable. Suppress it instead
of emitting incompatible C.
Parallels the existing narrowed_int_idx skip in the same function:
both kill user-class arms that would emit unsound C for a runtime
path that doesn't exist. int↔symbol and int↔bool stay compatible
(both lower to mrb_int).
Real-blog archive: errors 16 → 0 after this commit. blog binary now compiles and links end-to-end. Tests 436 pass / 0 fail (added one regression for the suppression rule, alongside the parent commit's Hash-arm test).
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- cadd330ea4b9
- Parent
- 83e4c0d075d1
spinel_codegen.rbmodifiedView file
this_rt = cls_method_return(owner_idx, mname) rhs = box_val_to_poly(call_expr, this_rt) end # Suppress user-class arms whose param-slot types are # incompatible with the dispatch-site arg types — when the # box/unbox logic above can't bridge them. E.g. `sub.fetch( # "title", "")` dispatched at a site where `sub` may be a # FlashLike whose `fetch(key, default)` has `key: mrb_int` # (committed by sym-only direct callers) — the String "title" # can't be cast to `mrb_int` without a runtime sym lookup, and # the receiver in any source-level-valid path is a Hash, never # FlashLike, so the arm is unreachable at runtime. Parallels # the `narrowed_int_idx` skip below. arm_incompat = 0 pk_chk = 0 while pk_chk < arm_ptypes.length && pk_chk < arg_compiled.length && pk_chk < arg_types.length at_b = base_type(arg_types[pk_chk]) pt_b = base_type(arm_ptypes[pk_chk]) if at_b != "" && pt_b != "" && at_b != "poly" && pt_b != "poly" && at_b != pt_b # int/symbol/bool all lower to mrb_int — compatible. 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 arm_incompat = 1 end end pk_chk = pk_chk + 1 end # Narrowed `[]` arms (Approach 2): suppress user-class arms # whose `[]` doesn't return int — the dead-code emit would # otherwise assign e.g. a string to the now-mrb_int result # temp and fail the C compile. The runtime can't reach these # arms anyway because the observation set narrowed past them. if narrowed_int_idx == 1 && cls_method_return(owner_idx, mname) != "int" if arm_incompat == 1 # skip — runtime can't reach this arm in a source-level-valid # path because the dispatch-site arg types preclude the user # class as the actual receiver. elsif narrowed_int_idx == 1 && cls_method_return(owner_idx, mname) != "int" # skip else emit(" if (" + recv_tmp + ".cls_id == " + cls_id_for_user_internal(i).to_s + ") " + tmp + " = " + rhs + ";")test/poly_recv_fetch_arm_incompat.rbaddedView file
Loading diff…
test/poly_recv_fetch_arm_incompat.rb.expectedaddedView file
Loading diff…