Skip to content

Add const support for float rounding methods #141521

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

Merged
merged 1 commit into from
Jun 1, 2025

Conversation

ruancomelli
Copy link
Contributor

@ruancomelli ruancomelli commented May 24, 2025

Add const support for float rounding methods

This PR makes the following float rounding methods const:

  • f64::{floor, ceil, trunc, round, round_ties_even}
  • and the corresponding methods for f16, f32 and f128

Tracking issue: #141555

Procedure

I followed c09ed3e as closely as I could in making float methods const, and also received great guidance from https://internals.rust-lang.org/t/const-rounding-methods-in-float-types/22957/3?u=ruancomelli.

Note

This is my first code contribution to the Rust project, so please let me know if I missed anything - I'd be more than happy to revise and learn more. Thank you for taking the time to review it!

@rustbot
Copy link
Collaborator

rustbot commented May 24, 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 May 24, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 24, 2025

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

The Miri subtree was changed

cc @rust-lang/miri

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

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

@RalfJung
Copy link
Member

However, I could have implemented a single one taking a rustc_apfloat::Round parameter:

Yes, that's a lot better I think. :)

@RalfJung
Copy link
Member

Note that your branch has conflicts with the master branch. Not sure how that happened given that the PR is fresh, but it means you'll have to rebase and resolve those conflicts.

@ruancomelli ruancomelli force-pushed the const-float-rounding branch from bed4525 to 372e4cd Compare May 24, 2025 21:49
@rustbot

This comment has been minimized.

@ruancomelli
Copy link
Contributor Author

Strange, CI is failing with intrinsic is not supported at compile-time for each one of the new methods.
I'm looking into that.

@ruancomelli
Copy link
Contributor Author

ruancomelli commented May 25, 2025

As far as I can see, the main difference between the newly const methods (e.g. floor) and the ones that already work (e.g. abs) is the attributes. So my current hypothesis is that I got them wrong.

Copy link
Contributor

@CAD97 CAD97 left a comment

Choose a reason for hiding this comment

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

AIUI, the reason you're hitting "intrinsic not supported at compile time" is that std currently gets built with the bootstrap compiler, which is then used when building the new compiler. So you need to have a #[cfg(bootstrap)] version of the function/intrinsic which isn't const and a #[cfg(not(bootstrap))] which is.

IIUC, we're in the process of flipping this so that the compiler gets built with the bootstrap std, and std always gets built with the new compiler. Once that switch happens, this won't be necessary anymore.

@RalfJung
Copy link
Member

AIUI, the reason you're hitting "intrinsic not supported at compile time" is that std currently gets built with the bootstrap compiler, which is then used when building the new compiler. So you need to have a #[cfg(bootstrap)] version of the function/intrinsic which isn't const and a #[cfg(not(bootstrap))] which is.

The problem occurs when building the tests. So you don't need to do any cfg in libcore itself, but the new tests in library/coretests need a #[cfg(not(bootstrap))].

@RalfJung
Copy link
Member

RalfJung commented May 25, 2025 via email

@rust-log-analyzer

This comment has been minimized.

@ruancomelli
Copy link
Contributor Author

Hey @RalfJung and @CAD97, I've addressed or replied to all of your review comments.

I've also created a tracking issue for this feature here: #141555.

I have two main open questions now.

First one is: should the rounding methods on f16 and f128 be gated behind #[rustc_const_unstable(feature = {"f16", "f128"}, issue = "116909")] (from #116909) or #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]? I think it should be the former one, but I'm not sure.

The second question: I added #![feature(const_float_round_methods)] to library/std/src/lib.rs, which was suggested by the compiler itself. Without it, ./x check results in the following errors:

error: `core::f32::math::floor` is not yet stable as a const fn
  --> library/std/src/f32.rs:50:9
   |
50 |         core::f32::math::floor(self)
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: add `#![feature(const_float_round_methods)]` to the crate attributes to enable
  --> library/std/src/lib.rs:433:1
   |
433+ #![feature(const_float_round_methods)]
   |

... and similar for the other rounding methods

error: could not compile `std` (lib) due to 12 previous errors

This is similar to what was done in the reference PR, even though they changed library/core/src/lib.rs (notice core and not std).
However, I'm not sure if this is the correct way to proceed. I read the dev guide book, but that question remained unanswered.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member

RalfJung commented May 25, 2025

