-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add lint against function pointer comparisons #118833
base: master
Are you sure you want to change the base?
Conversation
r? @cjgillot (rustbot has picked a reviewer for you, use r? to override) |
Some changes occurred in src/tools/clippy cc @rust-lang/clippy The Miri subtree was changed cc @rust-lang/miri |
This comment has been minimized.
This comment has been minimized.
The lint name is very long, it would be nice if we could come up with a shorter name. I don't have a suggestion though. |
Sure, but I don't think it matter that much, I'm not expecting anyone to write the lint name by hand (since it's warn-by-default, people can just copy/paste in the diagnostic output). We also already have some pretty long lint name, like unknown_or_malformed_diagnostic_attributes. But if someone comes up with a name as descriptive but shorter I can integrate it, I'm just not sure it's worth anyone time. |
e0a6772
to
4798319
Compare
This comment has been minimized.
This comment has been minimized.
444a0b9
to
23214cb
Compare
If it's not too much work, could we do a crater run here to find out what kind of places are using this kind of comparison in the wild? |
Sure, I just pushed a commit making the lint deny-by-default. I will let you do the bors+craterbot commands. |
@bors try |
…ons, r=<try> Add lint against function pointer comparisons This is kind of a follow-up to rust-lang#117758 where we added a lint against wide pointer comparisons for being ambiguous and unreliable; well function pointer comparisons are also unreliable. We should IMO follow a similar logic and warn people about it. ----- ## `unpredictable_function_pointer_comparisons` *warn-by-default* The `unpredictable_function_pointer_comparisons` lint checks comparison of function pointer as the operands. ### Example ```rust fn foo() {} let a = foo as fn(); let _ = a == foo; ``` ### Explanation Function pointers comparisons do not produce meaningful result since they are never guaranteed to be unique and could vary between different code generation units. Furthermore different function could have the same address after being merged together. ---- This PR also uplift the very similar `clippy::fn_address_comparisons` lint, which only linted on if one of the operand was an `ty::FnDef` while this PR lints proposes to lint on all `ty::FnPtr` and `ty::FnDef`. `@rustbot` labels +I-lang-nominated
This comment has been minimized.
This comment has been minimized.
☀️ Try build successful - checks-actions |
@rustbot labels -I-lang-nominated We discussed this in the T-lang meeting today. There was some support for this, but people were concerned about whether there may be legitimate use cases and what we would be suggesting that those people do instead. For wide pointer comparisons, we suggest that people use The consensus was that seeing use cases would help, and that a crater run would help to find those use cases. That's now happening in the comments above. Once the crater run comes back and has been analyzed, please renominate for T-lang discussion. For the analysis, we're looking to find 1) use cases of this that are correct in the sense that they rely only on the actual semantics, and 2) the prevalence of bugs where people are using this in ways that rely on semantics that do not actually hold. |
There's no such thing, comparing Rust functions for equality is in general just inherently meaningless. That doesn't make code using The flip side of this is that the standard library itself actually relies on such a comparison in the format-args machinery... it's for a special case (a monomorphic function) where the current implementation AFAIK does not generate duplicates (but I don't know how multi-CGU handling works so I am not sure). It's certainly not guaranteed by the language. |
@craterbot check |
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
One thought expressed in the meeting was as follows: Comparing functions for equality via pointers may yield false negatives but not false positives. The fact that two functions may compare unequal (based on pointers to them) but in fact do the same thing is a rather fundamental property of not just Rust, but of any language. In general, it's impossible to know whether two different functions may in fact do the same thing. In this light, maybe it's OK that these comparisons produce false negatives, and maybe there exist valid use cases that only rely on the property that we will not return false positives. |
That's not true. There are both false negatives and false positives. That's exactly why I wanted the lint description to be clear about this. False positives arise when LLVM merges two functions because they optimized to the same code. |
02d32a1
to
e621bba
Compare
@rfcbot reviewed |
rfcbot doesn't seem to have registered my checkbox, so @rfcbot reviewed |
Expand `ptr::fn_addr_eq()` documentation. * Describe more clearly what is (not) guaranteed, and de-emphasize the description of rustc implementation details. * Explain what you *can* reliably use it for. Tracking issue for `ptr_fn_addr_eq`: rust-lang#129322 The motivation for this PR is that I just learned that `ptr::fn_addr_eq()` exists, read the documentation, and thought: “*I* know what this means, but someone not already familiar with how `rustc` works could be left wondering whether this is even good for anything.” Fixing that seems especially important if we’re going to recommend people use it instead of `==` (as per rust-lang#118833).
Rollup merge of rust-lang#131457 - kpreid:fnaddr, r=dtolnay Expand `ptr::fn_addr_eq()` documentation. * Describe more clearly what is (not) guaranteed, and de-emphasize the description of rustc implementation details. * Explain what you *can* reliably use it for. Tracking issue for `ptr_fn_addr_eq`: rust-lang#129322 The motivation for this PR is that I just learned that `ptr::fn_addr_eq()` exists, read the documentation, and thought: “*I* know what this means, but someone not already familiar with how `rustc` works could be left wondering whether this is even good for anything.” Fixing that seems especially important if we’re going to recommend people use it instead of `==` (as per rust-lang#118833).
This comment was marked as resolved.
This comment was marked as resolved.
e621bba
to
45152d9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nits and r=me
45152d9
to
6ec6352
Compare
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
Hooray 🎉 This is now just waiting on the FCP for @rustbot label -S-waiting-on-author +S-blocked |
This comment was marked as resolved.
This comment was marked as resolved.
6ec6352
to
33e1745
Compare
@rfcbot reviewed |
This comment was marked as resolved.
This comment was marked as resolved.
33e1745
to
2ed43d9
Compare
This is kind of a follow-up to #117758 where we added a lint against wide pointer comparisons for being ambiguous and unreliable; well function pointer comparisons are also unreliable. We should IMO follow a similar logic and warn people about it.
unpredictable_function_pointer_comparisons
warn-by-default
The
unpredictable_function_pointer_comparisons
lint checks comparison of function pointer as the operands.Example
Explanation
Function pointers comparisons do not produce meaningful result since they are never guaranteed to be unique and could vary between different code generation units. Furthermore different function could have the same address after being merged together.
This PR also uplift the very similar
clippy::fn_address_comparisons
lint, which only linted on if one of the operand was anty::FnDef
while this PR lints proposes to lint on allty::FnPtr
andty::FnDef
.@rustbot labels +I-lang-nominated
Edit: Blocked on rust-lang/libs-team#323 being accepted and it's follow-up pr