rustc_mir_transform/erase_deref_temps.rs
1//! This pass converts all `DerefTemp` locals into normal temporaries
2//! and turns their `CopyForDeref` rvalues into normal copies.
3
4use rustc_middle::mir::visit::MutVisitor;
5use rustc_middle::mir::*;
6use rustc_middle::ty::TyCtxt;
7
8struct EraseDerefTempsVisitor<'tcx> {
9 tcx: TyCtxt<'tcx>,
10}
11
12impl<'tcx> MutVisitor<'tcx> for EraseDerefTempsVisitor<'tcx> {
13 fn tcx(&self) -> TyCtxt<'tcx> {
14 self.tcx
15 }
16
17 fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, _: Location) {
18 if let &mut Rvalue::CopyForDeref(place) = rvalue {
19 // We do *NOT* want a retag here! This assignment might copy a mutable reference we
20 // can't actually copy, we just need it temporarily to create another pointer.
21 *rvalue = Rvalue::Use(Operand::Copy(place), WithRetag::No)
22 }
23 }
24
25 fn visit_local_decl(&mut self, _: Local, local_decl: &mut LocalDecl<'tcx>) {
26 if local_decl.is_deref_temp() {
27 let info = local_decl.local_info.as_mut().unwrap_crate_local();
28 **info = LocalInfo::Boring;
29 }
30 }
31}
32
33pub(super) struct EraseDerefTemps;
34
35impl<'tcx> crate::MirPass<'tcx> for EraseDerefTemps {
36 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
37 EraseDerefTempsVisitor { tcx }.visit_body_preserves_cfg(body);
38 }
39
40 fn is_required(&self) -> bool {
41 true
42 }
43}