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
@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 14, 2025
@rust-bors
Copy link

rust-bors bot commented Aug 14, 2025

☀️ Try build successful (CI)
Build commit: 755ce71 (755ce7187800ec4265e0a582d1b48de6998d9916, parent: 30017c36d6b5e3382ee7cf018d330a6a4a937d39)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (755ce71): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.2%] 3
Regressions ❌
(secondary)
0.3% [0.1%, 1.7%] 19
Improvements ✅
(primary)
-1.6% [-1.6%, -1.6%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.2% [-1.6%, 0.2%] 4

Max RSS (memory usage)

Results (primary 1.2%, secondary 3.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.2% [1.0%, 1.4%] 3
Regressions ❌
(secondary)
3.2% [3.0%, 3.4%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.2% [1.0%, 1.4%] 3

Cycles

Results (primary -2.4%, secondary 1.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.2% [1.2%, 1.2%] 1
Improvements ✅
(primary)
-2.4% [-2.4%, -2.4%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.4% [-2.4%, -2.4%] 1

Binary size

Results (primary -0.1%, secondary -0.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.2%] 1
Regressions ❌
(secondary)
0.1% [0.0%, 0.2%] 6
Improvements ✅
(primary)
-0.1% [-0.2%, -0.0%] 11
Improvements ✅
(secondary)
-0.3% [-0.9%, -0.1%] 9
All ❌✅ (primary) -0.1% [-0.2%, 0.2%] 12

Bootstrap: 470.205s -> 469.279s (-0.20%)
Artifact size: 377.38 MiB -> 377.44 MiB (0.02%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Aug 14, 2025
@Randl
Copy link
Contributor Author

Randl commented Aug 15, 2025

I've removed some of these; I left arrays, NonZero, ControlFlow, slices, str, TypeId, primitives, Option, ranges, Result, and tuples. Again, all seems to be the stuff that we want to be const.

As for perf, I'm not sure how to read it and if something needs to be done? The largest changes seem to be oscillating for some time before this commit, so it may be unrelated?

@clarfonthey
Copy link
Contributor

#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.

@Randl
Copy link
Contributor Author

Randl commented Aug 15, 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 15, 2025
@@ -1012,7 +1023,7 @@ pub trait Ord: Eq + PartialOrd<Self> + PointeeSized {
#[rustc_diagnostic_item = "cmp_ord_max"]
fn max(self, other: Self) -> Self
where
Self: Sized,
Self: Sized + [const] Destruct + [const] PartialOrd,
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't [const] PartialOrd implied from the super trait bounds?

pub const fn min_by<T: [const] Destruct, F: [const] FnOnce(&T, &T) -> Ordering>(
v1: T,
v2: T,
compare: F,
Copy link
Contributor

Choose a reason for hiding this comment

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

How come F doesn't need a Destruct bound?

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 not sure? I've added const and then fixed the error messages until it compiled.

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.

9 participants