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

@@ -12633,14 +12633,17 @@
1263312633 cpk = cpk + 1
1263412634 end
1263512635 if bid >= 0
12636+ # Issue #411: route through refine_method_body_locals so a
12637+ # `result = nil; ...; result = obj` shape upgrades the
12638+ # local from "nil" to `obj_<C>?` before infer_body_return
12639+ # reads the tail expression. Without this, the cmeth's
12640+ # return type stays "nil" -> `mrb_int` even though the
12641+ # actually-emitted local is correctly typed `sp_<C> *`,
12642+ # so the function signature and the body's `return`
12643+ # statement disagree at the C boundary.
1263612644 ml = "".split(",")
1263712645 mt = "".split(",")
12638- scan_locals(bid, ml, mt, cpnames)
12639- lk = 0
12640- while lk < ml.length
12641- declare_var(ml[lk], mt[lk])
12642- lk = lk + 1
12643- end
12646+ refine_method_body_locals(bid, ml, mt, cpnames)
1264412647 end
1264512648 rt = infer_body_return(bid)
1264612649 if j < cm_returns.length