提交 72675d82 编写于 作者: N Niko Matsakis

replace `RegionIndex` with `RegionVid` (which now impls Idx)

上级 89c1b600
......@@ -22,7 +22,7 @@
use dataflow::{BitDenotation, BlockSets, DataflowOperator};
pub use dataflow::indexes::BorrowIndex;
use transform::nll::region_infer::RegionInferenceContext;
use transform::nll::ToRegionIndex;
use transform::nll::ToRegionVid;
use syntax_pos::Span;
......@@ -145,7 +145,7 @@ fn kill_loans_out_of_scope_at_location(&self,
location: Location) {
if let Some(regioncx) = self.nonlexical_regioncx {
for (borrow_index, borrow_data) in self.borrows.iter_enumerated() {
let borrow_region = borrow_data.region.to_region_index();
let borrow_region = borrow_data.region.to_region_vid();
if !regioncx.region_contains_point(borrow_region, location) {
// The region checker really considers the borrow
// to start at the point **after** the location of
......
......@@ -23,7 +23,7 @@
use super::subtype;
use super::LivenessResults;
use super::ToRegionIndex;
use super::ToRegionVid;
use super::region_infer::RegionInferenceContext;
pub(super) fn generate_constraints<'a, 'gcx, 'tcx>(
......@@ -102,7 +102,7 @@ fn add_regular_live_constraint<T>(&mut self, live_ty: T, location: Location)
self.infcx
.tcx
.for_each_free_region(&live_ty, |live_region| {
let vid = live_region.to_region_index();
let vid = live_region.to_region_vid();
self.regioncx.add_live_point(vid, location);
});
}
......@@ -197,8 +197,8 @@ fn add_borrow_constraint(
};
self.regioncx.add_outlives(span,
borrow_region.to_region_index(),
destination_region.to_region_index(),
borrow_region.to_region_vid(),
destination_region.to_region_vid(),
location.successor_within_block());
}
......@@ -227,8 +227,8 @@ fn add_reborrow_constraint(
let span = self.mir.source_info(location).span;
self.regioncx.add_outlives(span,
base_region.to_region_index(),
borrow_region.to_region_index(),
base_region.to_region_vid(),
borrow_region.to_region_vid(),
location.successor_within_block());
}
}
......
......@@ -25,20 +25,18 @@
use rustc::hir::def_id::DefId;
use rustc::infer::InferCtxt;
use rustc::middle::free_region::FreeRegionMap;
use rustc::ty;
use rustc::ty::{self, RegionVid};
use rustc::ty::subst::Substs;
use rustc::util::nodemap::FxHashMap;
use rustc_data_structures::indexed_vec::Idx;
use super::RegionIndex;
#[derive(Debug)]
pub struct FreeRegions<'tcx> {
/// Given a free region defined on this function (either early- or
/// late-bound), this maps it to its internal region index. When
/// the region context is created, the first N variables will be
/// created based on these indices.
pub indices: FxHashMap<ty::Region<'tcx>, RegionIndex>,
pub indices: FxHashMap<ty::Region<'tcx>, RegionVid>,
/// The map from the typeck tables telling us how to relate free regions.
pub free_region_map: &'tcx FreeRegionMap<'tcx>,
......@@ -81,9 +79,9 @@ pub fn free_regions<'a, 'gcx, 'tcx>(
}
fn insert_free_region<'tcx>(
free_regions: &mut FxHashMap<ty::Region<'tcx>, RegionIndex>,
free_regions: &mut FxHashMap<ty::Region<'tcx>, RegionVid>,
region: ty::Region<'tcx>,
) {
let next = RegionIndex::new(free_regions.len());
let next = RegionVid::new(free_regions.len());
free_regions.entry(region).or_insert(next);
}
......@@ -11,9 +11,8 @@
use rustc::hir::def_id::DefId;
use rustc::mir::Mir;
use rustc::infer::InferCtxt;
use rustc::ty::{self, RegionKind};
use rustc::ty::{self, RegionKind, RegionVid};
use rustc::util::nodemap::FxHashMap;
use rustc_data_structures::indexed_vec::Idx;
use std::collections::BTreeSet;
use transform::MirSource;
use util::liveness::{self, LivenessMode, LivenessResult, LocalSet};
......@@ -152,23 +151,19 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
});
}
newtype_index!(RegionIndex {
DEBUG_FORMAT = "'_#{}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.
pub trait ToRegionIndex {
fn to_region_index(&self) -> RegionIndex;
/// regions. These are indexed with `RegionVid`. This method will
/// assert that the region is a `ReVar` and extract its interal index.
/// This is reasonable because in our MIR we replace all free regions
/// with inference variables.
pub trait ToRegionVid {
fn to_region_vid(&self) -> RegionVid;
}
impl ToRegionIndex for RegionKind {
fn to_region_index(&self) -> RegionIndex {
impl ToRegionVid for RegionKind {
fn to_region_vid(&self) -> RegionVid {
if let &ty::ReVar(vid) = self {
RegionIndex::new(vid.index as usize)
vid
} else {
bug!("region is not an ReVar: {:?}", self)
}
......
......@@ -8,11 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use super::RegionIndex;
use super::free_regions::FreeRegions;
use rustc::infer::InferCtxt;
use rustc::mir::{Location, Mir};
use rustc::ty;
use rustc::ty::{self, RegionVid};
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_data_structures::fx::FxHashSet;
use std::collections::BTreeSet;
......@@ -21,10 +20,10 @@
pub struct RegionInferenceContext<'tcx> {
/// Contains the definition for every region variable. Region
/// variables are identified by their index (`RegionIndex`). The
/// variables are identified by their index (`RegionVid`). The
/// definition contains information about where the region came
/// from as well as its final inferred value.
definitions: IndexVec<RegionIndex, RegionDefinition<'tcx>>,
definitions: IndexVec<RegionVid, RegionDefinition<'tcx>>,
/// The indices of all "free regions" in scope. These are the
/// lifetime parameters (anonymous and named) declared in the
......@@ -35,7 +34,7 @@ pub struct RegionInferenceContext<'tcx> {
///
/// These indices will be from 0..N, as it happens, but we collect
/// them into a vector for convenience.
free_regions: Vec<RegionIndex>,
free_regions: Vec<RegionVid>,
/// The constraints we have accumulated and used during solving.
constraints: Vec<Constraint>,
......@@ -66,7 +65,7 @@ struct RegionDefinition<'tcx> {
#[derive(Clone, Default, PartialEq, Eq)]
struct Region {
points: BTreeSet<Location>,
free_regions: BTreeSet<RegionIndex>,
free_regions: BTreeSet<RegionVid>,
}
impl fmt::Debug for Region {
......@@ -84,7 +83,7 @@ fn add_point(&mut self, point: Location) -> bool {
self.points.insert(point)
}
fn add_free_region(&mut self, region: RegionIndex) -> bool {
fn add_free_region(&mut self, region: RegionVid) -> bool {
self.free_regions.insert(region)
}
......@@ -99,10 +98,10 @@ pub struct Constraint {
span: Span,
/// The region SUP must outlive SUB...
sup: RegionIndex,
sup: RegionVid,
/// Region that must be outlived.
sub: RegionIndex,
sub: RegionVid,
/// At this location.
point: Location,
......@@ -198,24 +197,24 @@ fn init_free_regions(&mut self, free_regions: &FreeRegions<'tcx>, mir: &Mir<'tcx
}
/// Returns an iterator over all the region indices.
pub fn regions(&self) -> impl Iterator<Item = RegionIndex> {
pub fn regions(&self) -> impl Iterator<Item = RegionVid> {
self.definitions.indices()
}
/// Returns true if the region `r` contains the point `p`.
///
/// Until `solve()` executes, this value is not particularly meaningful.
pub fn region_contains_point(&self, r: RegionIndex, p: Location) -> bool {
pub fn region_contains_point(&self, r: RegionVid, p: Location) -> bool {
self.definitions[r].value.contains_point(p)
}
/// Returns access to the value of `r` for debugging purposes.
pub(super) fn region_value(&self, r: RegionIndex) -> &fmt::Debug {
pub(super) fn region_value(&self, r: RegionVid) -> &fmt::Debug {
&self.definitions[r].value
}
/// Indicates that the region variable `v` is live at the point `point`.
pub(super) fn add_live_point(&mut self, v: RegionIndex, point: Location) {
pub(super) fn add_live_point(&mut self, v: RegionVid, point: Location) {
debug!("add_live_point({:?}, {:?})", v, point);
let definition = &mut self.definitions[v];
if !definition.constant {
......@@ -231,8 +230,8 @@ pub(super) fn add_live_point(&mut self, v: RegionIndex, point: Location) {
pub(super) fn add_outlives(
&mut self,
span: Span,
sup: RegionIndex,
sub: RegionIndex,
sup: RegionVid,
sub: RegionVid,
point: Location,
) {
debug!("add_outlives({:?}: {:?} @ {:?}", sup, sub, point);
......@@ -268,7 +267,7 @@ pub(super) fn solve(&mut self, infcx: &InferCtxt<'a, 'gcx, 'tcx>, mir: &Mir<'tcx
fn propagate_constraints(
&mut self,
mir: &Mir<'tcx>,
) -> Vec<(RegionIndex, Span, RegionIndex)> {
) -> Vec<(RegionVid, Span, RegionVid)> {
let mut changed = true;
let mut dfs = Dfs::new(mir);
let mut error_regions = FxHashSet();
......
......@@ -17,7 +17,6 @@
use syntax_pos::DUMMY_SP;
use std::collections::HashMap;
use super::RegionIndex;
use super::free_regions::FreeRegions;
/// Replaces all free regions appearing in the MIR with fresh
......@@ -52,7 +51,7 @@ struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
num_region_variables: usize,
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
free_regions: &'a FreeRegions<'tcx>,
free_region_inference_vars: IndexVec<RegionIndex, ty::Region<'tcx>>,
free_region_inference_vars: IndexVec<RegionVid, ty::Region<'tcx>>,
arg_count: usize,
}
......
......@@ -8,15 +8,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use super::RegionIndex;
use transform::nll::ToRegionIndex;
use rustc::ty::{self, Ty, TyCtxt};
use transform::nll::ToRegionVid;
use rustc::ty::{self, Ty, TyCtxt, RegionVid};
use rustc::ty::relate::{self, Relate, RelateResult, TypeRelation};
pub fn outlives_pairs<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
a: Ty<'tcx>,
b: Ty<'tcx>)
-> Vec<(RegionIndex, RegionIndex)>
-> Vec<(RegionVid, RegionVid)>
{
let mut subtype = Subtype::new(tcx);
match subtype.relate(&a, &b) {
......@@ -28,7 +27,7 @@ pub fn outlives_pairs<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
struct Subtype<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
tcx: TyCtxt<'a, 'gcx, 'tcx>,
outlives_pairs: Vec<(RegionIndex, RegionIndex)>,
outlives_pairs: Vec<(RegionVid, RegionVid)>,
ambient_variance: ty::Variance,
}
......@@ -67,8 +66,8 @@ fn tys(&mut self, t: Ty<'tcx>, t2: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
fn regions(&mut self, r_a: ty::Region<'tcx>, r_b: ty::Region<'tcx>)
-> RelateResult<'tcx, ty::Region<'tcx>> {
let a = r_a.to_region_index();
let b = r_b.to_region_index();
let a = r_a.to_region_vid();
let b = r_b.to_region_vid();
match self.ambient_variance {
ty::Covariant => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册