Skip to main content

backhopper_core/model/
resolver_coverage.rs

1// Copyright (C) 2026 Michael S. Klishin and Contributors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// See LICENSE-APACHE and LICENSE-MIT for details.
4
5//! Which symbol classes the analyzer resolves, and the classifier from a
6//! symbol to its class.
7//!
8//! A symbol-presence taxonomy across mechanisms: macro, record, local
9//! call, include, and behaviour resolve on the target tree; qualified
10//! call and type through the snapshot. The fold routes a break by class
11//! against the producing row's coverage: a checked class is a resolver
12//! bug, an unchecked one a coverage gap.
13
14use std::collections::BTreeSet;
15
16use serde::{Deserialize, Serialize};
17
18use crate::model::symbol::SymbolKind;
19
20/// A class of symbol whose presence a backport can dangle on.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
22#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
23#[serde(rename_all = "snake_case")]
24pub enum ResolverClass {
25    Macro,
26    Include,
27    Behaviour,
28    QualifiedCall,
29    Record,
30    LocalCall,
31    Type,
32    IndirectCall,
33}
34
35impl ResolverClass {
36    /// Stable label matching the serde spelling, for text roll-ups.
37    #[must_use]
38    pub fn as_str(self) -> &'static str {
39        match self {
40            ResolverClass::Macro => "macro",
41            ResolverClass::Include => "include",
42            ResolverClass::Behaviour => "behaviour",
43            ResolverClass::QualifiedCall => "qualified_call",
44            ResolverClass::Record => "record",
45            ResolverClass::LocalCall => "local_call",
46            ResolverClass::Type => "type",
47            ResolverClass::IndirectCall => "indirect_call",
48        }
49    }
50
51    pub const ALL: [ResolverClass; 8] = [
52        ResolverClass::Macro,
53        ResolverClass::Include,
54        ResolverClass::Behaviour,
55        ResolverClass::QualifiedCall,
56        ResolverClass::Record,
57        ResolverClass::LocalCall,
58        ResolverClass::Type,
59        ResolverClass::IndirectCall,
60    ];
61
62    /// Whether the analyzer resolves this class. Macro, include,
63    /// behaviour, record, local call, and indirect call resolve on the
64    /// target tree; qualified call and type resolve through the
65    /// snapshot and self-pin.
66    // exhaustive so a newly added class fails to compile here rather than silently return true
67    #[must_use]
68    pub fn is_covered(self) -> bool {
69        match self {
70            ResolverClass::Macro
71            | ResolverClass::Include
72            | ResolverClass::Behaviour
73            | ResolverClass::QualifiedCall
74            | ResolverClass::Record
75            | ResolverClass::LocalCall
76            | ResolverClass::Type
77            | ResolverClass::IndirectCall => true,
78        }
79    }
80
81    /// The class a symbol falls in. One class per kind, so the producer
82    /// and the consumer agree without a hand-copied table: a function is
83    /// a qualified call, a type its own class, a callback its behaviour.
84    #[must_use]
85    pub fn of_symbol_kind(kind: &SymbolKind) -> ResolverClass {
86        match kind {
87            SymbolKind::Macro { .. } => ResolverClass::Macro,
88            SymbolKind::Record { .. } => ResolverClass::Record,
89            SymbolKind::Behaviour { .. } | SymbolKind::Callback { .. } => ResolverClass::Behaviour,
90            SymbolKind::Type { .. } => ResolverClass::Type,
91            SymbolKind::Function { .. } | SymbolKind::FunctionAnyArity { .. } => {
92                ResolverClass::QualifiedCall
93            }
94        }
95    }
96}
97
98/// The set of classes a producer checks, stamped on a verdict envelope
99/// so a corpus row self-describes its producer's coverage. Stores only
100/// the checked set: `unchecked` is derived, so a class can never be in
101/// both or neither.
102#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
103#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
104pub struct ResolverCoverage {
105    checked: BTreeSet<ResolverClass>,
106}
107
108impl ResolverCoverage {
109    /// Coverage of the running binary.
110    #[must_use]
111    pub fn current() -> Self {
112        Self {
113            checked: ResolverClass::ALL
114                .into_iter()
115                .filter(|c| c.is_covered())
116                .collect(),
117        }
118    }
119
120    /// Whether this producer checked `class`: the fold's bug-vs-gap test.
121    #[must_use]
122    pub fn is_checked(&self, class: ResolverClass) -> bool {
123        self.checked.contains(&class)
124    }
125
126    /// Classes this producer did not check: the calibration sampler steers
127    /// toward candidates touching one.
128    pub fn unchecked(&self) -> impl Iterator<Item = ResolverClass> + '_ {
129        ResolverClass::ALL
130            .into_iter()
131            .filter(|c| !self.checked.contains(c))
132    }
133}