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

@@ -18392,12 +18392,42 @@
1839218392 this_rt = cls_method_return(owner_idx, mname)
1839318393 rhs = box_val_to_poly(call_expr, this_rt)
1839418394 end
18395+ # Suppress user-class arms whose param-slot types are
18396+ # incompatible with the dispatch-site arg types — when the
18397+ # box/unbox logic above can't bridge them. E.g. `sub.fetch(
18398+ # "title", "")` dispatched at a site where `sub` may be a
18399+ # FlashLike whose `fetch(key, default)` has `key: mrb_int`
18400+ # (committed by sym-only direct callers) — the String "title"
18401+ # can't be cast to `mrb_int` without a runtime sym lookup, and
18402+ # the receiver in any source-level-valid path is a Hash, never
18403+ # FlashLike, so the arm is unreachable at runtime. Parallels
18404+ # the `narrowed_int_idx` skip below.
18405+ arm_incompat = 0
18406+ pk_chk = 0
18407+ while pk_chk < arm_ptypes.length && pk_chk < arg_compiled.length && pk_chk < arg_types.length
18408+ at_b = base_type(arg_types[pk_chk])
18409+ pt_b = base_type(arm_ptypes[pk_chk])
18410+ if at_b != "" && pt_b != "" && at_b != "poly" && pt_b != "poly" && at_b != pt_b
18411+ # int/symbol/bool all lower to mrb_int — compatible.
18412+ if (at_b == "int" && pt_b == "symbol") || (at_b == "symbol" && pt_b == "int") ||
18413+ (at_b == "int" && pt_b == "bool") || (at_b == "bool" && pt_b == "int")
18414+ # compatible
18415+ else
18416+ arm_incompat = 1
18417+ end
18418+ end
18419+ pk_chk = pk_chk + 1
18420+ end
1839518421 # Narrowed `[]` arms (Approach 2): suppress user-class arms
1839618422 # whose `[]` doesn't return int — the dead-code emit would
1839718423 # otherwise assign e.g. a string to the now-mrb_int result
1839818424 # temp and fail the C compile. The runtime can't reach these
1839918425 # arms anyway because the observation set narrowed past them.
18400- if narrowed_int_idx == 1 && cls_method_return(owner_idx, mname) != "int"
18426+ if arm_incompat == 1
18427+ # skip — runtime can't reach this arm in a source-level-valid
18428+ # path because the dispatch-site arg types preclude the user
18429+ # class as the actual receiver.
18430+ elsif narrowed_int_idx == 1 && cls_method_return(owner_idx, mname) != "int"
1840118431 # skip
1840218432 else
1840318433 emit(" if (" + recv_tmp + ".cls_id == " + cls_id_for_user_internal(i).to_s + ") " + tmp + " = " + rhs + ";")