Skip to main content

charon_lib/transform/resugar/
inline_local_panic_functions.rs

1//! `panic!()` expands to:
2//! ```ignore
3//! fn panic_cold_explicit() -> ! {
4//!     core::panicking::panic_explicit()
5//! }
6//! panic_cold_explicit()
7//! ```
8//! Which defines a new function each time. This pass recognizes these functions and replaces calls
9//! to them by a `Panic` terminator.
10use std::collections::HashSet;
11
12use crate::transform::{TransformCtx, ctx::UllbcPass};
13use crate::{builtins, names::Name, ullbc_ast::*};
14
15pub struct Transform;
16impl UllbcPass for Transform {
17    fn transform_ctx(&self, ctx: &mut TransformCtx) {
18        // Collect the functions that were generated by the `panic!` macro.
19        let mut panic_fns = HashSet::new();
20        ctx.for_each_fun_decl(|_ctx, decl| {
21            if let Some(body) = decl.body.as_unstructured() {
22                // If the whole body is only a call to this specific panic function.
23                if body.body.len() == 1
24                    && let Some(block) = body.body.iter().next()
25                    && block.statements.is_empty()
26                    && let TerminatorKind::Abort(AbortKind::Panic(Some(name))) =
27                        &block.terminator.kind
28                    && name.equals_ref_name(builtins::EXPLICIT_PANIC_NAME)
29                {
30                    // FIXME: also check that the name of the function is
31                    // `panic_cold_explicit`?
32                    panic_fns.insert(decl.def_id);
33                }
34            }
35        });
36
37        let panic_name = Name::from_path(builtins::EXPLICIT_PANIC_NAME);
38        let panic_terminator = TerminatorKind::Abort(AbortKind::Panic(Some(panic_name)));
39
40        // Replace each call to one such function with a `Panic`.
41        ctx.for_each_fun_decl(|_ctx, decl| {
42            if let Some(body) = decl.body.as_unstructured_mut() {
43                for block_id in body.body.indices() {
44                    let Some(block) = body.body.get_mut(block_id) else {
45                        continue;
46                    };
47                    if let TerminatorKind::Call {
48                        call:
49                            Call {
50                                func:
51                                    FnOperand::Regular(FnPtr {
52                                        kind: box FnPtrKind::Fun(FunId::Regular(fun_id)),
53                                        ..
54                                    }),
55                                ..
56                            },
57                        on_unwind: _, // TODO: shouldn't we use this?
58                        ..
59                    } = &block.terminator.kind
60                        && panic_fns.contains(fun_id)
61                    {
62                        block.terminator.kind = panic_terminator.clone();
63                    }
64                }
65            }
66        });
67
68        // Remove these functions from the context.
69        for id in &panic_fns {
70            ctx.translated.fun_decls.remove(*id);
71        }
72    }
73}