Skip to content

Commit b7e2157

Browse files
authored
Auto merge of #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 1987131 + c043a27 commit b7e2157

File tree

18 files changed

+278
-254
lines changed

18 files changed

+278
-254
lines changed

src/librustc/mir/repr.rs

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

src/librustc/mir/visit.rs

+122-89
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] {
@@ -473,7 +472,7 @@ impl<'a, 'tcx> BitDenotation for MovingOutStatements<'a, 'tcx> {
473472
let (mir, move_data) = (self.mir, &ctxt.move_data);
474473
let term = mir[bb].terminator();
475474
let loc_map = &move_data.loc_map;
476-
let loc = Location { block: bb, index: statements_len };
475+
let loc = Location { block: bb, statement_index: statements_len };
477476
debug!("terminator {:?} at loc {:?} moves out of move_indexes {:?}",
478477
term, loc, &loc_map[loc]);
479478
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
);
@@ -924,7 +924,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
924924
}
925925

926926
fn drop_flags_on_init(&mut self) {
927-
let loc = Location { block: START_BLOCK, index: 0 };
927+
let loc = Location { block: START_BLOCK, statement_index: 0 };
928928
let span = self.patch.source_info_for_location(self.mir, loc).span;
929929
let false_ = self.constant_bool(span, false);
930930
for flag in self.drop_flags.values() {
@@ -939,7 +939,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
939939
} = data.terminator().kind {
940940
assert!(!self.patch.is_patched(bb));
941941

942-
let loc = Location { block: tgt, index: 0 };
942+
let loc = Location { block: tgt, statement_index: 0 };
943943
let path = self.move_data().rev_lookup.find(lv);
944944
on_all_children_bits(
945945
self.tcx, self.mir, self.move_data(), path,
@@ -950,7 +950,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
950950
}
951951

952952
fn drop_flags_for_args(&mut self) {
953-
let loc = Location { block: START_BLOCK, index: 0 };
953+
let loc = Location { block: START_BLOCK, statement_index: 0 };
954954
super::drop_flag_effects_for_function_entry(
955955
self.tcx, self.mir, self.env, |path, ds| {
956956
self.set_drop_flag(loc, path, ds);
@@ -990,7 +990,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
990990
}
991991
}
992992
}
993-
let loc = Location { block: bb, index: i };
993+
let loc = Location { block: bb, statement_index: i };
994994
super::drop_flag_effects_for_location(
995995
self.tcx, self.mir, self.env, loc, |path, ds| {
996996
if ds == DropFlagState::Absent || allow_initializations {
@@ -1008,7 +1008,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
10081008
} = data.terminator().kind {
10091009
assert!(!self.patch.is_patched(bb));
10101010

1011-
let loc = Location { block: bb, index: data.statements.len() };
1011+
let loc = Location { block: bb, statement_index: data.statements.len() };
10121012
let path = self.move_data().rev_lookup.find(lv);
10131013
on_all_children_bits(
10141014
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>>,
@@ -569,7 +554,7 @@ fn gather_moves<'a, 'tcx>(mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> MoveD
569554
};
570555

571556
for (i, stmt) in bb_data.statements.iter().enumerate() {
572-
let source = Location { block: bb, index: i };
557+
let source = Location { block: bb, statement_index: i };
573558
match stmt.kind {
574559
StatementKind::Assign(ref lval, ref rval) => {
575560
bb_ctxt.builder.create_move_path(lval);
@@ -631,14 +616,14 @@ 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
debug!("gather_moves Return on_move_out_lval return {:?}", source);
636621
bb_ctxt.on_move_out_lval(SK::Return, &Lvalue::ReturnPointer, source);
637622
}
638623

639624
TerminatorKind::If { ref cond, targets: _ } => {
640625
let source = Location { block: bb,
641-
index: bb_data.statements.len() };
626+
statement_index: bb_data.statements.len() };
642627
bb_ctxt.on_operand(SK::If, cond, source);
643628
}
644629

@@ -669,20 +654,20 @@ fn gather_moves<'a, 'tcx>(mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> MoveD
669654

670655
TerminatorKind::Drop { ref location, target: _, unwind: _ } => {
671656
let source = Location { block: bb,
672-
index: bb_data.statements.len() };
657+
statement_index: bb_data.statements.len() };
673658
bb_ctxt.on_move_out_lval(SK::Drop, location, source);
674659
}
675660
TerminatorKind::DropAndReplace { ref location, ref value, .. } => {
676661
let assigned_path = bb_ctxt.builder.move_path_for(location);
677662
bb_ctxt.path_map.fill_to(assigned_path.index());
678663

679664
let source = Location { block: bb,
680-
index: bb_data.statements.len() };
665+
statement_index: bb_data.statements.len() };
681666
bb_ctxt.on_operand(SK::Use, value, source);
682667
}
683668
TerminatorKind::Call { ref func, ref args, ref destination, cleanup: _ } => {
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::CallFn, func, source);
687672
for arg in args {
688673
debug!("gather_moves Call on_operand {:?} {:?}", arg, source);
@@ -757,7 +742,7 @@ impl<'b, 'tcx: 'b> BlockContext<'b, 'tcx> {
757742
stmt_kind: StmtKind,
758743
lval: &Lvalue<'tcx>,
759744
source: Location) {
760-
let i = source.index;
745+
let i = source.statement_index;
761746
let index = MoveOutIndex::new(self.moves.len());
762747

763748
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
@@ -42,6 +42,7 @@ use rustc_const_math::ConstInt;
4242

4343
use rustc::mir;
4444
use rustc::mir::visit::MutVisitor;
45+
use rustc::mir::repr::Location;
4546

4647
use std::cell::Cell;
4748
use std::io;
@@ -846,7 +847,7 @@ pub fn maybe_get_item_mir<'a, 'tcx>(cdata: Cmd,
846847
impl<'v, 'cdata, 'codemap> mir::visit::MutVisitor<'v>
847848
for MirDefIdAndSpanTranslator<'cdata, 'codemap>
848849
{
849-
fn visit_def_id(&mut self, def_id: &mut DefId) {
850+
fn visit_def_id(&mut self, def_id: &mut DefId, _: Location) {
850851
*def_id = translate_def_id(self.crate_metadata, *def_id);
851852
}
852853

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)