Commit 8e1166f0

8e1166f0fdb0Merged

Sam Ruby <[email protected]> · 6 months ago · 8 files · +4 added · ~4 modified

Description

spike(analyze,rbs): opportunistic RBS seeding via --rbs DIR

Per the design conversation on OriPekelman/tep#6, this is a speculative branch exploring whether spinel can consume a strict subset of RBS as advisory seeds for its existing inference. The goal is to provide library authors with a declarative alternative to the type-seeding pattern Ori documented in lib/tep.rb -- a ~200-line block of force-calls whose only purpose is to pin parameter types for methods user apps may not call.

The whole flow lands behind --rbs DIR. With the flag absent, spinel behavior is byte-identical to current master.

Architecture (mirrors prism)

sig/**/*.rbs --[spinel_rbs_extract]--> text seed file | v spinel_analyze (ARGV[2])

  1. vendor/rbs/ -- the rbs C parser, fetched from rubygems.org via make deps the same way prism is fetched. Pinned to v4.0.1. Build target make rbs_extract produces the consumer binary. Not in make all -- opt-in only.

  2. spinel_rbs_extract (C) -- walks a directory for *.rbs files, parses each via rbs's C API, maps a strict subset of RBS types to spinel's vocabulary, emits a line-oriented seed file. Any construct outside the subset is silently skipped.

    Subset:

    • Primitives: Integer, Float, String, Symbol, TrueClass, FalseClass, NilClass, bool, nil, void
    • Nominal classes (qualified with _ to match spinel's @cls_names convention -- module Tep; class Json -> Tep_Json)
    • Array[T] -> str_array / int_array / float_array / sym_array / obj_X_ptr_array / poly_array
    • Hash[K, V] -> str_int_hash / sym_str_hash / etc.
    • Optional T (and the T | nil shape of Union) -> T?

    Dropped silently: generics with type vars, multi-overloads, blocks, proc types, intersections, records, tuples, interfaces, literal types, type aliases, self / instance / class / top / bottom / any types.

  3. spinel_analyze.rb learns to read an optional ARGV[2] seed file (load_rbs_seeds + apply_rbs_seeds). Runs after collect_all so class/method tables exist, before the inference fixpoint so seeds become its starting point. Three dispatch paths to match spinel's three storage tables:

    • meth -> @cls_meth_* (instance methods)
    • cmeth -> @cls_cmeth_* (singleton on a class)
    • cmeth -> @meth_* with prefix (singleton on a module -- collect_module stores these as top-level methods named Foo_cls_bar)

    Seeds are advisory: unify_call_types still widens on observed contradiction (mirrors the gradual-typing escape hatch RBS itself uses for untyped). Missing classes / missing methods in the seed file are silently skipped -- the RBS may cover surface the current compilation unit doesn't touch.

  4. spinel shell script gains --rbs DIR: runs the extractor between parse and analyze, passes the seed tmpfile as ARGV[2] to spinel_analyze. Falls back to building without seeds (with a warning) if the extractor isn't built or the directory is missing.

Measured against tep's sig/

Tep's lib/tep.rb contains a ~200-line seed block labeled "Type-seeding: methods that may not be called by a given user app would otherwise default their param C types to mrb_int and mismatch the typed ivars they touch."

Ori has shipped phase 1 of sig/ (OriPekelman/tep@3c5ab23, 12 files covering tep's public surface) with the explicit plan to delete the seed block once spinel can consume RBS. Phase 2 (Tep::SQLite, Tep::App, Tep::Scheduler, ...) is deferred.

Three-way comparison on examples/hello.rb:

StatewarningsC errors
Seed block present, no --rbs (baseline)00
Seed block present, with --rbs00
Seed block deleted, no --rbs1518
Seed block deleted, with --rbs02

The 2 remaining errors with seeds deleted + --rbs are in Tep::SQLite#open and Tep::App#set_session_secret -- both in classes Ori explicitly deferred to phase 2 (no sig/ files yet). When those land, --rbs should close those too.

The pattern body-usage inference (#542, today's 6b2ae3b) doesn't catch and RBS does:

  • string-parsing methods that iterate char-by-char on a String param (Tep::Json.escape and 12 related). Body usage of .length, [i], c == "x" is ambiguous with Array; RBS pins it as String.
  • top-level module methods (Tep.h) where the seed point is a pure helper with no body fingerprinting.
  • attr_accessor types (forces per-class ivar slot typing without requiring an initializer in the body).

Status

Behind a flag, off by default. make all doesn't build the extractor. Test suite: 516/516 pass with all changes (no regression on no-flag path; self-hosted analyzer also honors the seed file). The experiments/rbs/ directory carries the synthetic Box fixture + harness that proved the analyzer-side mechanism end-to-end before validating against tep.

Not for merge as-is -- this is the spike that determines whether the approach is worth productizing. Open questions for follow-up: multi-overload handling (currently dropped entirely), generic parameters, return-type pinning for the body-cascade case documented in tep#6.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
8e1166f0fdb0
Parent
c3adf401f22e

.gitignoremodifiedView file

@@ -21,6 +21,8 @@
2121 /spinel_codegen.exe
2222 /spinel_parse
2323 /spinel_parse.exe
24+/spinel_rbs_extract
25+/spinel_rbs_extract.exe
2426 /perf.data
2527 /perf.data.old
2628 /lib/regexp/libspre.a