8000 od: warning: function pointer comparisons do not produce meaningful results · Issue #7187 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

od: warning: function pointer comparisons do not produce meaningful results #7187

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
ic3man5 opened this issue Jan 21, 2025 · 1 comment · Fixed by #7211
Closed

od: warning: function pointer comparisons do not produce meaningful results #7187

ic3man5 opened this issue Jan 21, 2025 · 1 comment · Fixed by #7211
Labels

Comments

@ic3man5
Copy link
Contributor
ic3man5 commented Jan 21, 2025

This might be a false positive, I had nightly defaulted by accident and ran across this warning. Stable 1.84 doesn't display this warning.

 rustc --version
rustc 1.86.0-nightly (824759493 2025-01-09)
   Compiling uu_sum v0.0.29 (/home/drebbe/dev/coreutils/src/uu/sum)
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
  --> src/uu/od/src/formatteriteminfo.rs:29:45
   |
29 |             (IntWriter(a), IntWriter(b)) => a == b,
   |                                             ^^^^^^
   |
   = note: the address of the same function can vary between different codegen units
   = note: furthermore, different functions could have the same address after being merged together
   = note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
   = note: `#[warn(unpredictable_function_pointer_comparisons)]` on by default
help: refactor your code, or use `std::ptr::fn_addr_eq` to suppress the lint
   |
29 |             (IntWriter(a), IntWriter(b)) => std::ptr::fn_addr_eq(*a, *b),
   |                                             ++++++++++++++++++++++ ~~~ +

warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
  --> src/uu/od/src/formatteriteminfo.rs:30:49
   |
30 |             (FloatWriter(a), FloatWriter(b)) => a == b,
   |                                                 ^^^^^^
   |
   = note: the address of the same function can vary between different codegen units
   = note: furthermore, different functions could have the same address after being merged together
   = note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
help: refactor your code, or use `std::ptr::fn_addr_eq` to suppress the lint
   |
30 |             (FloatWriter(a), FloatWriter(b)) => std::ptr::fn_addr_eq(*a, *b),
   |                                                 ++++++++++++++++++++++ ~~~ +
@alexs-sh
Copy link
Contributor

@ic3man5 Thank you for the observations.

Some notes from my side

  1. It seems that this is not a sum issue but an od issue. It is possible to reproduce the issue by running cargo build from src/uu/od . The source of the issue (src/uu/od/src/formatteriteminfo.rs) also informs about od.

  2. Using fn_addr_eq for comparison will lead to broken tests. For example:

thread 'parse_formats::test_two_separate_options' panicked at src/uu/od/src/parse_formats.rs:353:5:
assertion `left == right` failed

left:  [FormatterItemInfo { byte_size: 8, print_width: 25, formatter: FloatWriter:0x55f506900fe0 },
        FormatterItemInfo { byte_size: 2, print_width: 5, formatter: IntWriter:0x55f5068ebcc0 }]

right: [FormatterItemInfo { byte_size: 8, print_width: 25, formatter: FloatWriter:0x55f506900fe0 },
        FormatterItemInfo { byte_size: 2, print_width: 5, formatter: IntWriter:0x55f5068ebcc0 }]

Here is a commit & CI build. The test results look strange, but reading the docs for fn_addr_eq could shed light on that.

The first assumption here is that PartialEq and Eq for FormatterItemInfo should take into account byte_size, print_width fields, and the type of formatter, but not the addresses of formatting functions. In general, comparing addresses could be useless because equivalent (in terms of behavior) functions could have different addresses.

  1. I'm not sure we really need manual PartialEq and Eq implementations here. As the simplest first step, I asked the compiler to generate the trait implementations and allowed it to decide how to compare. As a result, I see the issue is gone, and the tests pass (cargo test for the whole coreutils and the od module). Here is PR for reviewing & further discussion.

@cakebaker cakebaker changed the title sum: warning: function pointer comparisons do not produce meaningful results od: warning: function pointer comparisons do not produce meaningful results Jan 26, 2025
@cakebaker cakebaker added U - od and removed U - sum labels Jan 26, 2025
@cakebaker cakebaker linked a pull request Jan 26, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
0