Skip to content

Constify Eq, Ord, PartialOrd #144847

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
Open

Constify Eq, Ord, PartialOrd #144847

wants to merge 1 commit into from

Conversation

Randl
Copy link
Contributor

@Randl Randl commented Aug 3, 2025

Adds #[const_trait] and impls for Eq, Ord, PartialOrd. Impl for some other traits (e.g., slices and arrays) are blocked mainly on const closures (#106003).
For TypeId Ord we need const pointer comparison (#53020)
Tracking issue #143800

@rustbot
Copy link
Collaborator

rustbot commented Aug 3, 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-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 3, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 3, 2025

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

Squash this into one commit please

@rustbot author

@rustbot rustbot 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-review Status: Awaiting review from the assignee but also interested parties. labels Aug 3, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 3, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@Randl
Copy link
Contributor Author

Randl commented Aug 3, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 3, 2025
@ShoyuVanilla
Copy link
Member

Not a review but just a question as I don't know the contexts very well 😅
I guess PartialEq and Eq could be const impl'd on tuples as well?

@rust-log-analyzer

This comment has been minimized.

@Randl
Copy link
Contributor Author

Randl commented Aug 8, 2025

Fair enough, I've added impl const for tuples

@compiler-errors
Copy link
Member

Sorry for taking a long time to review this. I'm actually somewhat uncomfortable by the scope of this PR. Could you please limit the number of types that this adds const impls for to just types that seem relevant for const programming?

Specifically, things like:

#[derive(Copy, Debug)]
#[derive_const(Clone, PartialEq, Eq)]
pub struct AllocError;

Don't seem really relevant here. I think this constification could probably just stick to constifying some impls for built-in types and other important types, and not just try to constify everything that can be constified in the standard library.

Also, since this adds new bounds to things, let's run a perf test.

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Aug 14, 2025
Constify Eq, Ord, PartialOrd
@compiler-errors
Copy link
Member

@rustbot author

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 14, 2025
@fee1-dead
Copy link
Member

#143949 also caused some minor perf regressions, but so far nobody from wg-const-eval has commented on whether this is considered acceptable or not. Right now my understanding is that the goal is to constify everything, but there may need to be some changes with consteval since my suspicion is that the built-in stuff to allow these operators to work even without trait impls on primitives may now be slower with the const trait impls present and enabled.

Note that regressions on the PR you linked was ultimately triaged at the rollup.

Those negative perf results are due to making built-in traits const, which then increases the times where we have to check const conditions of traits, find implied predicates (super traits), etc. They're pretty much unavoidable, there's nothing much we can do, and we'll have to stick with it if we want libstd constified.

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

Some leftover syntax, otherwise lgtm

@rustbot
Copy link
Collaborator

rustbot commented Aug 16, 2025

This PR was rebased onto a different master commit! Check out the changes with our range-diff.

@oli-obk
Copy link
Contributor

oli-obk commented Aug 17, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 17, 2025

📌 Commit f70c98f has been approved by oli-obk

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-review Status: Awaiting review from the assignee but also interested parties. labels Aug 17, 2025
@jieyouxu
Copy link
Member

@bors p=4 (rollup interleaving)

bors added a commit that referenced this pull request Aug 19, 2025
Constify Eq, Ord, PartialOrd

Adds `#[const_trait]` and impls for `Eq`, `Ord`, `PartialOrd`. Impl for some other traits (e.g., slices and arrays) are blocked mainly on const closures (#106003).
For TypeId Ord we need const pointer comparison (#53020)
Tracking issue #143800
@bors
Copy link
Collaborator

bors commented Aug 19, 2025

⌛ Testing commit f70c98f with merge 953f713...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Aug 19, 2025

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 19, 2025
@clarfonthey
Copy link
Contributor

clarfonthey commented Aug 20, 2025

Appears to be a legitimate codegen failure related to Ord, but I'm not exactly sure why there'd be a regression here. Might just fix itself with a rebase.

@Randl
Copy link
Contributor Author

Randl commented Aug 20, 2025

Ok. It is a legitimate codegen failure; I can reproduce it locally, including after rebase. I do not understand what is specifically wrong with the codegen (cc @scottmcm ), but the reason appears to be this change:

https://github.com/rust-lang/rust/pull/144847/files#diff-b3bc3e9849f0b9690cf061db39e229a23495e0ff8dd3eded8e2ac5d9bb3eb7a7L215-R232

I'm unsure why the difference between the while loop and the for loop results in incorrect codegen. I can revert this specific change for now (it doesn't give us slice Ord anyway because that requires const closures, for example, in

let len_chain = |a: &_, b: &_| ControlFlow::Break(usize::partial_cmp(a, b));

) until we either figure out how to make the while loop work with codegen, or we can use for in const code (though if I understand correctly, this will require iterators, which by themselves will probably depend on this code).

@bors
Copy link
Collaborator

bors commented Aug 20, 2025

☔ The latest upstream changes (presumably #145644) made this pull request unmergeable. Please resolve the merge conflicts.

@Randl
Copy link
Contributor Author

Randl commented Aug 24, 2025

Ok I've pushed the version that drops the const-hack in slice code. cc @oli-obk

@rustbot
Copy link
Collaborator

rustbot commented Aug 24, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@clarfonthey
Copy link
Contributor

Since this was removed from the queue, could you also replace the references to the const_from feature with const_convert so it no longer conflicts with my PR? You don't need to fix it everywhere, just on the traits you make const for this PR.

@jhpratt
Copy link
Member

jhpratt commented Aug 24, 2025

@bors p=0

No longer needed to thread between rollups, as the queue is back to normal now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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.