Skip to content

Commit 485397e

Browse files
committed
Auto merge of rust-lang#55589 - oli-obk:min_length_💣, r=pnkfelix
Add `VariantIdx` type and use instead of `usize`
2 parents c4371c8 + d46a207 commit 485397e

File tree

44 files changed

+308
-189
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+308
-189
lines changed

‎src/Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,7 @@ version = "0.0.0"
22462246
dependencies = [
22472247
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
22482248
"rustc 0.0.0",
2249+
"rustc_data_structures 0.0.0",
22492250
"rustc_mir 0.0.0",
22502251
"rustc_target 0.0.0",
22512252
"syntax 0.0.0",
@@ -2400,6 +2401,7 @@ dependencies = [
24002401
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
24012402
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
24022403
"rustc_cratesio_shim 0.0.0",
2404+
"rustc_data_structures 0.0.0",
24032405
"serialize 0.0.0",
24042406
]
24052407

‎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

+10-4
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;
@@ -1753,7 +1754,7 @@ pub enum StatementKind<'tcx> {
17531754
/// Write the discriminant for a variant to the enum Place.
17541755
SetDiscriminant {
17551756
place: Place<'tcx>,
1756-
variant_index: usize,
1757+
variant_index: VariantIdx,
17571758
},
17581759

17591760
/// Start a live range for the storage of the local.
@@ -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, usize),
1943+
Downcast(&'tcx AdtDef, VariantIdx),
19431944
}
19441945

19451946
/// Alias for projections as they appear in places, where the base is a place
@@ -1950,6 +1951,11 @@ pub type PlaceProjection<'tcx> = Projection<'tcx, Place<'tcx>, Local, Ty<'tcx>>;
19501951
/// and the index is a local.
19511952
pub type PlaceElem<'tcx> = ProjectionElem<'tcx, Local, Ty<'tcx>>;
19521953

1954+
// at least on 64 bit systems, `PlaceElem` should not be larger than two pointers
1955+
static_assert!(PROJECTION_ELEM_IS_2_PTRS_LARGE:
1956+
mem::size_of::<PlaceElem<'_>>() <= 16
1957+
);
1958+
19531959
/// Alias for projections as they appear in `UserTypeProjection`, where we
19541960
/// need neither the `V` parameter for `Index` nor the `T` for `Field`.
19551961
pub type ProjectionKind<'tcx> = ProjectionElem<'tcx, (), ()>;
@@ -1969,7 +1975,7 @@ impl<'tcx> Place<'tcx> {
19691975
self.elem(ProjectionElem::Deref)
19701976
}
19711977

1972-
pub fn downcast(self, adt_def: &'tcx AdtDef, variant_index: usize) -> Place<'tcx> {
1978+
pub fn downcast(self, adt_def: &'tcx AdtDef, variant_index: VariantIdx) -> Place<'tcx> {
19731979
self.elem(ProjectionElem::Downcast(adt_def, variant_index))
19741980
}
19751981

@@ -2211,7 +2217,7 @@ pub enum AggregateKind<'tcx> {
22112217
/// active field index would identity the field `c`
22122218
Adt(
22132219
&'tcx AdtDef,
2214-
usize,
2220+
VariantIdx,
22152221
&'tcx Substs<'tcx>,
22162222
Option<UserTypeAnnotation<'tcx>>,
22172223
Option<usize>,

‎src/librustc/mir/tcx.rs

+8-3
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,9 +28,13 @@ 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: usize },
31+
variant_index: VariantIdx },
3132
}
3233

34+
static_assert!(PLACE_TY_IS_3_PTRS_LARGE:
35+
mem::size_of::<PlaceTy<'_>>() <= 24
36+
);
37+
3338
impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
3439
pub fn from_ty(ty: Ty<'tcx>) -> PlaceTy<'tcx> {
3540
PlaceTy::Ty { ty }
@@ -54,7 +59,7 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
5459
pub fn field_ty(self, tcx: TyCtxt<'a, 'gcx, 'tcx>, f: &Field) -> Ty<'tcx>
5560
{
5661
// Pass `0` here so it can be used as a "default" variant_index in first arm below
57-
let answer = match (self, 0) {
62+
let answer = match (self, VariantIdx::new(0)) {
5863
(PlaceTy::Ty {
5964
ty: &ty::TyS { sty: ty::TyKind::Adt(adt_def, substs), .. } }, variant_index) |
6065
(PlaceTy::Downcast { adt_def, substs, variant_index }, _) => {
@@ -134,7 +139,7 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
134139
match self.to_ty(tcx).sty {
135140
ty::Adt(adt_def, substs) => {
136141
assert!(adt_def.is_enum());
137-
assert!(index < adt_def.variants.len());
142+
assert!(index.as_usize() < adt_def.variants.len());
138143
assert_eq!(adt_def, adt_def1);
139144
PlaceTy::Downcast { adt_def,
140145
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)