Skip to content

Suggestion for fuzzy_provenance_casts on a macro uses its expansion #95919

Closed
@PatchMixolydic

Description

@PatchMixolydic

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

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-strict-provenanceArea: Strict provenance for raw pointersD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions