1
//! A whole-state digest of a `SymbolTable`, for rollback tests. Every field
2
//! is destructured, so a new field fails to compile until it is covered.
3

            
4
use std::collections::HashMap;
5
use std::fmt::Debug;
6
use std::fmt::Write as _;
7

            
8
use super::table::SymbolTable;
9

            
10
1572
fn map<K: Debug, V: Debug>(out: &mut String, name: &str, value: &HashMap<K, V>) {
11
44519
    let mut entries: Vec<String> = value.iter().map(|entry| format!("{entry:?}")).collect();
12
1572
    entries.sort();
13
1572
    let _ = writeln!(out, "{name}: {entries:?}");
14
1572
}
15

            
16
impl SymbolTable {
17
262
    pub(crate) fn state_digest(&self) -> String {
18
        let SymbolTable {
19
            symbols,
20
            structs,
21
            instances,
22
            tests,
23
            native_coverage,
24
            host_names,
25
            special_names,
26
            created_globals,
27
            unit_slots,
28
            unit_closures,
29
            unit_pre,
30
            unit_versions,
31
            inline_stack,
32
            closure_results,
33
            closure_params,
34
            closure_bodies,
35
            in_macro_expansion,
36
            runtime_value_seen,
37
            // A monotonic allocator is not rolled back, like `budget`.
38
            last_definition_id: _,
39
            redefinition_epoch,
40
            runtime_guard_depth,
41
            shared_recursion_depth,
42
            expansion_depths,
43
            budget,
44
            stored_globals,
45
            lost_homes,
46
            unit_specials,
47
            dynamic_specials,
48
            skippable_depth,
49
            exit_regions,
50
            function_owner,
51
            scopes,
52
            macro_locals,
53
            shadowed_globals,
54
            eval_blocks,
55
            loop_blocks,
56
            unroll_share,
57
            dead_code,
58
            homes: _,
59
            in_init_trial: _,
60
262
        } = self;
61
262
        let mut out = String::new();
62
262
        map(&mut out, "symbols", symbols);
63
262
        let _ = writeln!(out, "structs: {structs:?}");
64
262
        let _ = writeln!(out, "instances: {instances:?}");
65
262
        map(&mut out, "shadowed_globals", shadowed_globals);
66
262
        let _ = writeln!(out, "unit_slots: {unit_slots:?}");
67
262
        let _ = writeln!(out, "unit_closures: {unit_closures:?}");
68
262
        let _ = writeln!(out, "unit_pre: {unit_pre:?}");
69
262
        let _ = writeln!(out, "unit_versions: {unit_versions:?}");
70
262
        map(&mut out, "native_coverage", native_coverage);
71
262
        map(&mut out, "closure_results", closure_results);
72
262
        map(&mut out, "closure_params", closure_params);
73
262
        map(&mut out, "closure_bodies", closure_bodies);
74
262
        let mut hosts: Vec<&String> = host_names.iter().collect();
75
262
        hosts.sort();
76
262
        let _ = writeln!(out, "host_names: {hosts:?}");
77
262
        let mut specials: Vec<&String> = special_names.iter().collect();
78
262
        specials.sort();
79
262
        let _ = writeln!(out, "special_names: {specials:?}");
80
262
        let mut created: Vec<&String> = created_globals.iter().collect();
81
262
        created.sort();
82
262
        let _ = writeln!(out, "created_globals: {created:?}");
83
262
        let _ = writeln!(out, "tests: {tests:?}");
84
262
        let _ = writeln!(out, "inline_stack: {inline_stack:?}");
85
262
        let _ = writeln!(
86
262
            out,
87
262
            "depths: {in_macro_expansion} {} {runtime_guard_depth} {shared_recursion_depth} {expansion_depths:?}",
88
262
            runtime_value_seen.load(std::sync::atomic::Ordering::Relaxed)
89
        );
90
262
        let _ = writeln!(out, "charged: {}", budget.charged());
91
262
        let _ = writeln!(
92
262
            out,
93
            "epoch: {}",
94
262
            redefinition_epoch.load(std::sync::atomic::Ordering::Relaxed)
95
        );
96
262
        let mut stored: Vec<&String> = stored_globals.iter().collect();
97
262
        stored.sort();
98
262
        let _ = writeln!(out, "stored_globals: {stored:?}");
99
262
        let _ = writeln!(out, "lost_homes: {lost_homes:?}");
100
262
        let mut unit: Vec<&String> = unit_specials.iter().collect();
101
262
        unit.sort();
102
262
        let _ = writeln!(out, "unit_specials: {unit:?}");
103
262
        let mut dynamic: Vec<&String> = dynamic_specials.iter().collect();
104
262
        dynamic.sort();
105
262
        let _ = writeln!(out, "dynamic_specials: {dynamic:?}");
106
262
        let _ = writeln!(out, "skippable_depth: {skippable_depth} {exit_regions:?}");
107
262
        let _ = writeln!(out, "function_owner: {function_owner:?}");
108
262
        let _ = writeln!(out, "scopes: {scopes:?}");
109
262
        let mut locals: Vec<&String> = macro_locals.iter().collect();
110
262
        locals.sort();
111
262
        let _ = writeln!(out, "macro_locals: {locals:?}");
112
262
        let _ = writeln!(out, "homes: {:?}", self.home_book());
113
262
        let _ = writeln!(out, "eval_blocks: {} {dead_code}", eval_blocks.len());
114
262
        let _ = writeln!(out, "loop_blocks: {} {unroll_share:?}", loop_blocks.len());
115
262
        out
116
262
    }
117
}