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
# Float; zero-arg / Integer#ceil etc. return Integer. (truncate's arm # used to live next to nan?/infinite? — folded in here for one place # to update.) # # Issue #314: gate on a Float receiver. `ViewHelpers.truncate(s, # length: 100)` is a user-defined module method that returns a # string; without this gate it matched here purely by name and # returned "float", so the call site boxed truncate's `const char *` # return as `sp_box_float(...)` — invalid C. # # The gate is `rt == "float"` exclusively, not "int": a module # ConstantReadNode's infer_type falls back to "int" by default, # and we don't want module method calls to slide through. Integer # ceil/floor/round/truncate with an arg are rare in practice — if # the codebase needs them later, a future pass on call-site type # narrowing can claim it more carefully. if mname == "ceil" || mname == "floor" || mname == "round" || mname == "truncate" if @nd_arguments[nid] >= 0 return "float" if recv >= 0 && infer_type(recv) == "float" if @nd_arguments[nid] >= 0 return "float" end return "int" end return "int" end if mname == "upcase" || mname == "downcase" if recv >= 0 && infer_type(recv) == "symbol"test/truncate_module_method.rbaddedView file
Loading diff…
test/truncate_module_method.rb.expectedaddedView file
Loading diff…