rustc_mir_transform/
deref_separator.rs

1use rustc_middle::mir::visit::NonUseContext::VarDebugInfo;
2use rustc_middle::mir::visit::{MutVisitor, PlaceContext};
3use rustc_middle::mir::*;
4use rustc_middle::ty::TyCtxt;
5
6use crate::patch::MirPatch;
7
8pub(super) struct Derefer;
9
10struct DerefChecker<'a, 'tcx> {
11    tcx: TyCtxt<'tcx>,
12    patcher: MirPatch<'tcx>,
13    local_decls: &'a LocalDecls<'tcx>,
14    add_deref_metadata: bool,
15}
16
17impl<'a, 'tcx> MutVisitor<'tcx> for DerefChecker<'a, 'tcx> {
18    fn tcx(&self) -> TyCtxt<'tcx> {
19        self.tcx
20    }
21
22    fn visit_place(&mut self, place: &mut Place<'tcx>, cntxt: PlaceContext, loc: Location) {
23        if !place.projection.is_empty()
24            && cntxt != PlaceContext::NonUse(VarDebugInfo)
25            && place.projection[1..].contains(&ProjectionElem::Deref)
26        {
27            let mut place_local = place.local;
28            let mut last_len = 0;
29            let mut last_deref_idx = 0;
30
31            for (idx, elem) in place.projection[0..].iter().enumerate() {
32                if *elem == ProjectionElem::Deref {
33                    last_deref_idx = idx;
34                }
35            }
36
37            for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
38                if !p_ref.projection.is_empty() && p_elem == ProjectionElem::Deref {
39                    let ty = p_ref.ty(self.local_decls, self.tcx).ty;
40                    let temp = self.patcher.new_local_with_info(
41                        ty,
42                        self.local_decls[p_ref.local].source_info.span,
43                        if self.add_deref_metadata {
44                            LocalInfo::DerefTemp
45                        } else {
46                            LocalInfo::Boring
47                        },
48                    );
49
50                    // We are adding current p_ref's projections to our
51                    // temp value, excluding projections we already covered.
52                    let deref_place = Place::from(place_local)
53                        .project_deeper(&p_ref.projection[last_len..], self.tcx);
54
55                    self.patcher.add_assign(
56                        loc,
57                        Place::from(temp),
58                        if self.add_deref_metadata {
59                            Rvalue::CopyForDeref(deref_place)
60                        } else {
61                            Rvalue::Use(Operand::Copy(deref_place))
62                        },
63                    );
64                    place_local = temp;
65                    last_len = p_ref.projection.len();
66
67                    // Change `Place` only if we are actually at the Place's last deref
68                    if idx == last_deref_idx {
69                        let temp_place =
70                            Place::from(temp).project_deeper(&place.projection[idx..], self.tcx);
71                        *place = temp_place;
72                    }
73                }
74            }
75        }
76    }
77}
78
79pub(super) fn deref_finder<'tcx>(
80    tcx: TyCtxt<'tcx>,
81    body: &mut Body<'tcx>,
82    add_deref_metadata: bool,
83) {
84    let patch = MirPatch::new(body);
85    let mut checker =
86        DerefChecker { tcx, patcher: patch, local_decls: &body.local_decls, add_deref_metadata };
87
88    for (bb, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
89        checker.visit_basic_block_data(bb, data);
90    }
91
92    checker.patcher.apply(body);
93}
94
95impl<'tcx> crate::MirPass<'tcx> for Derefer {
96    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
97        deref_finder(tcx, body, true);
98    }
99
100    fn is_required(&self) -> bool {
101        true
102    }
103}