Skip to content

Commit 5bbaead

Browse files
committed
Move mir::Fieldabi::FieldIdx
The first PR for rust-lang/compiler-team#606 This is just the move-and-rename, because it's plenty big-and-bitrotty already. Future PRs will start using `FieldIdx` more broadly, and concomitantly removing `FieldIdx::new`s.
1 parent acd27bb commit 5bbaead

Some content is hidden

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

46 files changed

+192
-157
lines changed

compiler/rustc_abi/src/lib.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,32 @@ impl Scalar {
10571057
}
10581058
}
10591059

1060+
rustc_index::newtype_index! {
1061+
/// The *source-order* index of a field in a variant.
1062+
///
1063+
/// This is how most code after type checking refers to fields, rather than
1064+
/// using names (as names have hygiene complications and more complex lookup).
1065+
///
1066+
/// Particularly for `repr(Rust)` types, this may not be the same as *layout* order.
1067+
/// (It is for `repr(C)` `struct`s, however.)
1068+
///
1069+
/// For example, in the following types,
1070+
/// ```rust
1071+
/// # enum Never {}
1072+
/// # #[repr(u16)]
1073+
/// enum Demo1 {
1074+
/// Variant0 { a: Never, b: i32 } = 100,
1075+
/// Variant1 { c: u8, d: u64 } = 10,
1076+
/// }
1077+
/// struct Demo2 { e: u8, f: u16, g: u8 }
1078+
/// ```
1079+
/// `b` is `FieldIdx(1)` in `VariantIdx(0)`,
1080+
/// `d` is `FieldIdx(1)` in `VariantIdx(1)`, and
1081+
/// `f` is `FieldIdx(1)` in `VariantIdx(0)`.
1082+
#[derive(HashStable_Generic)]
1083+
pub struct FieldIdx {}
1084+
}
1085+
10601086
/// Describes how the fields of a type are located in memory.
10611087
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
10621088
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]

