Skip to main content

charon_lib/transform/resugar/
resugar_drops.rs

1//! Reconstruct conditional drops using the drop flags identified by `detect_drop_flags`.
2use crate::options::TranslateOptions;
3use crate::transform::TransformCtx;
4use crate::transform::ctx::UllbcPass;
5use crate::ullbc_ast::*;
6
7struct ConditionalDrop {
8    /// The block that switched on the drop flag.
9    switch_block: BlockId,
10    /// The block that contains the unconditional drop.
11    drop_block: BlockId,
12}
13
14#[derive(Default)]
15struct FlagUses {
16    assignments: Vec<StmtLoc>,
17    if_count: usize,
18    resugared_if_count: usize,
19}
20
21pub struct Transform;
22impl UllbcPass for Transform {
23    fn should_run(&self, options: &TranslateOptions) -> bool {
24        options.resugar_drops
25    }
26
27    fn transform_body(&self, _ctx: &mut TransformCtx, body: &mut ExprBody) {
28        let mut predecessor_counts = body.body.map_ref(|_| 0usize);
29        for block in &body.body {
30            for target in block.terminator.targets() {
31                predecessor_counts[target] += 1;
32            }
33        }
34
35        let mut flag_uses = body.locals.locals.map_ref(|_| FlagUses::default());
36        let mut conditional_drops = Vec::new();
37
38        for (block_id, block) in body.body.iter_enumerated() {
39            for (statement_id, statement) in block.statements.iter().enumerate() {
40                if let StatementKind::Assign(place, _) = &statement.kind
41                    && let Some(flag) = place.as_local()
42                    && body.locals.locals[flag].drop_flag_for.is_some()
43                {
44                    flag_uses[flag]
45                        .assignments
46                        .push(StmtLoc::new(block_id, statement_id));
47                }
48            }
49
50            if let TerminatorKind::Switch { data, branches } = &block.terminator.kind
51                && let SwitchScrutinee::Value(
52                    Operand::Copy(flag_place, _) | Operand::Move(flag_place),
53                ) = &data.scrutinee
54                && let Some(flag) = flag_place.as_local()
55                && let Some(flagged_place) = &body.locals.locals[flag].drop_flag_for
56                && let Some((true_branch, false_branch)) = data.as_if()
57            {
58                flag_uses[flag].if_count += 1;
59                let true_target = branches[true_branch];
60                let false_target = branches[false_branch];
61                let drop_block = &body.body[true_target];
62
63                // Detect a simple pattern: `if drop_flag { unconditional_drop } else {}`.
64                if true_target != false_target
65                    && predecessor_counts[true_target] == 1
66                    && let TerminatorKind::Drop {
67                        kind: DropKind::Precise,
68                        place,
69                        target,
70                        ..
71                    } = &drop_block.terminator.kind
72                    && place == flagged_place
73                    && drop_block.statements.is_empty()
74                    && *target == false_target
75                {
76                    flag_uses[flag].resugared_if_count += 1;
77                    conditional_drops.push(ConditionalDrop {
78                        switch_block: block_id,
79                        drop_block: true_target,
80                    });
81                }
82            }
83        }
84
85        for drop in conditional_drops {
86            let mut terminator = body.body[drop.drop_block].terminator.kind.take();
87            let TerminatorKind::Drop { kind, .. } = &mut terminator else {
88                unreachable!()
89            };
90            *kind = DropKind::Conditional;
91            body.body[drop.switch_block].terminator.kind = terminator;
92        }
93
94        // If every use of a flag has been resugared, remove its assignments. The unused-locals
95        // pass will then remove the local entirely.
96        for uses in flag_uses {
97            if uses.if_count == uses.resugared_if_count {
98                for loc in uses.assignments {
99                    body[loc].kind = StatementKind::Nop;
100                }
101            }
102        }
103    }
104}