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

@@ -3515,6 +3515,19 @@
35153515 if mname == "chomp" || mname == "chop"
35163516 return "string"
35173517 end
3518+ # `force_encoding` / `encode` / `b` lower to a receiver
3519+ # passthrough at codegen (line 13953); mirror the type
3520+ # inference so methods like `def normalize(s); s.force_encoding("UTF-8"); end`
3521+ # pick up the receiver's string type for the function return
3522+ # signature. Gated on receiver type being string to avoid
3523+ # colliding with user-defined methods of the same name (`b` is
3524+ # the shortest collision target; struct field accessors etc.
3525+ # commonly take that single-letter shape).
3526+ if mname == "force_encoding" || mname == "encode" || mname == "b"
3527+ if recv >= 0 && infer_type(recv) == "string"
3528+ return "string"
3529+ end
3530+ end
35183531 if mname == "include?"
35193532 return "bool"
35203533 end