Skip to content

ambiguous_wide_pointer_comparisons false positive for Ord and PartialOrd #141510

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

Closed
tyilo opened this issue May 24, 2025 · 7 comments · Fixed by #141536
Closed

ambiguous_wide_pointer_comparisons false positive for Ord and PartialOrd #141510

tyilo opened this issue May 24, 2025 · 7 comments · Fixed by #141536
Labels
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.

Comments

@tyilo
Copy link
Contributor

tyilo commented May 24, 2025

Code

use std::cmp::Ordering;

fn ptr_cmp<T: ?Sized>(p1: *const T, p2: *const T) -> Ordering {
    p1.cmp(&p2)
}

fn less_than<T: ?Sized>(p1: *const T, p2: *const T) -> bool {
    p1 < p2
}

fn main() {
    let s = "abc";

    assert_eq!(ptr_cmp(s, s), Ordering::Equal);
    assert_eq!(ptr_cmp(&s[..1], s), Ordering::Less);

    assert_eq!(less_than(s, s), false);
    assert_eq!(less_than(&s[..1], s), true);
}

Current output

warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
 --> src/main.rs:4:5
  |
4 |     p1.cmp(&p2)
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(ambiguous_wide_pointer_comparisons)]` on by default
help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
  |
4 |     p1.cast::<()>().cmp(&p2.cast::<()>())
  |       +++++++++++++        +++++++++++++

warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
 --> src/main.rs:8:5
  |
8 |     p1 < p2
  |     ^^^^^^^
  |
help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
  |
8 |     p1.cast::<()>() < p2.cast::<()>()
  |       +++++++++++++     +++++++++++++

warning: `wide-ptr-cmp` (bin "wide-ptr-cmp") generated 2 warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s

Desired output

No output

Rationale and extra context

The warning references std::ptr::addr_eq, however that can't be used for ordering two pointers.

Either std::ptr::cmp should be added to std (and core) which can be referenced instead or the lint should simply not be triggered by <pointer>::cmp and <pointer>::partial_cmp.

Other cases

Rust Version

$ rustc --version --verbose
rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

Anything else?

No response

@tyilo tyilo 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 May 24, 2025
@Urgau
Copy link
Member

Urgau commented May 24, 2025

The warning states "use std::ptr::addr_eq or untyped pointers" and then proceeds to explicitly suggest the "untyped pointers" approach.

warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
 --> src/main.rs:4:5
  |
4 |     p1.cmp(&p2)
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(ambiguous_wide_pointer_comparisons)]` on by default
help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
  |
4 |     p1.cast::<()>().cmp(&p2.cast::<()>())
  |       +++++++++++++        +++++++++++++

We should probably not mention std::ptr::addr_eq for cmp/partial_cmp cases, but the lint is still valid either way.

@tyilo
Copy link
Contributor Author

tyilo commented May 24, 2025

I actually need the cmp behavior that includes the metadata of wide pointers.

@Urgau
Copy link
Member

Urgau commented May 24, 2025

Then you can just allow the lint, since it's expected for you, put #[allow(ambiguous_wide_pointer_comparisons)] on your comparisons.

If we had some version of std::ptr::addr_eq for cmp/partial_cmp it would also strip the metadata. For equality we have std::ptr::eq, which also compares the metadata, but we don't have anything similar for cmp.

@Urgau
Copy link
Member

Urgau commented May 24, 2025

Either std::ptr::cmp should be added to std (and core) which can be referenced instead or the lint should simply not be triggered by <pointer>::cmp and <pointer>::partial_cmp.

I can see a rational for that. Allowing the lint is also an option.

I think it would be quite unfortunate to reduce the scope of the lint simply because we don't have a "nice" std function to avoid it. Comparing pointers with metadata is also never what you want.

@tyilo
Copy link
Contributor Author

tyilo commented May 24, 2025

Comparing pointers with metadata is also never what you want.

I need to use it for this: facet-rs/facet#675

@Urgau
Copy link
Member

Urgau commented May 24, 2025

What if the suggested output for the cmp/partial_cmp cases was? Would you still think it's a "false positive"?

help: use untyped pointers to only compare their addresses
  |
4 |     p1.cast::<()>().cmp(&p2.cast::<()>());
  |       +++++++++++++        +++++++++++++
help: or allow the lint to compare metadata and addresses
  |
4 -     p1.cmp(&p2)
4 +     #[allow(ambiguous_wide_pointer_comparisons)] p1.cmp(&p2)
  |

@tyilo
Copy link
Contributor Author

tyilo commented May 24, 2025

That would be nice 👍

jieyouxu added a commit to jieyouxu/rust that referenced this issue May 26, 2025
…e1-dead

Improve `ambiguous_wide_pointer_comparisons` lint compare diagnostics

This PR improves the `ambiguous_wide_pointer_comparisons` lint compare diagnostics: `cmp`/`partial_cmp`, but also the operators `<`/`>`/`>=`/`<=`, by:
1. removing the reference to `std::ptr::addr_eq` which only works for equality
2. and adding an `#[expect]` suggestion for keeping the current behavior

Fixes rust-lang#141510
@bors bors closed this as completed in a0d77f3 May 27, 2025
rust-timer added a commit that referenced this issue May 27, 2025
Rollup merge of #141536 - Urgau:ambi_wide_ptr-cmp-diag, r=fee1-dead

Improve `ambiguous_wide_pointer_comparisons` lint compare diagnostics

This PR improves the `ambiguous_wide_pointer_comparisons` lint compare diagnostics: `cmp`/`partial_cmp`, but also the operators `<`/`>`/`>=`/`<=`, by:
1. removing the reference to `std::ptr::addr_eq` which only works for equality
2. and adding an `#[expect]` suggestion for keeping the current behavior

Fixes #141510
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 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.

2 participants