rustc_const_eval/util/
alignment.rs
1use rustc_abi::Align;
2use rustc_middle::mir::*;
3use rustc_middle::ty::{self, TyCtxt};
4use tracing::debug;
5
6pub 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 debug!(
38 "is_disaligned({:?}) - align = {}, packed = {}; not disaligned",
39 place,
40 layout.align.abi.bytes(),
41 pack.bytes()
42 );
43 false
44 }
45 _ => {
46 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 .take_while(|(_base, elem)| !matches!(elem, ProjectionElem::Deref))
66 .filter_map(|(base, _elem)| {
68 base.ty(local_decls, tcx).ty.ty_adt_def().and_then(|adt| adt.repr().pack)
69 })
70 .min()
73}