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.elem_count() == 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 {
29 if name.equals_ref_name(builtins::EXPLICIT_PANIC_NAME) {
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
38 let panic_name = Name::from_path(builtins::EXPLICIT_PANIC_NAME);
39 let panic_terminator = TerminatorKind::Abort(AbortKind::Panic(Some(panic_name)));
40
41 // Replace each call to one such function with a `Panic`.
42 ctx.for_each_fun_decl(|_ctx, decl| {
43 if let Some(body) = decl.body.as_unstructured_mut() {
44 for block_id in body.body.all_indices() {
45 let Some(block) = body.body.get_mut(block_id) else {
46 continue;
47 };
48 if let TerminatorKind::Call {
49 call:
50 Call {
51 func:
52 FnOperand::Regular(FnPtr {
53 kind: box FnPtrKind::Fun(FunId::Regular(fun_id)),
54 ..
55 }),
56 ..
57 },
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}