Commit 087d01fd

087d01fd3171Merged

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

Description

feat(codegen): warn on missing required positional arg at call site

A call site that omits a positional arg with no default was silently filled with 0 (or sp_box_nil() for poly slots) by compile_call_args_with_defaults. The binary then ran with garbage in the missing slot — the reporter for #642 hit this when tep grew a 4th scheduled param and every existing 3-arg caller miscompiled into a server that returned a fixed 6-byte body for every route.

Add warn_missing_positional_arg: emits a one-shot per (callee, missing-slot) stderr warning. Dedupe stash piggybacks on @unresolved_call_warnings so 50 call sites with the same omission surface once.

Kept the 0 fallback so codegen still completes — a hard error would tear up partially-implemented patterns. Matz's call on whether to escalate to a runtime ArgumentError later.

Closes #642.

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

Details

Commit
087d01fd3171
Parent
133fb24a0a2c

BIGINT.mdaddedView file

1+# Bigint promotion: detection + escape hatch
2+
3+Working notes (not for commit; .gitignore'd via /tmp-style scratch).
4+
5+## The undecidable core
6+
7+"Does this Integer ever exceed 2^63?" reduces to value-range analysis,
8+which is undecidable in general. No static pass will ever close this
9+soundly for arbitrary Ruby. We need to accept that and design around it.
10+
11+## Spectrum of strategies
12+
13+| Strategy | Cost | Coverage | Notes |
14+|---|---|---|---|
15+| (1) Always bigint | ~10x slowdown for all int math | 100% | CRuby's choice. Negates spinel's "fast AOT" pitch. |
16+| (2) Pattern-based auto-detect (current) | None when not detected | Specific shapes only | `x *= y` in loops, fib-style accumulators. Lots of false negatives. |
17+| (3) Type-annotated (RBS / explicit) | None when not used | Whatever user marks | Requires user action. Most precise. |
18+| (4) Boundary promotion (tagged union) | Per-op overflow check + dispatch | 100% | Same cost shape as (1) but lazier promotion. Still costly per arithmetic op. |
19+| (5) Profile-guided | Toolchain complexity | High with good corpus | Overkill for spinel's size. |
20+
21+Spinel sits between (2) and (3) today: pattern detection covers the
22+canonical fibonacci / factorial-in-loop shapes; everything else falls
23+through to mrb_int and silently overflows.
24+
25+## User's CLI-flag proposal
26+
27+> Pattern detection by default; for undetectable cases, a CLI flag
28+> ("this program uses bigint, every integer op is overflow-checked
29+> and slower").
30+
31+### Strengths
32+
33+- Simple mental model: auto when possible, flag when not.
34+- Zero source changes for the safety-net path.
35+- Clear responsibility split: best-effort detector + user-controlled
36+ fallback for the cover gaps.
37+
38+### Concerns
39+
40+1. **Whole-program granularity is too coarse.** Real programs commonly
41+ want bigint for one method while keeping the hot loop on mrb_int.
42+ NES emulator with a SHA-256 checksum: the per-op overflow check
43+ would tank the emulator path. Single flag forces an
44+ all-or-nothing choice that erodes spinel's main selling point.
45+
46+2. **Implementation needs tagged-int representation for every local.**
47+ Today spinel emits `mrb_int lv_x = 5;` and arithmetic compiles to
48+ single native ops. Adding overflow-check + auto-promote means each
49+ int local has to be able to hold either a small int OR an
50+ sp_bigint pointer — i.e. tagged at the C level (sp_RbVal-shaped).
51+ Each arithmetic op becomes:
52+ ```c
53+ if (lv_x.tag==SMALL && lv_y.tag==SMALL) {
54+ if (!__builtin_mul_overflow(lv_x.v.i, lv_y.v.i, &r)) {
55+ lv_z = small(r);
56+ } else {
57+ lv_z = bigint(sp_bigint_mul_si(lv_x.v.i, lv_y.v.i));
58+ }
59+ } else {
60+ lv_z = bigint(sp_bigint_mul_poly(...));
61+ }
62+ ```
63+ This is the CRuby Fixnum/Bignum shape. Spinel's "AOT to native int"
64+ advantage gets traded for CRuby-like runtime overhead.
65+
66+3. **Two communities, one flag.** The "I want speed even if I have to
67+ write annotations" user and the "I want correctness no matter how
68+ slow" user have opposite preferences. A single boolean doesn't serve
69+ either well; it serves both badly.
70+
71+## Better granularity
72+
73+Listing escape hatches finest-to-coarsest:
74+
75+### A. RBS-typed bigint return / param
76+```rbs
77+def factorial: (Integer n) -> BigInt
78+```
79+The function's return slot and any local assigned from it are bigint.
80+Surrounding code stays mrb_int. Best granularity for cost-conscious
81+users. Reuses the existing RBS plumbing.
82+
83+### B. Source-level `BigInt(...)` cast
84+```ruby
85+n = BigInt(gets.to_i)
86+result = (1..n).inject(:*)
87+```
88+`BigInt(expr)` is an explicit type-conversion point. From that LV
89+onward arithmetic dispatches to sp_bigint. Smallest surface change in
90+the compiler (just another constructor).
91+
92+### C. `--strict-bigint-check` warning mode
93+Analyzer walks the program looking for "this multiply / power / chain
94+might overflow given the inputs we can see" and emits a warning
95+pointing at the source line. User reads the warnings, fixes via A
96+or B. Pattern: same warn_unresolved_call shape we use today. No
97+runtime cost.
98+
99+### D. `--all-bigint` whole-program flag (user's original proposal)
100+Last-resort, every int local becomes tagged. Documentation must
101+prominently say "expect 5-10x slowdown on integer-hot code". Right
102+choice for someone running a small Ruby script through spinel just
103+to get a binary, where they'd rather have correctness than speed.
104+
105+## Recommended priority order
106+
107+1. Extend pattern detection (the existing approach, more shapes):
108+ - `x ** n` and `Integer#pow`
109+ - Recursive accumulators (`def f(n); n<=1 ? 1 : n*f(n-1); end`)
110+ - `gets.to_i` / `ARGV[i].to_i` flowing into multiply chain
111+ - Whole-array `inject(:*)` / `reduce(:+)` over numeric arrays where
112+ the array source isn't bounded by a static literal
113+ These are syntactically detectable, decidable individually, and
114+ catch the common scientific / competitive-programming shapes.
115+
116+2. `BigInt(...)` explicit cast — small compiler change, gives the
117+ "ergonomic narrow" path.
118+
119+3. RBS-typed bigint declarations — surfaces the existing bigint type
120+ through the existing RBS pipeline.
121+
122+4. `--strict-bigint-check` analyzer warning — closes the loop by
123+ pointing the user at gaps the detector missed.
124+
125+5. `--all-bigint` whole-program flag — last resort, document the cost.
126+
127+Step 1 is the "near-term plan" from project_bigint_promotion_plan.md.
128+Steps 2-5 are the escape-hatch fan-out.
129+
130+## Why I'd push back on starting with the whole-program flag
131+
132+The single flag is the easiest to ship, but it locks the project into
133+a future where bigint-needing users compile slow and bigint-not-needing
134+users got nothing new. Whereas A/B/C in any order each delivers a
135+narrower, faster improvement for the common case, and D becomes the
136+no-thinking-required fallback once nothing else fits.
137+
138+Phrased differently: D answers "how do I make spinel safe for bigint?"
139+A-C answer "how do I make spinel useful for someone whose program
140+sometimes needs bigint?" The second question is the one the
141+ecosystem benefits from.