1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use session::{self, DataTypeKind};
12 use ty::{self, Ty, TyCtxt, TypeFoldable, ReprOptions};
14 use syntax::ast::{self, FloatTy, IntTy, UintTy};
16 use syntax_pos::DUMMY_SP;
23 use ich::StableHashingContext;
24 use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
27 pub use rustc_target::abi::*;
29 pub trait IntegerExt {
30 fn to_ty<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, signed: bool) -> Ty<'tcx>;
31 fn from_attr<C: HasDataLayout>(cx: C, ity: attr::IntType) -> Integer;
32 fn repr_discr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
40 impl IntegerExt for Integer {
41 fn to_ty<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, signed: bool) -> Ty<'tcx> {
42 match (*self, signed) {
43 (I8, false) => tcx.types.u8,
44 (I16, false) => tcx.types.u16,
45 (I32, false) => tcx.types.u32,
46 (I64, false) => tcx.types.u64,
47 (I128, false) => tcx.types.u128,
48 (I8, true) => tcx.types.i8,
49 (I16, true) => tcx.types.i16,
50 (I32, true) => tcx.types.i32,
51 (I64, true) => tcx.types.i64,
52 (I128, true) => tcx.types.i128,
56 /// Get the Integer type from an attr::IntType.
57 fn from_attr<C: HasDataLayout>(cx: C, ity: attr::IntType) -> Integer {
58 let dl = cx.data_layout();
61 attr::SignedInt(IntTy::I8) | attr::UnsignedInt(UintTy::U8) => I8,
62 attr::SignedInt(IntTy::I16) | attr::UnsignedInt(UintTy::U16) => I16,
63 attr::SignedInt(IntTy::I32) | attr::UnsignedInt(UintTy::U32) => I32,
64 attr::SignedInt(IntTy::I64) | attr::UnsignedInt(UintTy::U64) => I64,
65 attr::SignedInt(IntTy::I128) | attr::UnsignedInt(UintTy::U128) => I128,
66 attr::SignedInt(IntTy::Isize) | attr::UnsignedInt(UintTy::Usize) => {
67 dl.ptr_sized_integer()
72 /// Find the appropriate Integer type and signedness for the given
73 /// signed discriminant range and #[repr] attribute.
74 /// N.B.: u128 values above i128::MAX will be treated as signed, but
75 /// that shouldn't affect anything, other than maybe debuginfo.
76 fn repr_discr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
82 // Theoretically, negative values could be larger in unsigned representation
83 // than the unsigned representation of the signed minimum. However, if there
84 // are any negative values, the only valid unsigned representation is u128
85 // which can fit all i128 values, so the result remains unaffected.
86 let unsigned_fit = Integer::fit_unsigned(cmp::max(min as u128, max as u128));
87 let signed_fit = cmp::max(Integer::fit_signed(min), Integer::fit_signed(max));
89 let mut min_from_extern = None;
92 if let Some(ity) = repr.int {
93 let discr = Integer::from_attr(tcx, ity);
94 let fit = if ity.is_signed() { signed_fit } else { unsigned_fit };
96 bug!("Integer::repr_discr: `#[repr]` hint too small for \
97 discriminant range of enum `{}", ty)
99 return (discr, ity.is_signed());
103 match &tcx.sess.target.target.arch[..] {
104 // WARNING: the ARM EABI has two variants; the one corresponding
105 // to `at_least == I32` appears to be used on Linux and NetBSD,
106 // but some systems may use the variant corresponding to no
107 // lower bound. However, we don't run on those yet...?
108 "arm" => min_from_extern = Some(I32),
109 _ => min_from_extern = Some(I32),
113 let at_least = min_from_extern.unwrap_or(min_default);
115 // If there are no negative values, we can use the unsigned fit.
117 (cmp::max(unsigned_fit, at_least), false)
119 (cmp::max(signed_fit, at_least), true)
124 pub trait PrimitiveExt {
125 fn to_ty<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx>;
128 impl PrimitiveExt for Primitive {
129 fn to_ty<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx> {
131 Int(i, signed) => i.to_ty(tcx, signed),
132 F32 => tcx.types.f32,
133 F64 => tcx.types.f64,
134 Pointer => tcx.mk_mut_ptr(tcx.mk_nil()),
139 /// The first half of a fat pointer.
141 /// - For a trait object, this is the address of the box.
142 /// - For a slice, this is the base address.
143 pub const FAT_PTR_ADDR: usize = 0;
145 /// The second half of a fat pointer.
147 /// - For a trait object, this is the address of the vtable.
148 /// - For a slice, this is the length.
149 pub const FAT_PTR_EXTRA: usize = 1;
151 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
152 pub enum LayoutError<'tcx> {
154 SizeOverflow(Ty<'tcx>)
157 impl<'tcx> fmt::Display for LayoutError<'tcx> {
158 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
160 LayoutError::Unknown(ty) => {
161 write!(f, "the type `{:?}` has an unknown layout", ty)
163 LayoutError::SizeOverflow(ty) => {
164 write!(f, "the type `{:?}` is too big for the current architecture", ty)
170 fn layout_raw<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
171 query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
172 -> Result<&'tcx LayoutDetails, LayoutError<'tcx>>
174 ty::tls::with_related_context(tcx, move |icx| {
175 let rec_limit = *tcx.sess.recursion_limit.get();
176 let (param_env, ty) = query.into_parts();
178 if icx.layout_depth > rec_limit {
180 &format!("overflow representing the type `{}`", ty));
183 // Update the ImplicitCtxt to increase the layout_depth
184 let icx = ty::tls::ImplicitCtxt {
185 layout_depth: icx.layout_depth + 1,
189 ty::tls::enter_context(&icx, |_| {
190 let cx = LayoutCx { tcx, param_env };
191 cx.layout_raw_uncached(ty)
196 pub fn provide(providers: &mut ty::maps::Providers) {
197 *providers = ty::maps::Providers {
203 #[derive(Copy, Clone)]
204 pub struct LayoutCx<'tcx, C> {
206 pub param_env: ty::ParamEnv<'tcx>
209 impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
210 fn layout_raw_uncached(self, ty: Ty<'tcx>)
211 -> Result<&'tcx LayoutDetails, LayoutError<'tcx>> {
213 let param_env = self.param_env;
214 let dl = self.data_layout();
215 let scalar_unit = |value: Primitive| {
216 let bits = value.size(dl).bits();
217 assert!(bits <= 128);
220 valid_range: 0..=(!0 >> (128 - bits))
223 let scalar = |value: Primitive| {
224 tcx.intern_layout(LayoutDetails::scalar(self, scalar_unit(value)))
226 let scalar_pair = |a: Scalar, b: Scalar| {
227 let align = a.value.align(dl).max(b.value.align(dl)).max(dl.aggregate_align);
228 let b_offset = a.value.size(dl).abi_align(b.value.align(dl));
229 let size = (b_offset + b.value.size(dl)).abi_align(align);
231 variants: Variants::Single { index: 0 },
232 fields: FieldPlacement::Arbitrary {
233 offsets: vec![Size::from_bytes(0), b_offset],
234 memory_index: vec![0, 1]
236 abi: Abi::ScalarPair(a, b),
242 #[derive(Copy, Clone, Debug)]
244 /// A tuple, closure, or univariant which cannot be coerced to unsized.
246 /// A univariant, the last field of which may be coerced to unsized.
248 /// A univariant, but with a prefix of an arbitrary size & alignment (e.g. enum tag).
249 Prefixed(Size, Align),
251 let univariant_uninterned = |fields: &[TyLayout], repr: &ReprOptions, kind| {
252 let packed = repr.packed();
253 if packed && repr.align > 0 {
254 bug!("struct cannot be packed and aligned");
258 let pack = repr.pack as u64;
259 Align::from_bytes(pack, pack).unwrap()
262 let mut align = if packed {
268 let mut sized = true;
269 let mut offsets = vec![Size::from_bytes(0); fields.len()];
270 let mut inverse_memory_index: Vec<u32> = (0..fields.len() as u32).collect();
272 let mut optimize = !repr.inhibit_struct_field_reordering_opt();
273 if let StructKind::Prefixed(_, align) = kind {
274 optimize &= align.abi() == 1;
278 let end = if let StructKind::MaybeUnsized = kind {
283 let optimizing = &mut inverse_memory_index[..end];
284 let field_align = |f: &TyLayout| {
285 if packed { f.align.min(pack).abi() } else { f.align.abi() }
288 StructKind::AlwaysSized |
289 StructKind::MaybeUnsized => {
290 optimizing.sort_by_key(|&x| {
291 // Place ZSTs first to avoid "interesting offsets",
292 // especially with only one or two non-ZST fields.
293 let f = &fields[x as usize];
294 (!f.is_zst(), cmp::Reverse(field_align(f)))
297 StructKind::Prefixed(..) => {
298 optimizing.sort_by_key(|&x| field_align(&fields[x as usize]));
303 // inverse_memory_index holds field indices by increasing memory offset.
304 // That is, if field 5 has offset 0, the first element of inverse_memory_index is 5.
305 // We now write field offsets to the corresponding offset slot;
306 // field 5 with offset 0 puts 0 in offsets[5].
307 // At the bottom of this function, we use inverse_memory_index to produce memory_index.
309 let mut offset = Size::from_bytes(0);
311 if let StructKind::Prefixed(prefix_size, prefix_align) = kind {
313 let prefix_align = prefix_align.min(pack);
314 align = align.max(prefix_align);
316 align = align.max(prefix_align);
318 offset = prefix_size.abi_align(prefix_align);
321 for &i in &inverse_memory_index {
322 let field = fields[i as usize];
324 bug!("univariant: field #{} of `{}` comes after unsized field",
328 if field.abi == Abi::Uninhabited {
329 return Ok(LayoutDetails::uninhabited(fields.len()));
332 if field.is_unsized() {
336 // Invariant: offset < dl.obj_size_bound() <= 1<<61
338 let field_pack = field.align.min(pack);
339 offset = offset.abi_align(field_pack);
340 align = align.max(field_pack);
343 offset = offset.abi_align(field.align);
344 align = align.max(field.align);
347 debug!("univariant offset: {:?} field: {:#?}", offset, field);
348 offsets[i as usize] = offset;
350 offset = offset.checked_add(field.size, dl)
351 .ok_or(LayoutError::SizeOverflow(ty))?;
355 let repr_align = repr.align as u64;
356 align = align.max(Align::from_bytes(repr_align, repr_align).unwrap());
357 debug!("univariant repr_align: {:?}", repr_align);
360 debug!("univariant min_size: {:?}", offset);
361 let min_size = offset;
363 // As stated above, inverse_memory_index holds field indices by increasing offset.
364 // This makes it an already-sorted view of the offsets vec.
365 // To invert it, consider:
366 // If field 5 has offset 0, offsets[0] is 5, and memory_index[5] should be 0.
367 // Field 5 would be the first element, so memory_index is i:
368 // Note: if we didn't optimize, it's already right.
370 let mut memory_index;
372 memory_index = vec![0; inverse_memory_index.len()];
374 for i in 0..inverse_memory_index.len() {
375 memory_index[inverse_memory_index[i] as usize] = i as u32;
378 memory_index = inverse_memory_index;
381 let size = min_size.abi_align(align);
382 let mut abi = Abi::Aggregate { sized };
384 // Unpack newtype ABIs and find scalar pairs.
385 if sized && size.bytes() > 0 {
386 // All other fields must be ZSTs, and we need them to all start at 0.
387 let mut zst_offsets =
388 offsets.iter().enumerate().filter(|&(i, _)| fields[i].is_zst());
389 if zst_offsets.all(|(_, o)| o.bytes() == 0) {
390 let mut non_zst_fields =
391 fields.iter().enumerate().filter(|&(_, f)| !f.is_zst());
393 match (non_zst_fields.next(), non_zst_fields.next(), non_zst_fields.next()) {
394 // We have exactly one non-ZST field.
395 (Some((i, field)), None, None) => {
396 // Field fills the struct and it has a scalar or scalar pair ABI.
397 if offsets[i].bytes() == 0 &&
398 align.abi() == field.align.abi() &&
401 // For plain scalars, or vectors of them, we can't unpack
402 // newtypes for `#[repr(C)]`, as that affects C ABIs.
403 Abi::Scalar(_) | Abi::Vector { .. } if optimize => {
404 abi = field.abi.clone();
406 // But scalar pairs are Rust-specific and get
407 // treated as aggregates by C ABIs anyway.
408 Abi::ScalarPair(..) => {
409 abi = field.abi.clone();
416 // Two non-ZST fields, and they're both scalars.
417 (Some((i, &TyLayout {
418 details: &LayoutDetails { abi: Abi::Scalar(ref a), .. }, ..
419 })), Some((j, &TyLayout {
420 details: &LayoutDetails { abi: Abi::Scalar(ref b), .. }, ..
422 // Order by the memory placement, not source order.
423 let ((i, a), (j, b)) = if offsets[i] < offsets[j] {
428 let pair = scalar_pair(a.clone(), b.clone());
429 let pair_offsets = match pair.fields {
430 FieldPlacement::Arbitrary {
434 assert_eq!(memory_index, &[0, 1]);
439 if offsets[i] == pair_offsets[0] &&
440 offsets[j] == pair_offsets[1] &&
441 align == pair.align &&
443 // We can use `ScalarPair` only when it matches our
444 // already computed layout (including `#[repr(C)]`).
455 variants: Variants::Single { index: 0 },
456 fields: FieldPlacement::Arbitrary {
465 let univariant = |fields: &[TyLayout], repr: &ReprOptions, kind| {
466 Ok(tcx.intern_layout(univariant_uninterned(fields, repr, kind)?))
468 assert!(!ty.has_infer_types());
473 tcx.intern_layout(LayoutDetails::scalar(self, Scalar {
474 value: Int(I8, false),
479 tcx.intern_layout(LayoutDetails::scalar(self, Scalar {
480 value: Int(I32, false),
481 valid_range: 0..=0x10FFFF
485 scalar(Int(Integer::from_attr(dl, attr::SignedInt(ity)), true))
488 scalar(Int(Integer::from_attr(dl, attr::UnsignedInt(ity)), false))
490 ty::TyFloat(FloatTy::F32) => scalar(F32),
491 ty::TyFloat(FloatTy::F64) => scalar(F64),
493 let mut ptr = scalar_unit(Pointer);
494 ptr.valid_range = 1..=*ptr.valid_range.end();
495 tcx.intern_layout(LayoutDetails::scalar(self, ptr))
500 tcx.intern_layout(LayoutDetails::uninhabited(0))
503 // Potentially-fat pointers.
504 ty::TyRef(_, ty::TypeAndMut { ty: pointee, .. }) |
505 ty::TyRawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
506 let mut data_ptr = scalar_unit(Pointer);
507 if !ty.is_unsafe_ptr() {
508 data_ptr.valid_range = 1..=*data_ptr.valid_range.end();
511 let pointee = tcx.normalize_erasing_regions(param_env, pointee);
512 if pointee.is_sized(tcx.at(DUMMY_SP), param_env) {
513 return Ok(tcx.intern_layout(LayoutDetails::scalar(self, data_ptr)));
516 let unsized_part = tcx.struct_tail(pointee);
517 let metadata = match unsized_part.sty {
518 ty::TyForeign(..) => {
519 return Ok(tcx.intern_layout(LayoutDetails::scalar(self, data_ptr)));
521 ty::TySlice(_) | ty::TyStr => {
522 scalar_unit(Int(dl.ptr_sized_integer(), false))
524 ty::TyDynamic(..) => {
525 let mut vtable = scalar_unit(Pointer);
526 vtable.valid_range = 1..=*vtable.valid_range.end();
529 _ => return Err(LayoutError::Unknown(unsized_part))
532 // Effectively a (ptr, meta) tuple.
533 tcx.intern_layout(scalar_pair(data_ptr, metadata))
536 // Arrays and slices.
537 ty::TyArray(element, mut count) => {
538 if count.has_projections() {
539 count = tcx.normalize_erasing_regions(param_env, count);
540 if count.has_projections() {
541 return Err(LayoutError::Unknown(ty));
545 let element = self.layout_of(element)?;
546 let count = count.val.unwrap_u64();
547 let size = element.size.checked_mul(count, dl)
548 .ok_or(LayoutError::SizeOverflow(ty))?;
550 tcx.intern_layout(LayoutDetails {
551 variants: Variants::Single { index: 0 },
552 fields: FieldPlacement::Array {
553 stride: element.size,
556 abi: Abi::Aggregate { sized: true },
557 align: element.align,
561 ty::TySlice(element) => {
562 let element = self.layout_of(element)?;
563 tcx.intern_layout(LayoutDetails {
564 variants: Variants::Single { index: 0 },
565 fields: FieldPlacement::Array {
566 stride: element.size,
569 abi: Abi::Aggregate { sized: false },
570 align: element.align,
571 size: Size::from_bytes(0)
575 tcx.intern_layout(LayoutDetails {
576 variants: Variants::Single { index: 0 },
577 fields: FieldPlacement::Array {
578 stride: Size::from_bytes(1),
581 abi: Abi::Aggregate { sized: false },
583 size: Size::from_bytes(0)
589 univariant(&[], &ReprOptions::default(), StructKind::AlwaysSized)?
591 ty::TyDynamic(..) | ty::TyForeign(..) => {
592 let mut unit = univariant_uninterned(&[], &ReprOptions::default(),
593 StructKind::AlwaysSized)?;
595 Abi::Aggregate { ref mut sized } => *sized = false,
598 tcx.intern_layout(unit)
601 // Tuples, generators and closures.
602 ty::TyGenerator(def_id, ref substs, _) => {
603 let tys = substs.field_tys(def_id, tcx);
604 univariant(&tys.map(|ty| self.layout_of(ty)).collect::<Result<Vec<_>, _>>()?,
605 &ReprOptions::default(),
606 StructKind::AlwaysSized)?
609 ty::TyClosure(def_id, ref substs) => {
610 let tys = substs.upvar_tys(def_id, tcx);
611 univariant(&tys.map(|ty| self.layout_of(ty)).collect::<Result<Vec<_>, _>>()?,
612 &ReprOptions::default(),
613 StructKind::AlwaysSized)?
616 ty::TyTuple(tys) => {
617 let kind = if tys.len() == 0 {
618 StructKind::AlwaysSized
620 StructKind::MaybeUnsized
623 univariant(&tys.iter().map(|ty| self.layout_of(ty)).collect::<Result<Vec<_>, _>>()?,
624 &ReprOptions::default(), kind)?
627 // SIMD vector types.
628 ty::TyAdt(def, ..) if def.repr.simd() => {
629 let element = self.layout_of(ty.simd_type(tcx))?;
630 let count = ty.simd_size(tcx) as u64;
632 let scalar = match element.abi {
633 Abi::Scalar(ref scalar) => scalar.clone(),
635 tcx.sess.fatal(&format!("monomorphising SIMD type `{}` with \
636 a non-machine element type `{}`",
640 let size = element.size.checked_mul(count, dl)
641 .ok_or(LayoutError::SizeOverflow(ty))?;
642 let align = dl.vector_align(size);
643 let size = size.abi_align(align);
645 tcx.intern_layout(LayoutDetails {
646 variants: Variants::Single { index: 0 },
647 fields: FieldPlacement::Array {
648 stride: element.size,
661 ty::TyAdt(def, substs) => {
662 // Cache the field layouts.
663 let variants = def.variants.iter().map(|v| {
664 v.fields.iter().map(|field| {
665 self.layout_of(field.ty(tcx, substs))
666 }).collect::<Result<Vec<_>, _>>()
667 }).collect::<Result<Vec<_>, _>>()?;
670 let packed = def.repr.packed();
671 if packed && def.repr.align > 0 {
672 bug!("Union cannot be packed and aligned");
676 let pack = def.repr.pack as u64;
677 Align::from_bytes(pack, pack).unwrap()
680 let mut align = if packed {
686 if def.repr.align > 0 {
687 let repr_align = def.repr.align as u64;
689 Align::from_bytes(repr_align, repr_align).unwrap());
692 let mut size = Size::from_bytes(0);
693 for field in &variants[0] {
694 assert!(!field.is_unsized());
697 let field_pack = field.align.min(pack);
698 align = align.max(field_pack);
700 align = align.max(field.align);
702 size = cmp::max(size, field.size);
705 return Ok(tcx.intern_layout(LayoutDetails {
706 variants: Variants::Single { index: 0 },
707 fields: FieldPlacement::Union(variants[0].len()),
708 abi: Abi::Aggregate { sized: true },
710 size: size.abi_align(align)
714 let (inh_first, inh_second) = {
715 let mut inh_variants = (0..variants.len()).filter(|&v| {
716 variants[v].iter().all(|f| f.abi != Abi::Uninhabited)
718 (inh_variants.next(), inh_variants.next())
720 if inh_first.is_none() {
721 // Uninhabited because it has no variants, or only uninhabited ones.
722 return Ok(tcx.intern_layout(LayoutDetails::uninhabited(0)));
725 let is_struct = !def.is_enum() ||
726 // Only one variant is inhabited.
727 (inh_second.is_none() &&
728 // Representation optimizations are allowed.
729 !def.repr.inhibit_enum_layout_opt());
731 // Struct, or univariant enum equivalent to a struct.
732 // (Typechecking will reject discriminant-sizing attrs.)
734 let v = inh_first.unwrap();
735 let kind = if def.is_enum() || variants[v].len() == 0 {
736 StructKind::AlwaysSized
738 let param_env = tcx.param_env(def.did);
739 let last_field = def.variants[v].fields.last().unwrap();
740 let always_sized = tcx.type_of(last_field.did)
741 .is_sized(tcx.at(DUMMY_SP), param_env);
742 if !always_sized { StructKind::MaybeUnsized }
743 else { StructKind::AlwaysSized }
746 let mut st = univariant_uninterned(&variants[v], &def.repr, kind)?;
747 st.variants = Variants::Single { index: v };
748 // Exclude 0 from the range of a newtype ABI NonZero<T>.
749 if Some(def.did) == self.tcx.lang_items().non_zero() {
751 Abi::Scalar(ref mut scalar) |
752 Abi::ScalarPair(ref mut scalar, _) => {
753 if *scalar.valid_range.start() == 0 {
754 scalar.valid_range = 1..=*scalar.valid_range.end();
760 return Ok(tcx.intern_layout(st));
763 // The current code for niche-filling relies on variant indices
764 // instead of actual discriminants, so dataful enums with
765 // explicit discriminants (RFC #2363) would misbehave.
766 let no_explicit_discriminants = def.variants.iter().enumerate()
767 .all(|(i, v)| v.discr == ty::VariantDiscr::Relative(i));
769 // Niche-filling enum optimization.
770 if !def.repr.inhibit_enum_layout_opt() && no_explicit_discriminants {
771 let mut dataful_variant = None;
772 let mut niche_variants = usize::max_value()..=0;
774 // Find one non-ZST variant.
775 'variants: for (v, fields) in variants.iter().enumerate() {
776 if fields.iter().any(|f| f.abi == Abi::Uninhabited) {
781 if dataful_variant.is_none() {
782 dataful_variant = Some(v);
785 dataful_variant = None;
790 niche_variants = *niche_variants.start().min(&v)..=v;
793 if niche_variants.start() > niche_variants.end() {
794 dataful_variant = None;
797 if let Some(i) = dataful_variant {
798 let count = (niche_variants.end() - niche_variants.start() + 1) as u128;
799 for (field_index, &field) in variants[i].iter().enumerate() {
800 let (offset, niche, niche_start) =
801 match self.find_niche(field, count)? {
802 Some(niche) => niche,
805 let mut align = dl.aggregate_align;
806 let st = variants.iter().enumerate().map(|(j, v)| {
807 let mut st = univariant_uninterned(v,
808 &def.repr, StructKind::AlwaysSized)?;
809 st.variants = Variants::Single { index: j };
811 align = align.max(st.align);
814 }).collect::<Result<Vec<_>, _>>()?;
816 let offset = st[i].fields.offset(field_index) + offset;
817 let size = st[i].size;
819 let abi = match st[i].abi {
820 Abi::Scalar(_) => Abi::Scalar(niche.clone()),
821 Abi::ScalarPair(ref first, ref second) => {
822 // We need to use scalar_unit to reset the
823 // valid range to the maximal one for that
824 // primitive, because only the niche is
825 // guaranteed to be initialised, not the
827 if offset.bytes() == 0 {
828 Abi::ScalarPair(niche.clone(), scalar_unit(second.value))
830 Abi::ScalarPair(scalar_unit(first.value), niche.clone())
833 _ => Abi::Aggregate { sized: true },
836 return Ok(tcx.intern_layout(LayoutDetails {
837 variants: Variants::NicheFilling {
844 fields: FieldPlacement::Arbitrary {
845 offsets: vec![offset],
846 memory_index: vec![0]
856 let (mut min, mut max) = (i128::max_value(), i128::min_value());
857 let discr_type = def.repr.discr_type();
858 let bits = Integer::from_attr(tcx, discr_type).size().bits();
859 for (i, discr) in def.discriminants(tcx).enumerate() {
860 if variants[i].iter().any(|f| f.abi == Abi::Uninhabited) {
863 let mut x = discr.val as i128;
864 if discr_type.is_signed() {
865 // sign extend the raw representation to be an i128
866 x = (x << (128 - bits)) >> (128 - bits);
868 if x < min { min = x; }
869 if x > max { max = x; }
871 assert!(min <= max, "discriminant range is {}...{}", min, max);
872 let (min_ity, signed) = Integer::repr_discr(tcx, ty, &def.repr, min, max);
874 let mut align = dl.aggregate_align;
875 let mut size = Size::from_bytes(0);
877 // We're interested in the smallest alignment, so start large.
878 let mut start_align = Align::from_bytes(256, 256).unwrap();
879 assert_eq!(Integer::for_abi_align(dl, start_align), None);
881 // repr(C) on an enum tells us to make a (tag, union) layout,
882 // so we need to grow the prefix alignment to be at least
883 // the alignment of the union. (This value is used both for
884 // determining the alignment of the overall enum, and the
885 // determining the alignment of the payload after the tag.)
886 let mut prefix_align = min_ity.align(dl);
888 for fields in &variants {
889 for field in fields {
890 prefix_align = prefix_align.max(field.align);
895 // Create the set of structs that represent each variant.
896 let mut layout_variants = variants.iter().enumerate().map(|(i, field_layouts)| {
897 let mut st = univariant_uninterned(&field_layouts,
898 &def.repr, StructKind::Prefixed(min_ity.size(), prefix_align))?;
899 st.variants = Variants::Single { index: i };
900 // Find the first field we can't move later
901 // to make room for a larger discriminant.
902 for field in st.fields.index_by_increasing_offset().map(|j| field_layouts[j]) {
903 if !field.is_zst() || field.align.abi() != 1 {
904 start_align = start_align.min(field.align);
908 size = cmp::max(size, st.size);
909 align = align.max(st.align);
911 }).collect::<Result<Vec<_>, _>>()?;
913 // Align the maximum variant size to the largest alignment.
914 size = size.abi_align(align);
916 if size.bytes() >= dl.obj_size_bound() {
917 return Err(LayoutError::SizeOverflow(ty));
920 let typeck_ity = Integer::from_attr(dl, def.repr.discr_type());
921 if typeck_ity < min_ity {
922 // It is a bug if Layout decided on a greater discriminant size than typeck for
923 // some reason at this point (based on values discriminant can take on). Mostly
924 // because this discriminant will be loaded, and then stored into variable of
925 // type calculated by typeck. Consider such case (a bug): typeck decided on
926 // byte-sized discriminant, but layout thinks we need a 16-bit to store all
927 // discriminant values. That would be a bug, because then, in trans, in order
928 // to store this 16-bit discriminant into 8-bit sized temporary some of the
929 // space necessary to represent would have to be discarded (or layout is wrong
930 // on thinking it needs 16 bits)
931 bug!("layout decided on a larger discriminant type ({:?}) than typeck ({:?})",
932 min_ity, typeck_ity);
933 // However, it is fine to make discr type however large (as an optimisation)
934 // after this point – we’ll just truncate the value we load in trans.
937 // Check to see if we should use a different type for the
938 // discriminant. We can safely use a type with the same size
939 // as the alignment of the first field of each variant.
940 // We increase the size of the discriminant to avoid LLVM copying
941 // padding when it doesn't need to. This normally causes unaligned
942 // load/stores and excessive memcpy/memset operations. By using a
943 // bigger integer size, LLVM can be sure about its contents and
944 // won't be so conservative.
946 // Use the initial field alignment
947 let mut ity = if def.repr.c() || def.repr.int.is_some() {
950 Integer::for_abi_align(dl, start_align).unwrap_or(min_ity)
953 // If the alignment is not larger than the chosen discriminant size,
954 // don't use the alignment as the final size.
958 // Patch up the variants' first few fields.
959 let old_ity_size = min_ity.size();
960 let new_ity_size = ity.size();
961 for variant in &mut layout_variants {
962 if variant.abi == Abi::Uninhabited {
965 match variant.fields {
966 FieldPlacement::Arbitrary { ref mut offsets, .. } => {
968 if *i <= old_ity_size {
969 assert_eq!(*i, old_ity_size);
973 // We might be making the struct larger.
974 if variant.size <= old_ity_size {
975 variant.size = new_ity_size;
983 let tag_mask = !0u128 >> (128 - ity.size().bits());
985 value: Int(ity, signed),
986 valid_range: (min as u128 & tag_mask)..=(max as u128 & tag_mask),
988 let mut abi = Abi::Aggregate { sized: true };
989 if tag.value.size(dl) == size {
990 abi = Abi::Scalar(tag.clone());
991 } else if !tag.is_bool() {
992 // HACK(nox): Blindly using ScalarPair for all tagged enums
993 // where applicable leads to Option<u8> being handled as {i1, i8},
994 // which later confuses SROA and some loop optimisations,
995 // ultimately leading to the repeat-trusted-len test
996 // failing. We make the trade-off of using ScalarPair only
997 // for types where the tag isn't a boolean.
998 let mut common_prim = None;
999 for (field_layouts, layout_variant) in variants.iter().zip(&layout_variants) {
1000 let offsets = match layout_variant.fields {
1001 FieldPlacement::Arbitrary { ref offsets, .. } => offsets,
1004 let mut fields = field_layouts
1007 .filter(|p| !p.0.is_zst());
1008 let (field, offset) = match (fields.next(), fields.next()) {
1009 (None, None) => continue,
1010 (Some(pair), None) => pair,
1016 let prim = match field.details.abi {
1017 Abi::Scalar(ref scalar) => scalar.value,
1023 if let Some(pair) = common_prim {
1024 // This is pretty conservative. We could go fancier
1025 // by conflating things like i32 and u32, or even
1026 // realising that (u8, u8) could just cohabit with
1028 if pair != (prim, offset) {
1033 common_prim = Some((prim, offset));
1036 if let Some((prim, offset)) = common_prim {
1037 let pair = scalar_pair(tag.clone(), scalar_unit(prim));
1038 let pair_offsets = match pair.fields {
1039 FieldPlacement::Arbitrary {
1043 assert_eq!(memory_index, &[0, 1]);
1048 if pair_offsets[0] == Size::from_bytes(0) &&
1049 pair_offsets[1] == *offset &&
1050 align == pair.align &&
1052 // We can use `ScalarPair` only when it matches our
1053 // already computed layout (including `#[repr(C)]`).
1058 tcx.intern_layout(LayoutDetails {
1059 variants: Variants::Tagged {
1061 variants: layout_variants,
1063 fields: FieldPlacement::Arbitrary {
1064 offsets: vec![Size::from_bytes(0)],
1065 memory_index: vec![0]
1073 // Types with no meaningful known layout.
1074 ty::TyProjection(_) | ty::TyAnon(..) => {
1075 let normalized = tcx.normalize_erasing_regions(param_env, ty);
1076 if ty == normalized {
1077 return Err(LayoutError::Unknown(ty));
1079 tcx.layout_raw(param_env.and(normalized))?
1082 return Err(LayoutError::Unknown(ty));
1084 ty::TyGeneratorWitness(..) | ty::TyInfer(_) | ty::TyError => {
1085 bug!("LayoutDetails::compute: unexpected type `{}`", ty)
1090 /// This is invoked by the `layout_raw` query to record the final
1091 /// layout of each type.
1093 fn record_layout_for_printing(self, layout: TyLayout<'tcx>) {
1094 // If we are running with `-Zprint-type-sizes`, record layouts for
1095 // dumping later. Ignore layouts that are done with non-empty
1096 // environments or non-monomorphic layouts, as the user only wants
1097 // to see the stuff resulting from the final trans session.
1099 !self.tcx.sess.opts.debugging_opts.print_type_sizes ||
1100 layout.ty.has_param_types() ||
1101 layout.ty.has_self_ty() ||
1102 !self.param_env.caller_bounds.is_empty()
1107 self.record_layout_for_printing_outlined(layout)
1110 fn record_layout_for_printing_outlined(self, layout: TyLayout<'tcx>) {
1111 // (delay format until we actually need it)
1112 let record = |kind, packed, opt_discr_size, variants| {
1113 let type_desc = format!("{:?}", layout.ty);
1114 self.tcx.sess.code_stats.borrow_mut().record_type_size(kind,
1123 let adt_def = match layout.ty.sty {
1124 ty::TyAdt(ref adt_def, _) => {
1125 debug!("print-type-size t: `{:?}` process adt", layout.ty);
1129 ty::TyClosure(..) => {
1130 debug!("print-type-size t: `{:?}` record closure", layout.ty);
1131 record(DataTypeKind::Closure, false, None, vec![]);
1136 debug!("print-type-size t: `{:?}` skip non-nominal", layout.ty);
1141 let adt_kind = adt_def.adt_kind();
1142 let adt_packed = adt_def.repr.packed();
1144 let build_variant_info = |n: Option<ast::Name>,
1146 layout: TyLayout<'tcx>| {
1147 let mut min_size = Size::from_bytes(0);
1148 let field_info: Vec<_> = flds.iter().enumerate().map(|(i, &name)| {
1149 match layout.field(self, i) {
1151 bug!("no layout found for field {}: `{:?}`", name, err);
1153 Ok(field_layout) => {
1154 let offset = layout.fields.offset(i);
1155 let field_end = offset + field_layout.size;
1156 if min_size < field_end {
1157 min_size = field_end;
1159 session::FieldInfo {
1160 name: name.to_string(),
1161 offset: offset.bytes(),
1162 size: field_layout.size.bytes(),
1163 align: field_layout.align.abi(),
1169 session::VariantInfo {
1170 name: n.map(|n|n.to_string()),
1171 kind: if layout.is_unsized() {
1172 session::SizeKind::Min
1174 session::SizeKind::Exact
1176 align: layout.align.abi(),
1177 size: if min_size.bytes() == 0 {
1186 match layout.variants {
1187 Variants::Single { index } => {
1188 debug!("print-type-size `{:#?}` variant {}",
1189 layout, adt_def.variants[index].name);
1190 if !adt_def.variants.is_empty() {
1191 let variant_def = &adt_def.variants[index];
1192 let fields: Vec<_> =
1193 variant_def.fields.iter().map(|f| f.name).collect();
1194 record(adt_kind.into(),
1197 vec![build_variant_info(Some(variant_def.name),
1201 // (This case arises for *empty* enums; so give it
1203 record(adt_kind.into(), adt_packed, None, vec![]);
1207 Variants::NicheFilling { .. } |
1208 Variants::Tagged { .. } => {
1209 debug!("print-type-size `{:#?}` adt general variants def {}",
1210 layout.ty, adt_def.variants.len());
1211 let variant_infos: Vec<_> =
1212 adt_def.variants.iter().enumerate().map(|(i, variant_def)| {
1213 let fields: Vec<_> =
1214 variant_def.fields.iter().map(|f| f.name).collect();
1215 build_variant_info(Some(variant_def.name),
1217 layout.for_variant(self, i))
1220 record(adt_kind.into(), adt_packed, match layout.variants {
1221 Variants::Tagged { ref tag, .. } => Some(tag.value.size(self)),
1229 /// Type size "skeleton", i.e. the only information determining a type's size.
1230 /// While this is conservative, (aside from constant sizes, only pointers,
1231 /// newtypes thereof and null pointer optimized enums are allowed), it is
1232 /// enough to statically check common usecases of transmute.
1233 #[derive(Copy, Clone, Debug)]
1234 pub enum SizeSkeleton<'tcx> {
1235 /// Any statically computable Layout.
1238 /// A potentially-fat pointer.
1240 /// If true, this pointer is never null.
1242 /// The type which determines the unsized metadata, if any,
1243 /// of this pointer. Either a type parameter or a projection
1244 /// depending on one, with regions erased.
1249 impl<'a, 'tcx> SizeSkeleton<'tcx> {
1250 pub fn compute(ty: Ty<'tcx>,
1251 tcx: TyCtxt<'a, 'tcx, 'tcx>,
1252 param_env: ty::ParamEnv<'tcx>)
1253 -> Result<SizeSkeleton<'tcx>, LayoutError<'tcx>> {
1254 assert!(!ty.has_infer_types());
1256 // First try computing a static layout.
1257 let err = match tcx.layout_of(param_env.and(ty)) {
1259 return Ok(SizeSkeleton::Known(layout.size));
1265 ty::TyRef(_, ty::TypeAndMut { ty: pointee, .. }) |
1266 ty::TyRawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
1267 let non_zero = !ty.is_unsafe_ptr();
1268 let tail = tcx.struct_tail(pointee);
1270 ty::TyParam(_) | ty::TyProjection(_) => {
1271 assert!(tail.has_param_types() || tail.has_self_ty());
1272 Ok(SizeSkeleton::Pointer {
1274 tail: tcx.erase_regions(&tail)
1278 bug!("SizeSkeleton::compute({}): layout errored ({}), yet \
1279 tail `{}` is not a type parameter or a projection",
1285 ty::TyAdt(def, substs) => {
1286 // Only newtypes and enums w/ nullable pointer optimization.
1287 if def.is_union() || def.variants.is_empty() || def.variants.len() > 2 {
1291 // Get a zero-sized variant or a pointer newtype.
1292 let zero_or_ptr_variant = |i: usize| {
1293 let fields = def.variants[i].fields.iter().map(|field| {
1294 SizeSkeleton::compute(field.ty(tcx, substs), tcx, param_env)
1297 for field in fields {
1300 SizeSkeleton::Known(size) => {
1301 if size.bytes() > 0 {
1305 SizeSkeleton::Pointer {..} => {
1316 let v0 = zero_or_ptr_variant(0)?;
1318 if def.variants.len() == 1 {
1319 if let Some(SizeSkeleton::Pointer { non_zero, tail }) = v0 {
1320 return Ok(SizeSkeleton::Pointer {
1321 non_zero: non_zero ||
1322 Some(def.did) == tcx.lang_items().non_zero(),
1330 let v1 = zero_or_ptr_variant(1)?;
1331 // Nullable pointer enum optimization.
1333 (Some(SizeSkeleton::Pointer { non_zero: true, tail }), None) |
1334 (None, Some(SizeSkeleton::Pointer { non_zero: true, tail })) => {
1335 Ok(SizeSkeleton::Pointer {
1344 ty::TyProjection(_) | ty::TyAnon(..) => {
1345 let normalized = tcx.normalize_erasing_regions(param_env, ty);
1346 if ty == normalized {
1349 SizeSkeleton::compute(normalized, tcx, param_env)
1357 pub fn same_size(self, other: SizeSkeleton) -> bool {
1358 match (self, other) {
1359 (SizeSkeleton::Known(a), SizeSkeleton::Known(b)) => a == b,
1360 (SizeSkeleton::Pointer { tail: a, .. },
1361 SizeSkeleton::Pointer { tail: b, .. }) => a == b,
1367 pub trait HasTyCtxt<'tcx>: HasDataLayout {
1368 fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx>;
1371 impl<'a, 'gcx, 'tcx> HasDataLayout for TyCtxt<'a, 'gcx, 'tcx> {
1372 fn data_layout(&self) -> &TargetDataLayout {
1377 impl<'a, 'gcx, 'tcx> HasTyCtxt<'gcx> for TyCtxt<'a, 'gcx, 'tcx> {
1378 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'gcx> {
1383 impl<'tcx, T: HasDataLayout> HasDataLayout for LayoutCx<'tcx, T> {
1384 fn data_layout(&self) -> &TargetDataLayout {
1385 self.tcx.data_layout()
1389 impl<'gcx, 'tcx, T: HasTyCtxt<'gcx>> HasTyCtxt<'gcx> for LayoutCx<'tcx, T> {
1390 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'gcx> {
1395 pub trait MaybeResult<T> {
1396 fn from_ok(x: T) -> Self;
1397 fn map_same<F: FnOnce(T) -> T>(self, f: F) -> Self;
1400 impl<T> MaybeResult<T> for T {
1401 fn from_ok(x: T) -> Self {
1404 fn map_same<F: FnOnce(T) -> T>(self, f: F) -> Self {
1409 impl<T, E> MaybeResult<T> for Result<T, E> {
1410 fn from_ok(x: T) -> Self {
1413 fn map_same<F: FnOnce(T) -> T>(self, f: F) -> Self {
1418 pub type TyLayout<'tcx> = ::rustc_target::abi::TyLayout<'tcx, Ty<'tcx>>;
1420 impl<'a, 'tcx> LayoutOf for LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
1422 type TyLayout = Result<TyLayout<'tcx>, LayoutError<'tcx>>;
1424 /// Computes the layout of a type. Note that this implicitly
1425 /// executes in "reveal all" mode.
1426 fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
1427 let param_env = self.param_env.with_reveal_all();
1428 let ty = self.tcx.normalize_erasing_regions(param_env, ty);
1429 let details = self.tcx.layout_raw(param_env.and(ty))?;
1430 let layout = TyLayout {
1435 // NB: This recording is normally disabled; when enabled, it
1436 // can however trigger recursive invocations of `layout_of`.
1437 // Therefore, we execute it *after* the main query has
1438 // completed, to avoid problems around recursive structures
1439 // and the like. (Admittedly, I wasn't able to reproduce a problem
1440 // here, but it seems like the right thing to do. -nmatsakis)
1441 self.record_layout_for_printing(layout);
1447 impl<'a, 'tcx> LayoutOf for LayoutCx<'tcx, ty::maps::TyCtxtAt<'a, 'tcx, 'tcx>> {
1449 type TyLayout = Result<TyLayout<'tcx>, LayoutError<'tcx>>;
1451 /// Computes the layout of a type. Note that this implicitly
1452 /// executes in "reveal all" mode.
1453 fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
1454 let param_env = self.param_env.with_reveal_all();
1455 let ty = self.tcx.normalize_erasing_regions(param_env, ty);
1456 let details = self.tcx.layout_raw(param_env.and(ty))?;
1457 let layout = TyLayout {
1462 // NB: This recording is normally disabled; when enabled, it
1463 // can however trigger recursive invocations of `layout_of`.
1464 // Therefore, we execute it *after* the main query has
1465 // completed, to avoid problems around recursive structures
1466 // and the like. (Admittedly, I wasn't able to reproduce a problem
1467 // here, but it seems like the right thing to do. -nmatsakis)
1470 param_env: self.param_env
1472 cx.record_layout_for_printing(layout);
1478 // Helper (inherent) `layout_of` methods to avoid pushing `LayoutCx` to users.
1479 impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
1480 /// Computes the layout of a type. Note that this implicitly
1481 /// executes in "reveal all" mode.
1483 pub fn layout_of(self, param_env_and_ty: ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
1484 -> Result<TyLayout<'tcx>, LayoutError<'tcx>> {
1487 param_env: param_env_and_ty.param_env
1489 cx.layout_of(param_env_and_ty.value)
1493 impl<'a, 'tcx> ty::maps::TyCtxtAt<'a, 'tcx, 'tcx> {
1494 /// Computes the layout of a type. Note that this implicitly
1495 /// executes in "reveal all" mode.
1497 pub fn layout_of(self, param_env_and_ty: ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
1498 -> Result<TyLayout<'tcx>, LayoutError<'tcx>> {
1501 param_env: param_env_and_ty.param_env
1503 cx.layout_of(param_env_and_ty.value)
1507 impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx>
1508 where C: LayoutOf<Ty = Ty<'tcx>> + HasTyCtxt<'tcx>,
1509 C::TyLayout: MaybeResult<TyLayout<'tcx>>
1511 fn for_variant(this: TyLayout<'tcx>, cx: C, variant_index: usize) -> TyLayout<'tcx> {
1512 let details = match this.variants {
1513 Variants::Single { index } if index == variant_index => this.details,
1515 Variants::Single { index } => {
1516 // Deny calling for_variant more than once for non-Single enums.
1517 cx.layout_of(this.ty).map_same(|layout| {
1518 assert_eq!(layout.variants, Variants::Single { index });
1522 let fields = match this.ty.sty {
1523 ty::TyAdt(def, _) => def.variants[variant_index].fields.len(),
1526 let mut details = LayoutDetails::uninhabited(fields);
1527 details.variants = Variants::Single { index: variant_index };
1528 cx.tcx().intern_layout(details)
1531 Variants::NicheFilling { ref variants, .. } |
1532 Variants::Tagged { ref variants, .. } => {
1533 &variants[variant_index]
1537 assert_eq!(details.variants, Variants::Single { index: variant_index });
1545 fn field(this: TyLayout<'tcx>, cx: C, i: usize) -> C::TyLayout {
1547 cx.layout_of(match this.ty.sty {
1556 ty::TyGeneratorWitness(..) |
1558 ty::TyDynamic(..) => {
1559 bug!("TyLayout::field_type({:?}): not applicable", this)
1562 // Potentially-fat pointers.
1563 ty::TyRef(_, ty::TypeAndMut { ty: pointee, .. }) |
1564 ty::TyRawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
1567 // Reuse the fat *T type as its own thin pointer data field.
1568 // This provides information about e.g. DST struct pointees
1569 // (which may have no non-DST form), and will work as long
1570 // as the `Abi` or `FieldPlacement` is checked by users.
1572 let nil = tcx.mk_nil();
1573 let ptr_ty = if this.ty.is_unsafe_ptr() {
1576 tcx.mk_mut_ref(tcx.types.re_static, nil)
1578 return cx.layout_of(ptr_ty).map_same(|mut ptr_layout| {
1579 ptr_layout.ty = this.ty;
1584 match tcx.struct_tail(pointee).sty {
1586 ty::TyStr => tcx.types.usize,
1587 ty::TyDynamic(..) => {
1588 // FIXME(eddyb) use an usize/fn() array with
1589 // the correct number of vtables slots.
1590 tcx.mk_imm_ref(tcx.types.re_static, tcx.mk_nil())
1592 _ => bug!("TyLayout::field_type({:?}): not applicable", this)
1596 // Arrays and slices.
1597 ty::TyArray(element, _) |
1598 ty::TySlice(element) => element,
1599 ty::TyStr => tcx.types.u8,
1601 // Tuples, generators and closures.
1602 ty::TyClosure(def_id, ref substs) => {
1603 substs.upvar_tys(def_id, tcx).nth(i).unwrap()
1606 ty::TyGenerator(def_id, ref substs, _) => {
1607 substs.field_tys(def_id, tcx).nth(i).unwrap()
1610 ty::TyTuple(tys) => tys[i],
1612 // SIMD vector types.
1613 ty::TyAdt(def, ..) if def.repr.simd() => {
1614 this.ty.simd_type(tcx)
1618 ty::TyAdt(def, substs) => {
1619 match this.variants {
1620 Variants::Single { index } => {
1621 def.variants[index].fields[i].ty(tcx, substs)
1624 // Discriminant field for enums (where applicable).
1625 Variants::Tagged { tag: ref discr, .. } |
1626 Variants::NicheFilling { niche: ref discr, .. } => {
1628 let layout = LayoutDetails::scalar(tcx, discr.clone());
1629 return MaybeResult::from_ok(TyLayout {
1630 details: tcx.intern_layout(layout),
1631 ty: discr.value.to_ty(tcx)
1637 ty::TyProjection(_) | ty::TyAnon(..) | ty::TyParam(_) |
1638 ty::TyInfer(_) | ty::TyError => {
1639 bug!("TyLayout::field_type: unexpected type `{}`", this.ty)
1645 impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
1646 /// Find the offset of a niche leaf field, starting from
1647 /// the given type and recursing through aggregates, which
1648 /// has at least `count` consecutive invalid values.
1649 /// The tuple is `(offset, scalar, niche_value)`.
1650 // FIXME(eddyb) traverse already optimized enums.
1651 fn find_niche(self, layout: TyLayout<'tcx>, count: u128)
1652 -> Result<Option<(Size, Scalar, u128)>, LayoutError<'tcx>>
1654 let scalar_component = |scalar: &Scalar, offset| {
1655 let Scalar { value, valid_range: ref v } = *scalar;
1657 let bits = value.size(self).bits();
1658 assert!(bits <= 128);
1659 let max_value = !0u128 >> (128 - bits);
1661 // Find out how many values are outside the valid range.
1662 let niches = if v.start() <= v.end() {
1663 v.start() + (max_value - v.end())
1665 v.start() - v.end() - 1
1668 // Give up if we can't fit `count` consecutive niches.
1673 let niche_start = v.end().wrapping_add(1) & max_value;
1674 let niche_end = v.end().wrapping_add(count) & max_value;
1675 Some((offset, Scalar {
1677 valid_range: *v.start()..=niche_end
1681 // Locals variables which live across yields are stored
1682 // in the generator type as fields. These may be uninitialized
1683 // so we don't look for niches there.
1684 if let ty::TyGenerator(..) = layout.ty.sty {
1689 Abi::Scalar(ref scalar) => {
1690 return Ok(scalar_component(scalar, Size::from_bytes(0)));
1692 Abi::ScalarPair(ref a, ref b) => {
1693 return Ok(scalar_component(a, Size::from_bytes(0)).or_else(|| {
1694 scalar_component(b, a.value.size(self).abi_align(b.value.align(self)))
1697 Abi::Vector { ref element, .. } => {
1698 return Ok(scalar_component(element, Size::from_bytes(0)));
1703 // Perhaps one of the fields is non-zero, let's recurse and find out.
1704 if let FieldPlacement::Union(_) = layout.fields {
1705 // Only Rust enums have safe-to-inspect fields
1706 // (a discriminant), other unions are unsafe.
1707 if let Variants::Single { .. } = layout.variants {
1711 if let FieldPlacement::Array { .. } = layout.fields {
1712 if layout.fields.count() > 0 {
1713 return self.find_niche(layout.field(self, 0)?, count);
1716 for i in 0..layout.fields.count() {
1717 let r = self.find_niche(layout.field(self, i)?, count)?;
1718 if let Some((offset, scalar, niche_value)) = r {
1719 let offset = layout.fields.offset(i) + offset;
1720 return Ok(Some((offset, scalar, niche_value)));
1727 impl<'a> HashStable<StableHashingContext<'a>> for Variants {
1728 fn hash_stable<W: StableHasherResult>(&self,
1729 hcx: &mut StableHashingContext<'a>,
1730 hasher: &mut StableHasher<W>) {
1731 use ty::layout::Variants::*;
1732 mem::discriminant(self).hash_stable(hcx, hasher);
1735 Single { index } => {
1736 index.hash_stable(hcx, hasher);
1742 tag.hash_stable(hcx, hasher);
1743 variants.hash_stable(hcx, hasher);
1752 dataful_variant.hash_stable(hcx, hasher);
1753 niche_variants.start().hash_stable(hcx, hasher);
1754 niche_variants.end().hash_stable(hcx, hasher);
1755 niche.hash_stable(hcx, hasher);
1756 niche_start.hash_stable(hcx, hasher);
1757 variants.hash_stable(hcx, hasher);
1763 impl<'a> HashStable<StableHashingContext<'a>> for FieldPlacement {
1764 fn hash_stable<W: StableHasherResult>(&self,
1765 hcx: &mut StableHashingContext<'a>,
1766 hasher: &mut StableHasher<W>) {
1767 use ty::layout::FieldPlacement::*;
1768 mem::discriminant(self).hash_stable(hcx, hasher);
1772 count.hash_stable(hcx, hasher);
1774 Array { count, stride } => {
1775 count.hash_stable(hcx, hasher);
1776 stride.hash_stable(hcx, hasher);
1778 Arbitrary { ref offsets, ref memory_index } => {
1779 offsets.hash_stable(hcx, hasher);
1780 memory_index.hash_stable(hcx, hasher);
1786 impl<'a> HashStable<StableHashingContext<'a>> for Abi {
1787 fn hash_stable<W: StableHasherResult>(&self,
1788 hcx: &mut StableHashingContext<'a>,
1789 hasher: &mut StableHasher<W>) {
1790 use ty::layout::Abi::*;
1791 mem::discriminant(self).hash_stable(hcx, hasher);
1795 Scalar(ref value) => {
1796 value.hash_stable(hcx, hasher);
1798 ScalarPair(ref a, ref b) => {
1799 a.hash_stable(hcx, hasher);
1800 b.hash_stable(hcx, hasher);
1802 Vector { ref element, count } => {
1803 element.hash_stable(hcx, hasher);
1804 count.hash_stable(hcx, hasher);
1806 Aggregate { sized } => {
1807 sized.hash_stable(hcx, hasher);
1813 impl<'a> HashStable<StableHashingContext<'a>> for Scalar {
1814 fn hash_stable<W: StableHasherResult>(&self,
1815 hcx: &mut StableHashingContext<'a>,
1816 hasher: &mut StableHasher<W>) {
1817 let Scalar { value, ref valid_range } = *self;
1818 value.hash_stable(hcx, hasher);
1819 valid_range.start().hash_stable(hcx, hasher);
1820 valid_range.end().hash_stable(hcx, hasher);
1824 impl_stable_hash_for!(struct ::ty::layout::LayoutDetails {
1832 impl_stable_hash_for!(enum ::ty::layout::Integer {
1840 impl_stable_hash_for!(enum ::ty::layout::Primitive {
1841 Int(integer, signed),
1847 impl<'gcx> HashStable<StableHashingContext<'gcx>> for Align {
1848 fn hash_stable<W: StableHasherResult>(&self,
1849 hcx: &mut StableHashingContext<'gcx>,
1850 hasher: &mut StableHasher<W>) {
1851 self.abi().hash_stable(hcx, hasher);
1852 self.pref().hash_stable(hcx, hasher);
1856 impl<'gcx> HashStable<StableHashingContext<'gcx>> for Size {
1857 fn hash_stable<W: StableHasherResult>(&self,
1858 hcx: &mut StableHashingContext<'gcx>,
1859 hasher: &mut StableHasher<W>) {
1860 self.bytes().hash_stable(hcx, hasher);
1864 impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for LayoutError<'gcx>
1866 fn hash_stable<W: StableHasherResult>(&self,
1867 hcx: &mut StableHashingContext<'a>,
1868 hasher: &mut StableHasher<W>) {
1869 use ty::layout::LayoutError::*;
1870 mem::discriminant(self).hash_stable(hcx, hasher);
1874 SizeOverflow(t) => t.hash_stable(hcx, hasher)