]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/transform/cleanup_post_borrowck.rs
New upstream version 1.44.1+dfsg1
[rustc.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_middle::mir::StatementKind::AscribeUserType
15 //! [`Shallow`]: rustc_middle::mir::BorrowKind::Shallow
16 //! [`FakeRead`]: rustc_middle::mir::StatementKind::FakeRead
17 //! [`Nop`]: rustc_middle::mir::StatementKind::Nop
18
19 use crate::transform::{MirPass, MirSource};
20 use rustc_middle::mir::visit::MutVisitor;
21 use rustc_middle::mir::{BodyAndCache, BorrowKind, Location, Rvalue};
22 use rustc_middle::mir::{Statement, StatementKind};
23 use rustc_middle::ty::TyCtxt;
24
25 pub struct CleanupNonCodegenStatements;
26
27 pub struct DeleteNonCodegenStatements<'tcx> {
28 tcx: TyCtxt<'tcx>,
29 }
30
31 impl<'tcx> MirPass<'tcx> for CleanupNonCodegenStatements {
32 fn run_pass(&self, tcx: TyCtxt<'tcx>, _source: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
33 let mut delete = DeleteNonCodegenStatements { tcx };
34 delete.visit_body(body);
35 body.user_type_annotations.raw.clear();
36 }
37 }
38
39 impl<'tcx> MutVisitor<'tcx> for DeleteNonCodegenStatements<'tcx> {
40 fn tcx(&self) -> TyCtxt<'tcx> {
41 self.tcx
42 }
43
44 fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
45 match statement.kind {
46 StatementKind::AscribeUserType(..)
47 | StatementKind::Assign(box (_, Rvalue::Ref(_, BorrowKind::Shallow, _)))
48 | StatementKind::FakeRead(..) => statement.make_nop(),
49 _ => (),
50 }
51 self.super_statement(statement, location);
52 }
53 }