Closed
Description
Given the following code (playground):
#![deny(fuzzy_provenance_casts)]
#![feature(strict_provenance)]
use memoffset::offset_of;
struct Foo {
bar: i32,
}
fn main() {
offset_of!(Foo, bar) as *const ();
}
The current output is:
error: strict provenance disallows casting integer `usize` to pointer `*const ()`
--> src/main.rs:11:5
|
11 | offset_of!(Foo, bar) as *const ();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
|
1 | #![deny(fuzzy_provenance_casts)]
| ^^^^^^^^^^^^^^^^^^^^^^
= help: if you can't comply with strict provenance and don't have a pointer with the correct provenance you can use `std::ptr::from_exposed_addr()` instead
help: use `.with_addr()` to adjust a valid pointer in the same allocation, to this address
|
11 ~ (...).with_addr({
12 + // Get a base pointer (non-dangling if rustc supports `MaybeUninit`).
13 + _memoffset__let_base_ptr!(base_ptr, $parent);
14 + // Get field pointer.
15 + let field_ptr = raw_field!(base_ptr, $parent, $field);
16 + // Compute offset.
...
Note that the block passed to with_addr
is the body of memoffset::offset_of!
:
macro_rules! offset_of {
($parent:path, $field:tt) => {{
// Get a base pointer (non-dangling if rustc supports `MaybeUninit`).
_memoffset__let_base_ptr!(base_ptr, $parent);
// Get field pointer.
let field_ptr = raw_field!(base_ptr, $parent, $field);
// Compute offset.
_memoffset_offset_from_unsafe!(field_ptr, base_ptr)
}};
}
Ideally, the suggestion should just use offset_of!
:
help: use `.with_addr()` to adjust a valid pointer in the same allocation, to this address
|
11 ~ (...).with_addr(offset_of!(Foo, bar))
|
@rustbot modify labels: +A-strict-provenance +D-papercut