rustc_const_eval/util/
alignment.rs1use rustc_abi::Align;
2use rustc_middle::mir::*;
3use rustc_middle::ty::{self, AdtDef, TyCtxt};
4use tracing::debug;
5
6pub fn place_unalignment<'tcx, L>(
10 tcx: TyCtxt<'tcx>,
11 local_decls: &L,
12 typing_env: ty::TypingEnv<'tcx>,
13 place: Place<'tcx>,
14) -> Option<(AdtDef<'tcx>, Align)>
15where
16 L: HasLocalDecls<'tcx>,
17{
18 debug!("unalignment({:?})", place);
19 let Some((descr, pack)) = most_packed_projection(tcx, local_decls, place) else {
20 debug!("unalignment({:?}) - not within packed", place);
21 return None;
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 debug!(
38 "unalignment({:?}) - align = {}, packed = {}; not unaligned",
39 place,
40 layout.align.bytes(),
41 pack.bytes()
42 );
43 None
44 }
45 _ => {
46 debug!("unalignment({:?}) - unaligned", place);
48 Some((descr, pack))
49 }
50 }
51}
52
53pub fn most_packed_projection<'tcx, L>(
57 tcx: TyCtxt<'tcx>,
58 local_decls: &L,
59 place: Place<'tcx>,
60) -> Option<(AdtDef<'tcx>, Align)>
61where
62 L: HasLocalDecls<'tcx>,
63{
64 place
65 .iter_projections()
66 .rev()
67 .take_while(|(_base, elem)| !matches!(elem, ProjectionElem::Deref))
69 .filter_map(|(base, _elem)| {
71 let adt = base.ty(local_decls, tcx).ty.ty_adt_def()?;
72 let pack = adt.repr().pack?;
73 Some((adt, pack))
74 })
75 .min_by_key(|(_, align)| *align)
78}