Skip to content

Constify SystemTime methods #144519

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

clarfonthey
Copy link
Contributor

@clarfonthey clarfonthey commented Jul 26, 2025

This is separated out from #143949 due to the fact that it touches nontrivial system code. While the same arithmetic methods on Instant could be made const, since that type explicitly cannot be constructed at const-time, we don't bother. However, due to the fact that SystemTime::UNIX_EPOCH exists, it can be useful to create other anchors at other points in time, and thus these methods should be possible to use in const context.

Side comment: I would honestly like to just move every single implementation which is a thin wrapper over Duration or some integer clock into their own module which is tested on all targets, so that we don't have to worry about code for lower-tier targets not being tested. However, the comments in std::sys_common pointing out the desire to move things to std::sys::common confused me, particularly due to the fact that std::sys::common is taken from the hellish asterisk-import of std::sys::pal::common and others.

I think that, for trivial types like this, we should just have a platform-independent module that can be tested on all platforms, to both ensure that the code is properly tested regardless of whether higher-tier platforms needed, and to avoid the extreme mental gymnastics required to determine where in the code it lies.

However, since I'm not on any of the teams maintaining this code and am not involved, I'll settle for just copy-pasting like everyone else has been doing. I just want to express how I'm not happy about that.

Reviewer note: please only pay attention to the last commit of this PR, until its blocker is merged.

Since this depends on the previous PR:
@rustbot blocked

@rustbot
Copy link
Collaborator

rustbot commented Jul 26, 2025

r? @jhpratt

rustbot has assigned @jhpratt.
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 O-hermit Operating System: Hermit O-SGX Target: SGX O-solid Operating System: SOLID O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 26, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 26, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added the S-blocked Status: Blocked on something else such as an RFC or other implementation work. label Jul 26, 2025
@clarfonthey
Copy link
Contributor Author

