Skip to content

Fix Ord, Eq and Hash implementation of panic::Location #144510

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

orlp
Copy link
Contributor

@orlp orlp commented Jul 26, 2025

Fixes #144486.

Now properly compares/hashes the filename rather than the pointer to the string.

@rustbot
Copy link
Collaborator

rustbot commented Jul 26, 2025

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
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 26, 2025
#[stable(feature = "panic_hooks", since = "1.10.0")]
impl PartialEq for Location<'_> {
fn eq(&self, other: &Self) -> bool {
self.file() == other.file() && self.line == other.line && self.col == other.col
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth flipping the order here? Comparing the filename is expensive, but in 99% of cases the line + column will mismatch on different locations I'd guess.

Copy link
Contributor Author

@orlp orlp Jul 27, 2025

Choose a reason for hiding this comment

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

I think that's fair enough for equality. What about the ordering? You could apply the same efficiency argument there although I think people would expect the ordering to be the filename > line > col.

EDIT: after double-checking the documentation, it very clearly states filename > line > col for both ordering and equality. However since that order isn't observable for equality I'll change this for efficiency.

self.col.hash(state);
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Can you add some test coverage for these? That would help prevent future accidental regressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm actually not 100% sure if I can? In what little tests I've written for the standard library it's been single-file-per-test.

Copy link
Contributor

Choose a reason for hiding this comment

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

You should be able to easily add some tests into library/coretests somewhere, I believe. Although you will need to have some sort of perma-unstable method to construct these that's marked #[doc(hidden)], since these are private fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

panic::Location ordering is defined by pointer ordering, not string ordering
5 participants