Commit 7ec2e96f
7ec2e96f5700Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): cmeth return inference uses multi-pass local refinement (#411)
A class method whose body initialized a local to nil and then
reassigned it through an intermediate variable emitted a function
return type of mrb_int even though the local itself was
correctly typed sp_Foo *:
static mrb_int sp_Bar_cls_find(void) { sp_Foo * lv_result = NULL; sp_Foo * lv_instance = NULL; lv_result = NULL; lv_instance = sp_Foo_new(); lv_result = lv_instance; return lv_result; // Foo* -> mrb_int — fails }
Root cause: infer_all_returns's cmeth branch ran a single
scan_locals pass before calling infer_body_return. The first
pass typed result as "nil" (only the initial result = nil
was visible), so the tail expression result inferred as nil
and unify_return_type collapsed the return to "nil" ->
mrb_int. The local-decl precompute later upgraded the local
correctly via refine_method_body_locals's 2-pass logic
("nil + nullable_pointer_type -> obj_?"), but by that point
@cls_cmeth_returns was pinned at "nil".
Fix: replace the single-pass scan_locals in
infer_all_returns's cmeth branch with the existing
refine_method_body_locals helper. Same multi-pass logic
precompute_all_scope_decls already runs end-of-analyze;
hoisting it into the iterative loop gets the cmeth's return
type aligned with what the local-decl pass eventually emits.
The instance-method branch uses an inline 2-pass refinement and the top-level-method branch uses the same shape, but only for the "int|nil -> concrete" upgrade (no nullable promotion). Those worked for the issue's repro because the test was a class method; the cmeth branch was the only one missing both passes. A future cleanup could route all three branches through refine_method_body_locals for consistency, but I'm keeping the change surgical -- the issue is specifically about cmeth, the other two paths are already passing.
Closes #411
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 7ec2e96f5700
- Parent
- 78b2bcf41002
spinel_analyze.rbmodifiedView file
cpk = cpk + 1 end if bid >= 0 # Issue #411: route through refine_method_body_locals so a # `result = nil; ...; result = obj` shape upgrades the # local from "nil" to `obj_<C>?` before infer_body_return # reads the tail expression. Without this, the cmeth's # return type stays "nil" -> `mrb_int` even though the # actually-emitted local is correctly typed `sp_<C> *`, # so the function signature and the body's `return` # statement disagree at the C boundary. ml = "".split(",") mt = "".split(",") scan_locals(bid, ml, mt, cpnames) lk = 0 while lk < ml.length declare_var(ml[lk], mt[lk]) lk = lk + 1 end refine_method_body_locals(bid, ml, mt, cpnames) end rt = infer_body_return(bid) if j < cm_returns.lengthtest/cmeth_return_nil_or_obj_via_intermediate.rbaddedView file
Loading diff…
test/cmeth_return_nil_or_obj_via_intermediate.rb.expectedaddedView file
Loading diff…