rustc_mir_transform/
cleanup_post_borrowck.rs

1//! This module provides a pass that removes parts of MIR that are no longer relevant after
2//! analysis phase and borrowck. In particular, it removes false edges, user type annotations and
3//! replaces following statements with [`Nop`]s:
4//!
5//!   - [`AscribeUserType`]
6//!   - [`FakeRead`]
7//!   - [`Assign`] statements with a [`Fake`] borrow
8//!   - [`Coverage`] statements of kind [`BlockMarker`] or [`SpanMarker`]
9//!
10//! [`AscribeUserType`]: rustc_middle::mir::StatementKind::AscribeUserType
11//! [`Assign`]: rustc_middle::mir::StatementKind::Assign
12//! [`FakeRead`]: rustc_middle::mir::StatementKind::FakeRead
13//! [`Nop`]: rustc_middle::mir::StatementKind::Nop
14//! [`Fake`]: rustc_middle::mir::BorrowKind::Fake
15//! [`Coverage`]: rustc_middle::mir::StatementKind::Coverage
16//! [`BlockMarker`]: rustc_middle::mir::coverage::CoverageKind::BlockMarker
17//! [`SpanMarker`]: rustc_middle::mir::coverage::CoverageKind::SpanMarker
18
19use rustc_middle::mir::coverage::CoverageKind;
20use rustc_middle::mir::*;
21use rustc_middle::ty::TyCtxt;
22use rustc_middle::ty::adjustment::PointerCoercion;
23
24pub(super) struct CleanupPostBorrowck;
25
26impl<'tcx> crate::MirPass<'tcx> for CleanupPostBorrowck {
27    fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
28        // Manually invalidate CFG caches if we actually change a terminator's edges.
29        let mut invalidate_cfg = false;
30        for basic_block in body.basic_blocks.as_mut_preserves_cfg().iter_mut() {
31            for statement in basic_block.statements.iter_mut() {
32                match statement.kind {
33                    StatementKind::AscribeUserType(..)
34                    | StatementKind::Assign(box (_, Rvalue::Ref(_, BorrowKind::Fake(_), _)))
35                    | StatementKind::Coverage(
36                        // These kinds of coverage statements are markers inserted during
37                        // MIR building, and are not needed after InstrumentCoverage.
38                        CoverageKind::BlockMarker { .. } | CoverageKind::SpanMarker { .. },
39                    )
40                    | StatementKind::FakeRead(..)
41                    | StatementKind::BackwardIncompatibleDropHint { .. } => {
42                        statement.make_nop(true)
43                    }
44                    StatementKind::Assign(box (
45                        _,
46                        Rvalue::Cast(
47                            ref mut cast_kind @ CastKind::PointerCoercion(
48                                PointerCoercion::ArrayToPointer
49                                | PointerCoercion::MutToConstPointer,
50                                _,
51                            ),
52                            ..,
53                        ),
54                    )) => {
55                        // BorrowCk needed to track whether these cases were coercions or casts,
56                        // to know whether to check lifetimes in their pointees,
57                        // but from now on that distinction doesn't matter,
58                        // so just make them ordinary pointer casts instead.
59                        *cast_kind = CastKind::PtrToPtr;
60                    }
61                    _ => (),
62                }
63            }
64
65            // If we change any terminator, we need to ensure that we invalidated the CFG cache.
66            let terminator = basic_block.terminator_mut();
67            match terminator.kind {
68                TerminatorKind::FalseEdge { real_target, .. }
69                | TerminatorKind::FalseUnwind { real_target, .. } => {
70                    invalidate_cfg = true;
71                    terminator.kind = TerminatorKind::Goto { target: real_target };
72                }
73                _ => {}
74            }
75        }
76
77        if invalidate_cfg {
78            body.basic_blocks.invalidate_cfg_cache();
79        }
80
81        body.user_type_annotations.raw.clear();
82
83        for decl in &mut body.local_decls {
84            decl.user_ty = None;
85        }
86    }
87
88    fn is_required(&self) -> bool {
89        true
90    }
91}