Commit a8e1b764
a8e1b7641e91Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): force_encoding/encode/b returns string when recv is string (#441)
def f(s); s.force_encoding(enc); end had its function return
type inferred as mrb_int even though the codegen at line 13953
already lowers force_encoding as a receiver passthrough
(return rc). The analyze side's infer_call_type had no matching
arm for force_encoding / encode / b, so the call's static type
fell back to "int" and the function signature committed to
mrb_int. Body emitted return lv_s (const char *), tripping the
C compile with a return-type mismatch.
Add a mname == "force_encoding" || mname == "encode" || mname == "b" arm to infer_call_type returning "string". Gated on the
receiver's static type being "string" to avoid colliding with
user-defined methods of the same name (b is the shortest
collision target; struct field accessors etc. commonly take that
single-letter shape).
Closes #441
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- a8e1b7641e91
- Parent
- e1fc6f9a521d
spinel_analyze.rbmodifiedView file
if mname == "chomp" || mname == "chop" return "string" end # `force_encoding` / `encode` / `b` lower to a receiver # passthrough at codegen (line 13953); mirror the type # inference so methods like `def normalize(s); s.force_encoding("UTF-8"); end` # pick up the receiver's string type for the function return # signature. Gated on receiver type being string to avoid # colliding with user-defined methods of the same name (`b` is # the shortest collision target; struct field accessors etc. # commonly take that single-letter shape). if mname == "force_encoding" || mname == "encode" || mname == "b" if recv >= 0 && infer_type(recv) == "string" return "string" end end if mname == "include?" return "bool" endtest/str_force_encoding_return.rbaddedView file
Loading diff…
test/str_force_encoding_return.rb.expectedaddedView file
Loading diff…