Commit 3e3066f1

3e3066f1c821Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(analyze): defer promote-mode int->bigint widening until after refinement passes

refine_method_body_locals promoted every int body local to bigint after pass 1 — before passes 2-3 had a chance to see the declared types of sibling locals. A local like c = a + b (where a / b are strings declared in the same body) got pass-1 type int (because a / b weren't in scope yet), got force-promoted to bigint, and then pass 2's correct string inference was dropped by merge_refined_local_type (which has no rule for downgrading bigint).

The result was C output like lv_c = sp_bigint_new_int(sp_str_concat (lv_a, lv_b)); — passing a const char * into sp_bigint_new_int, which -Werror rejected.

Mirror refine_locals_multi_pass_full's order: do the int->bigint promotion only after all five refinement passes have stabilized the real types. Locals that survive to the end as int are the ones the user actually meant as integers; promote those.

The return-coercion fix in the prior commit already handles the acc = 1; acc *= n; ...; return acc shape that motivated the early-promote — acc stays int through the body but the return site wraps via sp_bigint_new_int.

Promote-mode test count: 404 pass / 35 fail / 179 error -> 409 pass / 35 fail / 174 error. Default raise mode unchanged at 618 / 0 / 0.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
3e3066f1c821
Parent
71f9a20297b5

spinel_analyze.rbmodifiedView file

@@ -27558,24 +27558,6 @@
2755827558 # codegen would otherwise compute itself.
2755927559 def refine_method_body_locals(bid, lnames, ltypes, params)
2756027560 scan_locals(bid, lnames, ltypes, params)
27561- # `--int-overflow=promote`: every int body local becomes bigint
27562- # before the refinement passes see it. Mirrors the same step in
27563- # refine_locals_multi_pass_full but applied to method bodies
27564- # (where the caller passes the lnames / ltypes through this
27565- # path instead of the multi-pass full one). Without this,
27566- # `def f(n); acc = 1; while ...; acc *= n; ...; acc; end` had
27567- # the body local `acc` stuck at int and the C compile failed
27568- # to return mrb_int from a sp_Bigint *-returning function.
27569- if @int_overflow_mode == "promote"
27570- pj = 0
27571- while pj < lnames.length
27572- if ltypes[pj] == "int"
27573- ltypes[pj] = "bigint"
27574- @needs_bigint = 1
27575- end
27576- pj = pj + 1
27577- end
27578- end
2757927561 j = 0
2758027562 while j < lnames.length
2758127563 declare_var(lnames[j], ltypes[j])
@@ -27699,6 +27681,23 @@
2769927681 j = j + 1
2770027682 end
2770127683 @current_lexical_scope = saved_scope_p5
27684+ # `--int-overflow=promote`: after all refinement passes have
27685+ # settled the real types, widen the still-int body locals to
27686+ # bigint. Placed at the end (mirroring refine_locals_multi_pass_
27687+ # full) so a local like `c = a + b` where a/b are strings stays
27688+ # "string" instead of being clobbered to bigint on pass 1 before
27689+ # the string types of a/b were visible in scope.
27690+ if @int_overflow_mode == "promote"
27691+ pj = 0
27692+ while pj < lnames.length
27693+ if ltypes[pj] == "int"
27694+ ltypes[pj] = "bigint"
27695+ set_var_type(lnames[pj], "bigint")
27696+ @needs_bigint = 1
27697+ end
27698+ pj = pj + 1
27699+ end
27700+ end
2770227701 end
2770327702
2770427703 # Constant-initializer locals — block params introduced inside