Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit eb3e9c1

Browse files
committed
Auto merge of rust-lang#109762 - scottmcm:variantdef-indexvec, r=WaffleLapkin
Update `ty::VariantDef` to use `IndexVec<FieldIdx, FieldDef>` And while doing the updates for that, also uses `FieldIdx` in `ProjectionKind::Field` and `TypeckResults::field_indices`. There's more places that could use it (like `rustc_const_eval` and `LayoutS`), but I tried to keep this PR from exploding to *even more* places. Part 2/? of rust-lang/compiler-team#606
2 parents 276029d + 4abb455 commit eb3e9c1

File tree

47 files changed

+127
-104
lines changed

Some content is hidden

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

47 files changed

+127
-104
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
350350
if !including_tuple_field.0 && variant.ctor_kind() == Some(CtorKind::Fn) {
351351
return None;
352352
}
353-
Some(variant.fields[field.index()].name.to_string())
353+
Some(variant.fields[field].name.to_string())
354354
}
355355
ty::Tuple(_) => Some(field.index().to_string()),
356356
ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
854854
},
855855
};
856856

857-
if let Some(field) = variant.fields.get(field.index()) {
857+
if let Some(field) = variant.fields.get(field) {
858858
Ok(self.cx.normalize(field.ty(tcx, substs), location))
859859
} else {
860860
Err(FieldAccessError::OutOfRange { field_count: variant.fields.len() })
@@ -1725,7 +1725,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
17251725
AggregateKind::Adt(adt_did, variant_index, substs, _, active_field_index) => {
17261726
let def = tcx.adt_def(adt_did);
17271727
let variant = &def.variant(variant_index);
1728-
let adj_field_index = active_field_index.unwrap_or(field_index);
1728+
let adj_field_index =
1729+
FieldIdx::from_usize(active_field_index.unwrap_or(field_index));
17291730
if let Some(field) = variant.fields.get(adj_field_index) {
17301731
Ok(self.normalize(field.ty(tcx, substs), location))
17311732
} else {

compiler/rustc_codegen_cranelift/src/value_and_place.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,8 @@ impl<'tcx> CPlace<'tcx> {
701701
};
702702
}
703703
ty::Adt(adt_def, substs) if layout.ty.is_simd() => {
704-
let f0_ty = adt_def.non_enum_variant().fields[0].ty(fx.tcx, substs);
704+
let f0 = &adt_def.non_enum_variant().fields[FieldIdx::from_u32(0)];
705+
let f0_ty = f0.ty(fx.tcx, substs);
705706

706707
match f0_ty.kind() {
707708
ty::Array(_, _) => {

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ fn build_enum_variant_struct_type_di_node<'ll, 'tcx>(
274274
.map(|field_index| {
275275
let field_name = if variant_def.ctor_kind() != Some(CtorKind::Fn) {
276276
// Fields have names
277-
Cow::from(variant_def.fields[field_index].name.as_str())
277+
let field = &variant_def.fields[FieldIdx::from_usize(field_index)];
278+
Cow::from(field.name.as_str())
278279
} else {
279280
// Tuple-like
280281
super::tuple_field_name(field_index)

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 2 additions & 1 deletion
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, FIRST_VARIANT};
11+
use rustc_target::abi::{Align, FieldIdx, VariantIdx, FIRST_VARIANT};
1212

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

413413
let inner_ty = match ty.kind() {
414414
ty::Adt(def, substs) => {
415+
let i = FieldIdx::from_usize(i);
415416
def.variant(FIRST_VARIANT).fields[i].ty(tcx, substs)
416417
}
417418
ty::Tuple(inner_tys) => inner_tys[i],

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use rustc_middle::mir::interpret::InterpError;
1616
use rustc_middle::ty;
1717
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
1818
use rustc_span::symbol::{sym, Symbol};
19-
use rustc_target::abi::{Abi, Scalar as ScalarAbi, Size, VariantIdx, Variants, WrappingRange};
19+
use rustc_target::abi::{
20+
Abi, FieldIdx, Scalar as ScalarAbi, Size, VariantIdx, Variants, WrappingRange,
21+
};
2022

2123
use std::hash::Hash;
2224

@@ -269,14 +271,16 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
269271
match layout.variants {
270272
Variants::Single { index } => {
271273
// Inside a variant
272-
PathElem::Field(def.variant(index).fields[field].name)
274+
PathElem::Field(def.variant(index).fields[FieldIdx::from_usize(field)].name)
273275
}
274276
Variants::Multiple { .. } => bug!("we handled variants above"),
275277
}
276278
}
277279

278280
// other ADTs
279-
ty::Adt(def, _) => PathElem::Field(def.non_enum_variant().fields[field].name),
281+
ty::Adt(def, _) => {
282+
PathElem::Field(def.non_enum_variant().fields[FieldIdx::from_usize(field)].name)
283+
}
280284

281285
// arrays/slices
282286
ty::Array(..) | ty::Slice(..) => PathElem::ArrayElem(field),

compiler/rustc_const_eval/src/transform/validate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
360360
}
361361
ty::Adt(adt_def, substs) => {
362362
let var = parent_ty.variant_index.unwrap_or(FIRST_VARIANT);
363-
let Some(field) = adt_def.variant(var).fields.get(f.as_usize()) else {
363+
let Some(field) = adt_def.variant(var).fields.get(f) else {
364364
fail_out_of_bounds(self, location);
365365
return;
366366
};

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rustc_middle::ty::{
2727
use rustc_session::lint::builtin::{UNINHABITED_STATIC, UNSUPPORTED_CALLING_CONVENTIONS};
2828
use rustc_span::symbol::sym;
2929
use rustc_span::{self, Span};
30+
use rustc_target::abi::FieldIdx;
3031
use rustc_target::spec::abi::Abi;
3132
use rustc_trait_selection::traits::error_reporting::on_unimplemented::OnUnimplementedDirective;
3233
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
@@ -474,7 +475,7 @@ fn is_enum_of_nonnullable_ptr<'tcx>(
474475
let [var_one, var_two] = &adt_def.variants().raw[..] else {
475476
return false;
476477
};
477-
let (([], [field]) | ([field], [])) = (&var_one.fields[..], &var_two.fields[..]) else {
478+
let (([], [field]) | ([field], [])) = (&var_one.fields.raw[..], &var_two.fields.raw[..]) else {
478479
return false;
479480
};
480481
matches!(field.ty(tcx, substs).kind(), ty::FnPtr(..) | ty::Ref(..))
@@ -893,7 +894,7 @@ pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
893894
struct_span_err!(tcx.sess, sp, E0075, "SIMD vector cannot be empty").emit();
894895
return;
895896
}
896-
let e = fields[0].ty(tcx, substs);
897+
let e = fields[FieldIdx::from_u32(0)].ty(tcx, substs);
897898
if !fields.iter().all(|f| f.ty(tcx, substs) == e) {
898899
struct_span_err!(tcx.sess, sp, E0076, "SIMD vector should be homogeneous")
899900
.span_label(sp, "SIMD elements must have the same type")

compiler/rustc_hir_analysis/src/check/intrinsicck.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitableE
55
use rustc_session::lint;
66
use rustc_span::def_id::LocalDefId;
77
use rustc_span::{Symbol, DUMMY_SP};
8+
use rustc_target::abi::FieldIdx;
89
use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType};
910

1011
pub struct InlineAsmCtxt<'a, 'tcx> {
@@ -82,7 +83,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
8283
}
8384
ty::Adt(adt, substs) if adt.repr().simd() => {
8485
let fields = &adt.non_enum_variant().fields;
85-
let elem_ty = fields[0].ty(self.tcx, substs);
86+
let elem_ty = fields[FieldIdx::from_u32(0)].ty(self.tcx, substs);
8687
match elem_ty.kind() {
8788
ty::Never | ty::Error(_) => return None,
8889
ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => {

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ fn check_type_defn<'tcx>(tcx: TyCtxt<'tcx>, item: &hir::Item<'tcx>, all_sized: b
10301030
// intermediate types must be sized.
10311031
let needs_drop_copy = || {
10321032
packed && {
1033-
let ty = tcx.type_of(variant.fields.last().unwrap().did).subst_identity();
1033+
let ty = tcx.type_of(variant.fields.raw.last().unwrap().did).subst_identity();
10341034
let ty = tcx.erase_regions(ty);
10351035
if ty.needs_infer() {
10361036
tcx.sess
@@ -1046,7 +1046,7 @@ fn check_type_defn<'tcx>(tcx: TyCtxt<'tcx>, item: &hir::Item<'tcx>, all_sized: b
10461046
let all_sized = all_sized || variant.fields.is_empty() || needs_drop_copy();
10471047
let unsized_len = if all_sized { 0 } else { 1 };
10481048
for (idx, field) in
1049-
variant.fields[..variant.fields.len() - unsized_len].iter().enumerate()
1049+
variant.fields.raw[..variant.fields.len() - unsized_len].iter().enumerate()
10501050
{
10511051
let last = idx == variant.fields.len() - 1;
10521052
let field_id = field.did.expect_local();

0 commit comments

Comments
 (0)