Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion for fuzzy_provenance_casts on a macro uses its expansion #95919

Closed
PatchMixolydic opened this issue Apr 11, 2022 · 1 comment · Fixed by #95920
Closed

Suggestion for fuzzy_provenance_casts on a macro uses its expansion #95919

PatchMixolydic opened this issue Apr 11, 2022 · 1 comment · Fixed by #95920
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-strict-provenance Area: Strict provenance for raw pointers D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@PatchMixolydic
Copy link
Contributor

PatchMixolydic commented Apr 11, 2022

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

@PatchMixolydic PatchMixolydic added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 11, 2022
@rustbot rustbot added A-strict-provenance Area: Strict provenance for raw pointers D-papercut Diagnostics: An error or lint that needs small tweaks. labels Apr 11, 2022
@compiler-errors compiler-errors added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Apr 11, 2022
@compiler-errors
Copy link
Member

This is kinda a general error with suggestions that use expr spans that originate from macros... e.g.

macro_rules! foo {
    () => { 0 }
}

fn main() {
    let x = foo!() as *const [u8];
}

On nightly renders with:

error[E0606]: cannot cast `usize` to a pointer that is wide
 --> src/main.rs:6:23
  |
2 |     () => { 0 }
  |             - consider casting this expression to `*const ()`, then using `core::ptr::from_raw_parts`
...
6 |     let x = foo!() as *const [u8];
  |                       ^^^^^^^^^^^ creating a `*const [u8]` requires both an address and a length

For more information about this error, try `rustc --explain E0606`.

... It's somewhat unsatisfying for it to be underlining 0 instead of foo!().

While I think the choice of whether to choose the macro's inner expression contents or the macro usage span depends a bit on the type of error, at least for expr as Ty suggestions, I think it makes more sense to always use the call-site span...

@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 12, 2022
…n, r=oli-obk

use `Span::find_ancestor_inside` to get right span in CastCheck

This is a quick fix. This bad suggestion likely lives in other places... but thought it would be useful to fix all of the CastCheck ones first.

Let me know if reviewer would prefer I add more tests for each of the diagnostics in CastCheck, or would like to do a more thorough review of other suggestions that use spans in typeck. I would also be open to further suggestions on how to better expose an API that gives us the "best" span for a diagnostic suggestion.

Fixed rust-lang#95919
@bors bors closed this as completed in 8d46f9c Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-strict-provenance Area: Strict provenance for raw pointers D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants