From 5b2e8b4c66c56ef50a853de5a6ccb7f20a0d7921 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 17 Nov 2021 10:04:03 +0100 Subject: [PATCH] Revert "lang: get offsetof const fn ready" This reverts commit 8f89f9ad60948d4853c05801414cfd152ddef16a. generates broken code in release builds on current rustc 1.55 --- proxmox-lang/src/lib.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/proxmox-lang/src/lib.rs b/proxmox-lang/src/lib.rs index 06e0b319..f5c6ebec 100644 --- a/proxmox-lang/src/lib.rs +++ b/proxmox-lang/src/lib.rs @@ -72,21 +72,22 @@ macro_rules! static_assert_size { /// #[repr(C)] /// struct Stuff { /// first: u32, -/// second: u16, -/// third: u16, -/// fourth: u32, +/// second: u32, /// } /// -/// assert_eq!(offsetof!(Stuff, first), 0); /// assert_eq!(offsetof!(Stuff, second), 4); -/// assert_eq!(offsetof!(Stuff, third), 6); -/// assert_eq!(offsetof!(Stuff, fourth), 8); /// /// ``` +// FIXME: With 1.56 we get `const transmute` which may help making this usable `const fn` as we can +// avoid dereferencing the raw pointer by transmuting `0usize` into a proper reference instead. +// +// So with 1.56, do this instead: +// +// unsafe { &(std::mem::transmute::<_, &$ty>(0usize).$field) as *const _ as usize } #[macro_export] macro_rules! offsetof { ($ty:ty, $field:ident) => { - unsafe { &(std::mem::transmute::<_, &$ty>(0usize).$field) as *const _ as usize } + unsafe { &(*(std::ptr::null::<$ty>())).$field as *const _ as usize } }; }