Skip to content

Commit cef6e24

Browse files
authored
Rollup merge of rust-lang#35542 - scottcarr:visitor_refactor, r=nikomatsakis
[MIR] track Location in MirVisitor, combine Location All the users of MirVisitor::visit_statement implement their own statement index tracking. This PR move the tracking into MirVisitor itself. Also, there were 2 separate implementations of Location that were identical. This PR eliminates one of them.
2 parents c752da3 + fb84bb1 commit cef6e24

File tree

18 files changed

+274
-251
lines changed

18 files changed

+274
-251
lines changed

src/librustc/mir/repr.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1233,3 +1233,14 @@ impl<'a, 'b> GraphSuccessors<'b> for Mir<'a> {
12331233
type Item = BasicBlock;
12341234
type Iter = IntoIter<BasicBlock>;
12351235
}
1236+
1237+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, Ord, PartialOrd)]
1238+
pub struct Location {
1239+
/// the location is within this block
1240+
pub block: BasicBlock,
1241+
1242+
/// the location is the start of the this statement; or, if `statement_index`
1243+
/// == num-statements, then the start of the terminator.
1244+
pub statement_index: usize,
1245+
}
1246+

src/librustc/mir/visit.rs

+119-86
Large diffs are not rendered by default.

src/librustc_borrowck/borrowck/mir/dataflow/impls.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
// except according to those terms.
1010

1111
use rustc::ty::TyCtxt;
12-
use rustc::mir::repr::{self, Mir};
12+
use rustc::mir::repr::{self, Mir, Location};
1313
use rustc_data_structures::indexed_vec::Idx;
1414

