Commit 7188d67c

7188d67c0a06Merged

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

Description

fix(codegen): infer return type at module class-method call sites

A module-level singleton method (module M; def self.X; ...; end) whose body returned a non-int value was typed as mrb_int at every call site. Capturing to a local emitted mrb_int = const char * (a -Wint-conversion warning, hard error under -Werror); inline puts M.greet compiled but printed the pointer as an integer. The class form already worked because infer_constant_recv_type walks @cls_cmeth_returns for class methods, but module class methods live in the top-level @meth_* table as <Mod>_cls_<method> with no parallel lookup.

Adds the missing module branch alongside the class one, mirroring its shape (same != "" && != "int" filter so callers still fall through to name-based heuristics for genuinely-int returns).

The ENV side-finding flagged in #127's "possibly-related" section (ENV["USE_MEM"] == "1" always false) is independent of this fix and still reproduces; will spin off as its own issue.

Closes #127

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

Details

Commit
7188d67c0a06
Parent
136bb797f367

spinel_codegen.rbmodifiedView file

@@ -3054,6 +3054,21 @@
30543054 cj = cj + 1
30553055 end
30563056 end
3057+ # Issue #127: same lookup for module class methods. They live in
3058+ # the top-level @meth_* table as `<Mod>_cls_<method>`, not in
3059+ # @cls_cmeth_* (which is class-only). Without this branch every
3060+ # `Module.cls_method` call inferred as int regardless of return
3061+ # type, so `s = M.greet` declared `lv_s` as mrb_int even though
3062+ # `sp_M_cls_greet()` returns `const char *`.
3063+ if module_name_exists(rcname) == 1
3064+ mfi = find_method_idx(rcname + "_cls_" + mname)
3065+ if mfi >= 0 && mfi < @meth_return_types.length
3066+ mrt = @meth_return_types[mfi]
3067+ if mrt != "" && mrt != "int"
3068+ return mrt
3069+ end
3070+ end
3071+ end
30573072 end
30583073 end
30593074 # StringIO methods