Skip to content

Static-analysis ratchet: cargo-public-api baseline + dylint project lints + loom for parallel paths #1237

Closed

Description

@robcohen

Background

Repo already has these static-analysis gates:

This issue proposes three additions, each closing a distinct class of bug that the existing gates don't catch.

Add 1: cargo-public-api baseline diff

Generate crates/<name>/public-api.txt for the workspace crates with stable public APIs, check the files in, and add a CI step that regenerates and git diff --exit-code's them.

Catches:

  • Accidental pub-item removal / rename
  • Accidental pub-trait method-signature changes
  • Accidental pub-struct field addition without #[non_exhaustive]

Difference from issue #1233: cargo-public-api produces a human-readable text diff that shows up in PR review; cargo-semver-checks produces a semantic verdict. The two complement each other — public-api for visibility in the PR, semver-checks for the gate.

Add 2: dylint for project-specific lints

dylint runs custom lints written as Rust crates. Lints to start with:

no_unwrap_in_lib

Forbid .unwrap() and .expect() in crates/rustledger-*/src/ (excluding #[cfg(test)] modules and integration test files). CLAUDE.md's self-review checklist includes "No .unwrap() in library code" — dylint makes it structural.

no_panic_in_lib

Forbid panic!(), unreachable!(), todo!(), unimplemented!() in library crates. Production library code should return Result, not panic.

parking_lot_only

Forbid std::sync::Mutex, std::sync::RwLock. parking_lot was adopted as a perf quick win (#1076) but nothing prevents new std::sync mutexes from being introduced.

no_std_collections_in_hot_paths

Forbid HashMap / HashSet (which use the slow SipHash by default) in specific hot-path modules; require rustc_hash::FxHashMap or similar. FxHasher was adopted in #1076; this lint prevents regression.

Add 3: loom for shared-state critical sections

loom exhaustively explores thread interleavings for code in a loom::sync module. Recent example of a bug it would have caught:

The fix worked because the author identified the specific interleaving. loom-tested critical sections would have flagged the missing invariant at PR time.

Scope: the few modules that use rayon::par_iter or share mutable state via Mutex/RwLock. Not "everything that has threads" — loom's exhaustive search is exponential.

What "done" looks like

  • A new CI job that runs each of the three checks. Failures block merge.
  • Per-lint allowlists for legitimate exceptions, with a code comment naming the lint and the reason.
  • cargo-public-api baseline files checked in for the Tier-1 crates (plugin-types at minimum, core and parser as stretch goals — overlaps with Lock public API surface via cargo-semver-checks (+ cargo-public-api for visibility) #1233).
  • dylint lints checked in to a new lints/ workspace crate.
  • loom tests for at least the BQL parallel execution path; expand from there.

Open questions

1. dylint runtime cost

dylint rebuilds the lint crate when toolchain changes. On the order of 5-10s in CI, less locally with a warm cache. Probably acceptable but worth measuring before committing.

2. loom test scope creep

Easy to over-instrument code with loom::sync wrappers and then run only cargo loom weekly. Conservative scope: start with the BQL executor's parallel iteration paths; expand only when a concrete bug suggests it's worth it.

3. cargo-public-api for which crates?

Same question as #1233. Start with plugin-types; expand to core and parser once the baseline is comfortable.

Related

Activity

  1. added
    enhancement
    on May 28, 2026
  2. robcohen commented on Jun 15, 2026

    @robcohen
    MemberAuthor

    Review + first slice landed (#1363)

    Reviewed this against the current codebase. Summary: the three "Adds" are independent and have very different cost/value, and two of them carry a hidden nightly-toolchain cost that isn't called out in the issue.

    Current state

    None of it exists yet (no lints/ crate, no public-api.txt, no loom/dylint deps), and none of the four tools (cargo-public-api, cargo-dylint, dylint-link, cargo-loom) are in the nix dev shell — all would need flake additions.

    Violation counts (grounds the dylint effort)

    • std::sync::Mutex/RwLock in lib code: 1
    • .unwrap()/.expect() in non-test lib code: ~136 across 47 files
    • panic!/unreachable!/todo!/unimplemented! in non-test lib code: ~29

    (The naive grep showed 2119 unwraps, but that's almost entirely inline #[cfg(test)] modules.)

    Key friction not in the issue

    • dylint requires a pinned nightly toolchain to build lint crates. This repo pins stable 1.95.0 via nix, so dylint means carrying a second nightly toolchain — real flake/CI-cache complexity.
    • cargo-public-api also needs nightly rustdoc. So the "Add 1" piece shares the same cost, and it overlaps the still-open Lock public API surface via cargo-semver-checks (+ cargo-public-api for visibility) #1233 (cargo-semver-checks + public-api). Better tracked there.
    • no_unwrap_in_lib / no_panic_in_lib are not drop-in gates — the ~165 existing sites (many likely infallible) need a cleanup/allowlist pass first.

    Insight: most of these can be grep ratchets

    The repo already has the precedent (scripts/check-unsafe-invariant.sh, the "Unsafe Invariant" CI status). parking_lot_only, no_std_collections_in_hot_paths, and even baseline-style no_unwrap/no_panic checks can be implemented as grep ratchets — avoiding the dylint nightly cost entirely.

    Landed now: #1363 — parking_lot_only as a grep ratchet

    Recommended split of the remainder

    1. no_std_collections_in_hot_paths — do as a grep ratchet (cheap, same pattern as ci(ratchet): forbid std::sync Mutex/RwLock in library code (#1237) #1363).
    2. cargo-public-api — fold into Lock public API surface via cargo-semver-checks (+ cargo-public-api for visibility) #1233 rather than duplicate here.
    3. dylint lint crate + no_unwrap/no_panic cleanup — its own issue; weigh the nightly-toolchain cost vs. doing the unwrap/panic checks as baseline grep ratchets instead.
    4. loom — defer until a concrete parallel bug recurs (matches this issue's own Q2).

    Suggest narrowing this issue to track that remainder (or splitting into per-tool issues), now that the cheapest, highest-value piece is in flight.

  3. robcohen commented on Jun 15, 2026

    @robcohen
    MemberAuthor

    Item 1 of the recommended split is in flight: #1364 implements no_std_collections_in_hot_paths as a marker-based grep ratchet (// ratchet: fxhash-only), marking five hot modules (booking, inventory, loader booking pass, balance validator, query executor types) and converting book.rs's one std HashSet to FxHashSet. No nightly toolchain. Joins #1363 (parking_lot ratchet). Remaining: cargo-public-api (→ #1233), the dylint crate + unwrap/panic cleanup, and loom.

  4. robcohen commented on Jun 15, 2026

    @robcohen
    MemberAuthor

    Closing — the high-value ratchets shipped; heavier items deferred

    Delivered the two tractable, high-ROI pieces as grep ratchets (the existing check-unsafe-invariant.sh pattern), deliberately avoiding the nightly-toolchain cost a dylint lint crate would add to this stable-pinned flake:

    Deferred (closing rather than tracking indefinitely)

    • dylint lint crate (no_unwrap_in_lib, no_panic_in_lib): requires a separate nightly toolchain in CI, and there are ~136 unwrap/expect + ~29 panic-family sites in non-test lib code that would need a cleanup/allowlist pass first. Several of these could alternatively be done as grep ratchets if revisited.
    • cargo-public-api: folded into Lock public API surface via cargo-semver-checks (+ cargo-public-api for visibility) #1233.
    • loom: deferred per this issue's own conservative-scope note — revisit if a concrete parallel bug recurs.

    Closing as substantially complete; reopen or file a focused issue if the dylint/loom work is wanted later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions