Skip to content

Commit 7523c73

Browse files
committed
introduce liveness constraints into NLL code
And do a bunch of gratuitious refactoring that I did not bother to separate into nice commits.
1 parent 8535a4a commit 7523c73

File tree

7 files changed

+395
-211
lines changed

7 files changed

+395
-211
lines changed

src/librustc_data_structures/indexed_set.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,19 @@ pub struct IdxSet<T: Idx> {
5353
}
5454

5555
impl<T: Idx> fmt::Debug for IdxSetBuf<T> {
56-
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result { self.bits.fmt(w) }
56+
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
57+
w.debug_list()
58+
.entries(self.iter())
59+
.finish()
60+
}
5761
}
5862

5963
impl<T: Idx> fmt::Debug for IdxSet<T> {
60-
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result { self.bits.fmt(w) }
64+
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
65+
w.debug_list()
66+
.entries(self.iter())
67+
.finish()
68+
}
6169
}
6270

6371
impl<T: Idx> IdxSetBuf<T> {

src/librustc_mir/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
1818

1919
#![feature(box_patterns)]
2020
#![feature(box_syntax)]
21+
#![feature(conservative_impl_trait)]
2122
#![feature(const_fn)]
2223
#![feature(core_intrinsics)]
2324
#![feature(i128_type)]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use rustc::mir::Mir;
12+
use rustc::infer::InferCtxt;
13+
use util::liveness::LivenessResult;
14+
15+
use super::ToRegionIndex;
16+
use super::region_infer::RegionInferenceContext;
17+
18+
pub fn generate_constraints<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
19+
regioncx: &mut RegionInferenceContext,
20+
mir: &Mir<'tcx>,
21+
liveness: &LivenessResult)
22+
{
23+
ConstraintGeneration { infcx, regioncx, mir, liveness }.add_constraints();
24+
}
25+
26+
struct ConstraintGeneration<'constrain, 'gcx: 'tcx, 'tcx: 'constrain> {
27+
infcx: &'constrain InferCtxt<'constrain, 'gcx, 'tcx>,
28+
regioncx: &'constrain mut RegionInferenceContext,
29+
mir: &'constrain Mir<'tcx>,
30+
liveness: &'constrain LivenessResult,
31+
}
32+
33+
impl<'constrain, 'gcx, 'tcx> ConstraintGeneration<'constrain, 'gcx, 'tcx> {
34+
fn add_constraints(&mut self) {
35+
// To start, add the liveness constraints.
36+
self.add_liveness_constraints();
37+
}
38+
39+
/// Liveness constraints:
40+
///
41+
/// > If a variable V is live at point P, then all regions R in the type of V
42+
/// > must include the point P.
43+
fn add_liveness_constraints(&mut self) {
44+
let tcx = self.infcx.tcx;
45+
46+
debug!("add_liveness_constraints()");
47+
for bb in self.mir.basic_blocks().indices() {
48+
debug!("add_liveness_constraints: bb={:?}", bb);
49+
50+
self.liveness.simulate_block(self.mir, bb, |location, live_locals| {
51+
debug!("add_liveness_constraints: location={:?} live_locals={:?}",
52+
location, live_locals);
53+
54+
for live_local in live_locals.iter() {
55+
let live_local_ty = self.mir.local_decls[live_local].ty;
56+
tcx.for_each_free_region(&live_local_ty, |live_region| {
57+
let vid = live_region.to_region_index();
58+
self.regioncx.add_live_point(vid, location);
59+
})
60+
}
61+
});
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)