Skip to content

Fix parameter order for _by() variants of min / max/ minmax in std::cmp #139357

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

miried
Copy link

@miried miried commented Apr 4, 2025

We saw a regression introduced in version 1.86 that seems to be coming from switching the order of v1 and v2 when calling comparison functions in min_by / max_by / minmax_by (cf. this PR: #136307)

When the compare function is not symmetric in the arguments, this leads to false results. Apparently, the test cases do not cover this scenario currently. While asymmetric comparison may be an edge case, but current behavior is unexpected nevertheless.

@rustbot
Copy link
Collaborator

rustbot commented Apr 4, 2025

r? @thomcc

rustbot has assigned @thomcc.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 4, 2025
@miried miried changed the title Fix parameter order for _by() variants of min / max/ minmax i… Fix parameter order for _by() variants of min / max/ minmax in std::cmp Apr 4, 2025
@miried
Copy link
Author

miried commented Apr 22, 2025

@thomcc please let me know if you have any suggestions

@thomcc
Copy link
Member

thomcc commented Apr 24, 2025

Can you add a test covering this?

@thomcc thomcc added regression-from-stable-to-stable Performance or correctness regression from one stable version to another. I-libs-nominated Nominated for discussion during a libs team meeting. labels Apr 24, 2025
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Apr 24, 2025
@thomcc
Copy link
Member

thomcc commented Apr 24, 2025

Hi, I've nominated this because it's a behavioral change. I gather it reverts an earlier behavioral change that we made unintentionally.

@miried
Copy link
Author

miried commented Apr 29, 2025

doctest added & rebased on latest master

@Amanieu Amanieu added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. I-libs-nominated Nominated for discussion during a libs team meeting. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Apr 30, 2025
@Amanieu
Copy link
Member

Amanieu commented Apr 30, 2025

We discussed this in the libs meeting. We would like to make this a documented guarantee. Could you update the documentation to actually guarantee that the ordering is preserved when passed to the closure?

@rfcbot merge

@rfcbot
Copy link
Collaborator

rfcbot commented Apr 30, 2025

Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Apr 30, 2025
@scottmcm
Copy link
Member

scottmcm commented May 1, 2025

I just want to emphasize that violating duality like this is already explicitly called out as "a logic error" in the trait documentation, and thus anyone writing things like this should expect that their garbage in will result in garbage out in various situations.

It's ok if we want to give something stronger for these functions, since we know that they only need one check (to the extent that the _by ones use FnOnce), but this should not be considered a more general guarantee about the order in which comparisons will be done in the library.


Also, this was an intentional change, since historically lt has worked better than le, as seen by things like #106107.

Hopefully it's better now, as a bunch of work has happened in rust and LLVM to try to improve things, but...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants