Skip to content

Commit 492e2f0

Browse files
committed
make RegionVid implement Idx and use IndexVec
1 parent 2cb92d1 commit 492e2f0

File tree

4 files changed

+49
-25
lines changed

4 files changed

+49
-25
lines changed

src/librustc/infer/lexical_region_resolve/mod.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use infer::region_constraints::GenericKind;
1717
use infer::region_constraints::RegionConstraintData;
1818
use infer::region_constraints::VerifyBound;
1919
use middle::free_region::RegionRelations;
20+
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
2021
use rustc_data_structures::fx::FxHashSet;
2122
use rustc_data_structures::graph::{self, Direction, NodeIndex, OUTGOING};
2223
use std::fmt;
@@ -29,7 +30,7 @@ use ty::{ReLateBound, ReScope, ReSkolemized, ReVar};
2930
mod graphviz;
3031

3132
pub struct LexicalRegionResolutions<'tcx> {
32-
values: Vec<VarValue<'tcx>>,
33+
values: IndexVec<RegionVid, VarValue<'tcx>>,
3334
error_region: ty::Region<'tcx>,
3435
}
3536

@@ -114,7 +115,7 @@ impl<'tcx> RegionConstraintData<'tcx> {
114115

115116
(&ReVar(v_id), _) | (_, &ReVar(v_id)) => {
116117
span_bug!(
117-
self.var_origins[v_id.index as usize].span(),
118+
self.var_origins[v_id].span(),
118119
"lub_concrete_regions invoked with non-concrete \
119120
regions: {:?}, {:?}",
120121
a,
@@ -211,7 +212,7 @@ impl<'tcx> RegionConstraintData<'tcx> {
211212
fn construct_var_data(&self, tcx: TyCtxt<'_, '_, 'tcx>) -> LexicalRegionResolutions<'tcx> {
212213
LexicalRegionResolutions {
213214
error_region: tcx.types.re_static,
214-
values: (0..self.num_vars() as usize)
215+
values: (0..self.num_vars())
215216
.map(|_| VarValue::Value(tcx.types.re_empty))
216217
.collect(),
217218
}
@@ -240,11 +241,20 @@ impl<'tcx> RegionConstraintData<'tcx> {
240241

241242
let seeds: Vec<_> = self.givens.iter().cloned().collect();
242243
for (r, vid) in seeds {
244+
245+
// While all things transitively reachable in the graph
246+
// from the variable (`'0` in the example above).
243247
let seed_index = NodeIndex(vid.index as usize);
244248
for succ_index in graph.depth_traverse(seed_index, OUTGOING) {
245-
let succ_index = succ_index.0 as u32;
249+
let succ_index = succ_index.0;
250+
251+
// The first N nodes correspond to the region
252+
// variables. Other nodes correspond to constant
253+
// regions.
246254
if succ_index < self.num_vars() {
247-
let succ_vid = RegionVid { index: succ_index };
255+
let succ_vid = RegionVid::new(succ_index);
256+
257+
// Add `'c <= '1`.
248258
self.givens.insert((r, succ_vid));
249259
}
250260
}
@@ -442,11 +452,10 @@ impl<'tcx> RegionConstraintData<'tcx> {
442452
// idea is to report errors that derive from independent
443453
// regions of the graph, but not those that derive from
444454
// overlapping locations.
445-
let mut dup_vec = vec![u32::MAX; self.num_vars() as usize];
455+
let mut dup_vec = vec![u32::MAX; self.num_vars()];
446456

447-
for index in 0..self.num_vars() {
448-
let node_vid = RegionVid { index };
449-
match var_data.value(node_vid) {
457+
for (node_vid, value) in var_data.values.iter_enumerated() {
458+
match *value {
450459
VarValue::Value(_) => { /* Inference successful */ }
451460
VarValue::ErrorValue => {
452461
/* Inference impossible, this value contains
@@ -560,7 +569,7 @@ impl<'tcx> RegionConstraintData<'tcx> {
560569
for lower_bound in &lower_bounds {
561570
for upper_bound in &upper_bounds {
562571
if !region_rels.is_subregion_of(lower_bound.region, upper_bound.region) {
563-
let origin = self.var_origins[node_idx.index as usize].clone();
572+
let origin = self.var_origins[node_idx].clone();
564573
debug!(
565574
"region inference error at {:?} for {:?}: SubSupConflict sub: {:?} \
566575
sup: {:?}",
@@ -582,7 +591,7 @@ impl<'tcx> RegionConstraintData<'tcx> {
582591
}
583592

584593
span_bug!(
585-
self.var_origins[node_idx.index as usize].span(),
594+
self.var_origins[node_idx].span(),
586595
"collect_error_for_expanding_node() could not find \
587596
error for var {:?}, lower_bounds={:?}, \
588597
upper_bounds={:?}",
@@ -741,15 +750,15 @@ impl<'tcx> LexicalRegionResolutions<'tcx> {
741750
}
742751

743752
fn value(&self, rid: RegionVid) -> &VarValue<'tcx> {
744-
&self.values[rid.index as usize]
753+
&self.values[rid]
745754
}
746755

747756
fn value_mut(&mut self, rid: RegionVid) -> &mut VarValue<'tcx> {
748-
&mut self.values[rid.index as usize]
757+
&mut self.values[rid]
749758
}
750759

751760
pub fn resolve_var(&self, rid: RegionVid) -> ty::Region<'tcx> {
752-
let result = match self.values[rid.index as usize] {
761+
let result = match self.values[rid] {
753762
VarValue::Value(r) => r,
754763
VarValue::ErrorValue => self.error_region,
755764
};

src/librustc/infer/region_constraints/mod.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use self::CombineMapType::*;
1616
use super::{MiscVariable, RegionVariableOrigin, SubregionOrigin};
1717
use super::unify_key;
1818

19+
use rustc_data_structures::indexed_vec::IndexVec;
1920
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2021
use rustc_data_structures::unify::{self, UnificationTable};
2122
use ty::{self, Ty, TyCtxt};
@@ -51,14 +52,16 @@ pub struct RegionConstraintCollector<'tcx> {
5152
unification_table: UnificationTable<ty::RegionVid>,
5253
}
5354

55+
pub type VarOrigins = IndexVec<RegionVid, RegionVariableOrigin>;
56+
5457
/// The full set of region constraints gathered up by the collector.
5558
/// Describes a set of region variables ranging from 0..N (where N is
5659
/// the length of the `var_origins` vector), and various constraints
5760
/// between them.
5861
#[derive(Default)]
5962
pub struct RegionConstraintData<'tcx> {
6063
/// For each `RegionVid`, the corresponding `RegionVariableOrigin`.
61-
pub var_origins: Vec<RegionVariableOrigin>,
64+
pub var_origins: IndexVec<RegionVid, RegionVariableOrigin>,
6265

6366
/// Constraints of the form `A <= B`, where either `A` or `B` can
6467
/// be a region variable (or neither, as it happens).
@@ -344,10 +347,7 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
344347
}
345348

346349
pub fn new_region_var(&mut self, origin: RegionVariableOrigin) -> RegionVid {
347-
let vid = RegionVid {
348-
index: self.data.num_vars(),
349-
};
350-
self.data.var_origins.push(origin.clone());
350+
let vid = self.data.var_origins.push(origin.clone());
351351

352352
let u_vid = self.unification_table
353353
.new_key(unify_key::RegionVidKey { min_vid: vid });
@@ -364,7 +364,7 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
364364
}
365365

366366
pub fn var_origin(&self, vid: RegionVid) -> RegionVariableOrigin {
367-
self.data.var_origins[vid.index as usize].clone()
367+
self.data.var_origins[vid].clone()
368368
}
369369

370370
/// Creates a new skolemized region. Skolemized regions are fresh
@@ -862,10 +862,7 @@ impl<'a, 'gcx, 'tcx> VerifyBound<'tcx> {
862862
}
863863

864864
impl<'tcx> RegionConstraintData<'tcx> {
865-
pub fn num_vars(&self) -> u32 {
866-
let len = self.var_origins.len();
867-
// enforce no overflow
868-
assert!(len as u32 as usize == len);
869-
len as u32
865+
pub fn num_vars(&self) -> usize {
866+
self.var_origins.len()
870867
}
871868
}

src/librustc/ty/sty.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use hir::def_id::DefId;
1414

1515
use middle::const_val::ConstVal;
1616
use middle::region;
17+
use rustc_data_structures::indexed_vec::Idx;
1718
use ty::subst::{Substs, Subst};
1819
use ty::{self, AdtDef, TypeFlags, Ty, TyCtxt, TypeFoldable};
1920
use ty::{Slice, TyS};
@@ -898,6 +899,18 @@ pub struct RegionVid {
898899
pub index: u32,
899900
}
900901

902+
// TODO after rebasing, should be able to use `newtype_index!`
903+
impl Idx for RegionVid {
904+
fn new(value: usize) -> Self {
905+
assert!(value < ::std::u32::MAX as usize);
906+
RegionVid { index: value as u32 }
907+
}
908+
909+
fn index(self) -> usize {
910+
self.index as usize
911+
}
912+
}
913+
901914
#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, PartialOrd, Ord)]
902915
pub struct SkolemizedRegionVid {
903916
pub index: u32,

src/librustc_data_structures/indexed_vec.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,11 @@ impl<I: Idx, T> IndexVec<I, T> {
384384
idx
385385
}
386386

387+
#[inline]
388+
pub fn pop(&mut self) -> Option<T> {
389+
self.raw.pop()
390+
}
391+
387392
#[inline]
388393
pub fn len(&self) -> usize {
389394
self.raw.len()

0 commit comments

Comments
 (0)