Skip to content

Commit 1331cd4

Browse files
committed
Killing UserAssertTy in CleanupPostBorrowck pass.
1 parent 17b285d commit 1331cd4

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

src/librustc_mir/transform/clean_end_regions.rs renamed to src/librustc_mir/transform/cleanup_post_borrowck.rs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,27 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! This module provides one pass, `CleanEndRegions`, that reduces the
12-
//! set of `EndRegion` statements in the MIR.
11+
//! This module provides two passes:
1312
//!
14-
//! The "pass" is actually implemented as two traversals (aka visits)
15-
//! of the input MIR. The first traversal, `GatherBorrowedRegions`,
16-
//! finds all of the regions in the MIR that are involved in a borrow.
13+
//! - `CleanEndRegions`, that reduces the set of `EndRegion` statements
14+
//! in the MIR.
15+
//! - `CleanUserAssertTy`, that replaces all `UserAssertTy` statements
16+
//! with `Nop`.
17+
//!
18+
//! The `CleanEndRegions` "pass" is actually implemented as two
19+
//! traversals (aka visits) of the input MIR. The first traversal,
20+
//! `GatherBorrowedRegions`, finds all of the regions in the MIR
21+
//! that are involved in a borrow.
1722
//!
1823
//! The second traversal, `DeleteTrivialEndRegions`, walks over the
1924
//! MIR and removes any `EndRegion` that is applied to a region that
2025
//! was not seen in the previous pass.
26+
//!
27+
//! The `CleanUserAssertTy` pass runs at a distinct time from the
28+
//! `CleanEndRegions` pass. It is important that the `CleanUserAssertTy`
29+
//! pass runs after the MIR borrowck so that the NLL type checker can
30+
//! perform the type assertion when it encounters the `UserAssertTy`
31+
//! statements.
2132
2233
use rustc_data_structures::fx::FxHashSet;
2334

@@ -27,7 +38,7 @@ use rustc::mir::visit::{MutVisitor, Visitor, TyContext};
2738
use rustc::ty::{Ty, RegionKind, TyCtxt};
2839
use transform::{MirPass, MirSource};
2940

30-
pub struct CleanEndRegions;
41+
pub struct CleanupPostBorrowck;
3142

3243
struct GatherBorrowedRegions {
3344
seen_regions: FxHashSet<region::Scope>,
@@ -37,19 +48,24 @@ struct DeleteTrivialEndRegions<'a> {
3748
seen_regions: &'a FxHashSet<region::Scope>,
3849
}
3950

40-
impl MirPass for CleanEndRegions {
51+
pub struct DeleteUserAssertTy;
52+
53+
impl MirPass for CleanupPostBorrowck {
4154
fn run_pass<'a, 'tcx>(&self,
4255
tcx: TyCtxt<'a, 'tcx, 'tcx>,
4356
_source: MirSource,
4457
mir: &mut Mir<'tcx>) {
45-
if !tcx.emit_end_regions() { return; }
58+
if tcx.emit_end_regions() {
59+
let mut gather = GatherBorrowedRegions {
60+
seen_regions: FxHashSet()
61+
};
62+
gather.visit_mir(mir);
4663

47-
let mut gather = GatherBorrowedRegions {
48-
seen_regions: FxHashSet()
49-
};
50-
gather.visit_mir(mir);
64+
let mut delete = DeleteTrivialEndRegions { seen_regions: &mut gather.seen_regions };
65+
delete.visit_mir(mir);
66+
}
5167

52-
let mut delete = DeleteTrivialEndRegions { seen_regions: &mut gather.seen_regions };
68+
let mut delete = DeleteUserAssertTy;
5369
delete.visit_mir(mir);
5470
}
5571
}
@@ -93,7 +109,19 @@ impl<'a, 'tcx> MutVisitor<'tcx> for DeleteTrivialEndRegions<'a> {
93109
}
94110

95111
if delete_it {
96-
statement.kind = StatementKind::Nop;
112+
statement.make_nop();
113+
}
114+
self.super_statement(block, statement, location);
115+
}
116+
}
117+
118+
impl<'tcx> MutVisitor<'tcx> for DeleteUserAssertTy {
119+
fn visit_statement(&mut self,
120+
block: BasicBlock,
121+
statement: &mut Statement<'tcx>,
122+
location: Location) {
123+
if let StatementKind::UserAssertTy(..) = statement.kind {
124+
statement.make_nop();
97125
}
98126
self.super_statement(block, statement, location);
99127
}

src/librustc_mir/transform/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use syntax_pos::Span;
2525

2626
pub mod add_validation;
2727
pub mod add_moves_for_packed_drops;
28-
pub mod clean_end_regions;
28+
pub mod cleanup_post_borrowck;
2929
pub mod check_unsafety;
3030
pub mod simplify_branches;
3131
pub mod simplify;
@@ -192,8 +192,9 @@ fn mir_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Stea
192192

193193
let mut mir = tcx.mir_built(def_id).steal();
194194
run_passes![tcx, mir, def_id, 0;
195-
// Remove all `EndRegion` statements that are not involved in borrows.
196-
clean_end_regions::CleanEndRegions,
195+
// Remove all `UserAssertTy` statements and all `EndRegion` statements that are not
196+
// involved in borrows.
197+
cleanup_post_borrowck::CleanupPostBorrowck,
197198

198199
// What we need to do constant evaluation.
199200
simplify::SimplifyCfg::new("initial"),

0 commit comments

Comments
 (0)