]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/cleanup_post_borrowck.rs
Auto merge of #57609 - matthewjasper:more-restrictive-match, r=pnkfelix
[rust.git] / src / librustc_mir / transform / cleanup_post_borrowck.rs
1 //! This module provides a pass to replacing the following statements with
2 //! [`Nop`]s
3 //!
4 //!   - [`AscribeUserType`]
5 //!   - [`FakeRead`]
6 //!   - [`Assign`] statements with a [`Shallow`] borrow
7 //!
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.
13 //!
14 //! [`AscribeUserType`]: rustc::mir::StatementKind::AscribeUserType
15 //! [`Shallow`]: rustc::mir::BorrowKind::Shallow
16 //! [`FakeRead`]: rustc::mir::StatementKind::FakeRead
17 //! [`Nop`]: rustc::mir::StatementKind::Nop
18
19 use rustc::mir::{BasicBlock, BorrowKind, Rvalue, Location, Mir};
20 use rustc::mir::{Statement, StatementKind};
21 use rustc::mir::visit::MutVisitor;
22 use rustc::ty::TyCtxt;
23 use crate::transform::{MirPass, MirSource};
24
25 pub struct CleanupNonCodegenStatements;
26
27 pub struct DeleteNonCodegenStatements;
28
29 impl MirPass for CleanupNonCodegenStatements {
30     fn run_pass<'a, 'tcx>(&self,
31                           _tcx: TyCtxt<'a, 'tcx, 'tcx>,
32                           _source: MirSource<'tcx>,
33                           mir: &mut Mir<'tcx>) {
34         let mut delete = DeleteNonCodegenStatements;
35         delete.visit_mir(mir);
36     }
37 }
38
39 impl<'tcx> MutVisitor<'tcx> for DeleteNonCodegenStatements {
40     fn visit_statement(&mut self,
41                        block: BasicBlock,
42                        statement: &mut Statement<'tcx>,
43                        location: Location) {
44         match statement.kind {
45             StatementKind::AscribeUserType(..)
46             | StatementKind::Assign(_, box Rvalue::Ref(_, BorrowKind::Shallow, _))
47             | StatementKind::FakeRead(..) => statement.make_nop(),
48             _ => (),
49         }
50         self.super_statement(block, statement, location);
51     }
52 }