Skip to content

Commit 4aa4ce6

Browse files
authored
Rollup merge of #109621 - scottmcm:update-variantidx, r=compiler-errors
Refactor: `VariantIdx::from_u32(0)` -> `FIRST_VARIANT` Since structs are always `VariantIdx(0)`, there's a bunch of files where the only reason they had `VariantIdx` or `vec::Idx` imported at all was to get the first variant. So this uses a constant for that, and adds some doc-comments to `VariantIdx` while I'm there, since [it doesn't have any today](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_target/abi/struct.VariantIdx.html).
2 parents 776a8f4 + 0439d13 commit 4aa4ce6

File tree

25 files changed

+80
-78
lines changed

25 files changed

+80
-78
lines changed

compiler/rustc_abi/src/layout.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub trait LayoutCalculator {
4343
.max_by_key(|niche| niche.available(dl));
4444

4545
LayoutS {
46-
variants: Variants::Single { index: VariantIdx::new(0) },
46+
variants: Variants::Single { index: FIRST_VARIANT },
4747
fields: FieldsShape::Arbitrary {
4848
offsets: vec![Size::ZERO, b_offset],
4949
memory_index: vec![0, 1],
@@ -264,7 +264,7 @@ pub trait LayoutCalculator {
264264
abi = Abi::Uninhabited;
265265
}
266266
Some(LayoutS {
267-
variants: Variants::Single { index: VariantIdx::new(0) },
267+
variants: Variants::Single { index: FIRST_VARIANT },
268268
fields: FieldsShape::Arbitrary { offsets, memory_index },
269269
abi,
270270
largest_niche,
@@ -277,7 +277,7 @@ pub trait LayoutCalculator {
277277
let dl = self.current_data_layout();
278278
let dl = dl.borrow();
279279
LayoutS {
280-
variants: Variants::Single { index: VariantIdx::new(0) },
280+
variants: Variants::Single { index: FIRST_VARIANT },
281281
fields: FieldsShape::Primitive,
282282
abi: Abi::Uninhabited,
283283
largest_niche: None,
@@ -331,7 +331,7 @@ pub trait LayoutCalculator {
331331
}
332332
// If it's a struct, still compute a layout so that we can still compute the
333333
// field offsets.
334-
None => VariantIdx::new(0),
334+
None => FIRST_VARIANT,
335335
};
336336

337337
let is_struct = !is_enum ||
@@ -467,7 +467,7 @@ pub trait LayoutCalculator {
467467
.max_by_key(|(_i, layout)| layout.size.bytes())
468468
.map(|(i, _layout)| i)?;
469469

470-
let all_indices = (0..=variants.len() - 1).map(VariantIdx::new);
470+
let all_indices = variants.indices();
471471
let needs_disc =
472472
|index: VariantIdx| index != largest_variant_index && !absent(&variants[index]);
473473
let niche_variants = all_indices.clone().find(|v| needs_disc(*v)).unwrap().index()
@@ -896,8 +896,8 @@ pub trait LayoutCalculator {
896896
let optimize = !repr.inhibit_union_abi_opt();
897897
let mut size = Size::ZERO;
898898
let mut abi = Abi::Aggregate { sized: true };
899-
let index = VariantIdx::new(0);
900-
for field in &variants[index] {
899+
let only_variant = &variants[FIRST_VARIANT];
900+
for field in only_variant {
901901
assert!(field.0.is_sized());
902902
align = align.max(field.align());
903903

@@ -930,8 +930,8 @@ pub trait LayoutCalculator {
930930
}
931931

932932
Some(LayoutS {
933-
variants: Variants::Single { index },
934-
fields: FieldsShape::Union(NonZeroUsize::new(variants[index].len())?),
933+
variants: Variants::Single { index: FIRST_VARIANT },
934+
fields: FieldsShape::Union(NonZeroUsize::new(only_variant.len())?),
935935
abi,
936936
largest_niche: None,
937937
align,

compiler/rustc_abi/src/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,8 +1380,21 @@ impl Niche {
13801380
}
13811381

13821382
rustc_index::newtype_index! {
1383+
/// The *source-order* index of a variant in a type.
1384+
///
1385+
/// For enums, these are always `0..variant_count`, regardless of any
1386+
/// custom discriminants that may have been defined, and including any
1387+
/// variants that may end up uninhabited due to field types. (Some of the
1388+
/// variants may not be present in a monomorphized ABI [`Variants`], but
1389+
/// those skipped variants are always counted when determining the *index*.)
1390+
///
1391+
/// `struct`s, `tuples`, and `unions`s are considered to have a single variant
1392+
/// with variant index zero, aka [`FIRST_VARIANT`].
13831393
#[derive(HashStable_Generic)]
1384-
pub struct VariantIdx {}
1394+
pub struct VariantIdx {
1395+
/// Equivalent to `VariantIdx(0)`.
1396+
const FIRST_VARIANT = 0;
1397+
}
13851398
}
13861399

13871400
#[derive(PartialEq, Eq, Hash, Clone)]
@@ -1422,7 +1435,7 @@ impl LayoutS {
14221435
let size = scalar.size(cx);
14231436
let align = scalar.align(cx);
14241437
LayoutS {
1425-
variants: Variants::Single { index: VariantIdx::new(0) },
1438+
variants: Variants::Single { index: FIRST_VARIANT },
14261439
fields: FieldsShape::Primitive,
14271440
abi: Abi::Scalar(scalar),
14281441
largest_niche,

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_hir as hir;
1414
use rustc_hir::def::DefKind;
1515
use rustc_hir::def_id::LocalDefId;
1616
use rustc_hir::lang_items::LangItem;
17-
use rustc_index::vec::{Idx, IndexVec};
17+
use rustc_index::vec::IndexVec;
1818
use rustc_infer::infer::canonical::QueryRegionConstraints;
1919
use rustc_infer::infer::outlives::env::RegionBoundPairs;
2020
use rustc_infer::infer::region_constraints::RegionConstraintData;
@@ -36,7 +36,7 @@ use rustc_middle::ty::{
3636
};
3737
use rustc_span::def_id::CRATE_DEF_ID;
3838
use rustc_span::{Span, DUMMY_SP};
39-
use rustc_target::abi::VariantIdx;
39+
use rustc_target::abi::FIRST_VARIANT;
4040
use rustc_trait_selection::traits::query::type_op::custom::scrape_region_constraints;
4141
use rustc_trait_selection::traits::query::type_op::custom::CustomTypeOp;
4242
use rustc_trait_selection::traits::query::type_op::{TypeOp, TypeOpOutput};
@@ -812,7 +812,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
812812
},
813813
PlaceTy { ty, variant_index: None } => match *ty.kind() {
814814
ty::Adt(adt_def, substs) if !adt_def.is_enum() => {
815-
(adt_def.variant(VariantIdx::new(0)), substs)
815+
(adt_def.variant(FIRST_VARIANT), substs)
816816
}
817817
ty::Closure(_, substs) => {
818818
return match substs

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ fn codegen_stmt<'tcx>(
785785
let variant_dest = lval.downcast_variant(fx, variant_index);
786786
(variant_index, variant_dest, active_field_index)
787787
}
788-
_ => (VariantIdx::from_u32(0), lval, None),
788+
_ => (FIRST_VARIANT, lval, None),
789789
};
790790
if active_field_index.is_some() {
791791
assert_eq!(operands.len(), 1);

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ mod prelude {
8686
self, FloatTy, Instance, InstanceDef, IntTy, ParamEnv, Ty, TyCtxt, TypeAndMut,
8787
TypeFoldable, TypeVisitableExt, UintTy,
8888
};
89-
pub(crate) use rustc_target::abi::{Abi, Scalar, Size, VariantIdx};
89+
pub(crate) use rustc_target::abi::{Abi, Scalar, Size, VariantIdx, FIRST_VARIANT};
9090

9191
pub(crate) use rustc_data_structures::fx::FxHashMap;
9292

compiler/rustc_codegen_cranelift/src/unsize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ pub(crate) fn coerce_unsized_into<'tcx>(
146146
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
147147
assert_eq!(def_a, def_b);
148148

149-
for i in 0..def_a.variant(VariantIdx::new(0)).fields.len() {
149+
for i in 0..def_a.variant(FIRST_VARIANT).fields.len() {
150150
let src_f = src.value_field(fx, mir::Field::new(i));
151151
let dst_f = dst.place_field(fx, mir::Field::new(i));
152152

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use rustc_data_structures::sync::ParallelIterator;
2424
use rustc_hir as hir;
2525
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
2626
use rustc_hir::lang_items::LangItem;
27-
use rustc_index::vec::Idx;
2827
use rustc_metadata::EncodedMetadata;
2928
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
3029
use rustc_middle::middle::exported_symbols;
@@ -40,7 +39,7 @@ use rustc_session::Session;
4039
use rustc_span::symbol::sym;
4140
use rustc_span::Symbol;
4241
use rustc_span::{DebuggerVisualizerFile, DebuggerVisualizerType};
43-
use rustc_target::abi::{Align, VariantIdx};
42+
use rustc_target::abi::{Align, FIRST_VARIANT};
4443

4544
use std::collections::BTreeSet;
4645
use std::time::{Duration, Instant};
@@ -307,7 +306,7 @@ pub fn coerce_unsized_into<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
307306
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
308307
assert_eq!(def_a, def_b);
309308

310-
for i in 0..def_a.variant(VariantIdx::new(0)).fields.len() {
309+
for i in 0..def_a.variant(FIRST_VARIANT).fields.len() {
311310
let src_f = src.project_field(bx, i);
312311
let dst_f = dst.project_field(bx, i);
313312

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::ty::cast::{CastTy, IntTy};
1313
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
1414
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, Ty, TyCtxt};
1515
use rustc_span::source_map::{Span, DUMMY_SP};
16-
use rustc_target::abi::{self, VariantIdx};
16+
use rustc_target::abi::{self, FIRST_VARIANT};
1717

1818
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1919
#[instrument(level = "trace", skip(self, bx))]
@@ -118,7 +118,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
118118
let variant_dest = dest.project_downcast(bx, variant_index);
119119
(variant_index, variant_dest, active_field_index)
120120
}
121-
_ => (VariantIdx::from_u32(0), dest, None),
121+
_ => (FIRST_VARIANT, dest, None),
122122
};
123123
if active_field_index.is_some() {
124124
assert_eq!(operands.len(), 1);

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::interpret::{
88
use crate::interpret::{MPlaceTy, Value};
99
use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt};
1010
use rustc_span::source_map::DUMMY_SP;
11-
use rustc_target::abi::{Align, VariantIdx};
11+
use rustc_target::abi::{Align, VariantIdx, FIRST_VARIANT};
1212

1313
#[instrument(skip(ecx), level = "debug")]
1414
fn branches<'tcx>(
@@ -412,7 +412,7 @@ fn valtree_into_mplace<'tcx>(
412412

413413
let inner_ty = match ty.kind() {
414414
ty::Adt(def, substs) => {
415-
def.variant(VariantIdx::from_u32(0)).fields[i].ty(tcx, substs)
415+
def.variant(FIRST_VARIANT).fields[i].ty(tcx, substs)
416416
}
417417
ty::Tuple(inner_tys) => inner_tys[i],
418418
_ => bug!("unexpected unsized type {:?}", ty),

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::Mutability;
88
use rustc_middle::mir;
99
use rustc_middle::ty;
1010
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
11-
use rustc_target::abi::{self, Abi, Align, HasDataLayout, Size, VariantIdx};
11+
use rustc_target::abi::{self, Abi, Align, HasDataLayout, Size, FIRST_VARIANT};
1212

1313
use super::{
1414
alloc_range, mir_assign_valid_types, AllocId, AllocRef, AllocRefMut, CheckInAllocMsg,
@@ -796,7 +796,7 @@ where
796796
let variant_dest = self.place_downcast(&dest, variant_index)?;
797797
(variant_index, variant_dest, active_field_index)
798798
}
799-
_ => (VariantIdx::from_u32(0), dest.clone(), None),
799+
_ => (FIRST_VARIANT, dest.clone(), None),
800800
};
801801
if active_field_index.is_some() {
802802
assert_eq!(operands.len(), 1);

0 commit comments

Comments
 (0)