2017-07-16 16:26:37 -04:00
|
|
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
use rustc::ty::{self, RegionKind, TyCtxt};
|
|
|
|
use rustc::mir::{Location, Mir};
|
2017-07-16 16:26:37 -04:00
|
|
|
use rustc::mir::transform::{MirPass, MirSource};
|
2017-10-24 14:20:47 -04:00
|
|
|
use rustc::infer::InferCtxt;
|
|
|
|
use rustc::util::nodemap::FxHashMap;
|
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
|
|
|
use std::collections::BTreeSet;
|
2017-09-28 00:14:34 -04:00
|
|
|
use std::fmt;
|
2017-10-25 10:59:50 -04:00
|
|
|
use util::liveness::{self, LivenessMode, LivenessResult, LocalSet};
|
2017-09-28 00:14:34 -04:00
|
|
|
|
|
|
|
use util as mir_util;
|
|
|
|
use self::mir_util::PassWhere;
|
2017-07-16 16:26:37 -04:00
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
mod constraint_generation;
|
2017-10-25 18:04:57 -04:00
|
|
|
mod subtype;
|
2017-10-09 22:16:57 -04:00
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
mod region_infer;
|
|
|
|
use self::region_infer::RegionInferenceContext;
|
2017-07-16 16:26:37 -04:00
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
mod renumber;
|
2017-07-17 22:26:21 -04:00
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
// MIR Pass for non-lexical lifetimes
|
|
|
|
pub struct NLL;
|
2017-07-16 16:26:37 -04:00
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
impl MirPass for NLL {
|
|
|
|
fn run_pass<'a, 'tcx>(
|
|
|
|
&self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
source: MirSource,
|
|
|
|
input_mir: &mut Mir<'tcx>,
|
|
|
|
) {
|
|
|
|
if !tcx.sess.opts.debugging_opts.nll {
|
|
|
|
return;
|
2017-08-03 00:16:36 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 12:09:01 -04:00
|
|
|
tcx.infer_ctxt()
|
|
|
|
.enter(|ref infcx| drop(compute_regions(infcx, source, input_mir)));
|
2017-07-17 22:26:21 -04:00
|
|
|
}
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
2017-07-17 22:26:21 -04:00
|
|
|
|
2017-10-25 12:09:01 -04:00
|
|
|
pub struct RegionComputation<'tcx> {
|
|
|
|
/// A rewritten version of the input MIR where all the regions are
|
|
|
|
/// rewritten to refer to inference variables.
|
|
|
|
pub mir: Mir<'tcx>,
|
|
|
|
|
|
|
|
/// The definitions (along with their final values) for all regions.
|
|
|
|
pub regioncx: RegionInferenceContext,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the (non-lexical) regions from the input MIR.
|
|
|
|
///
|
|
|
|
/// This may result in errors being reported.
|
|
|
|
pub fn compute_regions<'a, 'gcx, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
source: MirSource,
|
|
|
|
input_mir: &Mir<'tcx>,
|
|
|
|
) -> RegionComputation<'tcx> {
|
|
|
|
// Clone mir so we can mutate it without disturbing the rest of the compiler
|
|
|
|
let mut mir = input_mir.clone();
|
|
|
|
|
|
|
|
// Replace all regions with fresh inference variables.
|
|
|
|
let num_region_variables = renumber::renumber_mir(infcx, &mut mir);
|
|
|
|
|
|
|
|
// Compute what is live where.
|
|
|
|
let liveness = &LivenessResults {
|
|
|
|
regular: liveness::liveness_of_locals(
|
|
|
|
&mir,
|
|
|
|
LivenessMode {
|
|
|
|
include_regular_use: true,
|
|
|
|
include_drops: false,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
|
|
|
|
drop: liveness::liveness_of_locals(
|
|
|
|
&mir,
|
|
|
|
LivenessMode {
|
|
|
|
include_regular_use: false,
|
|
|
|
include_drops: true,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create the region inference context, generate the constraints,
|
|
|
|
// and then solve them.
|
|
|
|
let mut regioncx = RegionInferenceContext::new(num_region_variables);
|
|
|
|
constraint_generation::generate_constraints(infcx, &mut regioncx, &mir, source, liveness);
|
|
|
|
let errors = regioncx.solve(infcx, &mir);
|
|
|
|
|
|
|
|
assert!(errors.is_empty(), "FIXME: report region inference failures");
|
|
|
|
|
|
|
|
let computation = RegionComputation { mir, regioncx };
|
|
|
|
|
|
|
|
// Dump MIR results into a file, if that is enabled. This let us
|
|
|
|
// write unit-tests.
|
|
|
|
dump_mir_results(infcx, liveness, source, &computation);
|
|
|
|
|
|
|
|
computation
|
|
|
|
}
|
|
|
|
|
2017-10-24 16:20:47 -04:00
|
|
|
struct LivenessResults {
|
|
|
|
regular: LivenessResult,
|
|
|
|
drop: LivenessResult,
|
|
|
|
}
|
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
fn dump_mir_results<'a, 'gcx, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
|
2017-10-24 16:20:47 -04:00
|
|
|
liveness: &LivenessResults,
|
2017-10-24 14:20:47 -04:00
|
|
|
source: MirSource,
|
2017-10-25 12:09:01 -04:00
|
|
|
computation: &RegionComputation<'tcx>,
|
2017-10-24 14:20:47 -04:00
|
|
|
) {
|
|
|
|
if !mir_util::dump_enabled(infcx.tcx, "nll", source) {
|
|
|
|
return;
|
2017-07-17 22:26:21 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 12:09:01 -04:00
|
|
|
let RegionComputation {
|
|
|
|
ref mir,
|
|
|
|
ref regioncx,
|
|
|
|
} = *computation;
|
|
|
|
|
2017-10-24 16:20:47 -04:00
|
|
|
let regular_liveness_per_location: FxHashMap<_, _> = mir.basic_blocks()
|
|
|
|
.indices()
|
|
|
|
.flat_map(|bb| {
|
|
|
|
let mut results = vec![];
|
2017-10-25 10:59:50 -04:00
|
|
|
liveness
|
|
|
|
.regular
|
|
|
|
.simulate_block(&mir, bb, |location, local_set| {
|
|
|
|
results.push((location, local_set.clone()));
|
|
|
|
});
|
2017-10-24 16:20:47 -04:00
|
|
|
results
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let drop_liveness_per_location: FxHashMap<_, _> = mir.basic_blocks()
|
2017-10-24 14:20:47 -04:00
|
|
|
.indices()
|
|
|
|
.flat_map(|bb| {
|
|
|
|
let mut results = vec![];
|
2017-10-25 10:59:50 -04:00
|
|
|
liveness
|
|
|
|
.drop
|
|
|
|
.simulate_block(&mir, bb, |location, local_set| {
|
|
|
|
results.push((location, local_set.clone()));
|
|
|
|
});
|
2017-10-24 14:20:47 -04:00
|
|
|
results
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
mir_util::dump_mir(infcx.tcx, None, "nll", &0, source, mir, |pass_where, out| {
|
|
|
|
match pass_where {
|
|
|
|
// Before the CFG, dump out the values for each region variable.
|
|
|
|
PassWhere::BeforeCFG => for region in regioncx.regions() {
|
2017-10-25 12:09:01 -04:00
|
|
|
writeln!(
|
|
|
|
out,
|
|
|
|
"| {:?}: {:?}",
|
|
|
|
region,
|
|
|
|
regioncx.region_value(region)
|
|
|
|
)?;
|
2017-10-24 14:20:47 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Before each basic block, dump out the values
|
|
|
|
// that are live on entry to the basic block.
|
|
|
|
PassWhere::BeforeBlock(bb) => {
|
2017-10-25 10:59:50 -04:00
|
|
|
let s = live_variable_set(&liveness.regular.ins[bb], &liveness.drop.ins[bb]);
|
|
|
|
writeln!(out, " | Live variables on entry to {:?}: {}", bb, s)?;
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
2017-07-16 16:26:37 -04:00
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
PassWhere::InCFG(location) => {
|
2017-10-25 10:59:50 -04:00
|
|
|
let s = live_variable_set(
|
|
|
|
®ular_liveness_per_location[&location],
|
|
|
|
&drop_liveness_per_location[&location],
|
|
|
|
);
|
2017-10-25 12:09:01 -04:00
|
|
|
writeln!(out, " | Live variables at {:?}: {}", location, s)?;
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
2017-07-16 16:26:37 -04:00
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
PassWhere::AfterCFG => {}
|
2017-07-17 22:26:21 -04:00
|
|
|
}
|
2017-10-24 14:20:47 -04:00
|
|
|
Ok(())
|
|
|
|
});
|
2017-09-26 22:24:19 -04:00
|
|
|
}
|
|
|
|
|
2017-09-28 00:14:34 -04:00
|
|
|
#[derive(Clone, Default, PartialEq, Eq)]
|
2017-10-10 14:25:39 -03:00
|
|
|
pub struct Region {
|
2017-10-24 14:20:47 -04:00
|
|
|
points: BTreeSet<Location>,
|
2017-09-26 22:24:19 -04:00
|
|
|
}
|
2017-09-27 14:29:11 -03:00
|
|
|
|
2017-09-28 00:14:34 -04:00
|
|
|
impl fmt::Debug for Region {
|
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
write!(formatter, "{:?}", self.points)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 22:16:57 -04:00
|
|
|
impl Region {
|
|
|
|
pub fn add_point(&mut self, point: Location) -> bool {
|
|
|
|
self.points.insert(point)
|
|
|
|
}
|
2017-09-28 00:14:34 -04:00
|
|
|
|
2017-10-09 22:16:57 -04:00
|
|
|
pub fn may_contain(&self, point: Location) -> bool {
|
|
|
|
self.points.contains(&point)
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 00:14:34 -04:00
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
newtype_index!(RegionIndex {
|
|
|
|
DEBUG_NAME = "R",
|
|
|
|
});
|
|
|
|
|
|
|
|
/// Right now, we piggy back on the `ReVar` to store our NLL inference
|
|
|
|
/// regions. These are indexed with `RegionIndex`. This method will
|
|
|
|
/// assert that the region is a `ReVar` and convert the internal index
|
|
|
|
/// into a `RegionIndex`. This is reasonable because in our MIR we
|
|
|
|
/// replace all free regions with inference variables.
|
|
|
|
trait ToRegionIndex {
|
|
|
|
fn to_region_index(&self) -> RegionIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToRegionIndex for RegionKind {
|
|
|
|
fn to_region_index(&self) -> RegionIndex {
|
|
|
|
if let &ty::ReVar(vid) = self {
|
|
|
|
RegionIndex::new(vid.index as usize)
|
|
|
|
} else {
|
|
|
|
bug!("region is not an ReVar: {:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 10:59:50 -04:00
|
|
|
|
|
|
|
fn live_variable_set(regular: &LocalSet, drops: &LocalSet) -> String {
|
|
|
|
// sort and deduplicate:
|
|
|
|
let all_locals: BTreeSet<_> = regular.iter().chain(drops.iter()).collect();
|
|
|
|
|
|
|
|
// construct a string with each local, including `(drop)` if it is
|
|
|
|
// only dropped, versus a regular use.
|
|
|
|
let mut string = String::new();
|
|
|
|
for local in all_locals {
|
|
|
|
string.push_str(&format!("{:?}", local));
|
|
|
|
|
|
|
|
if !regular.contains(&local) {
|
|
|
|
assert!(drops.contains(&local));
|
|
|
|
string.push_str(" (drop)");
|
|
|
|
}
|
|
|
|
|
|
|
|
string.push_str(", ");
|
|
|
|
}
|
|
|
|
|
2017-10-25 12:09:01 -04:00
|
|
|
let len = if string.is_empty() {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
string.len() - 2
|
|
|
|
};
|
2017-10-25 10:59:50 -04:00
|
|
|
|
|
|
|
format!("[{}]", &string[..len])
|
|
|
|
}
|