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::{Body, BorrowKind, CastKind, Rvalue, StatementKind, TerminatorKind};
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        for basic_block in body.basic_blocks.as_mut() {
29            for statement in basic_block.statements.iter_mut() {
30                match statement.kind {
31                    StatementKind::AscribeUserType(..)
32                    | StatementKind::Assign(box (_, Rvalue::Ref(_, BorrowKind::Fake(_), _)))
33                    | StatementKind::Coverage(
34                        // These kinds of coverage statements are markers inserted during
35                        // MIR building, and are not needed after InstrumentCoverage.
36                        CoverageKind::BlockMarker { .. } | CoverageKind::SpanMarker { .. },
37                    )
38                    | StatementKind::FakeRead(..) => statement.make_nop(),
39                    StatementKind::Assign(box (
40                        _,
41                        Rvalue::Cast(
42                            ref mut cast_kind @ CastKind::PointerCoercion(
43                                PointerCoercion::ArrayToPointer
44                                | PointerCoercion::MutToConstPointer,
45                                _,
46                            ),
47                            ..,
48                        ),
49                    )) => {
50                        // BorrowCk needed to track whether these cases were coercions or casts,
51                        // to know whether to check lifetimes in their pointees,
52                        // but from now on that distinction doesn't matter,
53                        // so just make them ordinary pointer casts instead.
54                        *cast_kind = CastKind::PtrToPtr;
55                    }
56                    _ => (),
57                }
58            }
59            let terminator = basic_block.terminator_mut();
60            match terminator.kind {
61                TerminatorKind::FalseEdge { real_target, .. }
62                | TerminatorKind::FalseUnwind { real_target, .. } => {
63                    terminator.kind = TerminatorKind::Goto { target: real_target };
64                }
65                _ => {}
66            }
67        }
68
69        body.user_type_annotations.raw.clear();
70
71        for decl in &mut body.local_decls {
72            decl.user_ty = None;
73        }
74    }
75
76    fn is_required(&self) -> bool {
77        true
78    }
79}