Commit 5a899bc3

5a899bc3c3f8Merged

Ori Pekelman <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Add spinel --emit-rbs: export inferred signatures as RBS

Roadmap item 3 of the typing arc. Makes the whole-program inference visible: dump every method/class signature the analyzer inferred as RBS, feeding Steep/Sorbet/ruby-lsp and doubling as a silent-miscompile diagnostic — you can see where a slot came out untyped (the boxed poly slow path).

  • spinel_analyze.rb: type_to_rbs (inverts spinel_rbs_extract's RBS->tag mapping; recursive for ?, obj_ptr_array, tuple:) + emit_rbs, which walks the same signature tables emit_poly_report uses (@meth, @cls_*). Top-level defs wrap in class Object (valid RBS); poly-degraded params /returns render untyped with a trailing # spinel: widened to untyped (slow path) comment. Injected builtin Method is filtered. Gated by SPINEL_EMIT_RBS (written after analyze_phase; IR still emitted).
  • spinel: --emit-rbs analyze-only mode -> app.rbs (or -o path), no binary.

Verified: emitted RBS passes rbs validate; a class + top-level methods render correct signatures; a return that unifies int|string shows -> untyped with the degrade comment. Normal compile unaffected (env-gated). On the ori fork (feat/typing); no PR to matz/spinel.

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

Details

Commit
5a899bc3c3f8
Parent
63f7f4505469

spinelmodifiedView file

@@ -43,6 +43,7 @@
4343 SOURCE=""
4444 OUTPUT=""
4545 C_ONLY=0
46+EMIT_RBS=0
4647 STDOUT_MODE=0
4748 RUN_MODE=0
4849 OPT_LEVEL="2"
@@ -107,6 +108,8 @@
107108 EVAL_USED=1
108109 shift ;;
109110 -c) C_ONLY=1; shift ;;
111+ --emit-rbs) # Analyze-only: dump inferred signatures as RBS, no binary.
112+ EMIT_RBS=1; shift ;;
110113 -S) STDOUT_MODE=1; shift ;;
111114 -E) RUN_MODE=1; shift ;;
112115 *.rb)
@@ -145,6 +148,23 @@
145148 fi
146149 fi
147150
151+# --emit-rbs is analyze-only (stops before codegen), so the binary/C-output
152+# modes would be silently ignored alongside it — reject the conflict up front.
153+if [ "$EMIT_RBS" -eq 1 ]; then
154+ if [ "$RUN_MODE" -eq 1 ]; then
155+ echo "spinel: --emit-rbs cannot be combined with -E (run)" >&2
156+ exit 2
157+ fi
158+ if [ "$C_ONLY" -eq 1 ]; then
159+ echo "spinel: --emit-rbs cannot be combined with -c (C source only)" >&2
160+ exit 2
161+ fi
162+ if [ "$STDOUT_MODE" -eq 1 ]; then
163+ echo "spinel: --emit-rbs cannot be combined with -S (stdout)" >&2
164+ exit 2
165+ fi
166+fi
167+
148168 # `-e` mode: write the accumulated inline script to a temp file and
149169 # treat it as the source. Default output name is `a` since the
150170 # inline form has no .rb basename to derive from. If both -e and a
@@ -173,6 +193,7 @@
173193 echo "Options:" >&2
174194 echo " -o FILE Output file" >&2
175195 echo " -c C source only (don't compile)" >&2
196+ echo " --emit-rbs Dump inferred type signatures as RBS (-> app.rbs), no binary" >&2
176197 echo " -S Print C to stdout" >&2
177198 echo " -E Run the compiled binary; leftover args become its ARGV" >&2
178199 echo " -O LEVEL Optimization level (default: 2)" >&2
@@ -239,6 +260,12 @@
239260 # int locals as bigint when mode == promote. Raise / wrap modes
240261 # only care about the cc-time preprocessor switch added below.
241262 export SPINEL_INT_OVERFLOW="$INT_OVERFLOW"
263+# RBS export mode: tell the analyzer to also dump inferred signatures.
264+# (Conflicting-mode checks are done up front with the other mode-conflict guards.)
265+if [ "$EMIT_RBS" -eq 1 ]; then
266+ RBS_OUT="${OUTPUT:-$BASENAME.rbs}"
267+ export SPINEL_EMIT_RBS="$RBS_OUT"
268+fi
242269 if [ -x "$ANALYZE_BIN" ]; then
243270 "$ANALYZE_BIN" "$AST_TMP" "$IR_TMP" $SEED_TMP
244271 else
@@ -249,6 +276,12 @@
249276 exit 1
250277 fi
251278
279+# --emit-rbs is analyze-only: the signatures are written, stop before codegen.
280+if [ "$EMIT_RBS" -eq 1 ]; then
281+ echo "Wrote $RBS_OUT" >&2
282+ exit 0
283+fi
284+
252285 # Step 3: Codegen
253286 if [ $STDOUT_MODE -eq 1 ]; then
254287 if [ -x "$CODEGEN_BIN" ]; then