Skip to main content

rucc_codegen/
coverage.rs

1//! Which IR opcodes have somewhere to go, and which do not.
2//!
3//! Design: `spec/10-backend.md` section 10.2, under **Coverage**.
4//!
5//! Every opcode has to be lowered by something or be a hole somebody wrote down. Without this the
6//! way a hole is found is that somebody compiles a program containing one and the selector reports
7//! that it cannot lower an instruction, which is a fine diagnostic and a bad discovery mechanism:
8//! it turns a gap in the rule set into a user's problem rather than a failing build.
9//!
10//! # The three answers
11//!
12//! An opcode is lowered by a rule, or somewhere a rule cannot reach, or nowhere.
13//!
14//! The first is the ordinary answer and the one this can check by itself. [`crate::term`] says
15//! every name a rule could be written at, the table says every name one is written at, and an
16//! opcode is covered when each of its names is in both. That is what makes this a check about
17//! widths rather than about opcodes: an `add` with a rule at four widths and no rule at the fifth
18//! is not covered, and would be reported here as the missing name rather than as a covered opcode.
19//!
20//! The second is a lowering, which is not a gap. `spec/10-backend.md` names five of them and there
21//! are more now, and they are all the same kind of thing: an opcode whose lowering depends on
22//! something no pattern can see. Where a call's arguments go depends on the signature, where a
23//! local lives depends on the frame, an unconditional jump is an edge and edges live on the block,
24//! and a `memcpy` is a run of moves whose length is a constant the pattern would have to count. A
25//! rule matches one term and can say none of that. Which opcodes those are is
26//! [`crate::capability::lowering`] and is not written down here, because it was written down here
27//! and in the lowering group both and the two could disagree.
28//!
29//! The third is [`GAPS`], which is the number `spec/15-testing.md` section 15.8 says we keep. Each
30//! entry names why it is there and the issue that closes it, so that an opcode nobody has written a
31//! rule for is a decision somebody wrote down rather than a surprise.
32//!
33//! [`WIDTHS`] and [`NAMES`] are the same third answer said about something smaller than an opcode.
34//! A width on [`WIDTHS`] has no names at all, so no opcode is missing a rule at it, and a name on
35//! [`NAMES`] is one width of an opcode that lowers at its other widths. Both carry the issue that
36//! closes them for the same reason [`GAPS`] does.
37//!
38//! # What makes the lists honest
39//!
40//! An entry that stops being true fails. An opcode on either list that a rule starts covering is a
41//! stale entry and the tests below say so by name, which is the same rule the exclusion lists in
42//! the compatibility harness are kept under: a list nothing checks is a list that only grows.
43//!
44//! The direction this cannot check is an opcode moving from [`GAPS`] to a hand written lowering
45//! without [`crate::capability::HAND`] following it, because where an opcode is lowered by name is
46//! a `match` arm and there is nothing to ask about a `match` arm from here. What that costs is one
47//! line of a list going out of date; what it does not cost is a gap going unnoticed, since the
48//! opcode is still on a list and still counted.
49//!
50//! # The other question
51//!
52//! All of the above is about the rule set as it is written. [`Fired`] is about the rule set as it
53//! is used: which rules a compilation actually reached. A rule nothing reaches is proved and dead
54//! weight, or it is a construct the corpus does not contain and somebody should know which. The
55//! selector marks a rule as it fires it, the driver writes the marks out under
56//! `-Zrule-coverage=FILE`, and the harness in `tamnd/rucc-compat` unions those files over a corpus,
57//! which is what turns coverage of the rule set into a number. `spec/20-execution-testing.md`
58//! section 20.9 is the design and `tamnd/rucc#261` is the work.
59
60use core::fmt;
61use core::fmt::Write as _;
62
63use rucc_ir::Opcode;
64use rucc_target::Arch;
65
66use crate::capability::{self, pattern_heads};
67use crate::select::Table;
68use crate::term;
69
70/// An opcode nothing lowers, why it is here, and the issue that closes it.
71///
72/// This is the count `spec/15-testing.md` section 15.8 asks for. It is not zero yet and the
73/// spec says it should be, which is the honest reading of where the back end is: every one of
74/// these is a feature nobody has written, and all of them but one are opcodes the front end
75/// cannot produce either, so a program that reaches one of these is a program that reaches an
76/// unimplemented builtin first. The one is the remainder of two floats, which a program writes
77/// with an operator and which is a call to the maths library rather than an instruction.
78pub static GAPS: &[(Opcode, &str, &str)] = &[
79    (Opcode::Splat, "a vector, and no rule is written about a lane count", "tamnd/rucc#200"),
80    (
81        Opcode::TargetIntrinsic,
82        "the same, since what needs one is a vector builtin",
83        "tamnd/rucc#200",
84    ),
85    (
86        Opcode::FRem,
87        "a call to `fmod`, so a link line question as much as a lowering one",
88        "tamnd/rucc#226",
89    ),
90    (
91        Opcode::Fma,
92        "a call or one instruction, depending on what the machine is told it has",
93        "tamnd/rucc#226",
94    ),
95    (Opcode::Bitreverse, "a node nothing writes and nothing lowers", "tamnd/rucc#363"),
96    (Opcode::TailCall, "a terminator nothing writes and nothing lowers", "tamnd/rucc#365"),
97    // Memory safety. These are a gap in a different sense from the rest: nothing emits one yet
98    // either, since the passes that would are milestones S5 and after, so there is no program the
99    // back end can be handed that reaches one. The ones the safety pass lowers are on `HAND`,
100    // and the five that make a capability all left this list without anything emitting them, which
101    // is the whole of tamnd/rucc#1085's lowering half: each has a lowering waiting for the pass that
102    // will write one, because a capability had to be a value the back end could hold before any of
103    // them could be written down at all. The two region markers left the same way and for a
104    // different reason, which is that what they cost is a count rather than a lowering.
105    // What is left is the plane writes, which the runtime does for itself today because the only
106    // ranges anything asks about are the ones its own allocator handed out. A stack object needs
107    // these, since nothing in the runtime sees a frame being set up or torn down.
108    (Opcode::MetaBegin, "a write over a range of the lifetime plane", "tamnd/rucc#856"),
109    (
110        Opcode::MetaEnd,
111        "the same write, with the version bumped past every capability",
112        "tamnd/rucc#856",
113    ),
114    (
115        Opcode::MetaTransfer,
116        "the same, and the state a range is in while a device owns it, which is S2's",
117        "tamnd/rucc#856",
118    ),
119];
120
121/// A width no rule is written at, why, and the issue that closes it.
122///
123/// The other half of coverage, and the half an opcode list cannot say. An opcode is covered when
124/// every name it has is a name a rule is written at, and a width with no name has no names to
125/// check: an `add` of two `__int128`s is not a missing rule for `add`, it is a width the rule
126/// language cannot spell. So the widths are written down here for the same reason the opcodes are
127/// written down above.
128pub static WIDTHS: &[(&str, &str, &str)] = &[
129    (
130        "one bit",
131        "everything but and, or, xor, a constant, and the widening out of one",
132        "tamnd/rucc#352",
133    ),
134    (
135        "a hundred and twenty eight bits",
136        "split into two halves before selection, except a division",
137        "tamnd/rucc#351",
138    ),
139    (
140        "eighty bits",
141        "a long double is on the x87 stack and no rule is about that stack",
142        "tamnd/rucc#326",
143    ),
144    (
145        "a hundred and twenty eight bits of float",
146        "turned into a call before selection, except a conditional move and the conversions \
147         against an integer that wide",
148        "tamnd/rucc#1064",
149    ),
150    (
151        "a vector of any lane count",
152        "a rule at a width says nothing about how many lanes",
153        "tamnd/rucc#200",
154    ),
155];
156
157/// A name a rule could be written at and deliberately is not, why, and the issue that puts it
158/// back.
159///
160/// The third list, and the one that is about a name rather than about an opcode or a width. An
161/// opcode on [`GAPS`] has no lowering at any width and a width on [`WIDTHS`] has no names at all,
162/// and neither of those can say that `add` is lowered at four widths and left alone at two.
163///
164/// This list used to be all of the narrow arithmetic. C promotes the operands of an arithmetic
165/// operator to `int` before the operator is applied, so `char a, b; a + b` is an `int` addition of
166/// two sign extended chars and there is no C program that asks the back end to add two bytes.
167/// Rules were written at those names anyway, ahead of the pass that would reach them, and they sat
168/// proved and never selected: `tamnd/rucc#261` measured that and `tamnd/rucc#368` took them out.
169/// They are back, because the width narrowing pass in `tamnd/rucc#375` is that caller and it
170/// writes a byte add out of the truncation the assignment back to a `char` already was. The last
171/// to come back were the divides, which narrow when the operands are zero extensions and, for
172/// sign extensions, when the ranges rule out the most negative value over minus one.
173///
174/// So the list is empty, and it stays here for the next name somebody decides to leave out, since
175/// the report and the capability table both read it.
176pub static NAMES: &[(&str, &str, &str)] = &[];
177
178/// What a target's rules cover, and what they do not.
179#[derive(Debug)]
180pub struct Report {
181    /// The rule file this is about, so that anything said about it names a file to open.
182    pub source: &'static str,
183    /// How many opcodes the IR has.
184    pub opcodes: usize,
185    /// The opcodes every name of which a rule is written at.
186    pub by_rule: Vec<Opcode>,
187    /// How many names those are, which is one per opcode and width.
188    pub names: usize,
189    /// A name a rule could be written at and none is, which is what a missing rule looks like.
190    pub uncovered: Vec<(Opcode, &'static str)>,
191    /// A name on [`NAMES`], which is a missing rule somebody decided to be missing.
192    pub deferred: Vec<(Opcode, &'static str)>,
193    /// A name a rule is written at that nothing can ever be called, which is a dead rule.
194    pub unreachable: Vec<&'static str>,
195    /// The opcodes lowered somewhere a rule cannot reach.
196    pub elsewhere: Vec<Opcode>,
197    /// The opcodes nothing lowers.
198    pub gaps: Vec<Opcode>,
199    /// The opcodes on none of the three lists, which is what a new opcode is until somebody says
200    /// where it goes.
201    pub unaccounted: Vec<Opcode>,
202}
203
204impl fmt::Display for Report {
205    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
206        write!(
207            f,
208            "rucc-codegen: {} lowers {} of the {} IR opcodes by rule at {} names, {} are lowered \
209             where no rule reaches, {} have no lowering yet and {} names are left for later",
210            self.source,
211            self.by_rule.len(),
212            self.opcodes,
213            self.names,
214            self.elsewhere.len(),
215            self.gaps.len(),
216            self.deferred.len()
217        )
218    }
219}
220
221/// What a table covers.
222///
223/// Nothing is executed and nothing is compiled. The rule set and the naming of instructions are
224/// both data, and the answer is a comparison of two lists.
225#[must_use]
226pub fn report(table: &Table) -> Report {
227    let named = term::heads();
228    let patterns = pattern_heads(table);
229
230    let mut by_rule = Vec::new();
231    let mut uncovered = Vec::new();
232    let mut deferred = Vec::new();
233    for &(opcode, name) in &named {
234        if patterns.contains(&name) {
235            by_rule.push(opcode);
236        } else if NAMES.iter().any(|&(deliberate, ..)| deliberate == name) {
237            deferred.push((opcode, name));
238        } else {
239            uncovered.push((opcode, name));
240        }
241    }
242    // An opcode is covered when every name it has is covered, so one missing width takes the
243    // whole opcode off the list however many of its other widths are there. A name on `NAMES` does
244    // not take it off, because the opcode is lowered and the entry says which widths were left for
245    // later and why: that is a narrower claim than the opcode having nowhere to go, and putting it
246    // on `GAPS` instead would say the wrong thing about an `add` that lowers perfectly well at
247    // four widths.
248    for &(opcode, _) in &uncovered {
249        by_rule.retain(|&covered| covered != opcode);
250    }
251    by_rule.sort_unstable();
252    by_rule.dedup();
253
254    let names = named.len() - uncovered.len() - deferred.len();
255    let unreachable: Vec<&'static str> = patterns
256        .iter()
257        .filter(|head| !named.iter().any(|(_, name)| name == *head))
258        .copied()
259        .collect();
260
261    let elsewhere: Vec<Opcode> =
262        Opcode::all().filter(|&opcode| capability::lowering(opcode).is_some()).collect();
263    let gaps: Vec<Opcode> = GAPS.iter().map(|&(opcode, ..)| opcode).collect();
264    let unaccounted: Vec<Opcode> = Opcode::all()
265        .filter(|opcode| {
266            !by_rule.contains(opcode)
267                && !elsewhere.contains(opcode)
268                && !gaps.contains(opcode)
269                && !capability::LIBCALLS.iter().any(|&(at, ..)| at == *opcode)
270        })
271        .collect();
272
273    Report {
274        source: table.source,
275        opcodes: Opcode::all().count(),
276        by_rule,
277        names,
278        uncovered,
279        deferred,
280        unreachable,
281        elsewhere,
282        gaps,
283        unaccounted,
284    }
285}
286
287/// The rules a target lowers by, or `None` where no back end in this crate covers it.
288///
289/// The same question [`crate::pipeline::Machine::for_target`] answers about the rest of a machine,
290/// and it is here as well because a caller that wants to write down what a run covered has a
291/// target and no machine. An architecture gets an arm here when it gets a rule file, and until then
292/// it has no rules to report coverage of rather than an empty set of them.
293#[must_use]
294pub fn table(arch: Arch) -> Option<&'static Table> {
295    match arch {
296        Arch::X86_64 => Some(&crate::select::x86_64::TABLE),
297        Arch::Aarch64 => Some(&crate::select::aarch64::TABLE),
298        Arch::Riscv64 => None,
299    }
300}
301
302/// Which rules fired, over one function or over a whole compilation.
303///
304/// A bit per rule and nothing else. This is on the path of every instruction selected, so what it
305/// costs is paid by every compilation whether or not anybody asked for the number, and the cheapest
306/// thing that answers the question is a flag per rule set once.
307///
308/// The index of a rule is how this is kept and not how it is written down. An index moves the
309/// moment a rule is added above it, so [`Fired::listing`] names the rule file and the line instead:
310/// a line is a place somebody can open, and a report written by one build can still be read against
311/// a rule file that has grown since.
312#[derive(Debug, Clone, Default, PartialEq, Eq)]
313pub struct Fired {
314    /// One entry per rule, true once that rule has fired. It grows to fit the highest index
315    /// marked rather than being sized from a table, so nothing here has to be told which target
316    /// is being compiled for.
317    seen: Vec<bool>,
318}
319
320impl Fired {
321    /// Nothing has fired yet.
322    #[must_use]
323    pub const fn new() -> Fired {
324        Fired { seen: Vec::new() }
325    }
326
327    /// Records that the rule at this index fired.
328    pub fn mark(&mut self, rule: usize) {
329        if self.seen.len() <= rule {
330            self.seen.resize(rule + 1, false);
331        }
332        self.seen[rule] = true;
333    }
334
335    /// Whether the rule at this index fired.
336    #[must_use]
337    pub fn has(&self, rule: usize) -> bool {
338        self.seen.get(rule).copied().unwrap_or(false)
339    }
340
341    /// How many rules fired.
342    #[must_use]
343    pub fn count(&self) -> usize {
344        self.seen.iter().filter(|fired| **fired).count()
345    }
346
347    /// Takes in everything another one recorded.
348    ///
349    /// One compilation is many functions and one command line is many files, and the question is
350    /// about all of them together. Merging rather than writing a file per function is also what
351    /// keeps the answer the same however the work was scheduled.
352    pub fn merge(&mut self, other: &Fired) {
353        if self.seen.len() < other.seen.len() {
354            self.seen.resize(other.seen.len(), false);
355        }
356        for (mine, theirs) in self.seen.iter_mut().zip(&other.seen) {
357            *mine |= *theirs;
358        }
359    }
360
361    /// What `-Zrule-coverage=FILE` writes.
362    ///
363    /// One line per rule in the table, in the order the rule file writes them, each saying whether
364    /// the rule fired and naming the file and line it is written at. Every rule is listed rather
365    /// than only the ones that fired, so that one of these files says what the whole rule set was
366    /// as well as what this compilation reached: a reader unioning them over a corpus needs both
367    /// and would otherwise have to parse the rule file to get the second.
368    ///
369    /// The first line is a comment holding the count, which is the number a person wants and the
370    /// one thing here that is not worth making them add up.
371    #[must_use]
372    pub fn listing(&self, table: &Table) -> String {
373        let fired = table.rules.iter().enumerate().filter(|(index, _)| self.has(*index)).count();
374        let mut out = format!(
375            "# rucc rule coverage: {fired} of {} rules in {} fired\n",
376            table.rules.len(),
377            table.source
378        );
379        for (index, rule) in table.rules.iter().enumerate() {
380            let word = if self.has(index) { "fired" } else { "unused" };
381            let _ = writeln!(out, "{word} {}:{} {}", table.source, rule.line, rule.pattern);
382        }
383        out
384    }
385}
386
387#[cfg(test)]
388mod tests {
389    use super::*;
390    use crate::select::x86_64::TABLE;
391
392    /// The claim the whole module is for, in the direction that matters: a name an instruction
393    /// can be called by is a name a rule is written at. This is the width check as much as the
394    /// opcode check, since a name is an opcode and a width together.
395    #[test]
396    fn every_name_an_instruction_can_have_is_one_a_rule_is_written_at() {
397        let report = report(&TABLE);
398        assert!(
399            report.uncovered.is_empty(),
400            "nothing in {} lowers these, and each is an opcode at a width the rule language can \
401             spell: {:?}",
402            report.source,
403            report.uncovered
404        );
405    }
406
407    /// And the other direction, which costs nothing to ask and finds a rule that can never fire.
408    /// A pattern head no instruction is ever called by is a rule written against a name that was
409    /// renamed or misspelled, and it would sit there proved and unreachable.
410    #[test]
411    fn every_name_a_rule_is_written_at_is_one_an_instruction_can_have() {
412        let report = report(&TABLE);
413        assert!(
414            report.unreachable.is_empty(),
415            "{} has rules for these and no instruction is ever called one: {:?}",
416            report.source,
417            report.unreachable
418        );
419    }
420
421    /// Every opcode is one of the three things, so a new opcode in the IR fails this until
422    /// somebody says where it goes. That is the whole point: the answer for a new opcode should
423    /// be written down when it is added rather than discovered by a user compiling a program.
424    #[test]
425    fn every_opcode_is_lowered_or_is_a_gap_somebody_wrote_down() {
426        let report = report(&TABLE);
427        assert!(
428            report.unaccounted.is_empty(),
429            "no rule lowers these, nothing rewrites them before selection, no runtime function \
430             stands for them and `GAPS` does not say why: {:?}",
431            report.unaccounted
432        );
433    }
434
435    /// An entry that starts being covered fails, which is the rule every list in this project is
436    /// kept under. An opcode a rule now lowers is one that should be off both lists, and a list
437    /// that keeps claiming otherwise is a list nobody can read.
438    #[test]
439    fn an_entry_a_rule_now_covers_is_a_stale_entry() {
440        let report = report(&TABLE);
441        for &(opcode, where_) in capability::HAND {
442            assert!(
443                !report.by_rule.contains(&opcode),
444                "`{}` is lowered by a rule now, so the `HAND` entry saying it is lowered by \
445                 {where_} is stale",
446                opcode.name()
447            );
448        }
449        for &(opcode, why, issue) in GAPS {
450            assert!(
451                !report.by_rule.contains(&opcode),
452                "`{}` is lowered by a rule now, so the `GAPS` entry saying it is {why} is stale \
453                 and {issue} may be closed",
454                opcode.name()
455            );
456            assert!(
457                !report.elsewhere.contains(&opcode),
458                "`{}` is on both lists, so it is both lowered and not lowered",
459                opcode.name()
460            );
461        }
462    }
463
464    /// The same staleness rule one list down. A name a rule is written at is a name that is not
465    /// left for later, and an entry claiming otherwise is one that should have gone when the rule
466    /// arrived. The other direction is checked too: a name no instruction can ever have is a
467    /// misspelling, and it would sit here excusing nothing.
468    #[test]
469    fn a_name_a_rule_is_written_at_is_not_a_name_left_for_later() {
470        let heads = pattern_heads(&TABLE);
471        let named = term::heads();
472        for &(name, why, issue) in NAMES {
473            assert!(
474                !heads.contains(&name),
475                "`{name}` is lowered by a rule now, so the `NAMES` entry saying it is {why} is \
476                 stale and {issue} may be closer than it says"
477            );
478            assert!(
479                named.iter().any(|&(_, head)| head == name),
480                "`{name}` is not a name any instruction can have, so the `NAMES` entry excuses \
481                 nothing"
482            );
483        }
484        let report = report(&TABLE);
485        assert_eq!(report.deferred.len(), NAMES.len(), "{:?}", report.deferred);
486    }
487
488    /// Every gap names an issue, since a gap with no issue behind it is a gap nobody has decided
489    /// anything about, which is the thing this module exists to stop.
490    #[test]
491    fn every_gap_names_the_issue_that_closes_it() {
492        let issues = GAPS
493            .iter()
494            .map(|&(_, _, issue)| issue)
495            .chain(WIDTHS.iter().map(|&(_, _, issue)| issue))
496            .chain(NAMES.iter().map(|&(_, _, issue)| issue));
497        for issue in issues {
498            let number = issue
499                .strip_prefix("tamnd/rucc#")
500                .unwrap_or_else(|| panic!("{issue} is not an issue in this project's tracker"));
501            assert!(number.parse::<u32>().is_ok(), "{issue} does not name an issue number");
502        }
503    }
504
505    /// The count, which `spec/15-testing.md` section 15.8 says we keep about ourselves. CI runs
506    /// this test with the output shown, so the number lands in a log next to the rule proof
507    /// rather than in a file somebody has to go and read.
508    #[test]
509    fn the_count_is_reported() {
510        let report = report(&TABLE);
511        println!("{report}");
512        for &(opcode, why, issue) in GAPS {
513            println!("rucc-codegen: no lowering for `{}`, which is {why}: {issue}", opcode.name());
514        }
515        for &(width, why, issue) in WIDTHS {
516            println!("rucc-codegen: no rule at {width}, which is {why}: {issue}");
517        }
518        for &(name, why, issue) in NAMES {
519            println!("rucc-codegen: no rule at `{name}`, which is {why}: {issue}");
520        }
521        assert_eq!(report.gaps.len(), GAPS.len());
522    }
523
524    /// What the root of the trie is, which is the assumption [`pattern_heads`] rests on. If the
525    /// rule compiler ever built the trie some other way this would say so, rather than the
526    /// coverage numbers quietly becoming a report about an empty list.
527    #[test]
528    fn the_root_of_the_trie_is_the_head_of_every_pattern() {
529        let heads = pattern_heads(&TABLE);
530        assert!(!heads.is_empty(), "the table has rules and the root of the trie tests nothing");
531        for rule in TABLE.rules {
532            let head = rule
533                .pattern
534                .strip_prefix('(')
535                .and_then(|rest| rest.split([' ', ')']).next())
536                .expect("a pattern is an application");
537            assert!(
538                heads.contains(&head),
539                "line {}: {} is a pattern whose head the root of the trie does not test",
540                rule.line,
541                rule.pattern
542            );
543        }
544    }
545
546    /// The two targets with a rule file, and the one still waiting for one. A machine that can be
547    /// compiled for has rules to report the coverage of, and one that cannot has none rather than
548    /// an empty set of them, which are different answers and would read the same as a number.
549    #[test]
550    fn a_target_with_a_back_end_is_a_target_with_a_rule_set() {
551        let x86 = table(Arch::X86_64).expect("x86-64 is what this crate lowers for");
552        assert_eq!(x86.source, TABLE.source);
553        assert!(!x86.rules.is_empty());
554        let arm = table(Arch::Aarch64).expect("aarch64 has a rule file");
555        assert_eq!(arm.source, crate::select::aarch64::TABLE.source);
556        assert!(!arm.rules.is_empty());
557        assert!(table(Arch::Riscv64).is_none(), "there is no riscv64 rule file yet");
558    }
559
560    /// What a rule is called outside this process. The index is not it: a rule added at the top of
561    /// the file moves every index below it, and a report from last week would then be a report
562    /// about the wrong rules. The file and the line do not move that way and are somewhere to look.
563    #[test]
564    fn a_rule_is_written_down_as_the_place_it_is_written_at() {
565        let mut fired = Fired::new();
566        fired.mark(0);
567        let listing = fired.listing(&TABLE);
568        let first =
569            format!("fired {}:{} {}", TABLE.source, TABLE.rules[0].line, TABLE.rules[0].pattern);
570        assert!(listing.contains(&first), "{listing}");
571        assert!(listing.lines().next().is_some_and(|line| line.starts_with('#')), "{listing}");
572    }
573
574    /// Every rule is listed and not only the ones that fired, which is what lets one of these files
575    /// be read on its own. A reader that only got the rules that fired would have to parse the rule
576    /// file to find out what the rest of them were.
577    #[test]
578    fn one_file_says_what_the_whole_rule_set_is() {
579        let listing = Fired::new().listing(&TABLE);
580        let lines: Vec<&str> = listing.lines().collect();
581        assert_eq!(lines.len(), TABLE.rules.len() + 1, "one line per rule and one for the count");
582        assert_eq!(
583            lines.iter().filter(|line| line.starts_with("unused ")).count(),
584            TABLE.rules.len()
585        );
586        assert!(lines[0].contains(&format!("0 of {} rules", TABLE.rules.len())), "{}", lines[0]);
587    }
588
589    /// A compilation is many functions and a command line is many files, and the question is about
590    /// all of them at once. Merging is also what keeps the answer the same however the work was
591    /// scheduled, which is the rule `spec/03-architecture.md` section 3.7 holds everything to.
592    #[test]
593    fn what_two_runs_reached_is_what_either_of_them_reached() {
594        let mut one = Fired::new();
595        one.mark(3);
596        one.mark(3);
597        assert_eq!(one.count(), 1, "a rule that fires twice is one rule");
598        let mut two = Fired::new();
599        two.mark(0);
600        two.mark(9);
601        one.merge(&two);
602        assert_eq!(one.count(), 3);
603        assert!(one.has(0) && one.has(3) && one.has(9));
604        assert!(!one.has(1));
605
606        // The merge is symmetric, since neither order of two files is the right one.
607        let mut back = Fired::new();
608        back.mark(0);
609        back.mark(9);
610        let mut three = Fired::new();
611        three.mark(3);
612        back.merge(&three);
613        assert_eq!(back, one);
614    }
615}