Skip to content

Commit 6e73d2d

Browse files
committed
Fix field_offset! macro to avoid ptr -> int casts
These have been removed in recent nightlies, since pointers can't nessicarrily have a meaningfull integer value. I beleive this is because of issues with pointer 'provenance'. Here is a good blog post explaining the issue in depth: https://www.ralfj.de/blog/2020/12/14/provenance.html
1 parent a32d197 commit 6e73d2d

File tree

3 files changed

+2
-9
lines changed

3 files changed

+2
-9
lines changed

lib/derive/tests/funcs.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(const_raw_ptr_to_usize_cast)]
21
use std::os::raw::c_void;
32

43
use static_reflect_derive::reflect_func;

src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,12 @@
1313
#![feature(
1414
const_panic, const_option, // We use Option::unwrap
1515
const_fn_fn_ptr_basics, // We use PhantomData<fn() -> T>
16+
const_fn_trait_bound,
1617
// Used for field_offset macro
1718
const_raw_ptr_deref,
1819
const_maybe_uninit_as_ptr,
1920
const_ptr_offset_from,
2021
)]
21-
/*
22-
* This is required on recent nightly
23-
*
24-
* However it breaks on the version that docs.rs is using (as of this writing).
25-
* Therefore, we have to turn it off there.
26-
*/
27-
#![cfg_attr(not(feature="docs-rs"), feature(const_fn_trait_bound))]
2822
#![cfg_attr(feature = "never", feature(never_type))]
2923

3024
mod macros;

src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ macro_rules! field_offset {
2020
unsafe {
2121
let uninit = core::mem::MaybeUninit::<$target>::uninit();
2222
let base = uninit.as_ptr();
23-
(core::ptr::addr_of!((*base)$(.$field)*).cast::<u8>().offset_from(base as *const u8))
23+
(core::ptr::addr_of!((*base)$(.$field)*).cast::<u8>().offset_from(base as *const u8) as usize)
2424
}
2525
}
2626
}

0 commit comments

Comments
 (0)