(This does not, in fact, include changes to clippy. That's from the other PR and can be safely ignored.)

@rust-log-analyzer

This comment has been minimized.

@jhpratt
Copy link
Member

jhpratt commented Jul 26, 2025

I'm mildly surprised that this is even possible at compile time, but I guess it makes sense given the internals. My concern is that this would (if/when stabilized) provide a guarantee that all future targets having SystemTime would necessarily have to have it supported in const contexts, which may not be technically possible (conceivably).

But if the team is fine with this, I'm not going to get in the way. The final commit LGTM once CI passes and it's unblocked.

@jhpratt jhpratt removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 26, 2025
@clarfonthey
Copy link
Contributor Author

clarfonthey commented Jul 27, 2025

My main thought process is that we've effectively guaranteed that all targets have the ability to represent this due to the presence of UNIX_EPOCH, since not all targets will necessarily use the unix epoch as their "zero" point. (Even though a lot of them will.)

We even have a direct example of this for Windows, documented here: the epoch for Windows' FILETIME is January 1, 1601.

I also just find it very doubtful that any target would have a concept of "system time" where the units are not defined by the spec. The only theoretical target we could support now which would not fall under this system is one where:

  1. The unix epoch is the zero time.
  2. The units of time depend on the current system configuration.

Imagine such a target where, for example, the "current time" system call had configurable units: depending on the system configuration, such a system call would return either milliseconds or seconds since the epoch. That just sounds like an absolute hell to deal with when it comes to figuring out relative times it makes a lot more sense for there to just be different system calls for different units. Plus, what happens if the configuration changes while the system is running?

Like, again, it's not strictly impossible to happen, but it feels like the inability to support this kind of functionality is just the target's problem, not ours. To me, it's equivalent to a target just not having any clock functionality, which is much more likely than this, despite that also being unlikely.

(Unrelated to that, I will also definitely fix the compilation errors before this is unblocked. I definitely missed a couple feature flags & const attributes.)

@clarfonthey

This comment was marked as resolved.

@clarfonthey
Copy link
Contributor Author

clarfonthey commented Aug 1, 2025

After much pain and installing dependencies, I managed to replicate what should ensure that all the builds succeed. Each module that was modified appears to have been covered:

./x build library/std --set build.build-stage=1 --target aarch64-kmc-solid_asp3,riscv32imac-unknown-xous-elf,wasm32-wasip2,x86_64-fortanix-unknown-sgx,x86_64-pc-windows-gnu,x86_64-unknown-hermit,x86_64-unknown-none,x86_64-unknown-linux-gnu,x86_64-unknown-uefi --warnings warn

With the target list being:

  • aarch64-kmc-solid_asp3
  • riscv32imac-unknown-xous-elf
  • wasm32-wasip2
  • x86_64-fortanix-unknown-sgx
  • x86_64-pc-windows-gnu
  • x86_64-unknown-hermit
  • x86_64-unknown-linux-gnu
  • x86_64-unknown-none
  • x86_64-unknown-uefi

Xous seems to have some issues with the default build command due to a lack of panic = "unwind", but --warnings warn lets that succeed anyway.


Also to clarify: these will all be caught by the tier-2 build checks from bors, but I wanted to run them myself without clogging the bors queue.

@clarfonthey clarfonthey force-pushed the const-system-time branch 2 times, most recently from f42d938 to e1320af Compare August 10, 2025 05:14
@clarfonthey
Copy link
Contributor Author

@rustbot author

Requisite PR merged; will rebase and mark as ready.

@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-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Aug 11, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 11, 2025

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

@clarfonthey
Copy link
Contributor Author

clarfonthey commented Aug 11, 2025

@rustbot ready

Local tier-2 build test still worked, gonna assume CI will pass as well. If it doesn't then I can fix it later.

@rustbot rustbot added the S-blocked Status: Blocked on something else such as an RFC or other implementation work. label Aug 11, 2025
@clarfonthey
Copy link
Contributor Author

@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-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Aug 11, 2025
@fmease
Copy link
Member

fmease commented Aug 12, 2025

@bors r- to wait for #139806

@bors bors removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Aug 12, 2025
@clarfonthey
Copy link
Contributor Author

@rustbot blocked (again)

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 12, 2025
@jhpratt
Copy link
Member

jhpratt commented Aug 12, 2025

@bors delegate+

@bors
Copy link
Collaborator

bors commented Aug 12, 2025

✌️ @clarfonthey, you can now approve this pull request!

If @jhpratt told you to "r=me" after making some further change, please make that change, then do @bors r=@jhpratt

@bors
Copy link
Collaborator

bors commented Aug 12, 2025

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

@clarfonthey
Copy link
Contributor Author

@bors r=@jhpratt r+ rollup

@bors
Copy link
Collaborator

bors commented Aug 12, 2025

📌 Commit 7ce620d has been approved by jhpratt

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-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Aug 12, 2025
@bors
Copy link
Collaborator

bors commented Aug 12, 2025

📌 Commit 7ce620d has been approved by clarfonthey

It is now in the queue for this repository.

@jhpratt
Copy link
Member

jhpratt commented Aug 12, 2025

Just to avoid bors thinking you're approving you're own PR

@bors r- r+

@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 12, 2025
@bors
Copy link
Collaborator

bors commented Aug 12, 2025

📌 Commit 7ce620d has been approved by jhpratt

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 12, 2025
@clarfonthey
Copy link
Contributor Author

Thank you 😓

Kobzol added a commit to Kobzol/rust that referenced this pull request Aug 13, 2025
…hpratt

Constify `SystemTime` methods

This is separated out from rust-lang#143949 due to the fact that it touches nontrivial system code. While the same arithmetic methods on `Instant` could be made const, since that type explicitly cannot be constructed at const-time, we don't bother. However, due to the fact that `SystemTime::UNIX_EPOCH` exists, it can be useful to create other anchors at other points in time, and thus these methods should be possible to use in const context.

> Side comment: I would honestly like to just move every single implementation which is a thin wrapper over `Duration` or some integer clock into their own module which is tested on all targets, so that we don't have to worry about code for lower-tier targets not being tested. However, the comments in `std::sys_common` pointing out the desire to move things to `std::sys::common` confused me, particularly due to the fact that `std::sys::common` is taken from the hellish asterisk-import of `std::sys::pal::common` and others.
>
> I think that, for trivial types like this, we should just have a platform-independent module that can be tested on all platforms, to both ensure that the code is properly tested regardless of whether higher-tier platforms needed, and to avoid the extreme mental gymnastics required to determine where in the code it lies.
>
> However, since I'm not on any of the teams maintaining this code and am not involved, I'll settle for just copy-pasting like everyone else has been doing. I just want to express how I'm not happy about that.

**Reviewer note: please only pay attention to the last commit of this PR, until its blocker is merged.**

Since this depends on the previous PR:
`@rustbot` blocked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-hermit Operating System: Hermit O-SGX Target: SGX O-solid Operating System: SOLID O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface O-windows Operating system: Windows S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-clippy Relevant to the Clippy team. 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.

6 participants