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
# codegen would otherwise compute itself. def refine_method_body_locals(bid, lnames, ltypes, params) scan_locals(bid, lnames, ltypes, params) # `--int-overflow=promote`: every int body local becomes bigint # before the refinement passes see it. Mirrors the same step in # refine_locals_multi_pass_full but applied to method bodies # (where the caller passes the lnames / ltypes through this # path instead of the multi-pass full one). Without this, # `def f(n); acc = 1; while ...; acc *= n; ...; acc; end` had # the body local `acc` stuck at int and the C compile failed # to return mrb_int from a sp_Bigint *-returning function. if @int_overflow_mode == "promote" pj = 0 while pj < lnames.length if ltypes[pj] == "int" ltypes[pj] = "bigint" @needs_bigint = 1 end pj = pj + 1 end end j = 0 while j < lnames.length declare_var(lnames[j], ltypes[j]) j = j + 1 end @current_lexical_scope = saved_scope_p5 # `--int-overflow=promote`: after all refinement passes have # settled the real types, widen the still-int body locals to # bigint. Placed at the end (mirroring refine_locals_multi_pass_ # full) so a local like `c = a + b` where a/b are strings stays # "string" instead of being clobbered to bigint on pass 1 before # the string types of a/b were visible in scope. if @int_overflow_mode == "promote" pj = 0 while pj < lnames.length if ltypes[pj] == "int" ltypes[pj] = "bigint" set_var_type(lnames[pj], "bigint") @needs_bigint = 1 end pj = pj + 1 end end end # Constant-initializer locals — block params introduced inside