15-
use super::super::gather_moves::{Location};
1615
use super::super::gather_moves::{MoveOutIndex, MovePathIndex};
1716
use super::super::MoveDataParamEnv;
1817
use super::super::DropFlagState;
@@ -252,7 +251,7 @@ impl<'a, 'tcx> BitDenotation for MaybeInitializedLvals<'a, 'tcx> {
252251
{
253252
drop_flag_effects_for_location(
254253
self.tcx, self.mir, ctxt,
255-
Location { block: bb, index: idx },
254+
Location { block: bb, statement_index: idx },
256255
|path, s| Self::update_bits(sets, path, s)
257256
)
258257
}
@@ -265,7 +264,7 @@ impl<'a, 'tcx> BitDenotation for MaybeInitializedLvals<'a, 'tcx> {
265264
{
266265
drop_flag_effects_for_location(
267266
self.tcx, self.mir, ctxt,
268-
Location { block: bb, index: statements_len },
267+
Location { block: bb, statement_index: statements_len },
269268
|path, s| Self::update_bits(sets, path, s)
270269
)
271270
}
@@ -314,7 +313,7 @@ impl<'a, 'tcx> BitDenotation for MaybeUninitializedLvals<'a, 'tcx> {
314313
{
315314
drop_flag_effects_for_location(
316315
self.tcx, self.mir, ctxt,
317-
Location { block: bb, index: idx },
316+
Location { block: bb, statement_index: idx },
318317
|path, s| Self::update_bits(sets, path, s)
319318
)
320319
}
@@ -327,7 +326,7 @@ impl<'a, 'tcx> BitDenotation for MaybeUninitializedLvals<'a, 'tcx> {
327326
{
328327
drop_flag_effects_for_location(
329328
self.tcx, self.mir, ctxt,
330-
Location { block: bb, index: statements_len },
329+
Location { block: bb, statement_index: statements_len },
331330
|path, s| Self::update_bits(sets, path, s)
332331
)
333332
}
@@ -375,7 +374,7 @@ impl<'a, 'tcx> BitDenotation for DefinitelyInitializedLvals<'a, 'tcx> {
375374
{
376375
drop_flag_effects_for_location(
377376
self.tcx, self.mir, ctxt,
378-
Location { block: bb, index: idx },
377+
Location { block: bb, statement_index: idx },
379378
|path, s| Self::update_bits(sets, path, s)
380379
)
381380
}
@@ -388,7 +387,7 @@ impl<'a, 'tcx> BitDenotation for DefinitelyInitializedLvals<'a, 'tcx> {
388387
{
389388
drop_flag_effects_for_location(
390389
self.tcx, self.mir, ctxt,
391-
Location { block: bb, index: statements_len },
390+
Location { block: bb, statement_index: statements_len },
392391
|path, s| Self::update_bits(sets, path, s)
393392
)
394393
}
@@ -431,7 +430,7 @@ impl<'a, 'tcx> BitDenotation for MovingOutStatements<'a, 'tcx> {
431430
let path_map = &move_data.path_map;
432431
let rev_lookup = &move_data.rev_lookup;
433432

434-
let loc = Location { block: bb, index: idx };
433+
let loc = Location { block: bb, statement_index: idx };
435434
debug!("stmt {:?} at loc {:?} moves out of move_indexes {:?}",
436435
stmt, loc, &loc_map[loc]);
437436
for move_index in &loc_map[loc] {
@@ -471,7 +470,7 @@ impl<'a, 'tcx> BitDenotation for MovingOutStatements<'a, 'tcx> {
471470
let (mir, move_data) = (self.mir, &ctxt.move_data);
472471
let term = mir[bb].terminator();
473472
let loc_map = &move_data.loc_map;
474-
let loc = Location { block: bb, index: statements_len };
473+
let loc = Location { block: bb, statement_index: statements_len };
475474
debug!("terminator {:?} at loc {:?} moves out of move_indexes {:?}",
476475
term, loc, &loc_map[loc]);
477476
let bits_per_block = self.bits_per_block(ctxt);

src/librustc_borrowck/borrowck/mir/elaborate_drops.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use indexed_set::IdxSetBuf;
12-
use super::gather_moves::{MoveData, MovePathIndex, MovePathContent, Location};
12+
use super::gather_moves::{MoveData, MovePathIndex, MovePathContent};
1313
use super::dataflow::{MaybeInitializedLvals, MaybeUninitializedLvals};
1414
use super::dataflow::{DataflowResults};
1515
use super::{drop_flag_effects_for_location, on_all_children_bits};
@@ -146,9 +146,9 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
146146
dead: self.flow_uninits.sets().on_entry_set_for(loc.block.index())
147147
.to_owned(),
148148
};
149-
for stmt in 0..loc.index {
149+
for stmt in 0..loc.statement_index {
150150
data.apply_location(self.tcx, self.mir, self.env,
151-
Location { block: loc.block, index: stmt });
151+
Location { block: loc.block, statement_index: stmt });
152152
}
153153
data
154154
}
@@ -226,7 +226,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
226226

227227
let init_data = self.initialization_data_at(Location {
228228
block: bb,
229-
index: data.statements.len()
229+
statement_index: data.statements.len()
230230
});
231231

232232
let path = self.move_data().rev_lookup.find(location);
@@ -249,7 +249,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
249249
fn elaborate_drops(&mut self)
250250
{
251251
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
252-
let loc = Location { block: bb, index: data.statements.len() };
252+
let loc = Location { block: bb, statement_index: data.statements.len() };
253253
let terminator = data.terminator();
254254

255255
let resume_block = self.patch.resume_block();
@@ -359,9 +359,9 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
359359
unwind: Some(unwind)
360360
}, bb);
361361
on_all_children_bits(self.tcx, self.mir, self.move_data(), path, |child| {
362-
self.set_drop_flag(Location { block: target, index: 0 },
362+
self.set_drop_flag(Location { block: target, statement_index: 0 },
363363
child, DropFlagState::Present);
364-
self.set_drop_flag(Location { block: unwind, index: 0 },
364+
self.set_drop_flag(Location { block: unwind, statement_index: 0 },
365365
child, DropFlagState::Present);
366366
});
367367
}
@@ -741,7 +741,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
741741
let drop_block = self.drop_block(c);
742742
if update_drop_flag {
743743
self.set_drop_flag(
744-
Location { block: drop_block, index: 0 },
744+
Location { block: drop_block, statement_index: 0 },
745745
c.path,
746746
DropFlagState::Absent
747747
);
@@ -927,7 +927,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
927927
}
928928

929929
fn drop_flags_on_init(&mut self) {
930-
let loc = Location { block: START_BLOCK, index: 0 };
930+
let loc = Location { block: START_BLOCK, statement_index: 0 };
931931
let span = self.patch.source_info_for_location(self.mir, loc).span;
932932
let false_ = self.constant_bool(span, false);
933933
for flag in self.drop_flags.values() {
@@ -942,7 +942,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
942942
} = data.terminator().kind {
943943
assert!(!self.patch.is_patched(bb));
944944

945-
let loc = Location { block: tgt, index: 0 };
945+
let loc = Location { block: tgt, statement_index: 0 };
946946
let path = self.move_data().rev_lookup.find(lv);
947947
on_all_children_bits(
948948
self.tcx, self.mir, self.move_data(), path,
@@ -953,7 +953,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
953953
}
954954

955955
fn drop_flags_for_args(&mut self) {
956-
let loc = Location { block: START_BLOCK, index: 0 };
956+
let loc = Location { block: START_BLOCK, statement_index: 0 };
957957
super::drop_flag_effects_for_function_entry(
958958
self.tcx, self.mir, self.env, |path, ds| {
959959
self.set_drop_flag(loc, path, ds);
@@ -993,7 +993,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
993993
}
994994
}
995995
}
996-
let loc = Location { block: bb, index: i };
996+
let loc = Location { block: bb, statement_index: i };
997997
super::drop_flag_effects_for_location(
998998
self.tcx, self.mir, self.env, loc, |path, ds| {
999999
if ds == DropFlagState::Absent || allow_initializations {
@@ -1011,7 +1011,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
10111011
} = data.terminator().kind {
10121012
assert!(!self.patch.is_patched(bb));
10131013

1014-
let loc = Location { block: bb, index: data.statements.len() };
1014+
let loc = Location { block: bb, statement_index: data.statements.len() };
10151015
let path = self.move_data().rev_lookup.find(lv);
10161016
on_all_children_bits(
10171017
self.tcx, self.mir, self.move_data(), path,

src/librustc_borrowck/borrowck/mir/gather_moves.rs

+9-24
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ impl Index<Location> for LocMap {
160160
type Output = [MoveOutIndex];
161161
fn index(&self, index: Location) -> &Self::Output {
162162
assert!(index.block.index() < self.map.len());
163-
assert!(index.index < self.map[index.block.index()].len());
164-
&self.map[index.block.index()][index.index]
163+
assert!(index.statement_index < self.map[index.block.index()].len());
164+
&self.map[index.block.index()][index.statement_index]
165165
}
166166
}
167167

@@ -200,21 +200,6 @@ impl fmt::Debug for MoveOut {
200200
}
201201
}
202202

203-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
204-
pub struct Location {
205-
/// block where action is located
206-
pub block: BasicBlock,
207-
/// index within above block; statement when < statments.len) or
208-
/// the terminator (when = statements.len).
209-
pub index: usize,
210-
}
211-
212-
impl fmt::Debug for Location {
213-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
214-
write!(fmt, "{:?}[{}]", self.block, self.index)
215-
}
216-
}
217-
218203
#[derive(Debug)]
219204
pub struct MovePathData<'tcx> {
220205
move_paths: Vec<MovePath<'tcx>>,
@@ -571,7 +556,7 @@ fn gather_moves<'a, 'tcx>(mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> MoveD
571556
};
572557

573558
for (i, stmt) in bb_data.statements.iter().enumerate() {
574-
let source = Location { block: bb, index: i };
559+
let source = Location { block: bb, statement_index: i };
575560
match stmt.kind {
576561
StatementKind::Assign(ref lval, ref rval) => {
577562
bb_ctxt.builder.create_move_path(lval);
@@ -631,7 +616,7 @@ fn gather_moves<'a, 'tcx>(mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> MoveD
631616

632617
TerminatorKind::Return => {
633618
let source = Location { block: bb,
634-
index: bb_data.statements.len() };
619+
statement_index: bb_data.statements.len() };
635620
if let FnOutput::FnConverging(_) = bb_ctxt.builder.mir.return_ty {
636621
debug!("gather_moves Return on_move_out_lval return {:?}", source);
637622
bb_ctxt.on_move_out_lval(SK::Return, &Lvalue::ReturnPointer, source);
@@ -643,7 +628,7 @@ fn gather_moves<'a, 'tcx>(mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> MoveD
643628

644629
TerminatorKind::If { ref cond, targets: _ } => {
645630
let source = Location { block: bb,
646-
index: bb_data.statements.len() };
631+
statement_index: bb_data.statements.len() };
647632
bb_ctxt.on_operand(SK::If, cond, source);
648633
}
649634

@@ -674,20 +659,20 @@ fn gather_moves<'a, 'tcx>(mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> MoveD
674659

675660
TerminatorKind::Drop { ref location, target: _, unwind: _ } => {
676661
let source = Location { block: bb,
677-
index: bb_data.statements.len() };
662+
statement_index: bb_data.statements.len() };
678663
bb_ctxt.on_move_out_lval(SK::Drop, location, source);
679664
}
680665
TerminatorKind::DropAndReplace { ref location, ref value, .. } => {
681666
let assigned_path = bb_ctxt.builder.move_path_for(location);
682667
bb_ctxt.path_map.fill_to(assigned_path.index());
683668

684669
let source = Location { block: bb,
685-
index: bb_data.statements.len() };
670+
statement_index: bb_data.statements.len() };
686671
bb_ctxt.on_operand(SK::Use, value, source);
687672
}
688673
TerminatorKind::Call { ref func, ref args, ref destination, cleanup: _ } => {
689674
let source = Location { block: bb,
690-
index: bb_data.statements.len() };
675+
statement_index: bb_data.statements.len() };
691676
bb_ctxt.on_operand(SK::CallFn, func, source);
692677
for arg in args {
693678
debug!("gather_moves Call on_operand {:?} {:?}", arg, source);
@@ -762,7 +747,7 @@ impl<'b, 'a: 'b, 'tcx: 'a> BlockContext<'b, 'a, 'tcx> {
762747
stmt_kind: StmtKind,
763748
lval: &Lvalue<'tcx>,
764749
source: Location) {
765-
let i = source.index;
750+
let i = source.statement_index;
766751
let index = MoveOutIndex::new(self.moves.len());
767752

768753
let path = self.builder.move_path_for(lval);

src/librustc_borrowck/borrowck/mir/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc::hir;
1919
use rustc::hir::intravisit::{FnKind};
2020

2121
use rustc::mir::repr;
22-
use rustc::mir::repr::{BasicBlock, BasicBlockData, Mir, Statement, Terminator};
22+
use rustc::mir::repr::{BasicBlock, BasicBlockData, Mir, Statement, Terminator, Location};
2323
use rustc::session::Session;
2424
use rustc::ty::{self, TyCtxt};
2525

@@ -35,7 +35,7 @@ use self::dataflow::{DataflowOperator};
3535
use self::dataflow::{Dataflow, DataflowAnalysis, DataflowResults};
3636
use self::dataflow::{MaybeInitializedLvals, MaybeUninitializedLvals};
3737
use self::dataflow::{DefinitelyInitializedLvals};
38-
use self::gather_moves::{MoveData, MovePathIndex, Location};
38+
use self::gather_moves::{MoveData, MovePathIndex};
3939
use self::gather_moves::{MovePathContent, MovePathData};
4040

4141
fn has_rustc_mir_with(attrs: &[ast::Attribute], name: &str) -> Option<P<MetaItem>> {
@@ -367,7 +367,7 @@ fn drop_flag_effects_for_location<'a, 'tcx, F>(
367367
}
368368

369369
let block = &mir[loc.block];
370-
match block.statements.get(loc.index) {
370+
match block.statements.get(loc.statement_index) {
371371
Some(stmt) => match stmt.kind {
372372
repr::StatementKind::SetDiscriminant{ .. } => {
373373
span_bug!(stmt.source_info.span, "SetDiscrimant should not exist during borrowck");

src/librustc_borrowck/borrowck/mir/patch.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use super::gather_moves::Location;
1211
use rustc::ty::Ty;
1312
use rustc::mir::repr::*;
1413
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
@@ -89,7 +88,7 @@ impl<'tcx> MirPatch<'tcx> {
8988
};
9089
Location {
9190
block: bb,
92-
index: offset
91+
statement_index: offset
9392
}
9493
}
9594

@@ -149,12 +148,12 @@ impl<'tcx> MirPatch<'tcx> {
149148
}
150149
debug!("MirPatch: adding statement {:?} at loc {:?}+{}",
151150
stmt, loc, delta);
152-
loc.index += delta;
151+
loc.statement_index += delta;
153152
let source_info = Self::source_info_for_index(
154153
&mir[loc.block], loc
155154
);
156155
mir[loc.block].statements.insert(
157-
loc.index, Statement {
156+
loc.statement_index, Statement {
158157
source_info: source_info,
159158
kind: stmt
160159
});
@@ -163,7 +162,7 @@ impl<'tcx> MirPatch<'tcx> {
163162
}
164163

165164
pub fn source_info_for_index(data: &BasicBlockData, loc: Location) -> SourceInfo {
166-
match data.statements.get(loc.index) {
165+
match data.statements.get(loc.statement_index) {
167166
Some(stmt) => stmt.source_info,
168167
None => data.terminator().source_info
169168
}

src/librustc_metadata/decoder.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use rustc_const_math::ConstInt;
4343

4444
use rustc::mir;
4545
use rustc::mir::visit::MutVisitor;
46+
use rustc::mir::repr::Location;
4647

4748
use std::cell::Cell;
4849
use std::io;
@@ -857,7 +858,7 @@ pub fn maybe_get_item_mir<'a, 'tcx>(cdata: Cmd,
857858
impl<'v, 'cdata, 'codemap> mir::visit::MutVisitor<'v>
858859
for MirDefIdAndSpanTranslator<'cdata, 'codemap>
859860
{
860-
fn visit_def_id(&mut self, def_id: &mut DefId) {
861+
fn visit_def_id(&mut self, def_id: &mut DefId, _: Location) {
861862
*def_id = translate_def_id(self.crate_metadata, *def_id);
862863
}
863864

src/librustc_mir/build/cfg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
//! Routines for manipulating the control-flow graph.
1515
16-
use build::{CFG, Location};
16+
use build::CFG;
1717
use rustc::mir::repr::*;
1818

1919
impl<'tcx> CFG<'tcx> {

0 commit comments

Comments
 (0)