|
1 | 1 | use rustc_hir::def_id::LocalDefId;
|
2 | 2 | use rustc_middle::mir;
|
3 | | -use rustc_middle::mir::interpret::{AllocInit, Allocation, InterpResult, Pointer}; |
| 3 | +use rustc_middle::mir::interpret::{AllocInit, Allocation, GlobalAlloc,InterpResult, Pointer}; |
4 | 4 | use rustc_middle::ty::layout::TyAndLayout;
|
5 | 5 | use rustc_middle::ty::{TyCtxt, TypeVisitable, TypeVisitableExt};
|
6 | 6 | use tracing::debug;
|
@@ -38,13 +38,14 @@ pub(crate) fn create_static_alloc<'tcx>(
|
38 | 38 | static_def_id: LocalDefId,
|
39 | 39 | layout: TyAndLayout<'tcx>,
|
40 | 40 | ) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
|
41 | | - // Take over-alignment from attributes into account. |
42 | | - let align = match ecx.tcx.codegen_fn_attrs(static_def_id).alignment { |
43 | | - Some(align_from_attribute) => Ord::max(align_from_attribute, layout.align.abi), |
44 | | - None => layout.align.abi, |
45 | | - }; |
| 41 | + // Inherit size and align from the `GlobalAlloc::Static` so we can avoid duplicating |
| 42 | + // the alignment attribute logic. |
| 43 | + let (size, align) = |
| 44 | + GlobalAlloc::Static(static_def_id.into()).size_and_align(*ecx.tcx, ecx.typing_env); |
| 45 | + assert_eq!(size, layout.size); |
| 46 | + assert!(align >= layout.align.abi); |
46 | 47 |
|
47 | | - let alloc = Allocation::try_new(layout.size, align, AllocInit::Uninit, ())?; |
| 48 | + let alloc = Allocation::try_new(size, align, AllocInit::Uninit, ())?; |
48 | 49 | let alloc_id = ecx.tcx.reserve_and_set_static_alloc(static_def_id.into());
|
49 | 50 | assert_eq!(ecx.machine.static_root_ids, None);
|
50 | 51 | ecx.machine.static_root_ids = Some((alloc_id, static_def_id));
|
|
0 commit comments