Skip to content

Improve Ord violation help#128273

Merged
bors merged 6 commits into
rust-lang:masterfrom
Voultapher:improve-ord-violation-help
Aug 10, 2024
Merged

Improve Ord violation help#128273
bors merged 6 commits into
rust-lang:masterfrom
Voultapher:improve-ord-violation-help

Conversation

@Voultapher

Copy link
Copy Markdown
Contributor

Recent experience in #128083 showed that the panic message when an Ord violation is detected by the new sort implementations can be confusing. So this PR aims to improve it, together with minor bug fixes in the doc comments for sort*, sort_unstable* and select_nth_unstable*.

Is it possible to get these changes into the 1.81 release? It doesn't change behavior and would greatly help when users encounter this panic for the first time, which they may after upgrading to 1.81.

Tagging @orlp

The new sort implementations have the ability to
detect Ord violations in many cases. This commit
improves the message in a way that should help
users realize what went wrong in their program.
…ble*

- Move panic information into # Panics section
- Fix mentions of T: Ord that should be compare
- Add missing information
@Voultapher Voultapher changed the title Improve ord violation help Improve Ord violation help Jul 27, 2024
@rustbot

rustbot commented Jul 27, 2024

Copy link
Copy Markdown
Collaborator

r? @joboet

rustbot has assigned @joboet.
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 Jul 27, 2024
@compiler-errors compiler-errors added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Jul 27, 2024
Comment thread library/core/src/slice/sort/shared/smallsort.rs
Comment thread library/core/src/slice/mod.rs
@tgross35

tgross35 commented Jul 27, 2024

Copy link
Copy Markdown
Contributor

Thanks for the followup, this will be great to have.

For all the uses of "total order", could we link somewhere? Either the Wikipedia page, or link somewhere on the Ord page.

Maybe Ord should just get a "total order" section that shows an example incorrect implementation since it's not always easy to compare code to equations.

@workingjubilee workingjubilee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already anticipate someone is going to read this note and say "but that can't happen because K is Ord and Ord is a total order". This should be clearer about that case.

Comment thread library/alloc/src/slice.rs Outdated

@workingjubilee workingjubilee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, noticed some typos also.

Comment thread library/core/src/slice/sort/shared/smallsort.rs Outdated
Comment thread library/core/src/slice/sort/shared/smallsort.rs Outdated
Comment thread library/core/src/slice/sort/shared/smallsort.rs Outdated
Comment thread library/core/src/slice/sort/shared/smallsort.rs Outdated
@Voultapher

Copy link
Copy Markdown
Contributor Author

Thanks for the followup, this will be great to have.

For all the uses of "total order", could we link somewhere? Either the Wikipedia page, or link somewhere on the Ord page.

Maybe Ord should just get a "total order" section that shows an example incorrect implementation since it's not always easy to compare code to equations.

Good idea, currently the Ord docs links to wikipedia, we could do the same. That said doing so might seem a little intimidating to users unfamiliar with the matter. I like the idea of having concrete code that demonstrates the ideas at play.

@Voultapher Voultapher changed the title Improve Ord violation help Improve Ord violation help Jul 31, 2024
- Use if the implementation of [`Ord`] for `T`
  language
- Link to total order wiki page
- Rework total order help and examples
- Improve language to be more precise and less
  prone to misunderstandings.
- Fix usage of `sort_unstable_by` in `sort_by`
  example
- Fix missing author mention
- Use more consistent example input for sort
- Use more idiomatic assert_eq! in examples
- Use more natural "comparison function" language
  instead of "comparator function"
@Amanieu Amanieu added the beta-accepted Accepted for backporting to the compiler in the beta channel. label Jul 31, 2024
@Voultapher

Copy link
Copy Markdown
Contributor Author

Changes since last review:

  • Use if the implementation of [Ord] for T
    language
  • Link to total order wiki page
  • Rework total order help and examples
  • Improve language to be more precise and less
    prone to misunderstandings.
  • Fix usage of sort_unstable_by in sort_by
    example
  • Fix missing author mention
  • Use more consistent example input for sort
  • Use more idiomatic assert_eq! in examples
  • Use more natural "comparison function" language
    instead of "comparator function"
  • Hide sort module

@Voultapher

Voultapher commented Aug 1, 2024

Copy link
Copy Markdown
Contributor Author

There are something like 10 places that document sort usage, so chances are I missed something, please carefully review for typos and mistakes.

@Voultapher

Voultapher commented Aug 1, 2024

Copy link
Copy Markdown
Contributor Author

I've also started making changes to the Ord docs to better help people understand how to correctly derive and implement them, but that has grown out of scope and I plan on creating a separate PR for it. The sort docs link people to relevant information and surface some of it where sensible, but it's a tricky balance between too little information and linking to the relevant places and overloading and duplicating information in the sort docs. I hope the current iteration strikes a decent balance between the two.

@workingjubilee workingjubilee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. I noticed some remaining nits. They're just typos or non-blocking concerns because the original wording here was signed-off-on by libs already and they don't necessarily relate directly to the concern of the "total order" requirement.

Comment thread library/alloc/src/slice.rs
Comment thread library/alloc/src/slice.rs
Comment thread library/alloc/src/slice.rs Outdated
Comment thread library/alloc/src/slice.rs Outdated
Comment thread library/alloc/src/slice.rs Outdated
Comment thread library/alloc/src/slice.rs Outdated
Comment thread library/alloc/src/slice.rs
Comment thread library/core/src/slice/mod.rs Outdated
@joboet joboet assigned workingjubilee and unassigned joboet Aug 1, 2024
@Voultapher

Copy link
Copy Markdown
Contributor Author

@workingjubilee thanks for the detailed review so far.

@workingjubilee

Copy link
Copy Markdown
Member

Okie dokie! I think that everything else is stuff that we can defer to later. Thank you everyone.

@bors r+

@bors

bors commented Aug 5, 2024

Copy link
Copy Markdown
Collaborator

📌 Commit 613155c has been approved by workingjubilee

It is now in the queue for this repository.

@matthiaskrgr

Copy link
Copy Markdown
Member

@bors r-
guess this failed here: #128739 (comment)

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 6, 2024
@Voultapher

Copy link
Copy Markdown
Contributor Author

While I can't reproduce the issue with ./x.py --stage 0 test linkchecker html-checker I made small tweak based on the error message that should hopefully fix the issue.

@workingjubilee

Copy link
Copy Markdown
Member

@bors r+

@bors

bors commented Aug 9, 2024

Copy link
Copy Markdown
Collaborator

📌 Commit 1be60b5 has been approved by workingjubilee

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 9, 2024
@bors bors merged commit 65875b2 into rust-lang:master Aug 10, 2024
@rustbot rustbot added this to the 1.82.0 milestone Aug 10, 2024
@Voultapher

Copy link
Copy Markdown
Contributor Author

I see this PR hasn't made its way into the beta yet, what is needed for that?

@ChrisDenton

Copy link
Copy Markdown
Member

Patience. Merging into beta is a manual process. This PR is labelled as beta-accepted so it'll be merged (possibly along with other beta-accepted PRs) the next time someone does the backports.

@Voultapher Voultapher deleted the improve-ord-violation-help branch August 12, 2024 12:35
@cuviper cuviper modified the milestones: 1.82.0, 1.81.0 Aug 13, 2024
@cuviper cuviper removed the beta-nominated Nominated for backporting to the compiler in the beta channel. label Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beta-accepted Accepted for backporting to the compiler in the beta channel. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.