Skip to content

Commit d25231f

Browse files
committed
use LiveVariableMap as trait bound
1 parent f2b5583 commit d25231f

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

src/librustc_mir/borrow_check/nll/type_check/liveness.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc::ty::{Ty, TypeFoldable};
2222
use rustc_data_structures::fx::FxHashMap;
2323
use rustc_data_structures::indexed_vec::Idx;
2424
use std::rc::Rc;
25-
use util::liveness::LivenessResults;
25+
use util::liveness::{LivenessResults, LiveVariableMap};
2626

2727
use super::TypeChecker;
2828

@@ -34,10 +34,10 @@ use super::TypeChecker;
3434
///
3535
/// NB. This computation requires normalization; therefore, it must be
3636
/// performed before
37-
pub(super) fn generate<'gcx, 'tcx>(
37+
pub(super) fn generate<'gcx, 'tcx, V: LiveVariableMap>(
3838
cx: &mut TypeChecker<'_, 'gcx, 'tcx>,
3939
mir: &Mir<'tcx>,
40-
liveness: &LivenessResults<Local>,
40+
liveness: &LivenessResults<V>,
4141
flow_inits: &mut FlowAtLocation<MaybeInitializedPlaces<'_, 'gcx, 'tcx>>,
4242
move_data: &MoveData<'tcx>,
4343
) {
@@ -55,16 +55,17 @@ pub(super) fn generate<'gcx, 'tcx>(
5555
}
5656
}
5757

58-
struct TypeLivenessGenerator<'gen, 'typeck, 'flow, 'gcx, 'tcx>
58+
struct TypeLivenessGenerator<'gen, 'typeck, 'flow, 'gcx, 'tcx, V: LiveVariableMap>
5959
where
6060
'typeck: 'gen,
6161
'flow: 'gen,
6262
'tcx: 'typeck + 'flow,
6363
'gcx: 'tcx,
64+
V: 'gen,
6465
{
6566
cx: &'gen mut TypeChecker<'typeck, 'gcx, 'tcx>,
6667
mir: &'gen Mir<'tcx>,
67-
liveness: &'gen LivenessResults<Local>,
68+
liveness: &'gen LivenessResults<V>,
6869
flow_inits: &'gen mut FlowAtLocation<MaybeInitializedPlaces<'flow, 'gcx, 'tcx>>,
6970
move_data: &'gen MoveData<'tcx>,
7071
drop_data: FxHashMap<Ty<'tcx>, DropData<'tcx>>,
@@ -75,7 +76,7 @@ struct DropData<'tcx> {
7576
region_constraint_data: Option<Rc<Vec<QueryRegionConstraint<'tcx>>>>,
7677
}
7778

78-
impl<'gen, 'typeck, 'flow, 'gcx, 'tcx> TypeLivenessGenerator<'gen, 'typeck, 'flow, 'gcx, 'tcx> {
79+
impl<'gen, 'typeck, 'flow, 'gcx, 'tcx, V:LiveVariableMap> TypeLivenessGenerator<'gen, 'typeck, 'flow, 'gcx, 'tcx, V> {
7980
/// Liveness constraints:
8081
///
8182
/// > If a variable V is live at point P, then all regions R in the type of V
@@ -92,7 +93,7 @@ impl<'gen, 'typeck, 'flow, 'gcx, 'tcx> TypeLivenessGenerator<'gen, 'typeck, 'flo
9293
}
9394
});
9495

95-
let mut all_live_locals: Vec<(Location, Vec<Local>)> = vec![];
96+
let mut all_live_locals: Vec<(Location, Vec<V::LiveVar>)> = vec![];
9697
self.liveness
9798
.drop
9899
.simulate_block(self.mir, bb, |location, live_locals| {

src/librustc_mir/util/liveness.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ use rustc::ty::TyCtxt;
4848
use std::io::{self, Write};
4949
use transform::MirSource;
5050

51-
pub type LocalSet<V: Idx> = IdxSetBuf<V>;
51+
pub type LocalSet<V: LiveVariableMap> = IdxSetBuf<V::LiveVar>;
5252

5353
/// This gives the result of the liveness analysis at the boundary of
5454
/// basic blocks. You can use `simulate_block` to obtain the
5555
/// intra-block results.
56-
pub struct LivenessResult<V: Idx> {
56+
pub struct LivenessResult<V: LiveVariableMap> {
5757
/// Liveness mode in use when these results were computed.
5858
pub mode: LivenessMode,
5959

6060
/// Live variables on exit to each basic block. This is equal to
6161
/// the union of the `ins` for each successor.
62-
pub outs: IndexVec<BasicBlock, LocalSet<V>>,
62+
pub outs: IndexVec<BasicBlock, LocalSet<V::LiveVar>>,
6363
}
6464

65-
trait LiveVariableMap {
65+
pub(crate) trait LiveVariableMap {
6666
type LiveVar;
6767

6868
fn from_local(&self, local: Local) -> Option<Self::LiveVar>;
@@ -103,18 +103,18 @@ pub struct LivenessMode {
103103
}
104104

105105
/// A combination of liveness results, used in NLL.
106-
pub struct LivenessResults<V: Idx> {
106+
pub struct LivenessResults<V: LiveVariableMap> {
107107
/// Liveness results where a regular use makes a variable X live,
108108
/// but not a drop.
109-
pub regular: LivenessResult<V>,
109+
pub regular: LivenessResult<V::LiveVar>,
110110

111111
/// Liveness results where a drop makes a variable X live,
112112
/// but not a regular use.
113-
pub drop: LivenessResult<V>,
113+
pub drop: LivenessResult<V::LiveVar>,
114114
}
115115

116-
impl<V: Idx> LivenessResults<V> {
117-
pub fn compute<'tcx>(mir: &Mir<'tcx>, map: &dyn LiveVariableMap<LiveVar = V>) -> LivenessResults<V> {
116+
impl<V: LiveVariableMap> LivenessResults<V> {
117+
pub fn compute<'tcx>(mir: &Mir<'tcx>, map: &dyn LiveVariableMap<LiveVar = V>) -> LivenessResults<V::LiveVar> {
118118
LivenessResults {
119119
regular: liveness_of_locals(
120120
&mir,
@@ -138,7 +138,7 @@ impl<V: Idx> LivenessResults<V> {
138138
/// Compute which local variables are live within the given function
139139
/// `mir`. The liveness mode `mode` determines what sorts of uses are
140140
/// considered to make a variable live (e.g., do drops count?).
141-
pub fn liveness_of_locals<'tcx, V: Idx>(mir: &Mir<'tcx>, mode: LivenessMode) -> LivenessResult<V> {
141+
pub fn liveness_of_locals<'tcx, V: LiveVariableMap>(mir: &Mir<'tcx>, mode: LivenessMode) -> LivenessResult<V::LiveVar> {
142142
let locals = mir.local_decls.len();
143143
let def_use: IndexVec<_, _> = mir.basic_blocks()
144144
.iter()
@@ -179,8 +179,7 @@ pub fn liveness_of_locals<'tcx, V: Idx>(mir: &Mir<'tcx>, mode: LivenessMode) ->
179179
LivenessResult { mode, outs }
180180
}
181181

182-
impl<V> LivenessResult<V>
183-
where V:Idx
182+
impl<V: LiveVariableMap> LivenessResult<V>
184183
{
185184
/// Walks backwards through the statements/terminator in the given
186185
/// basic block `block`. At each point within `block`, invokes
@@ -422,12 +421,13 @@ fn block<'tcx, 'lv>(mode: LivenessMode, b: &BasicBlockData<'tcx>, locals: usize)
422421
visitor.defs_uses
423422
}
424423

425-
pub fn dump_mir<'a, 'tcx, V: Idx>(
424+
pub fn dump_mir<'a, 'tcx, V: LiveVariableMap>(
426425
tcx: TyCtxt<'a, 'tcx, 'tcx>,
427426
pass_name: &str,
428427
source: MirSource,
429428
mir: &Mir<'tcx>,
430429
result: &LivenessResult<Local>,
430+
map: &impl LiveVariableMap<LiveVar = V>
431431
) {
432432
if !dump_enabled(tcx, pass_name, source) {
433433
return;
@@ -439,13 +439,13 @@ pub fn dump_mir<'a, 'tcx, V: Idx>(
439439
dump_matched_mir_node(tcx, pass_name, &node_path, source, mir, result);
440440
}
441441

442-
fn dump_matched_mir_node<'a, 'tcx, V: Idx>(
442+
fn dump_matched_mir_node<'a, 'tcx, V: LiveVariableMap>(
443443
tcx: TyCtxt<'a, 'tcx, 'tcx>,
444444
pass_name: &str,
445445
node_path: &str,
446446
source: MirSource,
447447
mir: &Mir<'tcx>,
448-
result: &LivenessResult<V>,
448+
result: &LivenessResult<V::LiveVar>,
449449
) {
450450
let mut file_path = PathBuf::new();
451451
file_path.push(Path::new(&tcx.sess.opts.debugging_opts.dump_mir_dir));
@@ -462,16 +462,16 @@ fn dump_matched_mir_node<'a, 'tcx, V: Idx>(
462462
});
463463
}
464464

465-
pub fn write_mir_fn<'a, 'tcx, V :Idx>(
465+
pub fn write_mir_fn<'a, 'tcx, V: LiveVariableMap>(
466466
tcx: TyCtxt<'a, 'tcx, 'tcx>,
467467
src: MirSource,
468468
mir: &Mir<'tcx>,
469469
w: &mut dyn Write,
470-
result: &LivenessResult<V>,
470+
result: &LivenessResult<V::LiveVar>,
471471
) -> io::Result<()> {
472472
write_mir_intro(tcx, src, mir, w)?;
473473
for block in mir.basic_blocks().indices() {
474-
let print = |w: &mut dyn Write, prefix, result: &IndexVec<BasicBlock, LocalSet<V>>| {
474+
let print = |w: &mut dyn Write, prefix, result: &IndexVec<BasicBlock, LocalSet<Local>>| {
475475
let live: Vec<String> = mir.local_decls
476476
.indices()
477477
.filter(|i| result[block].contains(i))

0 commit comments

Comments
 (0)