|
1 |
| -//! This module provides a pass to replacing the following statements with |
2 |
| -//! [`Nop`]s |
| 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: |
3 | 4 | //!
|
4 | 5 | //! - [`AscribeUserType`]
|
5 | 6 | //! - [`FakeRead`]
|
6 | 7 | //! - [`Assign`] statements with a [`Shallow`] borrow
|
7 | 8 | //!
|
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 | 9 | //! [`AscribeUserType`]: rustc_middle::mir::StatementKind::AscribeUserType
|
15 |
| -//! [`Shallow`]: rustc_middle::mir::BorrowKind::Shallow |
16 |
| -//! [`FakeRead`]: rustc_middle::mir::StatementKind::FakeRead |
17 | 10 | //! [`Assign`]: rustc_middle::mir::StatementKind::Assign
|
18 |
| -//! [`ForMatchGuard`]: rustc_middle::mir::FakeReadCause::ForMatchGuard |
| 11 | +//! [`FakeRead`]: rustc_middle::mir::StatementKind::FakeRead |
19 | 12 | //! [`Nop`]: rustc_middle::mir::StatementKind::Nop
|
| 13 | +//! [`Shallow`]: rustc_middle::mir::BorrowKind::Shallow |
20 | 14 |
|
21 | 15 | use crate::MirPass;
|
22 |
| -use rustc_middle::mir::visit::MutVisitor; |
23 |
| -use rustc_middle::mir::{Body, BorrowKind, Location, Rvalue}; |
24 |
| -use rustc_middle::mir::{Statement, StatementKind}; |
| 16 | +use rustc_middle::mir::{Body, BorrowKind, Rvalue, StatementKind, TerminatorKind}; |
25 | 17 | use rustc_middle::ty::TyCtxt;
|
26 | 18 |
|
27 |
| -pub struct CleanupNonCodegenStatements; |
| 19 | +pub struct CleanupPostBorrowck; |
28 | 20 |
|
29 |
| -pub struct DeleteNonCodegenStatements<'tcx> { |
30 |
| - tcx: TyCtxt<'tcx>, |
31 |
| -} |
| 21 | +impl<'tcx> MirPass<'tcx> for CleanupPostBorrowck { |
| 22 | + fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { |
| 23 | + for basic_block in body.basic_blocks.as_mut() { |
| 24 | + for statement in basic_block.statements.iter_mut() { |
| 25 | + match statement.kind { |
| 26 | + StatementKind::AscribeUserType(..) |
| 27 | + | StatementKind::Assign(box (_, Rvalue::Ref(_, BorrowKind::Shallow, _))) |
| 28 | + | StatementKind::FakeRead(..) => statement.make_nop(), |
| 29 | + _ => (), |
| 30 | + } |
| 31 | + } |
| 32 | + let terminator = basic_block.terminator_mut(); |
| 33 | + match terminator.kind { |
| 34 | + TerminatorKind::FalseEdge { real_target, .. } |
| 35 | + | TerminatorKind::FalseUnwind { real_target, .. } => { |
| 36 | + terminator.kind = TerminatorKind::Goto { target: real_target }; |
| 37 | + } |
| 38 | + _ => {} |
| 39 | + } |
| 40 | + } |
32 | 41 |
|
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_preserves_cfg(body); |
37 | 42 | body.user_type_annotations.raw.clear();
|
38 | 43 |
|
39 | 44 | for decl in &mut body.local_decls {
|
40 | 45 | decl.user_ty = None;
|
41 | 46 | }
|
42 | 47 | }
|
43 | 48 | }
|
44 |
| - |
45 |
| -impl<'tcx> MutVisitor<'tcx> for DeleteNonCodegenStatements<'tcx> { |
46 |
| - fn tcx(&self) -> TyCtxt<'tcx> { |
47 |
| - self.tcx |
48 |
| - } |
49 |
| - |
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(), |
55 |
| - _ => (), |
56 |
| - } |
57 |
| - self.super_statement(statement, location); |
58 |
| - } |
59 |
| -} |
0 commit comments