Skip to content

Commit 4a9ed3f

Browse files
committed
Use type safe VariantIdx instead of usize everywhere
1 parent 740fb0c commit 4a9ed3f

File tree

33 files changed

+144
-104
lines changed

33 files changed

+144
-104
lines changed

src/Cargo.lock

Lines changed: 1 addition & 0 deletions
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",

src/librustc/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,7 @@ pub enum StatementKind<'tcx> {
17541754
/// Write the discriminant for a variant to the enum Place.
17551755
SetDiscriminant {
17561756
place: Place<'tcx>,
1757-
variant_index: usize,
1757+
variant_index: VariantIdx,
17581758
},
17591759

17601760
/// Start a live range for the storage of the local.

src/librustc/ty/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,11 +2149,12 @@ impl<'a, 'gcx, 'tcx> AdtDef {
21492149
.expect("variant_with_id: unknown variant")
21502150
}
21512151

2152-
pub fn variant_index_with_id(&self, vid: DefId) -> usize {
2152+
pub fn variant_index_with_id(&self, vid: DefId) -> VariantIdx {
21532153
self.variants
2154-
.iter()
2155-
.position(|v| v.did == vid)
2154+
.iter_enumerated()
2155+
.find(|(_, v)| v.did == vid)
21562156
.expect("variant_index_with_id: unknown variant")
2157+
.0
21572158
}
21582159

21592160
pub fn variant_of_def(&self, def: Def) -> &VariantDef {

src/librustc_codegen_llvm/base.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use rustc::middle::weak_lang_items;
3939
use rustc::mir::mono::{Linkage, Visibility, Stats, CodegenUnitNameBuilder};
4040
use rustc::middle::cstore::{EncodedMetadata};
4141
use rustc::ty::{self, Ty, TyCtxt};
42-
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf};
42+
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, VariantIdx};
4343
use rustc::ty::query::Providers;
4444
use rustc::middle::cstore::{self, LinkagePreference};
4545
use rustc::middle::exported_symbols;
@@ -73,6 +73,7 @@ use rustc::util::nodemap::FxHashMap;
7373
use CrateInfo;
7474
use rustc_data_structures::small_c_str::SmallCStr;
7575
use rustc_data_structures::sync::Lrc;
76+
use rustc_data_structures::indexed_vec::Idx;
7677

7778
use std::any::Any;
7879
use std::cmp;
@@ -309,7 +310,7 @@ pub fn coerce_unsized_into(
309310
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
310311
assert_eq!(def_a, def_b);
311312

312-
for i in 0..def_a.variants[0].fields.len() {
313+
for i in 0..def_a.variants[VariantIdx::new(0)].fields.len() {
313314
let src_f = src.project_field(bx, i);
314315
let dst_f = dst.project_field(bx, i);
315316

src/librustc_codegen_llvm/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_data_structures::small_c_str::SmallCStr;
2929
use rustc::mir::mono::Stats;
3030
use rustc::session::config::{self, DebugInfo};
3131
use rustc::session::Session;
32-
use rustc::ty::layout::{LayoutError, LayoutOf, Size, TyLayout};
32+
use rustc::ty::layout::{LayoutError, LayoutOf, Size, TyLayout, VariantIdx};
3333
use rustc::ty::{self, Ty, TyCtxt};
3434
use rustc::util::nodemap::FxHashMap;
3535
use rustc_target::spec::{HasTargetSpec, Target};
@@ -87,7 +87,7 @@ pub struct CodegenCx<'a, 'tcx: 'a> {
8787
/// See http://llvm.org/docs/LangRef.html#the-llvm-used-global-variable for details
8888
pub used_statics: RefCell<Vec<&'a Value>>,
8989

90-
pub lltypes: RefCell<FxHashMap<(Ty<'tcx>, Option<usize>), &'a Type>>,
90+
pub lltypes: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), &'a Type>>,
9191
pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, &'a Type>>,
9292
pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
9393
pub isize_ty: &'a Type,

src/librustc_codegen_llvm/debuginfo/metadata.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> {
12411241
// This doesn't matter in this case.
12421242
NoDiscriminant
12431243
};
1244-
(0..variants.len()).map(|i| {
1244+
variants.iter_enumerated().map(|(i, _)| {
12451245
let variant = self.layout.for_variant(cx, i);
12461246
let (variant_type_metadata, member_desc_factory) =
12471247
describe_enum_variant(cx,
@@ -1341,7 +1341,7 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> {
13411341
}
13421342
]
13431343
} else {
1344-
(0..variants.len()).map(|i| {
1344+
variants.iter_enumerated().map(|(i, _)| {
13451345
let variant = self.layout.for_variant(cx, i);
13461346
let (variant_type_metadata, member_desc_factory) =
13471347
describe_enum_variant(cx,
@@ -1361,8 +1361,8 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> {
13611361
let niche_value = if i == dataful_variant {
13621362
None
13631363
} else {
1364-
let value = (i as u128)
1365-
.wrapping_sub(*niche_variants.start() as u128)
1364+
let value = (i.as_u32() as u128)
1365+
.wrapping_sub(niche_variants.start().as_u32() as u128)
13661366
.wrapping_add(niche_start);
13671367
let value = value & ((1u128 << niche.value.size(cx).bits()) - 1);
13681368
Some(value as u64)
@@ -1530,7 +1530,7 @@ fn prepare_enum_metadata(
15301530
let def = enum_type.ty_adt_def().unwrap();
15311531
let enumerators_metadata: Vec<_> = def.discriminants(cx.tcx)
15321532
.zip(&def.variants)
1533-
.map(|(discr, v)| {
1533+
.map(|((_, discr), v)| {
15341534
let name = SmallCStr::new(&v.name.as_str());
15351535
unsafe {
15361536
Some(llvm::LLVMRustDIBuilderCreateEnumerator(

src/librustc_codegen_llvm/mir/place.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use llvm::{self, LLVMConstInBoundsGEP};
1212
use rustc::ty::{self, Ty};
13-
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, Size};
13+
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, Size, VariantIdx};
1414
use rustc::mir;
1515
use rustc::mir::tcx::PlaceTy;
1616
use base;
@@ -281,7 +281,7 @@ impl PlaceRef<'ll, 'tcx> {
281281
match self.layout.variants {
282282
layout::Variants::Single { index } => {
283283
let discr_val = self.layout.ty.ty_adt_def().map_or(
284-
index as u128,
284+
index.as_u32() as u128,
285285
|def| def.discriminant_for_variant(bx.cx.tcx, index).val);
286286
return C_uint_big(cast_to, discr_val);
287287
}
@@ -320,24 +320,24 @@ impl PlaceRef<'ll, 'tcx> {
320320
C_uint_big(niche_llty, niche_start)
321321
};
322322
bx.select(bx.icmp(llvm::IntEQ, lldiscr, niche_llval),
323-
C_uint(cast_to, *niche_variants.start() as u64),
324-
C_uint(cast_to, dataful_variant as u64))
323+
C_uint(cast_to, niche_variants.start().as_u32() as u64),
324+
C_uint(cast_to, dataful_variant.as_u32() as u64))
325325
} else {
326326
// Rebase from niche values to discriminant values.
327-
let delta = niche_start.wrapping_sub(*niche_variants.start() as u128);
327+
let delta = niche_start.wrapping_sub(niche_variants.start().as_u32() as u128);
328328
let lldiscr = bx.sub(lldiscr, C_uint_big(niche_llty, delta));
329-
let lldiscr_max = C_uint(niche_llty, *niche_variants.end() as u64);
329+
let lldiscr_max = C_uint(niche_llty, niche_variants.end().as_u32() as u64);
330330
bx.select(bx.icmp(llvm::IntULE, lldiscr, lldiscr_max),
331331
bx.intcast(lldiscr, cast_to, false),
332-
C_uint(cast_to, dataful_variant as u64))
332+
C_uint(cast_to, dataful_variant.as_u32() as u64))
333333
}
334334
}
335335
}
336336
}
337337

338338
/// Set the discriminant for a new value of the given case of the given
339339
/// representation.
340-
pub fn codegen_set_discr(&self, bx: &Builder<'a, 'll, 'tcx>, variant_index: usize) {
340+
pub fn codegen_set_discr(&self, bx: &Builder<'a, 'll, 'tcx>, variant_index: VariantIdx) {
341341
if self.layout.for_variant(bx.cx, variant_index).abi.is_uninhabited() {
342342
return;
343343
}
@@ -376,7 +376,8 @@ impl PlaceRef<'ll, 'tcx> {
376376

377377
let niche = self.project_field(bx, 0);
378378
let niche_llty = niche.layout.immediate_llvm_type(bx.cx);
379-
let niche_value = ((variant_index - *niche_variants.start()) as u128)
379+
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
380+
let niche_value = (niche_value as u128)
380381
.wrapping_add(niche_start);
381382
// FIXME(eddyb) Check the actual primitive type here.
382383
let niche_llval = if niche_value == 0 {
@@ -401,7 +402,7 @@ impl PlaceRef<'ll, 'tcx> {
401402
}
402403
}
403404

404-
pub fn project_downcast(&self, bx: &Builder<'a, 'll, 'tcx>, variant_index: usize)
405+
pub fn project_downcast(&self, bx: &Builder<'a, 'll, 'tcx>, variant_index: VariantIdx)
405406
-> PlaceRef<'ll, 'tcx> {
406407
let mut downcast = *self;
407408
downcast.layout = self.layout.for_variant(bx.cx, variant_index);

src/librustc_lint/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ rustc_mir = { path = "../librustc_mir"}
1616
rustc_target = { path = "../librustc_target" }
1717
syntax = { path = "../libsyntax" }
1818
syntax_pos = { path = "../libsyntax_pos" }
19+
rustc_data_structures = { path = "../librustc_data_structures" }

src/librustc_lint/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extern crate log;
4040
extern crate rustc_mir;
4141
extern crate rustc_target;
4242
extern crate syntax_pos;
43+
extern crate rustc_data_structures;
4344

4445
use rustc::lint;
4546
use rustc::lint::{LateContext, LateLintPass, LintPass, LintArray};

src/librustc_lint/types.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
use rustc::hir::Node;
1414
use rustc::ty::subst::Substs;
1515
use rustc::ty::{self, AdtKind, ParamEnv, Ty, TyCtxt};
16-
use rustc::ty::layout::{self, IntegerExt, LayoutOf};
16+
use rustc::ty::layout::{self, IntegerExt, LayoutOf, VariantIdx};
17+
use rustc_data_structures::indexed_vec::Idx;
1718
use util::nodemap::FxHashSet;
1819
use lint::{LateContext, LintContext, LintArray};
1920
use lint::{LintPass, LateLintPass};
@@ -452,10 +453,13 @@ fn is_repr_nullable_ptr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
452453
if def.variants.len() == 2 {
453454
let data_idx;
454455

455-
if def.variants[0].fields.is_empty() {
456-
data_idx = 1;
457-
} else if def.variants[1].fields.is_empty() {
458-
data_idx = 0;
456+
let zero = VariantIdx::new(0);
457+
let one = VariantIdx::new(1);
458+
459+
if def.variants[zero].fields.is_empty() {
460+
data_idx = one;
461+
} else if def.variants[one].fields.is_empty() {
462+
data_idx = zero;
459463
} else {
460464
return false;
461465
}

0 commit comments

Comments
 (0)