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(..)
39                    | StatementKind::BackwardIncompatibleDropHint { .. } => statement.make_nop(),
40                    StatementKind::Assign(box (
41                        _,
42                        Rvalue::Cast(
43                            ref mut cast_kind @ CastKind::PointerCoercion(
44                                PointerCoercion::ArrayToPointer
45                                | PointerCoercion::MutToConstPointer,
46                                _,
47                            ),
48                            ..,
49                        ),
50                    )) => {
51                        // BorrowCk needed to track whether these cases were coercions or casts,
52                        // to know whether to check lifetimes in their pointees,
53                        // but from now on that distinction doesn't matter,
54                        // so just make them ordinary pointer casts instead.
55                        *cast_kind = CastKind::PtrToPtr;
56                    }
57                    _ => (),
58                }
59            }
60            let terminator = basic_block.terminator_mut();
61            match terminator.kind {
62                TerminatorKind::FalseEdge { real_target, .. }
63                | TerminatorKind::FalseUnwind { real_target, .. } => {
64                    terminator.kind = TerminatorKind::Goto { target: real_target };
65                }
66                _ => {}
67            }
68        }
69
70        body.user_type_annotations.raw.clear();
71
72        for decl in &mut body.local_decls {
73            decl.user_ty = None;
74        }
75    }
76
77    fn is_required(&self) -> bool {
78        true
79    }
80}