]>
git.proxmox.com Git - rustc.git/blob - src/librustc_mir/util/alignment.rs
1 use rustc_middle
::mir
::*;
2 use rustc_middle
::ty
::{self, TyCtxt}
;
4 /// Returns `true` if this place is allowed to be less aligned
5 /// than its containing struct (because it is within a packed
7 pub fn is_disaligned
<'tcx
, L
>(
10 param_env
: ty
::ParamEnv
<'tcx
>,
14 L
: HasLocalDecls
<'tcx
>,
16 debug
!("is_disaligned({:?})", place
);
17 if !is_within_packed(tcx
, local_decls
, place
) {
18 debug
!("is_disaligned({:?}) - not within packed", place
);
22 let ty
= place
.ty(local_decls
, tcx
).ty
;
23 match tcx
.layout_raw(param_env
.and(ty
)) {
24 Ok(layout
) if layout
.align
.abi
.bytes() == 1 => {
25 // if the alignment is 1, the type can't be further
27 debug
!("is_disaligned({:?}) - align = 1", place
);
31 debug
!("is_disaligned({:?}) - true", place
);
37 fn is_within_packed
<'tcx
, L
>(tcx
: TyCtxt
<'tcx
>, local_decls
: &L
, place
: Place
<'tcx
>) -> bool
39 L
: HasLocalDecls
<'tcx
>,
41 let mut cursor
= place
.projection
.as_ref();
42 while let &[ref proj_base @
.., elem
] = cursor
{
46 // encountered a Deref, which is ABI-aligned
47 ProjectionElem
::Deref
=> break,
48 ProjectionElem
::Field(..) => {
49 let ty
= Place
::ty_from(place
.local
, proj_base
, local_decls
, tcx
).ty
;
51 ty
::Adt(def
, _
) if def
.repr
.packed() => return true,