-
Notifications
You must be signed in to change notification settings - Fork 19
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 ptr::fn_addr_eq
to compare functions pointers.
#323
Comments
Suggestion that came up in the @rust-lang/libs-api meeting: should this accept two pointers of the same
|
oh, maybe also accept function item and 0-capture lambda types and use the corresponding fn ptr |
Do we have any way to express that kind of type constraint? I think it would have to be a compiler builtin, either extending |
The current |
add a |
@cuviper wrote:
It wouldn't come up when translating |
Accepting any two function pointer types without guard rails sounds error prone to me. Would transmuting function pointers work?? |
For /// Compares the *addresses* of the two pointers for equality,
/// ignoring any metadata in fat pointers.
///
/// If the arguments are thin pointers of the same type,
/// then this is the same as [`eq`].
pub fn addr_eq<T: ?Sized, U: ?Sized>(p: *const T, q: *const U) -> bool { .. } |
This produced a fair bit of discussion in today's @rust-lang/lang meeting, and we didn't come to any conclusions about whether we'd recommend allowing different types or the same type in |
From the precedent of |
We discussed this again in today's @rust-lang/libs-api meeting. We definitely agreed that there are enough use cases for comparing two different types of function pointers. Thus, we'd like to accept this with the following signature: pub fn fn_addr_eq<T: FnPtr, U: FnPtr>(f: T, g: U) -> bool |
…acrum Implement `ptr::fn_addr_eq` This PR implements rust-lang/libs-team#323: `ptr::fn_addr_eq`. r? libs
…acrum Implement `ptr::fn_addr_eq` This PR implements rust-lang/libs-team#323: `ptr::fn_addr_eq`. r? libs
Rollup merge of rust-lang#129323 - Urgau:ptr_fn_addr_eq, r=Mark-Simulacrum Implement `ptr::fn_addr_eq` This PR implements rust-lang/libs-team#323: `ptr::fn_addr_eq`. r? libs
Implement `ptr::fn_addr_eq` This PR implements rust-lang/libs-team#323: `ptr::fn_addr_eq`. r? libs
…isons, r=cjgillot 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 ~~Edit: Blocked on rust-lang/libs-team#323 being accepted and it's follow-up pr~~
…isons, r=cjgillot 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 ~~Edit: Blocked on rust-lang/libs-team#323 being accepted and it's follow-up pr~~
…isons, r=cjgillot 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 ~~Edit: Blocked on rust-lang/libs-team#323 being accepted and it's follow-up pr~~
Rollup merge of rust-lang#118833 - Urgau:lint_function_pointer_comparisons, r=cjgillot 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 ~~Edit: Blocked on rust-lang/libs-team#323 being accepted and it's follow-up pr~~
…cjgillot Add lint against function pointer comparisons This is kind of a follow-up to rust-lang/rust#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 ~~Edit: Blocked on rust-lang/libs-team#323 being accepted and it's follow-up pr~~
…cjgillot Add lint against function pointer comparisons This is kind of a follow-up to rust-lang/rust#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 ~~Edit: Blocked on rust-lang/libs-team#323 being accepted and it's follow-up pr~~
Proposal
Problem statement
As discussed in the 2024-01-03 T-lang triage meeting, equality for function pointers is awkward (due to "equal" functions being merged together or duplicated across different CGUs).
Motivating examples or use cases
Solution sketch
In
core::ptr
andstd::ptr
:Alternatives
Links and related work
rust-lang/rust#118833 a lint I'm trying to add to warn against functions pointers and T-lang decided that they want to recommend something that won't warn, similar to
ptr::addr_eq
for thin pointer comparisons.What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: