-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
Conversation
r? @ibraheemdev rustbot has assigned @ibraheemdev. Use |
Some changes occurred to the CTFE machinery The Miri subtree was changed cc @rust-lang/miri Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri |
Yes, that's a lot better I think. :) |
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. |
bed4525
to
372e4cd
Compare
This comment has been minimized.
This comment has been minimized.
Strange, CI is failing with intrinsic is not supported at compile-time for each one of the new methods. |
As far as I can see, the main difference between the newly |
There was a problem hiding this 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.
The problem occurs when building the tests. So you don't need to do any |
Your functions are not meant to be stable, right? So do the part under "help: if the function is not (yet) meant to be exposed to stable". The other part explicitly says "as a last resort" and "requires team approval"... I don't know how much more scary we have to make that message to stop people from using the attribute. :(
Maybe we should just stop mentioning it entirely.
|
This comment has been minimized.
This comment has been minimized.
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 The second question: I added
This is similar to what was done in the reference PR, even though they changed library/core/src/lib.rs (notice |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Good question! The typical thing we do is to gate it behind the #[rustc_const_unstable(feature = "f16", issue = "116909")]
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
pub const fn trunc(self) -> self { ... }
Yes, that's fine. |
@rustbot author |
Reminder, once the PR becomes ready for a review, use |
This PR still has 20 commits -- if you squashed something, you didn't actually push it? |
d7cadf9
to
a1d9cc4
Compare
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. @rustbot ready |
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. |
No worries! It's all good. Thanks for your first PR, and congrats on pushing it through to the end. :) @bors r+ |
☔ The latest upstream changes (presumably #141739) made this pull request unmergeable. Please resolve the merge conflicts. |
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
a1d9cc4
to
f8e97ba
Compare
@bors r+ |
…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!
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
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!
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
…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!
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
Add
const
support for float rounding methodsThis PR makes the following float rounding methods
const
:f64::{floor, ceil, trunc, round, round_ties_even}
f16
,f32
andf128
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!