compiler/rustc_borrowck/src/diagnostics/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ use rustc_hir::GeneratorKind;
99
use rustc_infer::infer::{LateBoundRegionConversionTime, TyCtxtInferExt};
1010
use rustc_middle::mir::tcx::PlaceTy;
1111
use rustc_middle::mir::{
12-
AggregateKind, Constant, FakeReadCause, Field, Local, LocalInfo, LocalKind, Location, Operand,
13-
Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
12+
AggregateKind, Constant, FakeReadCause, Local, LocalInfo, LocalKind, Location, Operand, Place,
13+
PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
1414
};
1515
use rustc_middle::ty::print::Print;
1616
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
1717
use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult};
1818
use rustc_span::def_id::LocalDefId;
1919
use rustc_span::{symbol::sym, Span, Symbol, DUMMY_SP};
20-
use rustc_target::abi::VariantIdx;
20+
use rustc_target::abi::{FieldIdx, VariantIdx};
2121
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
2222
use rustc_trait_selection::traits::{
2323
type_known_to_meet_bound_modulo_regions, Obligation, ObligationCause,
@@ -302,7 +302,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
302302
fn describe_field(
303303
&self,
304304
place: PlaceRef<'tcx>,
305-
field: Field,
305+
field: FieldIdx,
306306
including_tuple_field: IncludingTupleField,
307307
) -> Option<String> {
308308
let place_ty = match place {
@@ -331,7 +331,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
331331
fn describe_field_from_ty(
332332
&self,
333333
ty: Ty<'_>,
334-
field: Field,
334+
field: FieldIdx,
335335
variant_index: Option<VariantIdx>,
336336
including_tuple_field: IncludingTupleField,
337337
) -> Option<String> {

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_middle::{
1212
use rustc_span::source_map::DesugaringKind;
1313
use rustc_span::symbol::{kw, Symbol};
1414
use rustc_span::{sym, BytePos, Span};
15+
use rustc_target::abi::FieldIdx;
1516

1617
use crate::diagnostics::BorrowedContentSource;
1718
use crate::MirBorrowckCtxt;
@@ -1275,7 +1276,7 @@ fn is_closure_or_generator(ty: Ty<'_>) -> bool {
12751276
fn get_mut_span_in_struct_field<'tcx>(
12761277
tcx: TyCtxt<'tcx>,
12771278
ty: Ty<'tcx>,
1278-
field: mir::Field,
1279+
field: FieldIdx,
12791280
) -> Option<Span> {
12801281
// Expect our local to be a reference to a struct of some kind.
12811282
if let ty::Ref(_, ty, _) = ty.kind()

compiler/rustc_borrowck/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ use rustc_middle::mir::{
3333
Place, PlaceElem, PlaceRef, VarDebugInfoContents,
3434
};
3535
use rustc_middle::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind};
36-
use rustc_middle::mir::{Field, ProjectionElem, Promoted, Rvalue, Statement, StatementKind};
3736
use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
37+
use rustc_middle::mir::{ProjectionElem, Promoted, Rvalue, Statement, StatementKind};
3838
use rustc_middle::ty::query::Providers;
3939
use rustc_middle::ty::{self, CapturedPlace, ParamEnv, RegionVid, TyCtxt};
4040
use rustc_session::lint::builtin::UNUSED_MUT;
4141
use rustc_span::{Span, Symbol};
42+
use rustc_target::abi::FieldIdx;
4243

4344
use either::Either;
4445
use smallvec::SmallVec;
@@ -597,7 +598,7 @@ struct MirBorrowckCtxt<'cx, 'tcx> {
597598
used_mut: FxIndexSet<Local>,
598599
/// If the function we're checking is a closure, then we'll need to report back the list of
599600
/// mutable upvars that have been used. This field keeps track of them.
600-
used_mut_upvars: SmallVec<[Field; 8]>,
601+
used_mut_upvars: SmallVec<[FieldIdx; 8]>,
601602
/// Region inference context. This contains the results from region inference and lets us e.g.
602603
/// find out which CFG points are contained in each borrow region.
603604
regioncx: Rc<RegionInferenceContext<'tcx>>,
@@ -2277,7 +2278,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22772278
/// then returns the index of the field being projected. Note that this closure will always
22782279
/// be `self` in the current MIR, because that is the only time we directly access the fields
22792280
/// of a closure type.
2280-
fn is_upvar_field_projection(&self, place_ref: PlaceRef<'tcx>) -> Option<Field> {
2281+
fn is_upvar_field_projection(&self, place_ref: PlaceRef<'tcx>) -> Option<FieldIdx> {
22812282
path_utils::is_upvar_field_projection(self.infcx.tcx, &self.upvars, place_ref, self.body())
22822283
}
22832284

compiler/rustc_borrowck/src/path_utils.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use crate::BorrowIndex;
77
use crate::Upvar;
88
use rustc_data_structures::graph::dominators::Dominators;
99
use rustc_middle::mir::BorrowKind;
10-
use rustc_middle::mir::{BasicBlock, Body, Field, Location, Place, PlaceRef, ProjectionElem};
10+
use rustc_middle::mir::{BasicBlock, Body, Location, Place, PlaceRef, ProjectionElem};
1111
use rustc_middle::ty::TyCtxt;
12+
use rustc_target::abi::FieldIdx;
1213

1314
/// Returns `true` if the borrow represented by `kind` is
1415
/// allowed to be split into separate Reservation and
@@ -148,7 +149,7 @@ pub(crate) fn is_upvar_field_projection<'tcx>(
148149
upvars: &[Upvar<'tcx>],
149150
place_ref: PlaceRef<'tcx>,
150151
body: &Body<'tcx>,
151-
) -> Option<Field> {
152+
) -> Option<FieldIdx> {
152153
let mut place_ref = place_ref;
153154
let mut by_ref = false;
154155

compiler/rustc_borrowck/src/type_check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -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::FIRST_VARIANT;
39+
use rustc_target::abi::{FieldIdx, 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};
@@ -786,7 +786,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
786786
&mut self,
787787
parent: &dyn fmt::Debug,
788788
base_ty: PlaceTy<'tcx>,
789-
field: Field,
789+
field: FieldIdx,
790790
location: Location,
791791
) -> Result<Ty<'tcx>, FieldAccessError> {
792792
let tcx = self.tcx();

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_
327327
ArgKind::Spread(params) => {
328328
for (i, param) in params.into_iter().enumerate() {
329329
if let Some(param) = param {
330-
place.place_field(fx, mir::Field::new(i)).write_cvalue(fx, param);
330+
place.place_field(fx, FieldIdx::new(i)).write_cvalue(fx, param);
331331
}
332332
}
333333
}
@@ -460,7 +460,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
460460
args.push(self_arg);
461461
for i in 0..tupled_arguments.len() {
462462
args.push(CallArgument {
463-
value: pack_arg.value.value_field(fx, mir::Field::new(i)),
463+
value: pack_arg.value.value_field(fx, FieldIdx::new(i)),
464464
is_owned: pack_arg.is_owned,
465465
});
466466
}

compiler/rustc_codegen_cranelift/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ fn codegen_stmt<'tcx>(
797797
let index = fx.bcx.ins().iconst(fx.pointer_type, field_index as i64);
798798
variant_dest.place_index(fx, index)
799799
} else {
800-
variant_dest.place_field(fx, mir::Field::new(field_index))
800+
variant_dest.place_field(fx, FieldIdx::new(field_index))
801801
};
802802
to.write_cvalue(fx, operand);
803803
}

compiler/rustc_codegen_cranelift/src/discriminant.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
2626
tag_encoding: TagEncoding::Direct,
2727
variants: _,
2828
} => {
29-
let ptr = place.place_field(fx, mir::Field::new(tag_field));
29+
let ptr = place.place_field(fx, FieldIdx::new(tag_field));
3030
let to = layout.ty.discriminant_for_variant(fx.tcx, variant_index).unwrap().val;
3131
let to = if ptr.layout().abi.is_signed() {
3232
ty::ScalarInt::try_from_int(
@@ -47,7 +47,7 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
4747
variants: _,
4848
} => {
4949
if variant_index != untagged_variant {
50-
let niche = place.place_field(fx, mir::Field::new(tag_field));
50+
let niche = place.place_field(fx, FieldIdx::new(tag_field));
5151
let niche_type = fx.clif_type(niche.layout().ty).unwrap();
5252
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
5353
let niche_value = (niche_value as u128).wrapping_add(niche_start);
@@ -107,7 +107,7 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
107107
let cast_to = fx.clif_type(dest_layout.ty).unwrap();
108108

109109
// Read the tag/niche-encoded discriminant from memory.
110-
let tag = value.value_field(fx, mir::Field::new(tag_field));
110+
let tag = value.value_field(fx, FieldIdx::new(tag_field));
111111
let tag = tag.load_scalar(fx);
112112

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

compiler/rustc_codegen_cranelift/src/intrinsics/llvm_x86.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ fn llvm_add_sub<'tcx>(
179179

180180
// c + carry -> c + first intermediate carry or borrow respectively
181181
let int0 = crate::num::codegen_checked_int_binop(fx, bin_op, a, b);
182-
let c = int0.value_field(fx, mir::Field::new(0));
183-
let cb0 = int0.value_field(fx, mir::Field::new(1)).load_scalar(fx);
182+
let c = int0.value_field(fx, FieldIdx::new(0));
183+
let cb0 = int0.value_field(fx, FieldIdx::new(1)).load_scalar(fx);
184184

185185
// c + carry -> c + second intermediate carry or borrow respectively
186186
let cb_in_as_u64 = fx.bcx.ins().uextend(types::I64, cb_in);

compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
253253
}
254254

255255
ret.write_cvalue(fx, base);
256-
let ret_lane = ret.place_field(fx, mir::Field::new(idx.try_into().unwrap()));
256+
let ret_lane = ret.place_field(fx, FieldIdx::new(idx.try_into().unwrap()));
257257
ret_lane.write_cvalue(fx, val);
258258
}
259259

compiler/rustc_codegen_cranelift/src/lib.rs

+1-1
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, FIRST_VARIANT};
89+
pub(crate) use rustc_target::abi::{Abi, FieldIdx, Scalar, Size, VariantIdx, FIRST_VARIANT};
9090

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

compiler/rustc_codegen_cranelift/src/unsize.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ pub(crate) fn coerce_unsized_into<'tcx>(
147147
assert_eq!(def_a, def_b);
148148

149149
for i in 0..def_a.variant(FIRST_VARIANT).fields.len() {
150-
let src_f = src.value_field(fx, mir::Field::new(i));
151-
let dst_f = dst.place_field(fx, mir::Field::new(i));
150+
let src_f = src.value_field(fx, FieldIdx::new(i));
151+
let dst_f = dst.place_field(fx, FieldIdx::new(i));
152152

153153
if dst_f.layout().is_zst() {
154154
continue;

compiler/rustc_codegen_cranelift/src/value_and_place.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn codegen_field<'tcx>(
1010
base: Pointer,
1111
extra: Option<Value>,
1212
layout: TyAndLayout<'tcx>,
13-
field: mir::Field,
13+
field: FieldIdx,
1414
) -> (Pointer, TyAndLayout<'tcx>) {
1515
let field_offset = layout.fields.offset(field.index());
1616
let field_layout = layout.field(&*fx, field.index());
@@ -210,7 +210,7 @@ impl<'tcx> CValue<'tcx> {
210210
pub(crate) fn value_field(
211211
self,
212212
fx: &mut FunctionCx<'_, '_, 'tcx>,
213-
field: mir::Field,
213+
field: FieldIdx,
214214
) -> CValue<'tcx> {
215215
let layout = self.1;
216216
match self.0 {
@@ -687,7 +687,7 @@ impl<'tcx> CPlace<'tcx> {
687687
pub(crate) fn place_field(
688688
self,
689689
fx: &mut FunctionCx<'_, '_, 'tcx>,
690-
field: mir::Field,
690+
field: FieldIdx,
691691
) -> CPlace<'tcx> {
692692
let layout = self.layout();
693693

compiler/rustc_codegen_cranelift/src/vtable.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub(crate) fn get_ptr_and_method_ref<'tcx>(
5050
if let Abi::Scalar(_) = arg.layout().abi {
5151
'descend_newtypes: while !arg.layout().ty.is_unsafe_ptr() && !arg.layout().ty.is_ref() {
5252
for i in 0..arg.layout().fields.count() {
53-
let field = arg.value_field(fx, mir::Field::new(i));
53+
let field = arg.value_field(fx, FieldIdx::new(i));
5454
if !field.layout().is_zst() {
5555
// we found the one non-zero-sized field that is allowed
5656
// now find *its* non-zero-sized field, or stop if it's a
@@ -68,9 +68,9 @@ pub(crate) fn get_ptr_and_method_ref<'tcx>(
6868
if ty.is_dyn_star() {
6969
let inner_layout = fx.layout_of(arg.layout().ty.builtin_deref(true).unwrap().ty);
7070
let dyn_star = CPlace::for_ptr(Pointer::new(arg.load_scalar(fx)), inner_layout);
71-
let ptr = dyn_star.place_field(fx, mir::Field::new(0)).to_ptr();
71+
let ptr = dyn_star.place_field(fx, FieldIdx::new(0)).to_ptr();
7272
let vtable =
73-
dyn_star.place_field(fx, mir::Field::new(1)).to_cvalue(fx).load_scalar(fx);
73+
dyn_star.place_field(fx, FieldIdx::new(1)).to_cvalue(fx).load_scalar(fx);
7474
break 'block (ptr, vtable);
7575
}
7676
}

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ use rustc_hir::def::CtorKind;
66
use rustc_index::vec::IndexVec;
77
use rustc_middle::{
88
bug,
9-
mir::{Field, GeneratorLayout, GeneratorSavedLocal},
9+
mir::{GeneratorLayout, GeneratorSavedLocal},
1010
ty::{
1111
self,
1212
layout::{IntegerExt, LayoutOf, PrimitiveExt, TyAndLayout},
1313
AdtDef, GeneratorSubsts, Ty, VariantDef,
1414
},
1515
};
1616
use rustc_span::Symbol;
17-
use rustc_target::abi::{HasDataLayout, Integer, Primitive, TagEncoding, VariantIdx, Variants};
17+
use rustc_target::abi::{
18+
FieldIdx, HasDataLayout, Integer, Primitive, TagEncoding, VariantIdx, Variants,
19+
};
1820
use std::borrow::Cow;
1921

2022
use crate::{
@@ -353,7 +355,7 @@ pub fn build_generator_variant_struct_type_di_node<'ll, 'tcx>(
353355
let state_specific_fields: SmallVec<_> = (0..variant_layout.fields.count())
354356
.map(|field_index| {
355357
let generator_saved_local = generator_layout.variant_fields[variant_index]
356-
[Field::from_usize(field_index)];
358+
[FieldIdx::from_usize(field_index)];
357359
let field_name_maybe = state_specific_upvar_names[generator_saved_local];
358360
let field_name = field_name_maybe
359361
.as_ref()

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
88
use rustc_session::config::DebugInfo;
99
use rustc_span::symbol::{kw, Symbol};
1010
use rustc_span::{BytePos, Span};
11-
use rustc_target::abi::{Abi, Size, VariantIdx};
11+
use rustc_target::abi::{Abi, FieldIdx, Size, VariantIdx};
1212

1313
use super::operand::{OperandRef, OperandValue};
1414
use super::place::PlaceRef;
@@ -79,7 +79,7 @@ impl<'tcx, S: Copy, L: Copy> DebugScope<S, L> {
7979
trait DebugInfoOffsetLocation<'tcx, Bx> {
8080
fn deref(&self, bx: &mut Bx) -> Self;
8181
fn layout(&self) -> TyAndLayout<'tcx>;
82-
fn project_field(&self, bx: &mut Bx, field: mir::Field) -> Self;
82+
fn project_field(&self, bx: &mut Bx, field: FieldIdx) -> Self;
8383
fn downcast(&self, bx: &mut Bx, variant: VariantIdx) -> Self;
8484
}
8585

@@ -94,7 +94,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> DebugInfoOffsetLocation<'tcx, Bx>
9494
self.layout
9595
}
9696

97-
fn project_field(&self, bx: &mut Bx, field: mir::Field) -> Self {
97+
fn project_field(&self, bx: &mut Bx, field: FieldIdx) -> Self {
9898
PlaceRef::project_field(*self, bx, field.index())
9999
}
100100

@@ -116,7 +116,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> DebugInfoOffsetLocation<'tcx, Bx>
116116
*self
117117
}
118118

119-
fn project_field(&self, bx: &mut Bx, field: mir::Field) -> Self {
119+
fn project_field(&self, bx: &mut Bx, field: FieldIdx) -> Self {
120120
self.field(bx.cx(), field.index())
121121
}
122122

0 commit comments

Comments
 (0)