Commit 8ff9aa91

8ff9aa915a30Merged

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

Description

fix(codegen): #314 module-method truncate(...) infers as user-defined return, not Float

infer_method_name_type had a name-only rule for ceil/floor/round/ truncate that returned float (with arg) or int (without) regardless of the receiver. A user-defined module method like ViewHelpers.truncate(s, length: 100) matched purely by the name and got typed as float, so the call site boxed truncate's actual const-char-* return through sp_box_float — invalid C, blocked all view rendering in Roundhouse-emitted real-blog.

Gate the rule on infer_type(recv) == "float". Float#truncate(2) keeps its float typing; module/class truncate methods fall through to the user-method-return lookup that picks up the actually declared return type. Restricting to "float" rather than also "int" is deliberate — a module ConstantReadNode's infer_type falls back to "int" by default, so allowing "int" would let module truncate slide right back through.

Surfaced via the Roundhouse archive (issue #314 setup) — minimal repro extracted from app/views/articles/_article.rb's html_escape(truncate(article.body, length: 100)) line.

Fixes #314

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

Details

Commit
8ff9aa915a30
Parent
2b02f7b1a841

spinel_codegen.rbmodifiedView file

@@ -3001,11 +3001,26 @@
30013001 # Float; zero-arg / Integer#ceil etc. return Integer. (truncate's arm
30023002 # used to live next to nan?/infinite? — folded in here for one place
30033003 # to update.)
3004+ #
3005+ # Issue #314: gate on a Float receiver. `ViewHelpers.truncate(s,
3006+ # length: 100)` is a user-defined module method that returns a
3007+ # string; without this gate it matched here purely by name and
3008+ # returned "float", so the call site boxed truncate's `const char *`
3009+ # return as `sp_box_float(...)` — invalid C.
3010+ #
3011+ # The gate is `rt == "float"` exclusively, not "int": a module
3012+ # ConstantReadNode's infer_type falls back to "int" by default,
3013+ # and we don't want module method calls to slide through. Integer
3014+ # ceil/floor/round/truncate with an arg are rare in practice — if
3015+ # the codebase needs them later, a future pass on call-site type
3016+ # narrowing can claim it more carefully.
30043017 if mname == "ceil" || mname == "floor" || mname == "round" || mname == "truncate"
3005- if @nd_arguments[nid] >= 0
3006- return "float"
3018+ if recv >= 0 && infer_type(recv) == "float"
3019+ if @nd_arguments[nid] >= 0
3020+ return "float"
3021+ end
3022+ return "int"
30073023 end
3008- return "int"
30093024 end
30103025 if mname == "upcase" || mname == "downcase"
30113026 if recv >= 0 && infer_type(recv) == "symbol"