]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs
New upstream version 1.57.0+dfsg1
[rustc.git] / compiler / rustc_mir_transform / src / cleanup_post_borrowck.rs
CommitLineData
9fa01778
XL
1//! This module provides a pass to replacing the following statements with
2//! [`Nop`]s
041b39d2 3//!
9fa01778
XL
4//! - [`AscribeUserType`]
5//! - [`FakeRead`]
6//! - [`Assign`] statements with a [`Shallow`] borrow
0531ce1d 7//!
0731742a 8//! The `CleanFakeReadsAndBorrows` "pass" is actually implemented as two
0531ce1d 9//! traversals (aka visits) of the input MIR. The first traversal,
f9f354fc
XL
10//! `DeleteAndRecordFakeReads`, deletes the fake reads and finds the
11//! temporaries read by [`ForMatchGuard`] reads, and `DeleteFakeBorrows`
0731742a
XL
12//! deletes the initialization of those temporaries.
13//!
ba9703b0
XL
14//! [`AscribeUserType`]: rustc_middle::mir::StatementKind::AscribeUserType
15//! [`Shallow`]: rustc_middle::mir::BorrowKind::Shallow
16//! [`FakeRead`]: rustc_middle::mir::StatementKind::FakeRead
f9f354fc
XL
17//! [`Assign`]: rustc_middle::mir::StatementKind::Assign
18//! [`ForMatchGuard`]: rustc_middle::mir::FakeReadCause::ForMatchGuard
ba9703b0 19//! [`Nop`]: rustc_middle::mir::StatementKind::Nop
041b39d2 20
c295e0f8 21use crate::MirPass;
ba9703b0 22use rustc_middle::mir::visit::MutVisitor;
f9f354fc 23use rustc_middle::mir::{Body, BorrowKind, Location, Rvalue};
ba9703b0
XL
24use rustc_middle::mir::{Statement, StatementKind};
25use rustc_middle::ty::TyCtxt;
041b39d2 26
9fa01778 27pub struct CleanupNonCodegenStatements;
0531ce1d 28
e74abb32
XL
29pub struct DeleteNonCodegenStatements<'tcx> {
30 tcx: TyCtxt<'tcx>,
31}
0531ce1d 32
e1599b0c 33impl<'tcx> MirPass<'tcx> for CleanupNonCodegenStatements {
29967ef6 34 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
e74abb32 35 let mut delete = DeleteNonCodegenStatements { tcx };
dc9dc135 36 delete.visit_body(body);
ba9703b0 37 body.user_type_annotations.raw.clear();
f9f354fc
XL
38
39 for decl in &mut body.local_decls {
40 decl.user_ty = None;
41 }
0531ce1d
XL
42 }
43}
44
e74abb32
XL
45impl<'tcx> MutVisitor<'tcx> for DeleteNonCodegenStatements<'tcx> {
46 fn tcx(&self) -> TyCtxt<'tcx> {
47 self.tcx
48 }
49
dfeec247 50 fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
9fa01778
XL
51 match statement.kind {
52 StatementKind::AscribeUserType(..)
dfeec247 53 | StatementKind::Assign(box (_, Rvalue::Ref(_, BorrowKind::Shallow, _)))
9fa01778
XL
54 | StatementKind::FakeRead(..) => statement.make_nop(),
55 _ => (),
0bf4aa26 56 }
48663c56 57 self.super_statement(statement, location);
0bf4aa26
XL
58 }
59}