First one is: should the rounding methods on f16 and f128 be gated behind #[rustc_const_unstable(feature = {"f16", "f128"}, issue = "116909")] (from #116909) or #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]? I think it should be the former one, but I'm not sure.

Good question! The typical thing we do is to gate it behind the f16/f128 feature, but then also have a second commented out more specific feature gate:

#[rustc_const_unstable(feature = "f16", issue = "116909")]
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
pub const fn trunc(self) -> self { ... }

The second question: I added #![feature(const_float_round_methods)] to library/std/src/lib.rs, which was suggested by the compiler itself.

Yes, that's fine.
Thanks for asking for confirmation. :)

@RalfJung
Copy link
Member

@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 May 25, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 25, 2025

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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 29, 2025
@RalfJung
Copy link
Member

This PR still has 20 commits -- if you squashed something, you didn't actually push it?

@ruancomelli ruancomelli force-pushed the const-float-rounding branch 2 times, most recently from d7cadf9 to a1d9cc4 Compare May 29, 2025 16:55
@ruancomelli
Copy link
Contributor Author

Hey @RalfJung!

Actually, I hadn't seen your comment #141521 (comment) when I posted #141521 (comment). I just meant to let you know that I had addressed all other comments and had the CI passing.

I have now squashed all commits into a single one as well. I didn't see value in keeping more than one commit in this PR. I again followed #130568 to make this decision.

Please let me know if you would like me to try and split the final commit into multiple ones.

And thank you so much for your patience and constant feedback in this PR! It really made the process much easier, and I am very happy with the end result.
Also, sorry for the friction... this is my first code contribution to the Rust project 😀

@rustbot ready

@tgross35
Copy link
Contributor

Fwiw usually "squash" actually means "fixup", it's not too useful to keep in the messages that you had to do things like "adjust after rebase" and "fix float tests" at some point :) Not that it's much of a problem of course.

@RalfJung
Copy link
Member

And thank you so much for your patience and constant feedback in this PR! It really made the process much easier, and I am very happy with the end result.
Also, sorry for the friction... this is my first code contribution to the Rust project

No worries! It's all good. Thanks for your first PR, and congrats on pushing it through to the end. :)

@bors r+

@bors
Copy link
Collaborator

bors commented May 29, 2025

📌 Commit a1d9cc4 has been approved by RalfJung

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

bors commented May 30, 2025

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

@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 May 30, 2025
Add const support for the float rounding methods floor, ceil, trunc,
fract, round and round_ties_even.
This works by moving the calculation logic from

     src/tools/miri/src/intrinsics/mod.rs

into

     compiler/rustc_const_eval/src/interpret/intrinsics.rs.

All relevant method definitions were adjusted to include the `const`
keyword for all supported float types: f16, f32, f64 and f128.

The constness is hidden behind the feature gate

     feature(const_float_round_methods)

which is tracked in

     rust-lang#141555

This commit is a squash of the following commits:
- test: add tests that we expect to pass when float rounding becomes const
- feat: make float rounding methods `const`
- fix: replace `rustc_allow_const_fn_unstable(core_intrinsics)` attribute with `#[rustc_const_unstable(feature = "f128", issue = "116909")]` in `library/core/src/num/f128.rs`
- revert: undo update to `library/stdarch`
- refactor: replace multiple `float_<mode>_intrinsic` rounding methods with a single, parametrized one
- fix: add `#[cfg(not(bootstrap))]` to new const method tests
- test: add extra sign tests to check `+0.0` and `-0.0`
- revert: undo accidental changes to `round` docs
- fix: gate `const` float round method behind `const_float_round_methods`
- fix: remove unnecessary `#![feature(const_float_methods)]`
- fix: remove unnecessary `#![feature(const_float_methods)]` [2]
- revert: undo changes to `tests/ui/consts/const-eval/float_methods.rs`
- fix: adjust after rebase
- test: fix float tests
- test: add tests for `fract`
- chore: add commented-out `const_float_round_methods` feature gates to `f16` and `f128`
- fix: adjust NaN when rounding floats
- chore: add FIXME comment for de-duplicating float tests
- test: remove unnecessary test file `tests/ui/consts/const-eval/float_methods.rs`
- test: fix tests after upstream simplification of how float tests are run
@ruancomelli ruancomelli force-pushed the const-float-rounding branch from a1d9cc4 to f8e97ba Compare May 31, 2025 18:34
@RalfJung
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented May 31, 2025

