blob: c64dfd0e19ff6b591658ec08517f31d215d9a26a [file] [edit]
#!/usr/bin/env bash
# Style gate for the Aeneas-facing Rust (DESIGN.md §3.4).
# Usage: scripts/lint-rust-style.sh [dir ...] (default: crates/con-ron-core/src)
#
# `#[cfg(test)]` modules are exempt: Charon never sees them (they are not in
# the non-test build), so they may use closures, iterators and loops.
set -u
dirs=("$@"); [ ${#dirs[@]} -eq 0 ] && dirs=(crates/con-ron-core/src)
# Every source line before the first `#[cfg(test)]`, as `file:line:text`.
gather() {
local f
find "${dirs[@]}" -name '*.rs' -type f 2>/dev/null | sort | while read -r f; do
awk -v F="$f" '/#\[cfg\(test\)\]/ { exit } { printf "%s:%d:%s\n", F, NR, $0 }' "$f"
done
}
fail=0
check() { # check <label> <regex>
local label=$1 re=$2 hits
hits=$(gather | grep -E "$re" | grep -v '^\S*:\S*:\s*//' | grep -v 'lint: allow')
if [ -n "$hits" ]; then echo "== $label"; echo "$hits"; fail=1; fi
}
check "derive(Debug) (mixed recursion groups in Charon)" 'derive\([^)]*Debug'
# A closure bar has at least one character between the bars, which is what
# keeps `a || b` and `x | y` out (`||` and `|_|`-less bars are not closures).
check "closures" '\|\s*(_|mut |&|[a-z])[a-z_0-9,&: ]*\|\s*(\{|[a-z])'
check "? operator" '\)\?[;.) ]|\)\?$'
# Loops (`while`, `loop`, `for … in a..b`) are allowed anywhere in the core
# (DESIGN.md §3.4's "Loops" bullet); there is nothing to check.
# `&str` CONSTANTS (task #86). Aeneas emits one as `toStr "..."`, whose size
# bound it discharges with `by decide +native` in the constant's own
# definition, so the constant seals an axiom into every theorem whose closure
# reaches it (AENEAS_FINDINGS.md §3.8) -- and it prints an inner `"` unescaped
# (F17). Spell a literal `const S: [u8; N] = *b"...";` and compare bytes. A
# `&str` PARAMETER is fine; only a constant carries the axiom. The ONE
# exception is `kernel/pins_text.rs`'s `PINS_TEXT`: 532 KB is past what an
# `Array.make` literal can hold, and the file is generated by
# `scripts/gen-pins.py`, so the exemption is by path rather than by an
# in-line `lint: allow` a regeneration would drop.
check_str_consts() {
local hits
hits=$(gather | grep -E 'const [A-Za-z_][A-Za-z0-9_]*[[:space:]]*:[[:space:]]*&([a-z'"'"']+ )?str\b' \
| grep -v '^\S*:\S*:\s*//' | grep -v 'lint: allow' \
| grep -v 'con-ron-core/src/kernel/pins_text.rs:') # unanchored: gates.sh passes an absolute dir
if [ -n "$hits" ]; then
echo "== &str constants (toStr spends a decide+native axiom; use [u8; N], DESIGN.md §3.4)"
echo "$hits"; fail=1
fi
}
check_str_consts
# `unsafe`. **The rule is none at all, and since task #97-SWAP-2 there is no
# exemption** (the 2026-09-12 ruling: `std` does it if it can).
#
# Tasks #94-#97-SWAP had one: `crates/con-ron-core/src/ron/tagged.rs`, the
# generic tagged counted handle, whose pointee type is chosen at run time from
# four tag bits so that no `std` smart pointer can express it. Task #97-SWAP
# then measured what it was still FOR -- the arena checker holds no `Expr`, so
# the only `Expr` values a run builds are the PINNED DATA
# (`kernel::{basis_raw,std_axioms,trust_axioms,trust_pins,
# pins_decode}`), walked once at startup by `arena::intern` -- and task
# #97-SWAP-2 put those few thousand nodes back on `ron::ptr::P` =
# `std::sync::Arc` and deleted `ron/{tagged,node}.rs`. So this check asserts
# ZERO, and any `unsafe` in the verified crate is now a lint failure with no
# path exemption to add.
check_unsafe() {
local hits
hits=$(gather | grep -E '\bunsafe\b' \
| grep -v '^\S*:\S*:\s*//' | grep -v 'lint: allow')
if [ -n "$hits" ]; then
echo "== unsafe (NONE is allowed in the verified crate, task #97-SWAP-2)"
echo "$hits"; fail=1
fi
}
check_unsafe
check "std::collections" 'std::collections'
# The counted-pointer API, and what the rule is actually about. The banned
# names all let the count or the pointee ESCAPE into a value the model cannot
# see: `get_mut`/`make_mut` mutate THROUGH a share, which is exactly what "an
# `Arc` is its contents" cannot express; `strong_count` puts the count itself
# into a number a program can compute with; the raw-pointer and `Weak` names
# hand out identities. A *read of the count that only selects an equivalent
# path* is a different thing and is allowed — `ron::tagged::Raw::is_exclusive`,
# the port of con-leche's `withExclusive` (task #98), whose answer never
# reaches a value and whose model is `false`, the branch that always memoises
# (OVERVIEW.md §8.1's row argues it). It is deliberately NOT spelled
# `Arc::strong_count`: it is one method on the crate's own handle, `pub(crate)`
# like the rest of `tagged.rs`, reachable from the core through the single
# `ron::node::is_exclusive`, and the names below stay banned so that nothing
# reads an `Arc`'s count -- `Name`, `Level`, `PropWhen` and `ConstantInfo`
# are still `Arc` and nothing needs their counts.
check "P/Rc/Arc API beyond new/clone/deref/ptr_eq" '\b(P|Rc|Arc)::(get_mut|make_mut|downgrade|try_unwrap|into_raw|from_raw|as_ptr|strong_count|weak_count|increment_strong_count|decrement_strong_count)|RefCell|Cell<'
check "panics as control flow" '\b(panic!|unwrap\(\)|expect\(|unreachable!|todo!|unimplemented!)'
# The filler-only `Clone` for `ron::hashmap2::Slot` (task #97-PERF-BULKFILL).
# `Vec::resize` is the one bulk fill Aeneas models, and it asks for `Clone`;
# `Slot`'s impl answers `Vacant` for every slot, a `Live` one included, so it
# is a filler and NOT a copy. What keeps it from being used as one: `Slot`
# values exist only inside `ron/hashmap2.rs` (`HashMap2::slots` is private and
# no `pub fn` hands a slot out), so (1) no other core file may name `Slot` at
# all, and (2) inside `hashmap2.rs` every call that can reach `Slot::clone` --
# `.clone()`, `Clone::clone`, `.resize(`, `extend_from_slice`, `to_vec`,
# `vec![` -- must be the single `slots.resize(len, Slot::Vacant);` in
# `allocate_slots`. Nothing copies a live slot.
check_slot_clone() {
local hits
hits=$(gather | grep -E '\bSlot\b' | grep -v '^\S*:\S*:\s*//' | grep -v 'lint: allow' \
| grep -E 'con-ron-core/src/' | grep -vE 'con-ron-core/src/ron/hashmap2\.rs:' \
| grep -E 'hashmap2::Slot|\bSlot::|\bSlot<')
hits+=$(gather | grep -E 'con-ron-core/src/ron/hashmap2\.rs:' | grep -v '^\S*:\S*:\s*//' \
| grep -E '\.clone\(\)|Clone::clone|\.resize\(|extend_from_slice|to_vec|vec!\[' \
| grep -vE ':[[:space:]]*slots\.resize\(len, Slot::Vacant\);$')
if [ -n "$hits" ]; then
echo "== Slot's filler-only Clone reached outside allocate_slots (ron/hashmap2.rs, task #97-PERF-BULKFILL)"
echo "$hits"; fail=1
fi
}
check_slot_clone
exit $fail