1 //! This module provides a pass to replacing the following statements with
4 //! - [`AscribeUserType`]
6 //! - [`Assign`] statements with a [`Shallow`] borrow
8 //! The `CleanFakeReadsAndBorrows` "pass" is actually implemented as two
9 //! traversals (aka visits) of the input MIR. The first traversal,
10 //! `DeleteAndRecordFakeReads`, deletes the fake reads and finds the
11 //! temporaries read by [`ForMatchGuard`] reads, and `DeleteFakeBorrows`
12 //! deletes the initialization of those temporaries.
14 //! [`AscribeUserType`]: rustc_middle::mir::StatementKind::AscribeUserType
15 //! [`Shallow`]: rustc_middle::mir::BorrowKind::Shallow
16 //! [`FakeRead`]: rustc_middle::mir::StatementKind::FakeRead
17 //! [`Assign`]: rustc_middle::mir::StatementKind::Assign
18 //! [`ForMatchGuard`]: rustc_middle::mir::FakeReadCause::ForMatchGuard
19 //! [`Nop`]: rustc_middle::mir::StatementKind::Nop
22 use rustc_middle::mir::visit::MutVisitor;
23 use rustc_middle::mir::{Body, BorrowKind, Location, Rvalue};
24 use rustc_middle::mir::{Statement, StatementKind};
25 use rustc_middle::ty::TyCtxt;
27 pub struct CleanupNonCodegenStatements;
29 pub struct DeleteNonCodegenStatements<'tcx> {
33 impl<'tcx> MirPass<'tcx> for CleanupNonCodegenStatements {
34 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
35 let mut delete = DeleteNonCodegenStatements { tcx };
36 delete.visit_body(body);
37 body.user_type_annotations.raw.clear();
39 for decl in &mut body.local_decls {
45 impl<'tcx> MutVisitor<'tcx> for DeleteNonCodegenStatements<'tcx> {
46 fn tcx(&self) -> TyCtxt<'tcx> {
50 fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
51 match statement.kind {
52 StatementKind::AscribeUserType(..)
53 | StatementKind::Assign(box (_, Rvalue::Ref(_, BorrowKind::Shallow, _)))
54 | StatementKind::FakeRead(..) => statement.make_nop(),
57 self.super_statement(statement, location);