Commit 83e4c0d0

83e4c0d075d1Merged

Sam Ruby <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified

Description

fix(analyze,codegen): emit Hash arms in poly-recv .fetch

#470. Polymorphic .fetch dispatch on an sp_RbVal receiver enumerated only user-class arms (def fetch) and missed every built-in Hash variant (StrIntHash / StrStrHash / StrPolyHash). At runtime a Hash receiver matched no arm and the result temp stayed at its nil default — silent miscompile, not a C compile failure.

Surfaced after #463 boxed lv_raw_sub to sp_RbVal in real-blog: the downstream sub.fetch "title", "" site's emitted dispatch contained only ActionDispatch::Flash#fetch / ActionDispatch::Session#fetch arms (both sym-keyed), so the cls_id never matched the actual StrStrHash receiver and the mismatched parameter types cascaded to 16 C compile errors.

Sibling to 4859894 (#456), which added Hash arms to poly-recv [] / []= when the key is statically a String. Two parts:

  • analyze: fetch on a poly recv now widens to poly, matching the [] poly-recv widening. Without this the result temp's static C type comes from the first user-class fetch return (e.g. const char *) and the int-valued Hash arm's sp_box_int(...) rhs is a C type mismatch.

  • codegen: emit_poly_builtin_dispatch gains a fetch block parallel to the [] Str-Hash arms. Each arm tests _has_key first, returns the boxed value on hit, or the boxed default on miss. Sym-keyed variants are deferred for the same @needs-gating reason as [].

Closes #470.

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

Details

Commit
83e4c0d075d1
Parent
1c3e51e42296

spinel_analyze.rbmodifiedView file

@@ -3709,6 +3709,19 @@
37093709 if rt == "str_str_hash" || rt == "sym_str_hash" || rt == "int_str_hash"
37103710 return "string"
37113711 end
3712+ # Poly recv: runtime can be any user class (with `def fetch`)
3713+ # OR any built-in Hash variant. The codegen-side dispatch
3714+ # (`compile_poly_method_call` + `emit_poly_builtin_dispatch`)
3715+ # boxes each per-cls_id arm to sp_RbVal because the value
3716+ # types diverge across arms. Surface that here so the caller's
3717+ # slot widens accordingly — without this, `lookup` keeps a
3718+ # `const char *` return slot and the boxed dispatch result is
3719+ # a C type mismatch. Sibling to the `[]` poly-recv widening
3720+ # at line 4520 above.
3721+ if rt == "poly"
3722+ @needs_rb_value = 1
3723+ return "poly"
3724+ end
37123725 end
37133726 # Don't claim "int" for fetch on receivers we don't recognize
37143727 # as a built-in collection — let later dispatch resolve a