Skip to content

Commit a9031aa

Browse files
committed
Change tag_field to FieldIdx in Variants::Multiple
It was already available as a generic parameter anyway, and it's not like we'll ever put a tag in the 5-billionth field.
1 parent aa8ceb8 commit a9031aa

File tree

14 files changed

+35
-33
lines changed

14 files changed

+35
-33
lines changed

compiler/rustc_abi/src/layout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
758758
niche_variants,
759759
niche_start,
760760
},
761-
tag_field: 0,
761+
tag_field: FieldIdx::new(0),
762762
variants: IndexVec::new(),
763763
},
764764
fields: FieldsShape::Arbitrary {
@@ -1072,7 +1072,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
10721072
variants: Variants::Multiple {
10731073
tag,
10741074
tag_encoding: TagEncoding::Direct,
1075-
tag_field: 0,
1075+
tag_field: FieldIdx::new(0),
10761076
variants: IndexVec::new(),
10771077
},
10781078
fields: FieldsShape::Arbitrary {

compiler/rustc_abi/src/layout/coroutine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub(super) fn layout<
158158
// Build a prefix layout, including "promoting" all ineligible
159159
// locals as part of the prefix. We compute the layout of all of
160160
// these fields at once to get optimal packing.
161-
let tag_index = prefix_layouts.len();
161+
let tag_index = prefix_layouts.next_index();
162162

163163
// `variant_fields` already accounts for the reserved variants, so no need to add them.
164164
let max_discr = (variant_fields.len() - 1) as u128;
@@ -187,7 +187,7 @@ pub(super) fn layout<
187187

188188
// "a" (`0..b_start`) and "b" (`b_start..`) correspond to
189189
// "outer" and "promoted" fields respectively.
190-
let b_start = FieldIdx::new(tag_index + 1);
190+
let b_start = tag_index.plus(1);
191191
let offsets_b = IndexVec::from_raw(offsets.raw.split_off(b_start.index()));
192192
let offsets_a = offsets;
193193

compiler/rustc_abi/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ pub enum Variants<FieldIdx: Idx, VariantIdx: Idx> {
15711571
Multiple {
15721572
tag: Scalar,
15731573
tag_encoding: TagEncoding<VariantIdx>,
1574-
tag_field: usize,
1574+
tag_field: FieldIdx,
15751575
variants: IndexVec<VariantIdx, LayoutData<FieldIdx, VariantIdx>>,
15761576
},
15771577
}

compiler/rustc_codegen_cranelift/src/discriminant.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
2828
tag_encoding: TagEncoding::Direct,
2929
variants: _,
3030
} => {
31-
let ptr = place.place_field(fx, FieldIdx::new(tag_field));
31+
let ptr = place.place_field(fx, tag_field);
3232
let to = layout.ty.discriminant_for_variant(fx.tcx, variant_index).unwrap().val;
3333
let to = match ptr.layout().ty.kind() {
3434
ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
@@ -53,7 +53,7 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
5353
variants: _,
5454
} => {
5555
if variant_index != untagged_variant {
56-
let niche = place.place_field(fx, FieldIdx::new(tag_field));
56+
let niche = place.place_field(fx, tag_field);
5757
let niche_type = fx.clif_type(niche.layout().ty).unwrap();
5858
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
5959
let niche_value = (niche_value as u128).wrapping_add(niche_start);
@@ -118,7 +118,7 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
118118
let cast_to = fx.clif_type(dest_layout.ty).unwrap();
119119

120120
// Read the tag/niche-encoded discriminant from memory.
121-
let tag = value.value_field(fx, FieldIdx::new(tag_field));
121+
let tag = value.value_field(fx, tag_field);
122122
let tag = tag.load_scalar(fx);
123123

124124
// Decode the discriminant (specifically if it's niche-encoded).

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::borrow::Cow;
22

33
use libc::c_uint;
4-
use rustc_abi::{Align, Endian, Size, TagEncoding, VariantIdx, Variants};
4+
use rustc_abi::{Align, Endian, FieldIdx, Size, TagEncoding, VariantIdx, Variants};
55
use rustc_codegen_ssa::debuginfo::type_names::compute_debuginfo_type_name;
66
use rustc_codegen_ssa::debuginfo::{tag_base_type, wants_c_like_enum_debuginfo};
77
use rustc_codegen_ssa::traits::{ConstCodegenMethods, MiscCodegenMethods};
@@ -401,7 +401,7 @@ fn build_union_fields_for_enum<'ll, 'tcx>(
401401
enum_type_and_layout: TyAndLayout<'tcx>,
402402
enum_type_di_node: &'ll DIType,
403403
variant_indices: impl Iterator<Item = VariantIdx> + Clone,
404-
tag_field: usize,
404+
tag_field: FieldIdx,
405405
untagged_variant_index: Option<VariantIdx>,
406406
) -> SmallVec<&'ll DIType> {
407407
let tag_base_type = tag_base_type(cx.tcx, enum_type_and_layout);
@@ -806,7 +806,7 @@ fn build_union_fields_for_direct_tag_enum_or_coroutine<'ll, 'tcx>(
806806
variant_field_infos: &[VariantFieldInfo<'ll>],
807807
discr_type_di_node: &'ll DIType,
808808
tag_base_type: Ty<'tcx>,
809-
tag_field: usize,
809+
tag_field: FieldIdx,
810810
untagged_variant_index: Option<VariantIdx>,
811811
di_flags: DIFlags,
812812
) -> SmallVec<&'ll DIType> {
@@ -859,7 +859,7 @@ fn build_union_fields_for_direct_tag_enum_or_coroutine<'ll, 'tcx>(
859859
}));
860860

861861
assert_eq!(
862-
cx.size_and_align_of(enum_type_and_layout.field(cx, tag_field).ty),
862+
cx.size_and_align_of(enum_type_and_layout.field(cx, tag_field.as_usize()).ty),
863863
cx.size_and_align_of(self::tag_base_type(cx.tcx, enum_type_and_layout))
864864
);
865865

@@ -876,7 +876,7 @@ fn build_union_fields_for_direct_tag_enum_or_coroutine<'ll, 'tcx>(
876876
Endian::Big => (8, 0),
877877
};
878878

879-
let tag_field_offset = enum_type_and_layout.fields.offset(tag_field).bytes();
879+
let tag_field_offset = enum_type_and_layout.fields.offset(tag_field.as_usize()).bytes();
880880
let lo_offset = Size::from_bytes(tag_field_offset + lo_offset);
881881
let hi_offset = Size::from_bytes(tag_field_offset + hi_offset);
882882

@@ -906,8 +906,8 @@ fn build_union_fields_for_direct_tag_enum_or_coroutine<'ll, 'tcx>(
906906
cx,
907907
enum_type_di_node,
908908
TAG_FIELD_NAME,
909-
enum_type_and_layout.field(cx, tag_field),
910-
enum_type_and_layout.fields.offset(tag_field),
909+
enum_type_and_layout.field(cx, tag_field.as_usize()),
910+
enum_type_and_layout.fields.offset(tag_field.as_usize()),
911911
di_flags,
912912
tag_base_type_di_node,
913913
None,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ fn build_discr_member_di_node<'ll, 'tcx>(
375375
file,
376376
UNKNOWN_LINE_NUMBER,
377377
layout,
378-
enum_or_coroutine_type_and_layout.fields.offset(tag_field),
378+
enum_or_coroutine_type_and_layout.fields.offset(tag_field.as_usize()),
379379
DIFlags::FlagArtificial,
380380
ty,
381381
))

compiler/rustc_codegen_ssa/src/mir/operand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,10 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
437437
let tag_op = match self.val {
438438
OperandValue::ZeroSized => bug!(),
439439
OperandValue::Immediate(_) | OperandValue::Pair(_, _) => {
440-
self.extract_field(fx, bx, tag_field)
440+
self.extract_field(fx, bx, tag_field.as_usize())
441441
}
442442
OperandValue::Ref(place) => {
443-
let tag = place.with_type(self.layout).project_field(bx, tag_field);
443+
let tag = place.with_type(self.layout).project_field(bx, tag_field.as_usize());
444444
bx.load_operand(tag)
445445
}
446446
};

compiler/rustc_codegen_ssa/src/mir/place.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -468,18 +468,18 @@ pub(super) fn codegen_tagged_field_value<'tcx, V>(
468468
Variants::Multiple { tag_encoding: TagEncoding::Direct, tag_field, .. } => {
469469
let discr = layout.ty.discriminant_for_variant(cx.tcx(), variant_index);
470470
let to = discr.unwrap().val;
471-
let tag_layout = layout.field(cx, tag_field);
471+
let tag_layout = layout.field(cx, tag_field.as_usize());
472472
let tag_llty = cx.immediate_backend_type(tag_layout);
473473
let imm = cx.const_uint_big(tag_llty, to);
474-
Some((FieldIdx::from_usize(tag_field), imm))
474+
Some((tag_field, imm))
475475
}
476476
Variants::Multiple {
477477
tag_encoding: TagEncoding::Niche { untagged_variant, ref niche_variants, niche_start },
478478
tag_field,
479479
..
480480
} => {
481481
if variant_index != untagged_variant {
482-
let niche_layout = layout.field(cx, tag_field);
482+
let niche_layout = layout.field(cx, tag_field.as_usize());
483483
let niche_llty = cx.immediate_backend_type(niche_layout);
484484
let BackendRepr::Scalar(scalar) = niche_layout.backend_repr else {
485485
bug!("expected a scalar placeref for the niche");
@@ -497,7 +497,7 @@ pub(super) fn codegen_tagged_field_value<'tcx, V>(
497497
scalar,
498498
niche_llty,
499499
);
500-
Some((FieldIdx::from_usize(tag_field), niche_llval))
500+
Some((tag_field, niche_llval))
501501
} else {
502502
None
503503
}

compiler/rustc_const_eval/src/interpret/discriminant.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Functions for reading and writing discriminants of multi-variant layouts (enums and coroutines).
22
3-
use rustc_abi::{self as abi, TagEncoding, VariantIdx, Variants};
3+
use rustc_abi::{self as abi, FieldIdx, TagEncoding, VariantIdx, Variants};
44
use rustc_middle::ty::layout::{LayoutOf, PrimitiveExt, TyAndLayout};
55
use rustc_middle::ty::{self, CoroutineArgsExt, ScalarInt, Ty};
66
use rustc_middle::{mir, span_bug};
@@ -26,7 +26,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
2626
// No need to validate that the discriminant here because the
2727
// `TyAndLayout::for_variant()` call earlier already checks the
2828
// variant is valid.
29-
let tag_dest = self.project_field(dest, tag_field)?;
29+
let tag_dest = self.project_field(dest, tag_field.as_usize())?;
3030
self.write_scalar(tag, &tag_dest)
3131
}
3232
None => {
@@ -96,7 +96,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
9696
let tag_layout = self.layout_of(tag_scalar_layout.primitive().to_int_ty(*self.tcx))?;
9797

9898
// Read tag and sanity-check `tag_layout`.
99-
let tag_val = self.read_immediate(&self.project_field(op, tag_field)?)?;
99+
let tag_val = self.read_immediate(&self.project_field(op, tag_field.as_usize())?)?;
100100
assert_eq!(tag_layout.size, tag_val.layout.size);
101101
assert_eq!(tag_layout.backend_repr.is_signed(), tag_val.layout.backend_repr.is_signed());
102102
trace!("tag value: {}", tag_val);
@@ -231,7 +231,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
231231
&self,
232232
layout: TyAndLayout<'tcx>,
233233
variant_index: VariantIdx,
234-
) -> InterpResult<'tcx, Option<(ScalarInt, usize)>> {
234+
) -> InterpResult<'tcx, Option<(ScalarInt, FieldIdx)>> {
235235
// Layout computation excludes uninhabited variants from consideration.
236236
// Therefore, there's no way to represent those variants in the given layout.
237237
// Essentially, uninhabited variants do not have a tag that corresponds to their

compiler/rustc_const_eval/src/interpret/validity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
294294
// First, check if we are projecting to a variant.
295295
match layout.variants {
296296
Variants::Multiple { tag_field, .. } => {
297-
if tag_field == field {
297+
if tag_field.as_usize() == field {
298298
return match layout.ty.kind() {
299299
ty::Adt(def, ..) if def.is_enum() => PathElem::EnumTag,
300300
ty::Coroutine(..) => PathElem::CoroutineTag,

compiler/rustc_middle/src/ty/layout.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ where
934934
.unwrap(),
935935
),
936936
Variants::Multiple { tag, tag_field, .. } => {
937-
if i == tag_field {
937+
if FieldIdx::from_usize(i) == tag_field {
938938
return TyMaybeWithLayout::TyAndLayout(tag_layout(tag));
939939
}
940940
TyMaybeWithLayout::Ty(args.as_coroutine().prefix_tys()[i])
@@ -1060,8 +1060,10 @@ where
10601060
tag_field,
10611061
variants,
10621062
..
1063-
} if variants.len() == 2 && this.fields.offset(*tag_field) == offset => {
1064-
let tagged_variant = if untagged_variant.as_u32() == 0 {
1063+
} if variants.len() == 2
1064+
&& this.fields.offset(tag_field.as_usize()) == offset =>
1065+
{
1066+
let tagged_variant = if *untagged_variant == VariantIdx::ZERO {
10651067
VariantIdx::from_u32(1)
10661068
} else {
10671069
VariantIdx::from_u32(0)

compiler/rustc_smir/src/rustc_smir/convert/abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl<'tcx> Stable<'tcx> for rustc_abi::Variants<rustc_abi::FieldIdx, rustc_abi::
172172
VariantsShape::Multiple {
173173
tag: tag.stable(tables),
174174
tag_encoding: tag_encoding.stable(tables),
175-
tag_field: *tag_field,
175+
tag_field: tag_field.stable(tables),
176176
variants: variants.iter().as_slice().stable(tables),
177177
}
178178
}

compiler/rustc_transmute/src/layout/tree.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ pub(crate) mod rustc {
351351

352352
// For enums (but not coroutines), the tag field is
353353
// currently always the first field of the layout.
354-
assert_eq!(*tag_field, 0);
354+
assert_eq!(*tag_field, FieldIdx::ZERO);
355355

356356
let variants = def.discriminants(cx.tcx()).try_fold(
357357
Self::uninhabited(),

compiler/rustc_ty_utils/src/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ fn variant_info_for_coroutine<'tcx>(
849849
// However, if the discriminant is placed past the end of the variant, then we need
850850
// to factor in the size of the discriminant manually. This really should be refactored
851851
// better, but this "works" for now.
852-
if layout.fields.offset(tag_field) >= variant_size {
852+
if layout.fields.offset(tag_field.as_usize()) >= variant_size {
853853
variant_size += match tag_encoding {
854854
TagEncoding::Direct => tag.size(cx),
855855
_ => Size::ZERO,

0 commit comments

Comments
 (0)