Lines
94.78 %
Functions
87.1 %
Branches
100 %
//! The stage-switch gate for plan 0014.
//!
//! The plan's ledger grows while a stage is worked, but a stage's `Closes:`
//! list is written once. Without a check the two drift: measured on
//! 2026-10-14, Stage 5 named 11 issues and owned 42. These tests make a
//! stage's `Closes:` list the complete index of what that stage owns, so
//! entering a stage cannot silently skip a row.
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::PathBuf;
/// Stages whose scope must be complete. A closed stage is excluded: its rows
/// are settled and its `Closes:` list is history, not a worklist.
const OPEN_STAGES: [&str; 4] = ["4", "5", "6", "7"];
fn plan() -> String {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../doc/ops/0014-compiler-defect-families.org");
fs::read_to_string(&path)
.unwrap_or_else(|e| panic!("plan 0014 unreadable at {}: {e}", path.display()))
}
struct Row {
id: String,
owner: String,
/// Ledger rows are not confined to one table: `* Issue ledger` carries
/// ISS-001..125 and the later issues are appended in the Stages section. Exit
/// tables and Stage 7's closure schedule also lead rows with an issue id, so
/// the owner cell's shape is what identifies a ledger row.
///
/// An owner is a stage number (`5`, `3 (slice 3.2b)`, `4 (A) / 7 (B)`), a
/// slice (`slice 6.12 after Stage 6 (A) …`), `WONTFIX`, `DEFERRED` or `—`.
/// Prose (`CL HANDLER-CASE`, `one pipeline, one accounting`) is not, and
/// neither is an exit table's `0011 rows` — hence the bound on the digits.
fn is_owner(cell: &str) -> bool {
const WORDS: [&str; 3] = ["WONTFIX", "DEFERRED", "slice "];
if cell == "—" || WORDS.iter().any(|w| cell.starts_with(w)) {
return true;
let digits = cell.chars().take_while(char::is_ascii_digit).count();
(1..=2).contains(&digits) && !cell[digits..].starts_with(|c: char| c.is_ascii_digit())
/// A ledger row is a table row whose first cell is an issue id and whose
/// third cell is an owner.
fn ledger_rows(plan: &str) -> Vec<Row> {
plan.lines()
.filter_map(|line| {
let cells: Vec<&str> = line.trim().split('|').map(str::trim).collect();
// A leading `|` yields an empty first cell, so the id is at 1.
match cells.as_slice() {
[_, id, _text, owner, ..] if id.starts_with("ISS-") && is_owner(owner) => {
Some(Row {
id: (*id).to_owned(),
owner: (*owner).to_owned(),
})
_ => None,
.collect()
/// The issue ids named in a stage's `Closes:` paragraph, which ends at the
/// `Rows:` sentence or the paragraph break after it.
fn closes(plan: &str, stage: &str) -> BTreeSet<String> {
let heading = format!("** Stage {stage} ");
let Some(start) = plan.find(&heading) else {
return BTreeSet::new();
};
let section = &plan[start..];
let end = section
.get(heading.len()..)
.and_then(|rest| rest.find("\n** ").map(|i| i + heading.len()))
.unwrap_or(section.len());
let section = §ion[..end];
// Stage 7 spells it `Closes (Branch B arms only):`, so the colon is not
// fixed to the word.
let Some(c) = section
.lines()
.scan(0usize, |at, line| {
let start = *at;
*at += line.len() + 1;
Some((start, line))
.find_map(|(at, line)| line.trim_start().starts_with("Closes").then_some(at))
else {
let tail = §ion[c..];
let scope_end = tail
.find("Rows:")
.or_else(|| tail.find("\n\n"))
.unwrap_or(tail.len());
issue_ids(&tail[..scope_end])
fn issue_ids(text: &str) -> BTreeSet<String> {
text.split(|c: char| !c.is_ascii_alphanumeric() && c != '-')
.filter(|t| t.starts_with("ISS-") && t.len() > 4)
.map(str::to_owned)
/// The owner cell may carry a branch-conditional pair (`4 (A) / 7 (B)`) or a
/// parenthesised note. A stage owns the row when any alternative names it.
fn owning_stages(owner: &str) -> BTreeSet<String> {
owner
.split('/')
.flat_map(|alt| alt.split('+'))
.filter_map(|alt| {
let digits: String = alt
.trim()
.chars()
.take_while(char::is_ascii_digit)
.collect();
(!digits.is_empty()).then_some(digits)
#[test]
fn every_issue_id_appears_once_in_the_ledger() {
let plan = plan();
let mut seen: BTreeMap<String, usize> = BTreeMap::new();
for row in ledger_rows(&plan) {
*seen.entry(row.id).or_default() += 1;
let duplicated: Vec<_> = seen
.iter()
.filter(|(_, n)| **n > 1)
.map(|(id, n)| format!("{id} x{n}"))
assert!(
duplicated.is_empty(),
"ledger rows duplicated: {}",
duplicated.join(", ")
);
seen.len() > 100,
"ledger looks truncated: {} rows",
seen.len()
fn every_issue_a_stage_closes_has_a_ledger_row() {
let rows: BTreeSet<String> = ledger_rows(&plan).into_iter().map(|r| r.id).collect();
let dangling: Vec<String> = OPEN_STAGES
.flat_map(|stage| {
closes(&plan, stage)
.into_iter()
.filter(|id| !rows.contains(id))
.map(move |id| format!("Stage {stage}: {id}"))
dangling.is_empty(),
"a stage closes an issue with no ledger row — a missing row, or an owner cell \
that no longer parses as one ({} ids):\n{}",
dangling.len(),
dangling.join("\n")
/// The stage-switch gate: a stage's `Closes:` list indexes every row it owns.
/// A row covered by an existing slice is still listed, with the covering
/// slice named in its provenance — otherwise the coverage is invisible here.
fn an_open_stage_closes_every_row_it_owns() {
let rows = ledger_rows(&plan);
let mut unplanned: Vec<String> = Vec::new();
for stage in OPEN_STAGES {
let named = closes(&plan, stage);
let owned: BTreeSet<String> = rows
.filter(|r| owning_stages(&r.owner).contains(stage))
.map(|r| r.id.clone())
unplanned.extend(
owned
.difference(&named)
.map(|id| format!("Stage {stage}: {id}")),
unplanned.is_empty(),
"ledger rows owned by an open stage but absent from its Closes list \
({} rows) — reconcile at the stage switch:\n{}",
unplanned.len(),
unplanned.join("\n")