Skip to content

Commit ff6d147

Browse files
rustc_abi::Abi to IrForm in ty_utils
1 parent 6c0ef5a commit ff6d147

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

compiler/rustc_ty_utils/src/abi.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::iter;
22

33
use rustc_abi::Primitive::Pointer;
4-
use rustc_abi::{Abi, PointerKind, Scalar, Size};
4+
use rustc_abi::{IrForm, PointerKind, Scalar, Size};
55
use rustc_hir as hir;
66
use rustc_hir::lang_items::LangItem;
77
use rustc_middle::bug;
@@ -469,7 +469,7 @@ fn fn_abi_sanity_check<'tcx>(
469469
// careful. Scalar/ScalarPair is fine, since backends will generally use
470470
// `layout.abi` and ignore everything else. We should just reject `Aggregate`
471471
// entirely here, but some targets need to be fixed first.
472-
if matches!(arg.layout.abi, Abi::Aggregate { .. }) {
472+
if matches!(arg.layout.ir_form, IrForm::Memory { .. }) {
473473
// For an unsized type we'd only pass the sized prefix, so there is no universe
474474
// in which we ever want to allow this.
475475
assert!(
@@ -500,7 +500,7 @@ fn fn_abi_sanity_check<'tcx>(
500500
// Similar to `Direct`, we need to make sure that backends use `layout.abi` and
501501
// ignore the rest of the layout.
502502
assert!(
503-
matches!(arg.layout.abi, Abi::ScalarPair(..)),
503+
matches!(arg.layout.ir_form, IrForm::ScalarPair(..)),
504504
"PassMode::Pair for type {}",
505505
arg.layout.ty
506506
);
@@ -658,9 +658,9 @@ fn fn_abi_adjust_for_abi<'tcx>(
658658
fn unadjust<'tcx>(arg: &mut ArgAbi<'tcx, Ty<'tcx>>) {
659659
// This still uses `PassMode::Pair` for ScalarPair types. That's unlikely to be intended,
660660
// but who knows what breaks if we change this now.
661-
if matches!(arg.layout.abi, Abi::Aggregate { .. }) {
661+
if matches!(arg.layout.ir_form, IrForm::Memory { .. }) {
662662
assert!(
663-
arg.layout.abi.is_sized(),
663+
arg.layout.ir_form.is_sized(),
664664
"'unadjusted' ABI does not support unsized arguments"
665665
);
666666
}
@@ -731,8 +731,8 @@ fn make_thin_self_ptr<'tcx>(
731731
// FIXME (mikeyhew) change this to use &own if it is ever added to the language
732732
Ty::new_mut_ptr(tcx, layout.ty)
733733
} else {
734-
match layout.abi {
735-
Abi::ScalarPair(..) | Abi::Scalar(..) => (),
734+
match layout.ir_form {
735+
IrForm::ScalarPair(..) | IrForm::Scalar(..) => (),
736736
_ => bug!("receiver type has unsupported layout: {:?}", layout),
737737
}
738738

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use hir::def_id::DefId;
55
use rustc_abi::Integer::{I8, I32};
66
use rustc_abi::Primitive::{self, Float, Int, Pointer};
77
use rustc_abi::{
8-
Abi, AbiAndPrefAlign, AddressSpace, Align, FieldsShape, HasDataLayout, LayoutCalculatorError,
9-
LayoutS, Niche, ReprOptions, Scalar, Size, StructKind, TagEncoding, Variants, WrappingRange,
8+
AbiAndPrefAlign, AddressSpace, Align, FieldsShape, HasDataLayout, IrForm,
9+
LayoutCalculatorError, LayoutS, Niche, ReprOptions, Scalar, Size, StructKind, TagEncoding,
10+
Variants, WrappingRange,
1011
};
1112
use rustc_index::bit_set::BitSet;
1213
use rustc_index::{IndexSlice, IndexVec};
@@ -173,7 +174,9 @@ fn layout_of_uncached<'tcx>(
173174
let mut layout = LayoutS::clone(&layout.0);
174175
match *pat {
175176
ty::PatternKind::Range { start, end, include_end } => {
176-
if let Abi::Scalar(scalar) | Abi::ScalarPair(scalar, _) = &mut layout.abi {
177+
if let IrForm::Scalar(scalar) | IrForm::ScalarPair(scalar, _) =
178+
&mut layout.ir_form
179+
{
177180
if let Some(start) = start {
178181
scalar.valid_range_mut().start = start
179182
.try_to_bits(tcx, param_env)
@@ -275,7 +278,7 @@ fn layout_of_uncached<'tcx>(
275278
return Ok(tcx.mk_layout(LayoutS::scalar(cx, data_ptr)));
276279
}
277280

278-
let Abi::Scalar(metadata) = metadata_layout.abi else {
281+
let IrForm::Scalar(metadata) = metadata_layout.ir_form else {
279282
return Err(error(cx, LayoutError::Unknown(pointee)));
280283
};
281284

@@ -330,17 +333,17 @@ fn layout_of_uncached<'tcx>(
330333
.ok_or_else(|| error(cx, LayoutError::SizeOverflow(ty)))?;
331334

332335
let abi = if count != 0 && ty.is_privately_uninhabited(tcx, param_env) {
333-
Abi::Uninhabited
336+
IrForm::Uninhabited
334337
} else {
335-
Abi::Aggregate { sized: true }
338+
IrForm::Memory { sized: true }
336339
};
337340

338341
let largest_niche = if count != 0 { element.largest_niche } else { None };
339342

340343
tcx.mk_layout(LayoutS {
341344
variants: Variants::Single { index: FIRST_VARIANT },
342345
fields: FieldsShape::Array { stride: element.size, count },
343-
abi,
346+
ir_form: abi,
344347
largest_niche,
345348
align: element.align,
346349
size,
@@ -353,7 +356,7 @@ fn layout_of_uncached<'tcx>(
353356
tcx.mk_layout(LayoutS {
354357
variants: Variants::Single { index: FIRST_VARIANT },
355358
fields: FieldsShape::Array { stride: element.size, count: 0 },
356-
abi: Abi::Aggregate { sized: false },
359+
ir_form: IrForm::Memory { sized: false },
357360
largest_niche: None,
358361
align: element.align,
359362
size: Size::ZERO,
@@ -364,7 +367,7 @@ fn layout_of_uncached<'tcx>(
364367
ty::Str => tcx.mk_layout(LayoutS {
365368
variants: Variants::Single { index: FIRST_VARIANT },
366369
fields: FieldsShape::Array { stride: Size::from_bytes(1), count: 0 },
367-
abi: Abi::Aggregate { sized: false },
370+
ir_form: IrForm::Memory { sized: false },
368371
largest_niche: None,
369372
align: dl.i8_align,
370373
size: Size::ZERO,
@@ -384,8 +387,8 @@ fn layout_of_uncached<'tcx>(
384387
&ReprOptions::default(),
385388
StructKind::AlwaysSized,
386389
)?;
387-
match unit.abi {
388-
Abi::Aggregate { ref mut sized } => *sized = false,
390+
match unit.ir_form {
391+
IrForm::Memory { ref mut sized } => *sized = false,
389392
_ => bug!(),
390393
}
391394
tcx.mk_layout(unit)
@@ -500,7 +503,7 @@ fn layout_of_uncached<'tcx>(
500503

501504
// Compute the ABI of the element type:
502505
let e_ly = cx.layout_of(e_ty)?;
503-
let Abi::Scalar(e_abi) = e_ly.abi else {
506+
let IrForm::Scalar(e_abi) = e_ly.ir_form else {
504507
// This error isn't caught in typeck, e.g., if
505508
// the element type of the vector is generic.
506509
tcx.dcx().emit_fatal(NonPrimitiveSimdType { ty, e_ty });
@@ -516,12 +519,12 @@ fn layout_of_uncached<'tcx>(
516519
// Non-power-of-two vectors have padding up to the next power-of-two.
517520
// If we're a packed repr, remove the padding while keeping the alignment as close
518521
// to a vector as possible.
519-
(Abi::Aggregate { sized: true }, AbiAndPrefAlign {
522+
(IrForm::Memory { sized: true }, AbiAndPrefAlign {
520523
abi: Align::max_for_offset(size),
521524
pref: dl.vector_align(size).pref,
522525
})
523526
} else {
524-
(Abi::Vector { element: e_abi, count: e_len }, dl.vector_align(size))
527+
(IrForm::Vector { element: e_abi, count: e_len }, dl.vector_align(size))
525528
};
526529
let size = size.align_to(align.abi);
527530

@@ -535,7 +538,7 @@ fn layout_of_uncached<'tcx>(
535538
tcx.mk_layout(LayoutS {
536539
variants: Variants::Single { index: FIRST_VARIANT },
537540
fields,
538-
abi,
541+
ir_form: abi,
539542
largest_niche: e_ly.largest_niche,
540543
size,
541544
align,
@@ -985,11 +988,12 @@ fn coroutine_layout<'tcx>(
985988

986989
size = size.align_to(align.abi);
987990

988-
let abi = if prefix.abi.is_uninhabited() || variants.iter().all(|v| v.abi.is_uninhabited()) {
989-
Abi::Uninhabited
990-
} else {
991-
Abi::Aggregate { sized: true }
992-
};
991+
let abi =
992+
if prefix.ir_form.is_uninhabited() || variants.iter().all(|v| v.ir_form.is_uninhabited()) {
993+
IrForm::Uninhabited
994+
} else {
995+
IrForm::Memory { sized: true }
996+
};
993997

994998
let layout = tcx.mk_layout(LayoutS {
995999
variants: Variants::Multiple {
@@ -999,7 +1003,7 @@ fn coroutine_layout<'tcx>(
9991003
variants,
10001004
},
10011005
fields: outer_fields,
1002-
abi,
1006+
ir_form: abi,
10031007
// Suppress niches inside coroutines. If the niche is inside a field that is aliased (due to
10041008
// self-referentiality), getting the discriminant can cause aliasing violations.
10051009
// `UnsafeCell` blocks niches for the same reason, but we don't yet have `UnsafePinned` that

compiler/rustc_ty_utils/src/layout/invariant.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ pub(super) fn partially_check_layout<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLa
6666

6767
fn check_layout_abi<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayout<'tcx>) {
6868
// Verify the ABI mandated alignment and size.
69-
let align = layout.abi.inherent_align(cx).map(|align| align.abi);
70-
let size = layout.abi.inherent_size(cx);
69+
let align = layout.ir_form.inherent_align(cx).map(|align| align.abi);
70+
let size = layout.ir_form.inherent_size(cx);
7171
let Some((align, size)) = align.zip(size) else {
7272
assert_matches!(
7373
layout.layout.abi(),
74-
Abi::Uninhabited | Abi::Aggregate { .. },
74+
IrForm::Uninhabited | IrForm::Memory { .. },
7575
"ABI unexpectedly missing alignment and/or size in {layout:#?}"
7676
);
7777
return;
@@ -89,11 +89,11 @@ pub(super) fn partially_check_layout<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLa
8989

9090
// Verify per-ABI invariants
9191
match layout.layout.abi() {
92-
Abi::Scalar(_) => {
92+
IrForm::Scalar(_) => {
9393
// Check that this matches the underlying field.
9494
let inner = skip_newtypes(cx, layout);
9595
assert!(
96-
matches!(inner.layout.abi(), Abi::Scalar(_)),
96+
matches!(inner.layout.abi(), IrForm::Scalar(_)),
9797
"`Scalar` type {} is newtype around non-`Scalar` type {}",
9898
layout.ty,
9999
inner.ty
@@ -132,7 +132,7 @@ pub(super) fn partially_check_layout<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLa
132132
"`Scalar` field with bad align in {inner:#?}",
133133
);
134134
assert!(
135-
matches!(field.abi, Abi::Scalar(_)),
135+
matches!(field.ir_form, IrForm::Scalar(_)),
136136
"`Scalar` field with bad ABI in {inner:#?}",
137137
);
138138
}
@@ -141,11 +141,11 @@ pub(super) fn partially_check_layout<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLa
141141
}
142142
}
143143
}
144-
Abi::ScalarPair(scalar1, scalar2) => {
144+
IrForm::ScalarPair(scalar1, scalar2) => {
145145
// Check that the underlying pair of fields matches.
146146
let inner = skip_newtypes(cx, layout);
147147
assert!(
148-
matches!(inner.layout.abi(), Abi::ScalarPair(..)),
148+
matches!(inner.layout.abi(), IrForm::ScalarPair(..)),
149149
"`ScalarPair` type {} is newtype around non-`ScalarPair` type {}",
150150
layout.ty,
151151
inner.ty
@@ -208,8 +208,8 @@ pub(super) fn partially_check_layout<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLa
208208
"`ScalarPair` first field with bad align in {inner:#?}",
209209
);
210210
assert_matches!(
211-
field1.abi,
212-
Abi::Scalar(_),
211+
field1.ir_form,
212+
IrForm::Scalar(_),
213213
"`ScalarPair` first field with bad ABI in {inner:#?}",
214214
);
215215
let field2_offset = size1.align_to(align2);
@@ -226,16 +226,16 @@ pub(super) fn partially_check_layout<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLa
226226
"`ScalarPair` second field with bad align in {inner:#?}",
227227
);
228228
assert_matches!(
229-
field2.abi,
230-
Abi::Scalar(_),
229+
field2.ir_form,
230+
IrForm::Scalar(_),
231231
"`ScalarPair` second field with bad ABI in {inner:#?}",
232232
);
233233
}
234-
Abi::Vector { element, .. } => {
234+
IrForm::Vector { element, .. } => {
235235
assert!(align >= element.align(cx).abi); // just sanity-checking `vector_align`.
236236
// FIXME: Do some kind of check of the inner type, like for Scalar and ScalarPair.
237237
}
238-
Abi::Uninhabited | Abi::Aggregate { .. } => {} // Nothing to check.
238+
IrForm::Uninhabited | IrForm::Memory { .. } => {} // Nothing to check.
239239
}
240240
}
241241

@@ -276,13 +276,13 @@ pub(super) fn partially_check_layout<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLa
276276
// The top-level ABI and the ABI of the variants should be coherent.
277277
let scalar_coherent =
278278
|s1: Scalar, s2: Scalar| s1.size(cx) == s2.size(cx) && s1.align(cx) == s2.align(cx);
279-
let abi_coherent = match (layout.abi, variant.abi) {
280-
(Abi::Scalar(s1), Abi::Scalar(s2)) => scalar_coherent(s1, s2),
281-
(Abi::ScalarPair(a1, b1), Abi::ScalarPair(a2, b2)) => {
279+
let abi_coherent = match (layout.ir_form, variant.ir_form) {
280+
(IrForm::Scalar(s1), IrForm::Scalar(s2)) => scalar_coherent(s1, s2),
281+
(IrForm::ScalarPair(a1, b1), IrForm::ScalarPair(a2, b2)) => {
282282
scalar_coherent(a1, a2) && scalar_coherent(b1, b2)
283283
}
284-
(Abi::Uninhabited, _) => true,
285-
(Abi::Aggregate { .. }, _) => true,
284+
(IrForm::Uninhabited, _) => true,
285+
(IrForm::Memory { .. }, _) => true,
286286
_ => false,
287287
};
288288
if !abi_coherent {

0 commit comments

Comments
 (0)