1use std::cmp;
2use std::fmt::{self, Write};
3use std::num::NonZero;
4use std::ops::Deref;
5use std::range::{RangeFrom, RangeToInclusive};
6
7pub use coroutine::PackCoroutineLayout;
8use rustc_hashes::Hash64;
9use rustc_index::Idx;
10use rustc_index::bit_set::BitMatrix;
11use tracing::debug;
12
13use crate::{
14 Align, BackendLaneCount, BackendRepr, FieldsShape, HasDataLayout, IndexSlice, IndexVec,
15 Integer, LayoutData, NicheOptimizations, NumScalableVectors, Primitive, ReprOptions, Scalar,
16 Size, StructKind, TargetDataLayout, Variants, WrappingRange,
17};
18
19mod coroutine;
20mod enum_layout;
21mod simple;
22
23#[cfg(feature = "nightly")]
24mod ty;
25
26#[cfg(feature = "nightly")]
27pub use ty::{Layout, TyAbiInterface, TyAndLayout};
28
29#[automatically_derived]impl ::core::marker::Copy for FieldIdx { }
impl FieldIdx {
#[doc = r" Maximum value the index can take, as a `u32`."]
pub const MAX_AS_U32: u32 = 0xFFFF_FF00;
#[doc = r" Maximum value the index can take."]
pub const MAX: Self = Self::from_u32(0xFFFF_FF00);
#[doc = r" Zero value of the index."]
pub const ZERO: Self = Self::from_u32(0);
#[doc = r" Creates a new index from a given `usize`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_usize(value: usize) -> Self {
if !(value <= (0xFFFF_FF00 as usize)) {
::core::panicking::panic("assertion failed: value <= (0xFFFF_FF00 as usize)")
};
unsafe { Self::from_u32_unchecked(value as u32) }
}
#[doc = r" Creates a new index from a given `u32`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_u32(value: u32) -> Self {
if !(value <= 0xFFFF_FF00) {
::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
};
unsafe { Self::from_u32_unchecked(value) }
}
#[doc = r" Creates a new index from a given `u16`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_u16(value: u16) -> Self {
let value = value as u32;
if !(value <= 0xFFFF_FF00) {
::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
};
unsafe { Self::from_u32_unchecked(value) }
}
#[doc = r" Creates a new index from a given `u32`."]
#[doc = r""]
#[doc = r" # Safety"]
#[doc = r""]
#[doc =
r" The provided value must be less than or equal to the maximum value for the newtype."]
#[doc =
r" Providing a value outside this range is undefined due to layout restrictions."]
#[doc = r""]
#[doc = r" Prefer using `from_u32`."]
#[inline]
pub const unsafe fn from_u32_unchecked(value: u32) -> Self {
Self {
private_use_as_methods_instead: unsafe {
std::mem::transmute(value)
},
}
}
#[doc = r" Extracts the value of this index as a `usize`."]
#[inline]
pub const fn index(self) -> usize { self.as_usize() }
#[doc = r" Extracts the value of this index as a `u32`."]
#[inline]
pub const fn as_u32(self) -> u32 {
unsafe { std::mem::transmute(self.private_use_as_methods_instead) }
}
#[doc = r" Extracts the value of this index as a `usize`."]
#[inline]
pub const fn as_usize(self) -> usize { self.as_u32() as usize }
}
impl std::ops::Add<usize> for FieldIdx {
type Output = Self;
#[inline]
fn add(self, other: usize) -> Self {
Self::from_usize(self.index() + other)
}
}
impl std::ops::AddAssign<usize> for FieldIdx {
#[inline]
fn add_assign(&mut self, other: usize) { *self = *self + other; }
}
impl rustc_index::Idx for FieldIdx {
#[inline]
fn new(value: usize) -> Self { Self::from_usize(value) }
#[inline]
fn index(self) -> usize { self.as_usize() }
}
impl ::std::iter::Step for FieldIdx {
#[inline]
fn steps_between(start: &Self, end: &Self) -> (usize, Option<usize>) {
<usize as
::std::iter::Step>::steps_between(&Self::index(*start),
&Self::index(*end))
}
#[inline]
fn forward_checked(start: Self, u: usize) -> Option<Self> {
Self::index(start).checked_add(u).map(Self::from_usize)
}
#[inline]
fn backward_checked(start: Self, u: usize) -> Option<Self> {
Self::index(start).checked_sub(u).map(Self::from_usize)
}
#[inline]
fn forward_overflowing(start: Self, u: usize) -> (Self, bool) {
let (s, o) = Self::index(start).overflowing_add(u);
(Self::from_usize(s), o)
}
#[inline]
fn backward_overflowing(start: Self, u: usize) -> (Self, bool) {
let (s, o) = Self::index(start).overflowing_sub(u);
(Self::from_usize(s), o)
}
}
impl ::std::cmp::Ord for FieldIdx {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_u32().cmp(&other.as_u32())
}
}
impl ::std::cmp::PartialOrd for FieldIdx {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl ::rustc_data_structures::stable_hash::StableHash for FieldIdx {
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
hcx: &mut __Hcx,
hasher: &mut ::rustc_data_structures::stable_hash::StableHasher) {
self.as_u32().stable_hash(hcx, hasher)
}
}
impl From<FieldIdx> for u32 {
#[inline]
fn from(v: FieldIdx) -> u32 { v.as_u32() }
}
impl From<FieldIdx> for usize {
#[inline]
fn from(v: FieldIdx) -> usize { v.as_usize() }
}
impl From<usize> for FieldIdx {
#[inline]
fn from(value: usize) -> Self { Self::from_usize(value) }
}
impl From<u32> for FieldIdx {
#[inline]
fn from(value: u32) -> Self { Self::from_u32(value) }
}
impl ::std::cmp::Eq for FieldIdx {}
impl ::std::cmp::PartialEq for FieldIdx {
fn eq(&self, other: &Self) -> bool { self.as_u32().eq(&other.as_u32()) }
}
impl ::std::marker::StructuralPartialEq for FieldIdx { }
impl ::std::hash::Hash for FieldIdx {
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
self.as_u32().hash(state)
}
}
impl<D: ::rustc_serialize::Decoder> ::rustc_serialize::Decodable<D> for
FieldIdx {
fn decode(d: &mut D) -> Self { Self::from_u32(d.read_u32()) }
}
impl<E: ::rustc_serialize::Encoder> ::rustc_serialize::Encodable<E> for
FieldIdx {
fn encode(&self, e: &mut E) { e.emit_u32(self.as_u32()); }
}
impl ::std::fmt::Debug for FieldIdx {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("{0}", self.as_u32()))
}
}rustc_index::newtype_index! {
30 #[stable_hash]
52 #[encodable]
53 #[orderable]
54 #[gate_rustc_only]
55 pub struct FieldIdx {}
56}
57
58impl FieldIdx {
59 pub const ONE: FieldIdx = FieldIdx::from_u32(1);
63}
64
65#[automatically_derived]impl ::core::marker::Copy for VariantIdx { }
#[doc = " Equivalent to `VariantIdx(0)`."]
pub const FIRST_VARIANT: VariantIdx = VariantIdx::from_u32(0);
impl VariantIdx {
#[doc = r" Maximum value the index can take, as a `u32`."]
pub const MAX_AS_U32: u32 = 0xFFFF_FF00;
#[doc = r" Maximum value the index can take."]
pub const MAX: Self = Self::from_u32(0xFFFF_FF00);
#[doc = r" Zero value of the index."]
pub const ZERO: Self = Self::from_u32(0);
#[doc = r" Creates a new index from a given `usize`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_usize(value: usize) -> Self {
if !(value <= (0xFFFF_FF00 as usize)) {
::core::panicking::panic("assertion failed: value <= (0xFFFF_FF00 as usize)")
};
unsafe { Self::from_u32_unchecked(value as u32) }
}
#[doc = r" Creates a new index from a given `u32`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_u32(value: u32) -> Self {
if !(value <= 0xFFFF_FF00) {
::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
};
unsafe { Self::from_u32_unchecked(value) }
}
#[doc = r" Creates a new index from a given `u16`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_u16(value: u16) -> Self {
let value = value as u32;
if !(value <= 0xFFFF_FF00) {
::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
};
unsafe { Self::from_u32_unchecked(value) }
}
#[doc = r" Creates a new index from a given `u32`."]
#[doc = r""]
#[doc = r" # Safety"]
#[doc = r""]
#[doc =
r" The provided value must be less than or equal to the maximum value for the newtype."]
#[doc =
r" Providing a value outside this range is undefined due to layout restrictions."]
#[doc = r""]
#[doc = r" Prefer using `from_u32`."]
#[inline]
pub const unsafe fn from_u32_unchecked(value: u32) -> Self {
Self {
private_use_as_methods_instead: unsafe {
std::mem::transmute(value)
},
}
}
#[doc = r" Extracts the value of this index as a `usize`."]
#[inline]
pub const fn index(self) -> usize { self.as_usize() }
#[doc = r" Extracts the value of this index as a `u32`."]
#[inline]
pub const fn as_u32(self) -> u32 {
unsafe { std::mem::transmute(self.private_use_as_methods_instead) }
}
#[doc = r" Extracts the value of this index as a `usize`."]
#[inline]
pub const fn as_usize(self) -> usize { self.as_u32() as usize }
}
impl std::ops::Add<usize> for VariantIdx {
type Output = Self;
#[inline]
fn add(self, other: usize) -> Self {
Self::from_usize(self.index() + other)
}
}
impl std::ops::AddAssign<usize> for VariantIdx {
#[inline]
fn add_assign(&mut self, other: usize) { *self = *self + other; }
}
impl rustc_index::Idx for VariantIdx {
#[inline]
fn new(value: usize) -> Self { Self::from_usize(value) }
#[inline]
fn index(self) -> usize { self.as_usize() }
}
impl ::std::iter::Step for VariantIdx {
#[inline]
fn steps_between(start: &Self, end: &Self) -> (usize, Option<usize>) {
<usize as
::std::iter::Step>::steps_between(&Self::index(*start),
&Self::index(*end))
}
#[inline]
fn forward_checked(start: Self, u: usize) -> Option<Self> {
Self::index(start).checked_add(u).map(Self::from_usize)
}
#[inline]
fn backward_checked(start: Self, u: usize) -> Option<Self> {
Self::index(start).checked_sub(u).map(Self::from_usize)
}
#[inline]
fn forward_overflowing(start: Self, u: usize) -> (Self, bool) {
let (s, o) = Self::index(start).overflowing_add(u);
(Self::from_usize(s), o)
}
#[inline]
fn backward_overflowing(start: Self, u: usize) -> (Self, bool) {
let (s, o) = Self::index(start).overflowing_sub(u);
(Self::from_usize(s), o)
}
}
impl ::std::cmp::Ord for VariantIdx {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_u32().cmp(&other.as_u32())
}
}
impl ::std::cmp::PartialOrd for VariantIdx {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl ::rustc_data_structures::stable_hash::StableHash for VariantIdx {
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
hcx: &mut __Hcx,
hasher: &mut ::rustc_data_structures::stable_hash::StableHasher) {
self.as_u32().stable_hash(hcx, hasher)
}
}
impl From<VariantIdx> for u32 {
#[inline]
fn from(v: VariantIdx) -> u32 { v.as_u32() }
}
impl From<VariantIdx> for usize {
#[inline]
fn from(v: VariantIdx) -> usize { v.as_usize() }
}
impl From<usize> for VariantIdx {
#[inline]
fn from(value: usize) -> Self { Self::from_usize(value) }
}
impl From<u32> for VariantIdx {
#[inline]
fn from(value: u32) -> Self { Self::from_u32(value) }
}
impl ::std::cmp::Eq for VariantIdx {}
impl ::std::cmp::PartialEq for VariantIdx {
fn eq(&self, other: &Self) -> bool { self.as_u32().eq(&other.as_u32()) }
}
impl ::std::marker::StructuralPartialEq for VariantIdx { }
impl ::std::hash::Hash for VariantIdx {
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
self.as_u32().hash(state)
}
}
impl<D: ::rustc_serialize::Decoder> ::rustc_serialize::Decodable<D> for
VariantIdx {
fn decode(d: &mut D) -> Self { Self::from_u32(d.read_u32()) }
}
impl<E: ::rustc_serialize::Encoder> ::rustc_serialize::Encodable<E> for
VariantIdx {
fn encode(&self, e: &mut E) { e.emit_u32(self.as_u32()); }
}
impl ::std::fmt::Debug for VariantIdx {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("{0}", self.as_u32()))
}
}rustc_index::newtype_index! {
66 #[stable_hash]
77 #[encodable]
78 #[orderable]
79 #[gate_rustc_only]
80 pub struct VariantIdx {
81 const FIRST_VARIANT = 0;
83 }
84}
85
86fn absent<'a, FieldIdx, VariantIdx, F>(fields: &IndexSlice<FieldIdx, F>) -> bool
92where
93 FieldIdx: Idx,
94 VariantIdx: Idx,
95 F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug,
96{
97 let uninhabited = fields.iter().any(|f| f.is_uninhabited());
98 let is_1zst = fields.iter().all(|f| f.is_1zst());
101 uninhabited && is_1zst
102}
103
104enum NicheBias {
106 Start,
107 End,
108}
109
110#[derive(#[automatically_derived]impl<F: ::core::marker::Copy> ::core::marker::Copy for
LayoutCalculatorError<F> {
}Copy, #[automatically_derived]impl<F: ::core::clone::Clone> ::core::clone::Clone for
LayoutCalculatorError<F> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
match self {
Self::UnexpectedUnsized(__self_0) =>
LayoutCalculatorError::UnexpectedUnsized(::core::clone::Clone::clone(__self_0)),
Self::SizeOverflow => LayoutCalculatorError::SizeOverflow,
Self::EmptyUnion => LayoutCalculatorError::EmptyUnion,
Self::ReprConflict => LayoutCalculatorError::ReprConflict,
Self::ZeroLengthSimdType =>
LayoutCalculatorError::ZeroLengthSimdType,
Self::OversizedSimdType { max_lanes: __self_0 } =>
LayoutCalculatorError::OversizedSimdType {
max_lanes: ::core::clone::Clone::clone(__self_0),
},
Self::NonPrimitiveSimdType(__self_0) =>
LayoutCalculatorError::NonPrimitiveSimdType(::core::clone::Clone::clone(__self_0)),
}
}
}Clone, #[automatically_derived]impl<F: ::core::fmt::Debug> ::core::fmt::Debug for
LayoutCalculatorError<F> {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::UnexpectedUnsized(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"UnexpectedUnsized", &__self_0),
Self::SizeOverflow =>
::core::fmt::Formatter::write_str(f, "SizeOverflow"),
Self::EmptyUnion =>
::core::fmt::Formatter::write_str(f, "EmptyUnion"),
Self::ReprConflict =>
::core::fmt::Formatter::write_str(f, "ReprConflict"),
Self::ZeroLengthSimdType =>
::core::fmt::Formatter::write_str(f, "ZeroLengthSimdType"),
Self::OversizedSimdType { max_lanes: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"OversizedSimdType max_lanes", &__self_0),
Self::NonPrimitiveSimdType(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"NonPrimitiveSimdType", &__self_0),
}
}
}Debug, #[automatically_derived]impl<F: ::core::cmp::PartialEq>
::core::marker::StructuralPartialEq for LayoutCalculatorError<F> {
}
#[automatically_derived]impl<F: ::core::cmp::PartialEq> ::core::cmp::PartialEq
for LayoutCalculatorError<F> {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other) &&
match (self, other) {
(Self::UnexpectedUnsized(__self_0),
Self::UnexpectedUnsized(__arg1_0)) => __self_0 == __arg1_0,
(Self::OversizedSimdType { max_lanes: __self_0 },
Self::OversizedSimdType { max_lanes: __arg1_0 }) =>
__self_0 == __arg1_0,
(Self::NonPrimitiveSimdType(__self_0),
Self::NonPrimitiveSimdType(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]impl<F: ::core::cmp::Eq> ::core::cmp::Eq for
LayoutCalculatorError<F> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<F>;
let _: ::core::cmp::AssertParamIsEq<usize>;
}
}Eq)]
111pub enum LayoutCalculatorError<F> {
112 UnexpectedUnsized(F),
119
120 SizeOverflow,
122
123 EmptyUnion,
125
126 ReprConflict,
128
129 ZeroLengthSimdType,
131
132 OversizedSimdType { max_lanes: usize },
134
135 NonPrimitiveSimdType(F),
137}
138
139impl<F> LayoutCalculatorError<F> {
140 pub fn without_payload(&self) -> LayoutCalculatorError<()> {
141 use LayoutCalculatorError::*;
142 match *self {
143 UnexpectedUnsized(_) => UnexpectedUnsized(()),
144 SizeOverflow => SizeOverflow,
145 EmptyUnion => EmptyUnion,
146 ReprConflict => ReprConflict,
147 ZeroLengthSimdType => ZeroLengthSimdType,
148 OversizedSimdType { max_lanes } => OversizedSimdType { max_lanes },
149 NonPrimitiveSimdType(_) => NonPrimitiveSimdType(()),
150 }
151 }
152
153 pub fn fallback_fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 use LayoutCalculatorError::*;
158 f.write_str(match self {
159 UnexpectedUnsized(_) => "an unsized type was found where a sized type was expected",
160 SizeOverflow => "size overflow",
161 EmptyUnion => "type is a union with no fields",
162 ReprConflict => "type has an invalid repr",
163 ZeroLengthSimdType | OversizedSimdType { .. } | NonPrimitiveSimdType(_) => {
164 "invalid simd type definition"
165 }
166 })
167 }
168}
169
170type LayoutCalculatorResult<FieldIdx, VariantIdx, F> =
171 Result<LayoutData<FieldIdx, VariantIdx>, LayoutCalculatorError<F>>;
172
173#[derive(#[automatically_derived]impl<Cx: ::core::clone::Clone> ::core::clone::Clone
for LayoutCalculator<Cx> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
LayoutCalculator { cx: ::core::clone::Clone::clone(&self.cx) }
}
}Clone, #[automatically_derived]impl<Cx: ::core::marker::Copy> ::core::marker::Copy
for LayoutCalculator<Cx> {
}Copy, #[automatically_derived]impl<Cx: ::core::fmt::Debug> ::core::fmt::Debug for
LayoutCalculator<Cx> {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f,
"LayoutCalculator cx", &&self.cx)
}
}Debug)]
174pub struct LayoutCalculator<Cx> {
175 pub cx: Cx,
176}
177
178impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
179 pub fn new(cx: Cx) -> Self {
180 Self { cx }
181 }
182
183 pub fn layout_of_array_like<FieldIdx: Idx, VariantIdx: Idx, F>(
184 &self,
185 element: &LayoutData<FieldIdx, VariantIdx>,
186 count_if_sized: Option<u64>, ) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F> {
188 let count = count_if_sized.unwrap_or(0);
189 let size =
190 element.size.checked_mul(count, &self.cx).ok_or(LayoutCalculatorError::SizeOverflow)?;
191
192 Ok(LayoutData {
193 variants: Variants::Single { index: VariantIdx::new(0) },
194 fields: FieldsShape::Array { stride: element.size, count },
195 backend_repr: BackendRepr::Memory { sized: count_if_sized.is_some() },
196 largest_niche: element.largest_niche.filter(|_| count != 0),
197 uninhabited: element.uninhabited && count != 0,
198 align: element.align,
199 size,
200 max_repr_align: None,
201 unadjusted_abi_align: element.align,
202 repr_c: element.repr_c,
203 randomization_seed: element.randomization_seed.wrapping_add(Hash64::new(count)),
204 })
205 }
206
207 pub fn layout_of_scalable_vector_type<FieldIdx, VariantIdx, F>(
208 &self,
209 element: F,
210 count: u64,
211 number_of_vectors: NumScalableVectors,
212 ) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
213 where
214 FieldIdx: Idx,
215 VariantIdx: Idx,
216 F: AsRef<LayoutData<FieldIdx, VariantIdx>> + fmt::Debug,
217 {
218 vector_type_layout(
219 SimdVectorKind::Scalable(number_of_vectors),
220 self.cx.data_layout(),
221 element,
222 count,
223 )
224 }
225
226 pub fn layout_of_simd_type<FieldIdx, VariantIdx, F>(
227 &self,
228 element: F,
229 count: u64,
230 repr_packed: bool,
231 ) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
232 where
233 FieldIdx: Idx,
234 VariantIdx: Idx,
235 F: AsRef<LayoutData<FieldIdx, VariantIdx>> + fmt::Debug,
236 {
237 let kind = if repr_packed { SimdVectorKind::PackedFixed } else { SimdVectorKind::Fixed };
238 vector_type_layout(kind, self.cx.data_layout(), element, count)
239 }
240
241 pub fn layout_of_coroutine<'a, F, VariantIdx, FieldIdx, LocalIdx>(
246 &self,
247 local_layouts: &IndexSlice<LocalIdx, F>,
248 prefix_layouts: IndexVec<FieldIdx, F>,
249 variant_fields: &IndexSlice<VariantIdx, IndexVec<FieldIdx, LocalIdx>>,
250 storage_conflicts: &BitMatrix<LocalIdx, LocalIdx>,
251 tag_to_layout: impl Fn(Scalar) -> F,
252 ) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
253 where
254 F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
255 VariantIdx: Idx,
256 FieldIdx: Idx,
257 LocalIdx: Idx,
258 {
259 coroutine::layout(
260 self,
261 local_layouts,
262 prefix_layouts,
263 variant_fields,
264 storage_conflicts,
265 tag_to_layout,
266 )
267 }
268
269 pub fn layout_of_univariant<'a, FieldIdx, VariantIdx, F>(
275 &self,
276 fields: &IndexSlice<FieldIdx, F>,
277 repr: &ReprOptions,
278 kind: StructKind,
279 ) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
280 where
281 FieldIdx: Idx,
282 VariantIdx: Idx,
283 F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
284 {
285 let dl = self.cx.data_layout();
286 let layout = self.layout_of_univariant_biased(fields, repr, kind, NicheBias::Start);
287 if let Ok(layout) = &layout
293 && !#[allow(non_exhaustive_omitted_patterns)] match kind {
StructKind::MaybeUnsized => true,
_ => false,
}matches!(kind, StructKind::MaybeUnsized)
297 && let Some(niche) = layout.largest_niche
298 && let head_space = niche.offset.bytes()
299 && let niche_len = niche.value.size(dl).bytes()
300 && let tail_space = layout.size.bytes() - head_space - niche_len
301 && (fields.len() > 1 && head_space != 0 && tail_space > 0)
305 {
306 let alt_layout = self
307 .layout_of_univariant_biased(fields, repr, kind, NicheBias::End)
308 .expect("alt layout should always work");
309 let alt_niche = alt_layout
310 .largest_niche
311 .expect("alt layout should have a niche like the regular one");
312 let alt_head_space = alt_niche.offset.bytes();
313 let alt_niche_len = alt_niche.value.size(dl).bytes();
314 let alt_tail_space = alt_layout.size.bytes() - alt_head_space - alt_niche_len;
315
316 if true {
{
match (&layout.size.bytes(), &alt_layout.size.bytes()) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};debug_assert_eq!(layout.size.bytes(), alt_layout.size.bytes());
317
318 let prefer_alt_layout = alt_head_space > head_space && alt_head_space > tail_space;
319
320 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/739adecd444342128b4ef32cbbd8352856cb1651/compiler/rustc_abi/src/layout.rs:320",
"rustc_abi::layout", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/739adecd444342128b4ef32cbbd8352856cb1651/compiler/rustc_abi/src/layout.rs"),
::tracing_core::__macro_support::Option::Some(320u32),
::tracing_core::__macro_support::Option::Some("rustc_abi::layout"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("sz: {0}, default_niche_at: {1}+{2}, default_tail_space: {3}, alt_niche_at/head_space: {4}+{5}, alt_tail: {6}, num_fields: {7}, better: {8}\nlayout: {9}\nalt_layout: {10}\n",
layout.size.bytes(), head_space, niche_len, tail_space,
alt_head_space, alt_niche_len, alt_tail_space,
layout.fields.count(), prefer_alt_layout,
self.format_field_niches(layout, fields),
self.format_field_niches(&alt_layout, fields)) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(
321 "sz: {}, default_niche_at: {}+{}, default_tail_space: {}, alt_niche_at/head_space: {}+{}, alt_tail: {}, num_fields: {}, better: {}\n\
322 layout: {}\n\
323 alt_layout: {}\n",
324 layout.size.bytes(),
325 head_space,
326 niche_len,
327 tail_space,
328 alt_head_space,
329 alt_niche_len,
330 alt_tail_space,
331 layout.fields.count(),
332 prefer_alt_layout,
333 self.format_field_niches(layout, fields),
334 self.format_field_niches(&alt_layout, fields),
335 );
336
337 if prefer_alt_layout {
338 return Ok(alt_layout);
339 }
340 }
341 layout
342 }
343
344 pub fn layout_of_struct_or_enum<'a, FieldIdx, VariantIdx, F>(
345 &self,
346 repr: &ReprOptions,
347 variants: &IndexSlice<VariantIdx, IndexVec<FieldIdx, F>>,
348 is_enum: bool,
349 niche_optimizations: NicheOptimizations,
350 discr_range_of_repr: impl Fn(RangeFrom<i128>, RangeToInclusive<u128>) -> (Integer, bool),
351 discriminants: impl Iterator<Item = (VariantIdx, u128)>,
352 always_sized: bool,
353 ) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
354 where
355 FieldIdx: Idx,
356 VariantIdx: Idx,
357 F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
358 {
359 let (present_first, present_second) = {
360 let mut present_variants = variants.iter_enumerated().filter_map(|(i, v)| {
361 if !repr.inhibit_enum_layout_opt() && absent(v) { None } else { Some(i) }
362 });
363 (present_variants.next(), present_variants.next())
364 };
365 let present_first = match present_first {
366 Some(present_first) => present_first,
367 None if is_enum => {
369 return Ok(LayoutData::never_type());
370 }
371 None => VariantIdx::new(0),
374 };
375
376 if !is_enum ||
378 (present_second.is_none() && !repr.inhibit_enum_layout_opt())
380 {
381 self.layout_of_struct(
382 repr,
383 variants,
384 present_first,
385 is_enum,
386 niche_optimizations,
387 always_sized,
388 )
389 } else {
390 if !is_enum { ::core::panicking::panic("assertion failed: is_enum") };assert!(is_enum);
394
395 self::enum_layout::layout_of_enum(
396 self,
397 repr,
398 variants,
399 discr_range_of_repr,
400 discriminants,
401 )
402 }
403 }
404
405 pub fn layout_of_union<'a, FieldIdx, VariantIdx, F>(
406 &self,
407 repr: &ReprOptions,
408 variants: &IndexSlice<VariantIdx, IndexVec<FieldIdx, F>>,
409 ) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
410 where
411 FieldIdx: Idx,
412 VariantIdx: Idx,
413 F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
414 {
415 let dl = self.cx.data_layout();
416 let mut align = if repr.pack.is_some() { Align::ONE } else { dl.aggregate_align };
417 let mut max_repr_align = repr.align;
418
419 struct AbiMismatch;
422 let mut common_non_zst_repr_and_align = if repr.inhibits_union_abi_opt() {
423 Err(AbiMismatch)
425 } else {
426 Ok(None)
427 };
428
429 let mut size = Size::ZERO;
430 let only_variant_idx = VariantIdx::new(0);
431 let only_variant = &variants[only_variant_idx];
432 for field in only_variant {
433 if field.is_unsized() {
434 return Err(LayoutCalculatorError::UnexpectedUnsized(*field));
435 }
436
437 align = align.max(field.align);
438 max_repr_align = max_repr_align.max(field.max_repr_align);
439 size = cmp::max(size, field.size);
440
441 if field.is_zst() {
442 continue;
444 }
445
446 if let Ok(common) = common_non_zst_repr_and_align {
447 let field_abi = field.backend_repr.to_union();
449
450 if let Some((common_abi, common_align)) = common {
451 if common_abi != field_abi {
452 common_non_zst_repr_and_align = Err(AbiMismatch);
454 } else {
455 if !#[allow(non_exhaustive_omitted_patterns)] match common_abi {
BackendRepr::Memory { .. } => true,
_ => false,
}matches!(common_abi, BackendRepr::Memory { .. }) {
458 {
match (&common_align, &field.align) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val,
::core::option::Option::Some(format_args!("non-Aggregate field with matching ABI but differing alignment")));
}
}
}
};assert_eq!(
459 common_align, field.align,
460 "non-Aggregate field with matching ABI but differing alignment"
461 );
462 }
463 }
464 } else {
465 common_non_zst_repr_and_align = Ok(Some((field_abi, field.align)));
467 }
468 }
469 }
470
471 if let Some(pack) = repr.pack {
472 align = align.min(pack);
473 }
474 let unadjusted_abi_align = align;
477 if let Some(repr_align) = repr.align {
478 align = align.max(repr_align);
479 }
480 let align = align;
482
483 let backend_repr = match common_non_zst_repr_and_align {
486 Err(AbiMismatch) | Ok(None) => BackendRepr::Memory { sized: true },
487 Ok(Some((repr, _))) => match repr {
488 BackendRepr::Scalar(_) | BackendRepr::ScalarPair { .. }
490 if repr.scalar_platform_align(dl).unwrap() != align =>
491 {
492 BackendRepr::Memory { sized: true }
493 }
494 BackendRepr::SimdVector { element, count: _ }
496 if element.default_align(dl) > align =>
497 {
498 BackendRepr::Memory { sized: true }
499 }
500 BackendRepr::Scalar(..)
502 | BackendRepr::ScalarPair { .. }
503 | BackendRepr::SimdVector { .. }
504 | BackendRepr::SimdScalableVector { .. }
505 | BackendRepr::Memory { .. } => repr,
506 },
507 };
508
509 let Some(union_field_count) = NonZero::new(only_variant.len()) else {
510 return Err(LayoutCalculatorError::EmptyUnion);
511 };
512
513 let repr_c =
515 repr.c() || (repr.transparent() && only_variant.iter().any(|field| field.repr_c));
516
517 let combined_seed = only_variant
518 .iter()
519 .map(|v| v.randomization_seed)
520 .fold(repr.field_shuffle_seed, |acc, seed| acc.wrapping_add(seed));
521
522 Ok(LayoutData {
523 variants: Variants::Single { index: only_variant_idx },
524 fields: FieldsShape::Union(union_field_count),
525 backend_repr,
526 largest_niche: None,
527 uninhabited: false,
528 align,
529 size: size.align_to(align),
530 max_repr_align,
531 unadjusted_abi_align,
532 repr_c,
533 randomization_seed: combined_seed,
534 })
535 }
536
537 fn layout_of_struct<'a, FieldIdx, VariantIdx, F>(
542 &self,
543 repr: &ReprOptions,
544 variants: &IndexSlice<VariantIdx, IndexVec<FieldIdx, F>>,
545 variant_idx: VariantIdx,
546 is_enum: bool,
547 niche_optimizations: NicheOptimizations,
548 always_sized: bool,
549 ) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
550 where
551 FieldIdx: Idx,
552 VariantIdx: Idx,
553 F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
554 {
555 let dl = self.cx.data_layout();
556 let v = variant_idx;
557 let kind = if is_enum || variants[v].is_empty() || always_sized {
558 StructKind::AlwaysSized
559 } else {
560 StructKind::MaybeUnsized
561 };
562
563 let mut st = self.layout_of_univariant(&variants[v], repr, kind)?;
564 st.variants = Variants::Single { index: v };
565
566 if niche_optimizations == NicheOptimizations::Disabled {
567 let hide_niches = |scalar: &mut _| match scalar {
568 Scalar::Initialized { value, valid_range } => {
569 if *value == Primitive::Bool {
572 *value = Primitive::Int(Integer::I8, false);
573 }
574 *valid_range = WrappingRange::full(value.size(dl));
575 }
576 Scalar::Union { .. } => {}
578 };
579 match &mut st.backend_repr {
580 BackendRepr::Scalar(scalar) => hide_niches(scalar),
581 BackendRepr::ScalarPair { a, b, b_offset: _ } => {
582 hide_niches(a);
583 hide_niches(b);
584 }
585 BackendRepr::SimdVector { element, .. }
586 | BackendRepr::SimdScalableVector { element, .. } => hide_niches(element),
587 BackendRepr::Memory { sized: _ } => {}
588 }
589 st.largest_niche = None;
590 return Ok(st);
591 }
592
593 Ok(st)
594 }
595
596 fn layout_of_univariant_biased<'a, FieldIdx, VariantIdx, F>(
597 &self,
598 fields: &IndexSlice<FieldIdx, F>,
599 repr: &ReprOptions,
600 kind: StructKind,
601 niche_bias: NicheBias,
602 ) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
603 where
604 FieldIdx: Idx,
605 VariantIdx: Idx,
606 F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug + Copy,
607 {
608 let dl = self.cx.data_layout();
609 let pack = repr.pack;
610 let mut align = if pack.is_some() { Align::ONE } else { dl.aggregate_align };
611 let mut max_repr_align = repr.align;
612 let mut in_memory_order: IndexVec<u32, FieldIdx> = fields.indices().collect();
613 let optimize_field_order = !repr.inhibit_struct_field_reordering();
614 let end = if let StructKind::MaybeUnsized = kind { fields.len() - 1 } else { fields.len() };
615 let optimizing = &mut in_memory_order.raw[..end];
616 let fields_excluding_tail = &fields.raw[..end];
617 let field_seed = fields_excluding_tail
619 .iter()
620 .fold(Hash64::ZERO, |acc, f| acc.wrapping_add(f.randomization_seed));
621
622 if optimize_field_order && fields.len() > 1 {
623 if repr.can_randomize_type_layout() && truecfg!(feature = "randomize") {
632 #[cfg(feature = "randomize")]
633 {
634 use rand::SeedableRng;
635 use rand::seq::SliceRandom;
636 let mut rng = rand_xoshiro::Xoshiro128StarStar::seed_from_u64(
639 field_seed.wrapping_add(repr.field_shuffle_seed).as_u64(),
640 );
641
642 optimizing.shuffle(&mut rng);
644 }
645 } else {
647 let max_field_align =
650 fields_excluding_tail.iter().map(|f| f.align.bytes()).max().unwrap_or(1);
651 let largest_niche_size = fields_excluding_tail
652 .iter()
653 .filter_map(|f| f.largest_niche)
654 .map(|n| n.available(dl))
655 .max()
656 .unwrap_or(0);
657
658 let alignment_group_key = |layout: &F| {
661 if let Some(pack) = pack {
665 layout.align.min(pack).bytes()
667 } else {
668 let align = layout.align.bytes();
671 let size = layout.size.bytes();
672 let niche_size = layout.largest_niche.map(|n| n.available(dl)).unwrap_or(0);
673 let size_as_align = align.max(size).trailing_zeros();
675 let size_as_align = if largest_niche_size > 0 {
676 match niche_bias {
677 NicheBias::Start => {
681 max_field_align.trailing_zeros().min(size_as_align)
682 }
683 NicheBias::End if niche_size == largest_niche_size => {
687 align.trailing_zeros()
688 }
689 NicheBias::End => size_as_align,
690 }
691 } else {
692 size_as_align
693 };
694 size_as_align as u64
695 }
696 };
697
698 match kind {
699 StructKind::AlwaysSized | StructKind::MaybeUnsized => {
700 optimizing.sort_by_key(|&x| {
709 let f = &fields[x];
710 let field_size = f.size.bytes();
711 let niche_size = f.largest_niche.map_or(0, |n| n.available(dl));
712 let niche_size_key = match niche_bias {
713 NicheBias::Start => !niche_size,
715 NicheBias::End => niche_size,
717 };
718 let inner_niche_offset_key = match niche_bias {
719 NicheBias::Start => f.largest_niche.map_or(0, |n| n.offset.bytes()),
720 NicheBias::End => f.largest_niche.map_or(0, |n| {
721 !(field_size - n.value.size(dl).bytes() - n.offset.bytes())
722 }),
723 };
724
725 (
726 cmp::Reverse(alignment_group_key(f)),
728 niche_size_key,
731 inner_niche_offset_key,
734 )
735 });
736 }
737
738 StructKind::Prefixed(..) => {
739 optimizing.sort_by_key(|&x| {
744 let f = &fields[x];
745 let niche_size = f.largest_niche.map_or(0, |n| n.available(dl));
746 (alignment_group_key(f), niche_size)
747 });
748 }
749 }
750
751 }
754 }
755 let mut unsized_field = None::<&F>;
760 let mut offsets = IndexVec::from_elem(Size::ZERO, fields);
761 let mut offset = Size::ZERO;
762 let mut largest_niche = None;
763 let mut largest_niche_available = 0;
764 if let StructKind::Prefixed(prefix_size, prefix_align) = kind {
765 let prefix_align =
766 if let Some(pack) = pack { prefix_align.min(pack) } else { prefix_align };
767 align = align.max(prefix_align);
768 offset = prefix_size.align_to(prefix_align);
769 }
770 for &i in &in_memory_order {
771 let field = &fields[i];
772 if let Some(unsized_field) = unsized_field {
773 return Err(LayoutCalculatorError::UnexpectedUnsized(*unsized_field));
774 }
775
776 if field.is_unsized() {
777 if let StructKind::MaybeUnsized = kind {
778 unsized_field = Some(field);
779 } else {
780 return Err(LayoutCalculatorError::UnexpectedUnsized(*field));
781 }
782 }
783
784 let field_align =
786 if let Some(pack) = pack { field.align.min(pack) } else { field.align };
787 offset = offset.align_to(field_align);
788 align = align.max(field_align);
789 max_repr_align = max_repr_align.max(field.max_repr_align);
790
791 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/739adecd444342128b4ef32cbbd8352856cb1651/compiler/rustc_abi/src/layout.rs:791",
"rustc_abi::layout", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/739adecd444342128b4ef32cbbd8352856cb1651/compiler/rustc_abi/src/layout.rs"),
::tracing_core::__macro_support::Option::Some(791u32),
::tracing_core::__macro_support::Option::Some("rustc_abi::layout"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("univariant offset: {0:?} field: {1:#?}",
offset, field) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("univariant offset: {:?} field: {:#?}", offset, field);
792 offsets[i] = offset;
793
794 if let Some(mut niche) = field.largest_niche {
795 let available = niche.available(dl);
796 let prefer_new_niche = match niche_bias {
798 NicheBias::Start => available > largest_niche_available,
799 NicheBias::End => available >= largest_niche_available,
801 };
802 if prefer_new_niche {
803 largest_niche_available = available;
804 niche.offset += offset;
805 largest_niche = Some(niche);
806 }
807 }
808
809 offset =
810 offset.checked_add(field.size, dl).ok_or(LayoutCalculatorError::SizeOverflow)?;
811 }
812
813 let unadjusted_abi_align = align;
816 if let Some(repr_align) = repr.align {
817 align = align.max(repr_align);
818 }
819 let align = align;
821
822 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/739adecd444342128b4ef32cbbd8352856cb1651/compiler/rustc_abi/src/layout.rs:822",
"rustc_abi::layout", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/739adecd444342128b4ef32cbbd8352856cb1651/compiler/rustc_abi/src/layout.rs"),
::tracing_core::__macro_support::Option::Some(822u32),
::tracing_core::__macro_support::Option::Some("rustc_abi::layout"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("univariant min_size: {0:?}",
offset) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("univariant min_size: {:?}", offset);
823 let min_size = offset;
824 let size = min_size.align_to(align);
825 if size.bytes() >= dl.obj_size_bound() {
827 return Err(LayoutCalculatorError::SizeOverflow);
828 }
829 let mut layout_of_single_non_zst_field = None;
830 let sized = unsized_field.is_none();
831 let mut abi = BackendRepr::Memory { sized };
832
833 let optimize_abi = !repr.inhibit_newtype_abi_optimization();
834
835 if sized && size.bytes() > 0 {
837 let mut non_zst_fields = fields.iter_enumerated().filter(|&(_, f)| !f.is_zst());
840
841 match (non_zst_fields.next(), non_zst_fields.next(), non_zst_fields.next()) {
842 (Some((i, field)), None, None) => {
844 layout_of_single_non_zst_field = Some(field);
845
846 if offsets[i].bytes() == 0 && align == field.align && size == field.size {
848 match field.backend_repr {
849 BackendRepr::Scalar(_) | BackendRepr::SimdVector { .. }
852 if optimize_abi =>
853 {
854 abi = field.backend_repr;
855 }
856 BackendRepr::ScalarPair { .. } => {
859 abi = field.backend_repr;
860 }
861 _ => {}
862 }
863 }
864 }
865
866 (Some((i, a)), Some((j, b)), None) => {
868 match (a.backend_repr, b.backend_repr) {
869 (BackendRepr::Scalar(a), BackendRepr::Scalar(b)) => {
870 let ((i, a), (j, b)) = if offsets[i] < offsets[j] {
872 ((i, a), (j, b))
873 } else {
874 ((j, b), (i, a))
875 };
876 let pair =
877 LayoutData::<FieldIdx, VariantIdx>::scalar_pair(&self.cx, a, b);
878 let pair_offsets = match pair.fields {
879 FieldsShape::Arbitrary { ref offsets, ref in_memory_order } => {
880 {
match (&in_memory_order.raw, &[FieldIdx::new(0), FieldIdx::new(1)]) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};assert_eq!(
881 in_memory_order.raw,
882 [FieldIdx::new(0), FieldIdx::new(1)]
883 );
884 offsets
885 }
886 FieldsShape::Primitive
887 | FieldsShape::Array { .. }
888 | FieldsShape::Union(..) => {
889 {
::core::panicking::panic_fmt(format_args!("encountered a non-arbitrary layout during enum layout"));
}panic!("encountered a non-arbitrary layout during enum layout")
890 }
891 };
892 if offsets[i] == pair_offsets[FieldIdx::new(0)]
893 && offsets[j] == pair_offsets[FieldIdx::new(1)]
894 && align == pair.align
895 && size == pair.size
896 {
897 abi = pair.backend_repr;
900 }
901 }
902 _ => {}
903 }
904 }
905
906 _ => {}
907 }
908 }
909 let uninhabited = fields.iter().any(|f| f.is_uninhabited());
910
911 let unadjusted_abi_align = if repr.transparent() {
912 match layout_of_single_non_zst_field {
913 Some(l) => l.unadjusted_abi_align,
914 None => {
915 align
917 }
918 }
919 } else {
920 unadjusted_abi_align
921 };
922
923 let repr_c = repr.c() || (repr.transparent() && fields.iter().any(|field| field.repr_c));
925
926 let seed = field_seed.wrapping_add(repr.field_shuffle_seed);
927
928 Ok(LayoutData {
929 variants: Variants::Single { index: VariantIdx::new(0) },
930 fields: FieldsShape::Arbitrary { offsets, in_memory_order },
931 backend_repr: abi,
932 largest_niche,
933 uninhabited,
934 align,
935 size,
936 max_repr_align,
937 unadjusted_abi_align,
938 repr_c,
939 randomization_seed: seed,
940 })
941 }
942
943 fn format_field_niches<'a, FieldIdx, VariantIdx, F>(
944 &self,
945 layout: &LayoutData<FieldIdx, VariantIdx>,
946 fields: &IndexSlice<FieldIdx, F>,
947 ) -> String
948 where
949 FieldIdx: Idx,
950 VariantIdx: Idx,
951 F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug,
952 {
953 let dl = self.cx.data_layout();
954 let mut s = String::new();
955 for i in layout.fields.index_by_increasing_offset() {
956 let offset = layout.fields.offset(i);
957 let f = &fields[FieldIdx::new(i)];
958 s.write_fmt(format_args!("[o{0}a{1}s{2}", offset.bytes(), f.align.bytes(),
f.size.bytes()))write!(s, "[o{}a{}s{}", offset.bytes(), f.align.bytes(), f.size.bytes()).unwrap();
959 if let Some(n) = f.largest_niche {
960 s.write_fmt(format_args!(" n{0}b{1}s{2}", n.offset.bytes(),
n.available(dl).ilog2(), n.value.size(dl).bytes()))write!(
961 s,
962 " n{}b{}s{}",
963 n.offset.bytes(),
964 n.available(dl).ilog2(),
965 n.value.size(dl).bytes()
966 )
967 .unwrap();
968 }
969 s.write_fmt(format_args!("] "))write!(s, "] ").unwrap();
970 }
971 s
972 }
973}
974
975enum SimdVectorKind {
976 Scalable(NumScalableVectors),
978 PackedFixed,
980 Fixed,
982}
983
984fn vector_type_layout<FieldIdx, VariantIdx, F>(
985 kind: SimdVectorKind,
986 dl: &TargetDataLayout,
987 element: F,
988 count: u64,
989) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F>
990where
991 FieldIdx: Idx,
992 VariantIdx: Idx,
993 F: AsRef<LayoutData<FieldIdx, VariantIdx>> + fmt::Debug,
994{
995 let elt = element.as_ref();
996 let count = BackendLaneCount::new(count)?;
997
998 let BackendRepr::Scalar(element) = elt.backend_repr else {
999 return Err(LayoutCalculatorError::NonPrimitiveSimdType(element));
1000 };
1001
1002 let size =
1004 elt.size.checked_mul(count.as_u64(), dl).ok_or(LayoutCalculatorError::SizeOverflow)?;
1005 let (repr, size, align) = match kind {
1006 SimdVectorKind::Scalable(number_of_vectors) => (
1007 BackendRepr::SimdScalableVector { element, count, number_of_vectors },
1008 size.checked_mul(number_of_vectors.0 as u64, dl)
1009 .ok_or(LayoutCalculatorError::SizeOverflow)?,
1010 dl.rust_vector_align(size),
1011 ),
1012 SimdVectorKind::PackedFixed if !count.is_power_of_two() => {
1016 (BackendRepr::Memory { sized: true }, size, Align::max_aligned_factor(size))
1017 }
1018 SimdVectorKind::PackedFixed | SimdVectorKind::Fixed => {
1019 (BackendRepr::SimdVector { element, count }, size, dl.rust_vector_align(size))
1020 }
1021 };
1022 let size = size.align_to(align);
1023
1024 Ok(LayoutData {
1025 variants: Variants::Single { index: VariantIdx::new(0) },
1026 fields: FieldsShape::Arbitrary {
1027 offsets: [Size::ZERO].into(),
1028 in_memory_order: [FieldIdx::new(0)].into(),
1029 },
1030 backend_repr: repr,
1031 largest_niche: elt.largest_niche,
1032 uninhabited: false,
1033 size,
1034 align,
1035 max_repr_align: None,
1036 unadjusted_abi_align: elt.align,
1037 repr_c: false,
1038 randomization_seed: elt.randomization_seed.wrapping_add(Hash64::new(count.as_u64())),
1039 })
1040}