Commit 87fd5065

87fd506580c2Merged

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

Description

fix(analyze): force_encoding/encode/b on mutable_str returns string (#446)

#441's fix gated the force_encoding / encode / b return-type inference on `recv == "string"`, leaving the sp_String * (mutable_str) receiver case unfixed. Codegen for a mutable_str receiver dispatches the inner call against `rc + "->data"` (line 12010), so force_encoding's body returns `lv_out->data` — a const char *. The function signature still defaulted to mrb_int and tripped the C compile with int-from-pointer.

Add the mutable_str arm: return "string" too, matching the body's `->data` value type so the C signature reads `const char *` and the return expression fits.

Closes #446

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

Details

Commit
87fd506580c2
Parent
40b88f06a99e

spinel_analyze.rbmodifiedView file

@@ -3524,8 +3524,21 @@
35243524 # the shortest collision target; struct field accessors etc.
35253525 # commonly take that single-letter shape).
35263526 if mname == "force_encoding" || mname == "encode" || mname == "b"
3527- if recv >= 0 && infer_type(recv) == "string"
3528- return "string"
3527+ if recv >= 0
3528+ rt_fe = infer_type(recv)
3529+ if rt_fe == "string"
3530+ return "string"
3531+ end
3532+ # A mutable_str (sp_String *) recv goes through codegen's
3533+ # mutable-str arm which dispatches the inner call against
3534+ # `rc + "->data"`; force_encoding's body return is then the
3535+ # mutable's `->data` (const char *), not the wrapping struct
3536+ # pointer. The function-return signature should match — pick
3537+ # "string", not "mutable_str", so the C signature reads
3538+ # `const char *` and the body's `return lv_out->data` fits.
3539+ if rt_fe == "mutable_str"
3540+ return "string"
3541+ end
35293542 end
35303543 end
35313544 if mname == "include?"