Skip to main content

rapx/vm/
diagnostics.rs

1//! Path-scoped diagnostic evidence and its current MIR source location.
2
3use crate::compat::FxHashSet;
4use rustc_hir::def_id::DefId;
5use rustc_middle::ty::TyCtxt;
6use rustc_span::Span;
7
8/// Source location of the MIR item currently being replayed.
9#[derive(Clone, Copy)]
10pub(crate) struct ActiveMirLocation {
11    pub def_id: DefId,
12    pub block: usize,
13    pub item: ActiveMirItem,
14    pub span: Span,
15}
16
17#[derive(Clone, Copy)]
18pub(crate) enum ActiveMirItem {
19    Statement(usize),
20    Terminator,
21}
22
23impl ActiveMirLocation {
24    fn label(&self, tcx: TyCtxt<'_>) -> String {
25        let function = tcx.def_path_str(self.def_id);
26        match self.item {
27            ActiveMirItem::Statement(index) => {
28                format!("{function} bb{} statement {index}", self.block)
29            }
30            ActiveMirItem::Terminator => format!("{function} bb{} terminator", self.block),
31        }
32    }
33}
34
35/// Diagnostic evidence collected while replaying one path.
36#[derive(Default, Clone)]
37pub(crate) struct Diagnostics {
38    /// Current MIR item used to locate new diagnostics and memory events.
39    pub(crate) active_mir: Option<ActiveMirLocation>,
40    unsupported_mir: bool,
41    unattributed_unsupported: bool,
42    unsupported_details: FxHashSet<String>,
43    unsupported_effects: FxHashSet<String>,
44    missing_mir: FxHashSet<String>,
45    opaque_control_flow: FxHashSet<String>,
46    incomplete_inline_branch: bool,
47}
48
49impl Diagnostics {
50    pub(crate) fn location_label(&self, tcx: TyCtxt<'_>) -> Option<String> {
51        self.active_mir.as_ref().map(|location| location.label(tcx))
52    }
53
54    pub(crate) fn is_unsupported(&self) -> bool {
55        self.unsupported_mir
56    }
57
58    pub(crate) fn mark_unsupported(&mut self) {
59        self.unsupported_mir = true;
60        self.unattributed_unsupported = true;
61    }
62
63    pub(crate) fn mark_unsupported_if(&mut self, condition: bool) {
64        if condition {
65            self.mark_unsupported();
66        }
67    }
68
69    pub(crate) fn has_incomplete_inline_branch(&self) -> bool {
70        self.incomplete_inline_branch
71    }
72
73    pub(crate) fn unsupported_details(&self) -> &FxHashSet<String> {
74        &self.unsupported_details
75    }
76
77    pub(crate) fn unsupported_effects(&self) -> &FxHashSet<String> {
78        &self.unsupported_effects
79    }
80
81    pub(crate) fn missing_mir(&self) -> &FxHashSet<String> {
82        &self.missing_mir
83    }
84
85    pub(crate) fn opaque_control_flow(&self) -> &FxHashSet<String> {
86        &self.opaque_control_flow
87    }
88
89    pub(crate) fn record_unsupported(&mut self, location: Option<&str>, operation: &str) {
90        self.unsupported_mir = true;
91        self.unsupported_details
92            .insert(format!("{}: {operation}", location.unwrap_or("unknown location")));
93    }
94
95    pub(crate) fn record_unsupported_effect(&mut self, location: Option<&str>, effect: &str) {
96        self.unsupported_mir = true;
97        self.unsupported_effects
98            .insert(format!("{}: {effect}", location.unwrap_or("unknown location")));
99    }
100
101    pub(crate) fn record_missing_mir(&mut self, location: Option<&str>, callee: &str) {
102        self.unsupported_mir = true;
103        self.missing_mir.insert(format!(
104            "{callee} at {}",
105            location.unwrap_or("unknown location")
106        ));
107    }
108
109    pub(crate) fn record_opaque_control_flow(&mut self, location: Option<&str>, operation: &str) {
110        self.unsupported_mir = true;
111        self.opaque_control_flow.insert(format!(
112            "{}: opaque call result controls {operation}",
113            location.unwrap_or("unknown location")
114        ));
115    }
116
117    pub(crate) fn only_copy_memory_target_unavailable_since(&self, before: &Self) -> bool {
118        self.unsupported_mir
119            && !before.unsupported_mir
120            && !self.unattributed_unsupported
121            && self.unsupported_effects.len() > before.unsupported_effects.len()
122            && self
123                .unsupported_effects
124                .difference(&before.unsupported_effects)
125                .all(|effect| effect.contains("CopyMemory target unavailable"))
126            && self.unsupported_details == before.unsupported_details
127            && self.missing_mir == before.missing_mir
128            && self.opaque_control_flow == before.opaque_control_flow
129    }
130
131    pub(crate) fn recover_copy_memory_target(&mut self, before: &Self) {
132        if self.only_copy_memory_target_unavailable_since(before) {
133            self.unsupported_effects = before.unsupported_effects.clone();
134            self.unsupported_mir = before.unsupported_mir;
135        }
136    }
137
138    pub(crate) fn only_missing_mir_since(&self, before: &Self) -> bool {
139        self.unsupported_mir
140            && !before.unsupported_mir
141            && !self.unattributed_unsupported
142            && self.missing_mir.len() > before.missing_mir.len()
143            && self.unsupported_details == before.unsupported_details
144            && self.unsupported_effects == before.unsupported_effects
145            && self.opaque_control_flow == before.opaque_control_flow
146    }
147
148    pub(crate) fn recover_missing_mir(&mut self, before: &Self) {
149        if self.only_missing_mir_since(before) {
150            self.missing_mir = before.missing_mir.clone();
151            self.unsupported_mir = before.unsupported_mir;
152        }
153    }
154
155    pub(crate) fn needs_statement_fallback(&self, was_unsupported: bool) -> bool {
156        !was_unsupported
157            && self.unsupported_mir
158            && self.unsupported_details.is_empty()
159            && self.unsupported_effects.is_empty()
160    }
161
162    pub(crate) fn needs_terminator_fallback(
163        &self,
164        was_unsupported: bool,
165        previous_missing_mir: usize,
166    ) -> bool {
167        self.needs_statement_fallback(was_unsupported)
168            && self.missing_mir.len() == previous_missing_mir
169            && self.opaque_control_flow.is_empty()
170    }
171
172    pub(crate) fn missing_mir_count(&self) -> usize {
173        self.missing_mir.len()
174    }
175
176    pub(crate) fn merge_inline_fallback(&mut self, explored: &Self) {
177        self.missing_mir.extend(explored.missing_mir.iter().cloned());
178        self.incomplete_inline_branch = true;
179    }
180}