Skip to content

ambiguous_wide_pointer_comparisons: bad suggestion #121330

Closed
@matthiaskrgr

Description

@matthiaskrgr

Code

//@ check-pass

use std::cmp::PartialEq;
use std::rc::Rc;
use std::sync::Arc;

struct A;
struct B;

trait T {}
impl T for A {}
impl T for B {}

fn main() {
    fn cmp<T: ?Sized>(a: *mut T, b: *mut T) -> bool {
        let _ = a == b;
        //~^ WARN ambiguous wide pointer comparison

        panic!();
    }
}

Current output

warning: unused import: `std::cmp::PartialEq`
 --> src/main.rs:3:5
  |
3 | use std::cmp::PartialEq;
  |     ^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `std::rc::Rc`
 --> src/main.rs:4:5
  |
4 | use std::rc::Rc;
  |     ^^^^^^^^^^^

warning: unused import: `std::sync::Arc`
 --> src/main.rs:5:5
  |
5 | use std::sync::Arc;
  |     ^^^^^^^^^^^^^^

warning: struct `A` is never constructed
 --> src/main.rs:7:8
  |
7 | struct A;
  |        ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: struct `B` is never constructed
 --> src/main.rs:8:8
  |
8 | struct B;
  |        ^

warning: trait `T` is never used
  --> src/main.rs:10:7
   |
10 | trait T {}
   |       ^

warning: function `cmp` is never used
  --> src/main.rs:15:8
   |
15 |     fn cmp<T: ?Sized>(a: *mut T, b: *mut T) -> bool {
   |        ^^^

warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
  --> src/main.rs:16:17
   |
16 |         let _ = a == b;
   |                 ^^^^^^
   |
   = note: `#[warn(ambiguous_wide_pointer_comparisons)]` on by default
help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
   |
16 |         let _ = std::ptr::addr_eq(a, b);
   |                 ++++++++++++++++++ ~  +
help: use explicit `std::ptr::eq` method to compare metadata and addresses
   |
16 |         let _ = std::ptr::eq(a, b);
   |                 +++++++++++++ ~  +

Desired output

No response

Rationale and extra context

The lint is marked cargo-fix able but applying the suggestion fails:

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/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[E0061]: this function takes 2 arguments but 1 argument was supplied
    --> src/main.rs:16:17
     |
16   |         let _ = std::ptr::eq(std::ptr::addr_eq(a, b));
     |                 ^^^^^^^^^^^^------------------------- an argument of type `*const _` is missing
     |
note: expected `*const _`, found `bool`
    --> src/main.rs:16:30
     |
16   |         let _ = std::ptr::eq(std::ptr::addr_eq(a, b));
     |                              ^^^^^^^^^^^^^^^^^^^^^^^
     = note: expected raw pointer `*const _`
                       found type `bool`
note: function defined here
    --> /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:1932:8
     |
1932 | pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
     |        ^^
help: provide the argument
     |
16   |         let _ = std::ptr::eq(/* a */, /* b */);
     |                             ~~~~~~~~~~~~~~~~~~

error: aborting due to 1 previous error

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

warning: `wiiide` (bin "wiiide" test) generated 7 warnings (7 duplicates)

Other cases

No response

Rust Version

rustc 1.78.0-nightly (3246e7951 2024-02-19)
binary: rustc
commit-hash: 3246e79513cb89ddbfc0f21cb5a877e5b321dcc5
commit-date: 2024-02-19
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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