Skip to content

Commit 740fb0c

Browse files
committed
Use IndexVec instead of usize in librustc
1 parent 19ae2b9 commit 740fb0c

File tree

13 files changed

+108
-68
lines changed

13 files changed

+108
-68
lines changed

src/Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,7 @@ dependencies = [
24002400
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
24012401
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
24022402
"rustc_cratesio_shim 0.0.0",
2403+
"rustc_data_structures 0.0.0",
24032404
"serialize 0.0.0",
24042405
]
24052406

src/librustc/middle/intrinsicck.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
use hir::def::Def;
1212
use hir::def_id::DefId;
1313
use ty::{self, Ty, TyCtxt};
14-
use ty::layout::{LayoutError, Pointer, SizeSkeleton};
14+
use ty::layout::{LayoutError, Pointer, SizeSkeleton, VariantIdx};
1515

1616
use rustc_target::spec::abi::Abi::RustIntrinsic;
17+
use rustc_data_structures::indexed_vec::Idx;
1718
use syntax_pos::Span;
1819
use hir::intravisit::{self, Visitor, NestedVisitorMap};
1920
use hir;
@@ -48,10 +49,13 @@ fn unpack_option_like<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
4849
if def.variants.len() == 2 && !def.repr.c() && def.repr.int.is_none() {
4950
let data_idx;
5051

51-
if def.variants[0].fields.is_empty() {
52-
data_idx = 1;
53-
} else if def.variants[1].fields.is_empty() {
54-
data_idx = 0;
52+
let one = VariantIdx::new(1);
53+
let zero = VariantIdx::new(0);
54+
55+
if def.variants[zero].fields.is_empty() {
56+
data_idx = one;
57+
} else if def.variants[one].fields.is_empty() {
58+
data_idx = zero;
5559
} else {
5660
return ty;
5761
}

src/librustc/middle/mem_categorization.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ use hir::def::{Def, CtorKind};
7676
use ty::adjustment;
7777
use ty::{self, Ty, TyCtxt};
7878
use ty::fold::TypeFoldable;
79+
use ty::layout::VariantIdx;
7980

8081
use hir::{MutImmutable, MutMutable, PatKind};
8182
use hir::pat_util::EnumerateAndAdjustIterator;
@@ -87,6 +88,7 @@ use std::borrow::Cow;
8788
use std::fmt;
8889
use std::hash::{Hash, Hasher};
8990
use rustc_data_structures::sync::Lrc;
91+
use rustc_data_structures::indexed_vec::Idx;
9092
use std::rc::Rc;
9193
use util::nodemap::ItemLocalSet;
9294

@@ -227,7 +229,7 @@ impl<'tcx> cmt_<'tcx> {
227229
}
228230
_ => {
229231
assert_eq!(adt_def.variants.len(), 1);
230-
&adt_def.variants[0]
232+
&adt_def.variants[VariantIdx::new(0)]
231233
}
232234
};
233235
Some((adt_def, &variant_def.fields[field_index]))

src/librustc/mir/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use syntax_pos::{Span, DUMMY_SP};
4040
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
4141
use ty::subst::{CanonicalUserSubsts, Subst, Substs};
4242
use ty::{self, AdtDef, CanonicalTy, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt};
43+
use ty::layout::VariantIdx;
4344
use util::ppaux;
4445

4546
pub use mir::interpret::AssertMessage;
@@ -1939,7 +1940,7 @@ pub enum ProjectionElem<'tcx, V, T> {
19391940
/// "Downcast" to a variant of an ADT. Currently, we only introduce
19401941
/// this for ADTs with more than one variant. It may be better to
19411942
/// just introduce it always, or always for enums.
1942-
Downcast(&'tcx AdtDef, u32),
1943+
Downcast(&'tcx AdtDef, VariantIdx),
19431944
}
19441945

19451946
/// Alias for projections as they appear in places, where the base is a place
@@ -1974,8 +1975,8 @@ impl<'tcx> Place<'tcx> {
19741975
self.elem(ProjectionElem::Deref)
19751976
}
19761977

1977-
pub fn downcast(self, adt_def: &'tcx AdtDef, variant_index: usize) -> Place<'tcx> {
1978-
self.elem(ProjectionElem::Downcast(adt_def, variant_index as u32))
1978+
pub fn downcast(self, adt_def: &'tcx AdtDef, variant_index: VariantIdx) -> Place<'tcx> {
1979+
self.elem(ProjectionElem::Downcast(adt_def, variant_index))
19791980
}
19801981

19811982
pub fn index(self, index: Local) -> Place<'tcx> {
@@ -2026,7 +2027,7 @@ impl<'tcx> Debug for Place<'tcx> {
20262027
Promoted(ref promoted) => write!(fmt, "({:?}: {:?})", promoted.0, promoted.1),
20272028
Projection(ref data) => match data.elem {
20282029
ProjectionElem::Downcast(ref adt_def, index) => {
2029-
write!(fmt, "({:?} as {})", data.base, adt_def.variants[index as usize].name)
2030+
write!(fmt, "({:?} as {})", data.base, adt_def.variants[index].name)
20302031
}
20312032
ProjectionElem::Deref => write!(fmt, "(*{:?})", data.base),
20322033
ProjectionElem::Field(field, ty) => {
@@ -2216,7 +2217,7 @@ pub enum AggregateKind<'tcx> {
22162217
/// active field index would identity the field `c`
22172218
Adt(
22182219
&'tcx AdtDef,
2219-
usize,
2220+
VariantIdx,
22202221
&'tcx Substs<'tcx>,
22212222
Option<UserTypeAnnotation<'tcx>>,
22222223
Option<usize>,

src/librustc/mir/tcx.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use mir::*;
1717
use ty::subst::{Subst, Substs};
1818
use ty::{self, AdtDef, Ty, TyCtxt};
19+
use ty::layout::VariantIdx;
1920
use hir;
2021
use ty::util::IntTypeExt;
2122

@@ -27,7 +28,7 @@ pub enum PlaceTy<'tcx> {
2728
/// Downcast to a particular variant of an enum.
2829
Downcast { adt_def: &'tcx AdtDef,
2930
substs: &'tcx Substs<'tcx>,
30-
variant_index: u32 },
31+
variant_index: VariantIdx },
3132
}
3233

3334
static_assert!(PLACE_TY_IS_3_PTRS_LARGE:
@@ -58,11 +59,11 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
5859
pub fn field_ty(self, tcx: TyCtxt<'a, 'gcx, 'tcx>, f: &Field) -> Ty<'tcx>
5960
{
6061
// Pass `0` here so it can be used as a "default" variant_index in first arm below
61-
let answer = match (self, 0) {
62+
let answer = match (self, VariantIdx::new(0)) {
6263
(PlaceTy::Ty {
6364
ty: &ty::TyS { sty: ty::TyKind::Adt(adt_def, substs), .. } }, variant_index) |
6465
(PlaceTy::Downcast { adt_def, substs, variant_index }, _) => {
65-
let variant_def = &adt_def.variants[variant_index as usize];
66+
let variant_def = &adt_def.variants[variant_index];
6667
let field_def = &variant_def.fields[f.index()];
6768
field_def.ty(tcx, substs)
6869
}
@@ -138,7 +139,7 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
138139
match self.to_ty(tcx).sty {
139140
ty::Adt(adt_def, substs) => {
140141
assert!(adt_def.is_enum());
141-
assert!(index < adt_def.variants.len() as u32);
142+
assert!(index.as_usize() < adt_def.variants.len());
142143
assert_eq!(adt_def, adt_def1);
143144
PlaceTy::Downcast { adt_def,
144145
substs,

src/librustc/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use ty::RegionKind;
4545
use ty::{TyVar, TyVid, IntVar, IntVid, FloatVar, FloatVid};
4646
use ty::TyKind::*;
4747
use ty::GenericParamDefKind;
48-
use ty::layout::{LayoutDetails, TargetDataLayout};
48+
use ty::layout::{LayoutDetails, TargetDataLayout, VariantIdx};
4949
use ty::query;
5050
use ty::steal::Steal;
5151
use ty::BindingMode;
@@ -1009,7 +1009,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10091009
pub fn alloc_adt_def(self,
10101010
did: DefId,
10111011
kind: AdtKind,
1012-
variants: Vec<ty::VariantDef>,
1012+
variants: IndexVec<VariantIdx, ty::VariantDef>,
10131013
repr: ReprOptions)
10141014
-> &'gcx ty::AdtDef {
10151015
let def = ty::AdtDef::new(self, did, kind, variants, repr);

0 commit comments

Comments
 (0)