Skip to content

core_float_math: Move functions to math module #141282

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
May 21, 2025

Conversation

DJMcNab
Copy link
Contributor

@DJMcNab DJMcNab commented May 20, 2025

When these functions were added in #138087 It made a relatively common pattern for emulating these functions using an extension trait (which internally uses libm) much more fragile. If core::f32 happened to be imported by the user (to access a constant, say), then that import in the module namespace would take precedence over the f32 in the type namespace for resolving these functions, running headfirst into the stability attribute.

We ran into this in Color and chose to release the remedial 0.3.1 and 0.2.4, to allow downstream crates to build on docs.rs.

As these methods are perma-unstable, moving them into a new module should not have any long-term concerns, and ensures that this "breakage" doesn't adversely impact anyone else.

I believe that I've made the module unstable correctly. I presume that this does not require a test to make sure stable code can't depend on the module existing?

I've left the stability attribute on each function - happy to tweak this if a different pattern is more correct.

Tracking issue for core_float_math: #137578.
This PR is as requested in #138087.

r? @tgross35

Recommended reviewing with whitespace hidden.

(This is my first PR to std/core/this repository, as far as I can remember)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 20, 2025
@@ -1,3 +1,4 @@
use core::f64;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, these functions were actually testing the inherent methods on f64, as defined in std, rather than the functions from the f64 module.

Practically, that's the same thing, but the use of UFCS made that not clear.

Copy link
Contributor

Choose a reason for hiding this comment

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

While you're here, mind making the couple of below imports also come from core here rather than std? That would make it match f32.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure - that's written (but not yet committed). One thing to note is that these tests do still depend on std, in particular for the powf function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, hopefully we can get that one in soon :)

Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

Two small requests, the rest looks good. Please also squash, then r=me.

Thank you for jumping on this!

@@ -1,3 +1,4 @@
use core::f64;
Copy link
Contributor

Choose a reason for hiding this comment

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

While you're here, mind making the couple of below imports also come from core here rather than std? That would make it match f32.

@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 20, 2025
@DJMcNab DJMcNab force-pushed the core-float-math-math branch from a51161b to 8d2f63d Compare May 20, 2025 14:38
@DJMcNab
Copy link
Contributor Author

DJMcNab commented May 20, 2025

r? @tgross35

@rustbot
Copy link
Collaborator

rustbot commented May 20, 2025

Requested reviewer is already assigned to this pull request.

Please choose another assignee.

@DJMcNab DJMcNab requested a review from tgross35 May 20, 2025 14:40
@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 May 20, 2025
@DJMcNab DJMcNab changed the title core_float_math: Move functions to math folder core_float_math: Move functions to math module May 20, 2025
@DJMcNab DJMcNab force-pushed the core-float-math-math branch from 8d2f63d to bdc66a4 Compare May 20, 2025 14:46
@rust-log-analyzer

This comment has been minimized.

When these functions were added in
rust-lang#138087
It made a relatively common pattern for emulating
these functions using an extension trait (which
internally uses `libm`) much more fragile.
If `core::f32` happened to be imported by the user
(to access a constant, say), then that import in
the module namespace would take precedence over
`f32` in the type namespace for resolving these
functions, running headfirst into the stability
attribute.

We ran into this in Color -
https://github.com/linebender/color - and chose to
release the remedial 0.3.1 and 0.2.4, to allow
downstream crates to build on `docs.rs`.
As these methods are perma-unstable, moving them
into a new module should not have any long-term
concerns, and ensures that this breakage doesn't
adversely impact anyone else.
@DJMcNab DJMcNab force-pushed the core-float-math-math branch from bdc66a4 to f6709bb Compare May 20, 2025 15:55
@tgross35
Copy link
Contributor

Thank you!

@bors r+

@bors
Copy link
Collaborator

bors commented May 20, 2025

📌 Commit f6709bb has been approved by tgross35

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 20, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request May 20, 2025
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#140972 (Add TRACING_ENABLED to Machine and add enter_trace_span!())
 - rust-lang#141282 (`core_float_math`: Move functions to `math` module)
 - rust-lang#141288 (Get rid of unnecessary `BufDisplay` abstraction)
 - rust-lang#141289 (use `Self` alias in self types rather than manually substituting it)
 - rust-lang#141291 (link tracking issue in explicit-extern-abis.md)
 - rust-lang#141294 (triagebot: ping me if rustdoc js is modified)
 - rust-lang#141303 (Fix pagetoc inactive color in rustc book)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 8e1ce20 into rust-lang:master May 21, 2025
6 checks passed
@rustbot rustbot added this to the 1.89.0 milestone May 21, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request May 21, 2025
Rollup merge of rust-lang#141282 - DJMcNab:core-float-math-math, r=tgross35

`core_float_math`: Move functions to `math` module

When these functions were added in rust-lang#138087 It made a relatively common pattern for emulating these functions using an extension trait (which internally uses `libm`) much more fragile. If `core::f32` happened to be imported by the user (to access a constant, say), then that import in the module namespace would take precedence over the `f32` in the type namespace for resolving these functions, running headfirst into the stability attribute.

We ran into this in [Color](https://github.com/linebender/color) and chose to release the remedial 0.3.1 and 0.2.4, to allow downstream crates to build on `docs.rs`.

As these methods are perma-unstable, moving them into a new module should not have any long-term concerns, and ensures that this "breakage" doesn't adversely impact anyone else.

I believe that I've made the module unstable correctly. I presume that this does not require a test to make sure stable code can't depend on the module existing?

I've left the stability attribute on each function - happy to tweak this if a different pattern is more correct.

Tracking issue for `core_float_math`: rust-lang#137578.
This PR is as requested in rust-lang#138087.

r? `@tgross35`

Recommended reviewing with whitespace hidden.

(This is my first PR to `std/core`/this repository, as far as I can remember)
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-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.

5 participants