📌 Commit f8e97ba has been approved by RalfJung

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 May 31, 2025
jhpratt added a commit to jhpratt/rust that referenced this pull request May 31, 2025
…r=RalfJung

Add `const` support for float rounding methods

# Add `const` support for float rounding methods

This PR makes the following float rounding methods `const`:

- `f64::{floor, ceil, trunc, round, round_ties_even}`
- and the corresponding methods for `f16`, `f32` and `f128`

Tracking issue: rust-lang#141555

## Procedure

I followed rust-lang@c09ed3e as closely as I could in making float methods `const`, and also received great guidance from https://internals.rust-lang.org/t/const-rounding-methods-in-float-types/22957/3?u=ruancomelli.

## Note

This is my first code contribution to the Rust project, so please let me know if I missed anything - I'd be more than happy to revise and learn more. Thank you for taking the time to review it!
bors added a commit that referenced this pull request Jun 1, 2025
Rollup of 6 pull requests

Successful merges:

 - #141072 (Stabilize feature `result_flattening`)
 - #141215 (std: clarify Clone trait documentation about duplication semantics)
 - #141277 (Miri CI: test aarch64-apple-darwin in PRs instead of the x86_64 target)
 - #141521 (Add `const` support for float rounding methods)
 - #141812 (Fix "consider borrowing" for else-if)
 - #141832 (library: explain TOCTOU races in `fs::remove_dir_all`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ac49339 into rust-lang:master Jun 1, 2025
9 checks passed
@rustbot rustbot added this to the 1.89.0 milestone Jun 1, 2025
rust-timer added a commit that referenced this pull request Jun 1, 2025
Rollup merge of #141521 - ruancomelli:const-float-rounding, r=RalfJung

Add `const` support for float rounding methods

# Add `const` support for float rounding methods

This PR makes the following float rounding methods `const`:

- `f64::{floor, ceil, trunc, round, round_ties_even}`
- and the corresponding methods for `f16`, `f32` and `f128`

Tracking issue: #141555

## Procedure

I followed c09ed3e as closely as I could in making float methods `const`, and also received great guidance from https://internals.rust-lang.org/t/const-rounding-methods-in-float-types/22957/3?u=ruancomelli.

## Note

This is my first code contribution to the Rust project, so please let me know if I missed anything - I'd be more than happy to revise and learn more. Thank you for taking the time to review it!
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Jun 1, 2025
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#141072 (Stabilize feature `result_flattening`)
 - rust-lang/rust#141215 (std: clarify Clone trait documentation about duplication semantics)
 - rust-lang/rust#141277 (Miri CI: test aarch64-apple-darwin in PRs instead of the x86_64 target)
 - rust-lang/rust#141521 (Add `const` support for float rounding methods)
 - rust-lang/rust#141812 (Fix "consider borrowing" for else-if)
 - rust-lang/rust#141832 (library: explain TOCTOU races in `fs::remove_dir_all`)

r? `@ghost`
`@rustbot` modify labels: rollup
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Jun 3, 2025
…r=RalfJung

Add `const` support for float rounding methods

# Add `const` support for float rounding methods

This PR makes the following float rounding methods `const`:

- `f64::{floor, ceil, trunc, round, round_ties_even}`
- and the corresponding methods for `f16`, `f32` and `f128`

Tracking issue: rust-lang#141555

## Procedure

I followed rust-lang@c09ed3e as closely as I could in making float methods `const`, and also received great guidance from https://internals.rust-lang.org/t/const-rounding-methods-in-float-types/22957/3?u=ruancomelli.

## Note

This is my first code contribution to the Rust project, so please let me know if I missed anything - I'd be more than happy to revise and learn more. Thank you for taking the time to review it!
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Jun 3, 2025
Rollup of 6 pull requests

Successful merges:

 - rust-lang#141072 (Stabilize feature `result_flattening`)
 - rust-lang#141215 (std: clarify Clone trait documentation about duplication semantics)
 - rust-lang#141277 (Miri CI: test aarch64-apple-darwin in PRs instead of the x86_64 target)
 - rust-lang#141521 (Add `const` support for float rounding methods)
 - rust-lang#141812 (Fix "consider borrowing" for else-if)
 - rust-lang#141832 (library: explain TOCTOU races in `fs::remove_dir_all`)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

8 participants