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