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
24use crate::PassPolicy;
25
26pub(super) struct CleanupPostBorrowck;
27
28impl<'tcx> crate::MirPass<'tcx> for CleanupPostBorrowck {
29 fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
30 // Manually invalidate CFG caches if we actually change a terminator's edges.
31 let mut invalidate_cfg = false;
32 for basic_block in body.basic_blocks.as_mut_preserves_cfg().iter_mut() {
33 for statement in basic_block.statements.iter_mut() {
34 match statement.kind {
35 StatementKind::AscribeUserType(..)
36 | StatementKind::Assign((_, Rvalue::Ref(_, BorrowKind::Fake(_), _)))
37 | StatementKind::Coverage(
38 // These kinds of coverage statements are markers inserted during
39 // MIR building, and are not needed after InstrumentCoverage.
40 CoverageKind::BlockMarker { .. } | CoverageKind::SpanMarker { .. },
41 )
42 | StatementKind::FakeRead(..)
43 | StatementKind::BackwardIncompatibleDropHint { .. } => {
44 statement.make_nop(true)
45 }
46 StatementKind::Assign((
47 _,
48 Rvalue::Cast(
49 ref mut cast_kind @ CastKind::PointerCoercion(
50 PointerCoercion::ArrayToPointer
51 | PointerCoercion::MutToConstPointer,
52 _,
53 ),
54 ..,
55 ),
56 )) => {
57 // BorrowCk needed to track whether these cases were coercions or casts,
58 // to know whether to check lifetimes in their pointees,
59 // but from now on that distinction doesn't matter,
60 // so just make them ordinary pointer casts instead.
61 *cast_kind = CastKind::PtrToPtr;
62 }
63 _ => (),
64 }
65 }
66
67 // If we change any terminator, we need to ensure that we invalidated the CFG cache.
68 let terminator = basic_block.terminator_mut();
69 match terminator.kind {
70 TerminatorKind::FalseEdge { real_target, .. }
71 | TerminatorKind::FalseUnwind { real_target, .. } => {
72 invalidate_cfg = true;
73 terminator.kind = TerminatorKind::Goto { target: real_target };
74 }
75 _ => {}
76 }
77 }
78
79 if invalidate_cfg {
80 body.basic_blocks.invalidate_cfg_cache();
81 }
82
83 body.user_type_annotations.raw.clear();
84
85 for decl in &mut body.local_decls {
86 decl.user_ty = None;
87 }
88 }
89
90 fn policy(&self, _sess: &rustc_session::Session) -> PassPolicy {
91 // Removes administrative MIR instructions that later passes must never see.
92 PassPolicy::Required
93 }
94}