Skip to content

Commit 5a24656

Browse files
authored
Rollup merge of rust-lang#63914 - hvenev:repr-fields, r=eddyb
ty: use Align for ReprOptions pack and align.
2 parents f33d02c + c8a69e2 commit 5a24656

File tree

3 files changed

+45
-46
lines changed

3 files changed

+45
-46
lines changed

src/librustc/ty/layout.rs

+19-30
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,12 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
273273
repr: &ReprOptions,
274274
kind: StructKind) -> Result<LayoutDetails, LayoutError<'tcx>> {
275275
let dl = self.data_layout();
276-
let packed = repr.packed();
277-
if packed && repr.align > 0 {
276+
let pack = repr.pack;
277+
if pack.is_some() && repr.align.is_some() {
278278
bug!("struct cannot be packed and aligned");
279279
}
280280

281-
let pack = Align::from_bytes(repr.pack as u64).unwrap();
282-
283-
let mut align = if packed {
281+
let mut align = if pack.is_some() {
284282
dl.i8_align
285283
} else {
286284
dl.aggregate_align
@@ -303,7 +301,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
303301
};
304302
let optimizing = &mut inverse_memory_index[..end];
305303
let field_align = |f: &TyLayout<'_>| {
306-
if packed { f.align.abi.min(pack) } else { f.align.abi }
304+
if let Some(pack) = pack { f.align.abi.min(pack) } else { f.align.abi }
307305
};
308306
match kind {
309307
StructKind::AlwaysSized |
@@ -334,7 +332,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
334332
let mut largest_niche_available = 0;
335333

336334
if let StructKind::Prefixed(prefix_size, prefix_align) = kind {
337-
let prefix_align = if packed {
335+
let prefix_align = if let Some(pack) = pack {
338336
prefix_align.min(pack)
339337
} else {
340338
prefix_align
@@ -355,7 +353,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
355353
}
356354

357355
// Invariant: offset < dl.obj_size_bound() <= 1<<61
358-
let field_align = if packed {
356+
let field_align = if let Some(pack) = pack {
359357
field.align.min(AbiAndPrefAlign::new(pack))
360358
} else {
361359
field.align
@@ -379,10 +377,8 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
379377
.ok_or(LayoutError::SizeOverflow(ty))?;
380378
}
381379

382-
if repr.align > 0 {
383-
let repr_align = repr.align as u64;
384-
align = align.max(AbiAndPrefAlign::new(Align::from_bytes(repr_align).unwrap()));
385-
debug!("univariant repr_align: {:?}", repr_align);
380+
if let Some(repr_align) = repr.align {
381+
align = align.max(AbiAndPrefAlign::new(repr_align));
386382
}
387383

388384
debug!("univariant min_size: {:?}", offset);
@@ -730,23 +726,18 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
730726
}).collect::<Result<IndexVec<VariantIdx, _>, _>>()?;
731727

732728
if def.is_union() {
733-
let packed = def.repr.packed();
734-
if packed && def.repr.align > 0 {
735-
bug!("Union cannot be packed and aligned");
729+
if def.repr.pack.is_some() && def.repr.align.is_some() {
730+
bug!("union cannot be packed and aligned");
736731
}
737732

738-
let pack = Align::from_bytes(def.repr.pack as u64).unwrap();
739-
740-
let mut align = if packed {
733+
let mut align = if def.repr.pack.is_some() {
741734
dl.i8_align
742735
} else {
743736
dl.aggregate_align
744737
};
745738

746-
if def.repr.align > 0 {
747-
let repr_align = def.repr.align as u64;
748-
align = align.max(
749-
AbiAndPrefAlign::new(Align::from_bytes(repr_align).unwrap()));
739+
if let Some(repr_align) = def.repr.align {
740+
align = align.max(AbiAndPrefAlign::new(repr_align));
750741
}
751742

752743
let optimize = !def.repr.inhibit_union_abi_opt();
@@ -755,13 +746,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
755746
let index = VariantIdx::new(0);
756747
for field in &variants[index] {
757748
assert!(!field.is_unsized());
758-
759-
let field_align = if packed {
760-
field.align.min(AbiAndPrefAlign::new(pack))
761-
} else {
762-
field.align
763-
};
764-
align = align.max(field_align);
749+
align = align.max(field.align);
765750

766751
// If all non-ZST fields have the same ABI, forward this ABI
767752
if optimize && !field.is_zst() {
@@ -796,6 +781,10 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
796781
size = cmp::max(size, field.size);
797782
}
798783

784+
if let Some(pack) = def.repr.pack {
785+
align = align.min(AbiAndPrefAlign::new(pack));
786+
}
787+
799788
return Ok(tcx.intern_layout(LayoutDetails {
800789
variants: Variants::Single { index },
801790
fields: FieldPlacement::Union(variants[index].len()),
@@ -1637,7 +1626,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
16371626
};
16381627

16391628
let adt_kind = adt_def.adt_kind();
1640-
let adt_packed = adt_def.repr.packed();
1629+
let adt_packed = adt_def.repr.pack.is_some();
16411630

16421631
let build_variant_info = |n: Option<Ident>,
16431632
flds: &[ast::Name],

src/librustc/ty/mod.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use arena::SyncDroplessArena;
3333
use crate::session::DataTypeKind;
3434

3535
use rustc_serialize::{self, Encodable, Encoder};
36+
use rustc_target::abi::Align;
3637
use std::cell::RefCell;
3738
use std::cmp::{self, Ordering};
3839
use std::fmt;
@@ -2057,8 +2058,8 @@ impl_stable_hash_for!(struct ReprFlags {
20572058
#[derive(Copy, Clone, Debug, Eq, PartialEq, RustcEncodable, RustcDecodable, Default)]
20582059
pub struct ReprOptions {
20592060
pub int: Option<attr::IntType>,
2060-
pub align: u32,
2061-
pub pack: u32,
2061+
pub align: Option<Align>,
2062+
pub pack: Option<Align>,
20622063
pub flags: ReprFlags,
20632064
}
20642065

@@ -2073,18 +2074,19 @@ impl ReprOptions {
20732074
pub fn new(tcx: TyCtxt<'_>, did: DefId) -> ReprOptions {
20742075
let mut flags = ReprFlags::empty();
20752076
let mut size = None;
2076-
let mut max_align = 0;
2077-
let mut min_pack = 0;
2077+
let mut max_align: Option<Align> = None;
2078+
let mut min_pack: Option<Align> = None;
20782079
for attr in tcx.get_attrs(did).iter() {
20792080
for r in attr::find_repr_attrs(&tcx.sess.parse_sess, attr) {
20802081
flags.insert(match r {
20812082
attr::ReprC => ReprFlags::IS_C,
20822083
attr::ReprPacked(pack) => {
2083-
min_pack = if min_pack > 0 {
2084-
cmp::min(pack, min_pack)
2084+
let pack = Align::from_bytes(pack as u64).unwrap();
2085+
min_pack = Some(if let Some(min_pack) = min_pack {
2086+
min_pack.min(pack)
20852087
} else {
20862088
pack
2087-
};
2089+
});
20882090
ReprFlags::empty()
20892091
},
20902092
attr::ReprTransparent => ReprFlags::IS_TRANSPARENT,
@@ -2094,7 +2096,7 @@ impl ReprOptions {
20942096
ReprFlags::empty()
20952097
},
20962098
attr::ReprAlign(align) => {
2097-
max_align = cmp::max(align, max_align);
2099+
max_align = max_align.max(Some(Align::from_bytes(align as u64).unwrap()));
20982100
ReprFlags::empty()
20992101
},
21002102
});
@@ -2113,7 +2115,7 @@ impl ReprOptions {
21132115
#[inline]
21142116
pub fn c(&self) -> bool { self.flags.contains(ReprFlags::IS_C) }
21152117
#[inline]
2116-
pub fn packed(&self) -> bool { self.pack > 0 }
2118+
pub fn packed(&self) -> bool { self.pack.is_some() }
21172119
#[inline]
21182120
pub fn transparent(&self) -> bool { self.flags.contains(ReprFlags::IS_TRANSPARENT) }
21192121
#[inline]
@@ -2133,8 +2135,12 @@ impl ReprOptions {
21332135
/// Returns `true` if this `#[repr()]` should inhibit struct field reordering
21342136
/// optimizations, such as with `repr(C)`, `repr(packed(1))`, or `repr(<int>)`.
21352137
pub fn inhibit_struct_field_reordering_opt(&self) -> bool {
2136-
self.flags.intersects(ReprFlags::IS_UNOPTIMISABLE) || self.pack == 1 ||
2137-
self.int.is_some()
2138+
if let Some(pack) = self.pack {
2139+
if pack.bytes() == 1 {
2140+
return true;
2141+
}
2142+
}
2143+
self.flags.intersects(ReprFlags::IS_UNOPTIMISABLE) || self.int.is_some()
21382144
}
21392145

21402146
/// Returns `true` if this `#[repr()]` should inhibit union ABI optimisations.

src/librustc_typeck/check/mod.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1859,14 +1859,18 @@ fn check_packed(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) {
18591859
for attr in tcx.get_attrs(def_id).iter() {
18601860
for r in attr::find_repr_attrs(&tcx.sess.parse_sess, attr) {
18611861
if let attr::ReprPacked(pack) = r {
1862-
if pack != repr.pack {
1863-
struct_span_err!(tcx.sess, sp, E0634,
1864-
"type has conflicting packed representation hints").emit();
1862+
if let Some(repr_pack) = repr.pack {
1863+
if pack as u64 != repr_pack.bytes() {
1864+
struct_span_err!(
1865+
tcx.sess, sp, E0634,
1866+
"type has conflicting packed representation hints"
1867+
).emit();
1868+
}
18651869
}
18661870
}
18671871
}
18681872
}
1869-
if repr.align > 0 {
1873+
if repr.align.is_some() {
18701874
struct_span_err!(tcx.sess, sp, E0587,
18711875
"type has conflicting packed and align representation hints").emit();
18721876
}
@@ -1885,7 +1889,7 @@ fn check_packed_inner(tcx: TyCtxt<'_>, def_id: DefId, stack: &mut Vec<DefId>) ->
18851889
}
18861890
if let ty::Adt(def, substs) = t.sty {
18871891
if def.is_struct() || def.is_union() {
1888-
if tcx.adt_def(def.did).repr.align > 0 {
1892+
if tcx.adt_def(def.did).repr.align.is_some() {
18891893
return true;
18901894
}
18911895
// push struct def_id before checking fields

0 commit comments

Comments
 (0)