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

borrow-as-ptr eats parenthesis which break code #13882

Open
matthiaskrgr opened this issue Dec 26, 2024 · 1 comment · May be fixed by #13884
Open

borrow-as-ptr eats parenthesis which break code #13882

matthiaskrgr opened this issue Dec 26, 2024 · 1 comment · May be fixed by #13884
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@matthiaskrgr
Copy link
Member

Summary

.

Lint Name

borrow-as-ptr

Reproducer

I tried this code:

// Make sure we cannot use raw ptrs that got transmuted from mutable references
// (i.e, no EscapeToRaw happened).
// We could, in principle, do EscapeToRaw lazily to allow this code, but that
// would no alleviate the need for EscapeToRaw (see `ref_raw_int_raw` in
// `run-pass/stacked-borrows.rs`), and thus increase overall complexity.
use std::mem;

fn main() {
    let mut x: [i32; 2] = [42, 43];
    let _raw: *mut i32 = unsafe { mem::transmute(&mut x[0]) };
    // `raw` still carries a tag, so we get another pointer to the same location that does not carry a tag
    let raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
    unsafe { *raw = 13 }; //~ ERROR: /write access .* tag does not exist in the borrow stack/
}

I saw this happen:
cargo +master clippy --fix -- -Wclippy::borrow-as-ptr

    Checking o v0.1.0 (/tmp/o)
warning: failed to automatically apply fixes suggested by rustc to crate `o`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0599]: no method named `wrapping_offset` found for type `i32` in the current scope
   --> src/main.rs:12:29
    |
12  |     let raw = &raw mut x[1].wrapping_offset(-1);
    |                             ^^^^^^^^^^^^^^^
    |
help: there is a method `wrapping_abs` with a similar name, but with different arguments
   --> /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/core/src/num/mod.rs:408:5
    |
408 | /     int_impl! {
409 | |         Self = i32,
410 | |         ActualT = i32,
411 | |         UnsignedT = u32,
...   |
426 | |         bound_condition = "",
427 | |     }
    | |_____^
    = note: this error originates in the macro `int_impl` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.
Original diagnostics will follow.

warning: transmute from a reference to a pointer
  --> src/main.rs:10:35
   |
10 |     let _raw: *mut i32 = unsafe { mem::transmute(&mut x[0]) };
   |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut x[0] as *mut i32`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_transmute
   = note: `#[warn(clippy::useless_transmute)]` on by default

warning: borrow as raw pointer
  --> src/main.rs:12:15
   |
12 |     let raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
   |               ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&raw mut x[1]`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_as_ptr
   = note: requested on the command line with `-W clippy::borrow-as-ptr`

warning: `o` (bin "o" test) generated 2 warnings (run `cargo clippy --fix --bin "o" --tests` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `o`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0599]: no method named `wrapping_offset` found for type `i32` in the current scope
   --> src/main.rs:12:29
    |
12  |     let raw = &raw mut x[1].wrapping_offset(-1);
    |                             ^^^^^^^^^^^^^^^
    |
help: there is a method `wrapping_abs` with a similar name, but with different arguments
   --> /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/core/src/num/mod.rs:408:5
    |
408 | /     int_impl! {
409 | |         Self = i32,
410 | |         ActualT = i32,
411 | |         UnsignedT = u32,
...   |
426 | |         bound_condition = "",
427 | |     }
    | |_____^
    = note: this error originates in the macro `int_impl` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.
Original diagnostics will follow.

warning: `o` (bin "o") generated 2 warnings (2 duplicates)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.03s

I expected to see this happen:

Version

rustc 1.85.0-nightly (19e75f4fb 2024-12-26)
binary: rustc
commit-hash: 19e75f4fb3f960267996e8788459e97b8769aac7
commit-date: 2024-12-26
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6

Additional Labels

No response

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 26, 2024
@matthiaskrgr
Copy link
Member Author

I think we want to keep the parens here like this: let raw = (&raw mut x[1]).wrapping_offset(-1);
looks like the span is just a bit too big?

@matthiaskrgr matthiaskrgr added L-suggestion Lint: Improving, adding or fixing lint suggestions I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied and removed I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 26, 2024
@matthiaskrgr matthiaskrgr changed the title borrow-as-ptr eats parenthesis which breask code borrow-as-ptr eats parenthesis which break code Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant