rustc_const_eval/util/
alignment.rs

1use rustc_abi::Align;
2use rustc_middle::mir::*;
3use rustc_middle::ty::{self, TyCtxt};
4use tracing::debug;
5
6/// Returns `true` if this place is allowed to be less aligned
7/// than its containing struct (because it is within a packed
8/// struct).
9pub fn is_disaligned<'tcx, L>(
10    tcx: TyCtxt<'tcx>,
11    local_decls: &L,
12    typing_env: ty::TypingEnv<'tcx>,
13    place: Place<'tcx>,
14) -> bool
15where
16    L: HasLocalDecls<'tcx>,
17{
18    debug!("is_disaligned({:?})", place);
19    let Some(pack) = is_within_packed(tcx, local_decls, place) else {
20        debug!("is_disaligned({:?}) - not within packed", place);
21        return false;
22    };
23
24    let ty = place.ty(local_decls, tcx).ty;
25    let unsized_tail = || tcx.struct_tail_for_codegen(ty, typing_env);
26    match tcx.layout_of(typing_env.as_query_input(ty)) {
27        Ok(layout)
28            if layout.align.abi <= pack
29                && (layout.is_sized()
30                    || matches!(unsized_tail().kind(), ty::Slice(..) | ty::Str)) =>
31        {
32            // If the packed alignment is greater or equal to the field alignment, the type won't be
33            // further disaligned.
34            // However we need to ensure the field is sized; for unsized fields, `layout.align` is
35            // just an approximation -- except when the unsized tail is a slice, where the alignment
36            // is fully determined by the type.
37            debug!(
38                "is_disaligned({:?}) - align = {}, packed = {}; not disaligned",
39                place,
40                layout.align.abi.bytes(),
41                pack.bytes()
42            );
43            false
44        }
45        _ => {
46            // We cannot figure out the layout. Conservatively assume that this is disaligned.
47            debug!("is_disaligned({:?}) - true", place);
48            true
49        }
50    }
51}
52
53pub fn is_within_packed<'tcx, L>(
54    tcx: TyCtxt<'tcx>,
55    local_decls: &L,
56    place: Place<'tcx>,
57) -> Option<Align>
58where
59    L: HasLocalDecls<'tcx>,
60{
61    place
62        .iter_projections()
63        .rev()
64        // Stop at `Deref`; standard ABI alignment applies there.
65        .take_while(|(_base, elem)| !matches!(elem, ProjectionElem::Deref))
66        // Consider the packed alignments at play here...
67        .filter_map(|(base, _elem)| {
68            base.ty(local_decls, tcx).ty.ty_adt_def().and_then(|adt| adt.repr().pack)
69        })
70        // ... and compute their minimum.
71        // The overall smallest alignment is what matters.
72        .min()
73}