Skip to content

Fallback {float} to f32 when f32: From<{float}> and add impl From<f16> for f32#139087

Open
beetrees wants to merge 4 commits intorust-lang:mainfrom
beetrees:impl-from-f16-for-f32
Open

Fallback {float} to f32 when f32: From<{float}> and add impl From<f16> for f32#139087
beetrees wants to merge 4 commits intorust-lang:mainfrom
beetrees:impl-from-f16-for-f32

Conversation

@beetrees
Copy link
Contributor

@beetrees beetrees commented Mar 28, 2025

View all comments

Currently, the following code compiles:

fn foo<T: Into<f32>>(_: T) {}

fn main() {
    foo(1.0);
}

This is because the only From<{float}> impl for f32 is currently From<f32>. However, once impl From<f16> for f32 is added this is no longer the case. This would cause the float literal to fallback to f64, subsequently causing a type error as f32 does not implement From<f64>. While this kind of change to type inference isn't technically a breaking change according to Rust's breaking change policy, the previous attempt to add impl From<f16> for f32 was removed #123830 due to the large number of crates affected (by my count, there were root regressions in 42 crates and 52 GitHub repos, not including duplicates). This PR solves this problem by using f32 as the fallback type for {float} when there is a trait predicate of f32: From<{float}>. This allows adding impl From<f16> for f32 without affecting the code that currently compiles (such as the example above; this PR shouldn't affect what is possible on stable).

This PR also allows adding a future-incompatibility warning for the fallback to f32 (currently implemented in the third commit) if the lang team wants one (allowing the f32 fallback to be removed in the future); alternatively this could be expanded in the future into something more general like @tgross35 suggested in #123831 (comment). I think it would be also possible to disallow the f32 fallback in a future edition.

As expected, a crater check showed no non-spurious regressions.

For reference, I've based the implementation loosely on the existing calculate_diverging_fallback. This first commit adds the f32 fallback, the second adds impl From<f16> for f32, and the third adds a FCW lint for the f32 fallback. I think this falls under the types team, so
r? types

Fixes: #123831
Tracking issue: #116909

@rustbot label +T-lang +T-types +T-libs-api +F-f16_and_f128

To decide on whether a future-incompatibility warning is desired or otherwise (see above):
@rustbot label +I-lang-nominated

cc #154024 #154005

@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 Mar 28, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 28, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added T-lang Relevant to the language team T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. I-lang-nominated Nominated for discussion during a lang team meeting. F-f16_and_f128 `#![feature(f16)]`, `#![feature(f128)]` labels Mar 28, 2025
@beetrees beetrees force-pushed the impl-from-f16-for-f32 branch from ef59666 to bb79c28 Compare March 28, 2025 21:34
@rust-log-analyzer

This comment has been minimized.

@beetrees beetrees force-pushed the impl-from-f16-for-f32 branch from bb79c28 to afe5b7f Compare March 28, 2025 22:35
@rust-log-analyzer

This comment has been minimized.

@beetrees beetrees force-pushed the impl-from-f16-for-f32 branch from afe5b7f to f4cb5a7 Compare March 28, 2025 23:32
@compiler-errors
Copy link
Contributor

@bors try

@bors
Copy link
Collaborator

bors commented Mar 28, 2025

⌛ Trying commit f4cb5a7 with merge 3ec2a77...

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 28, 2025
Fallback `{float}` to `f32` when `f32: From<{float}>` and add `impl From<f16> for f32`

Currently, the following code compiles:
```rust
fn foo::<T: Into<f32>>(_: T) {}

fn main() {
    foo(1.0);
}
```

This is because the only `From<{float}>` impl for `f32` is currently `From<f32>`. However, once `impl From<f16> for f32` is added this is no longer the case. This would cause the float literal to fallback to `f64`, subsequently causing a type error as `f32` does not implement `From<f64>`. While this kind of change to type inference isn't technically a breaking change according to Rust's breaking change policy, the previous attempt to add `impl From<f16> for f32` was removed rust-lang#123830 due to the large number of crates affected (by my count, there were root regressions in 42 crates and 52 GitHub repos, not including duplicates). This PR solves this problem by using `f32` as the fallback type for `{float}` when there is a trait predicate of `f32: From<{float}>`. This allows adding `impl From<f16> for f32` without affecting the code that currently compiles (such as the example above; this PR shouldn't affect what is possible on stable).

This PR also allows adding a future-incompatibility warning if the lang team wants one; alternatively this could be expanded in the future into something more general like `@tgross35` suggested in rust-lang#123831 (comment). I think it would be also possible to disallow the `f32` fallback in a future edition.

This will need a crater check.

For reference, I've based the implementation loosely on the existing `calculate_diverging_fallback`. This first commit adds the `f32` fallback and the second adds `impl From<f16> for f32`. I think this falls under the types team, so
r? types

Fixes: rust-lang#123831
Tracking issue: rust-lang#116909

`@rustbot` label +T-lang +T-types +T-libs-api +F-f16_and_f128

To decide on whether a future-incompatibility warning is desired or otherwise (see above):
`@rustbot` label +I-lang-nominated
@bors
Copy link
Collaborator

bors commented Mar 29, 2025

☀️ Try build successful - checks-actions
Build commit: 3ec2a77 (3ec2a775459d36c7d90654e47c56ff7bb0d542ab)

@compiler-errors
Copy link
Contributor

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-139087 created and queued.
🤖 Automatically detected try build 3ec2a77
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 29, 2025
@craterbot
Copy link
Collaborator

🚧 Experiment pr-139087 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🚨 Report generation of pr-139087 failed: timed out waiting for connection
🛠️ If the error is fixed use the retry-report command.

🆘 Can someone from the infra team check in on this? @rust-lang/infra
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@compiler-errors
Copy link
Contributor

@craterbot retry-report

@craterbot
Copy link
Collaborator

🛠️ Generation of the report for pr-139087 queued again.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-139087 is completed!
📊 9 regressed and 2 fixed (605729 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Mar 30, 2025
@rust-log-analyzer

This comment has been minimized.

@folkertdev folkertdev force-pushed the impl-from-f16-for-f32 branch from cacf239 to c3ca875 Compare March 17, 2026 16:09
@rustbot
Copy link
Collaborator

rustbot commented Mar 17, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer

This comment has been minimized.

@folkertdev folkertdev force-pushed the impl-from-f16-for-f32 branch from c3ca875 to 17b8efa Compare March 17, 2026 16:18
@folkertdev
Copy link
Contributor

Based on #t-types > `From<f16> for f32` FCW lint, the types team wants to add the fallback from {float} to f32, but also keep impl From<f16> for f32 unstable, so that this fallback can be removed in the future when a better solution comes up.

For now I've kept the lint, which should make this pattern slowly disappear.

@rust-log-analyzer

This comment has been minimized.

@folkertdev folkertdev force-pushed the impl-from-f16-for-f32 branch from 17b8efa to 8a10d58 Compare March 17, 2026 19:47
Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

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

Is there a test something that ensures that f32: From<f16> doesn't hold on stable?

View changes since this review

@folkertdev folkertdev force-pushed the impl-from-f16-for-f32 branch from 8a10d58 to 48aefdc Compare March 18, 2026 00:00
@folkertdev folkertdev force-pushed the impl-from-f16-for-f32 branch from 48aefdc to 635510c Compare March 18, 2026 00:07
@jackh726
Copy link
Member

jackh726 commented Mar 18, 2026

When CI is green we should do a crater run just to verify that there is no breakage.

This probably resolves Niko's concerns here: there shouldn't be any change to stable behavior here, other than just an FCW (I'm always a fan to have tests be the first commit to show the change in behavior over subsequent commits.)

Procedurally, I'm not quite sure what to do here: lang,types,libs all FCPed something stronger (the stable impl with some commitment to fallback behavior) - though it didn't actually finish given missing checkboxes+concerns. I'm inclined to just let it ride once the boxes are checked and the concerns are resolved. This is now almost effectively a two-way door (other than lint name, but behavior-wise yes).

@jackh726 jackh726 removed the I-types-nominated Nominated for discussion during a types team meeting. label Mar 18, 2026
@traviscross
Copy link
Contributor

@bors try

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Mar 18, 2026
Fallback `{float}` to `f32` when `f32: From<{float}>` and add `impl From<f16> for f32`
@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 18, 2026

☀️ Try build successful (CI)
Build commit: 9961e55 (9961e55b3687a6d0b2bab4af0e5e81494b09be81, parent: 91775dbec9771aa0c1b9ebe268eb5bd271e79a7a)

@traviscross
Copy link
Contributor

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-139087-1 created and queued.
🤖 Automatically detected try build 9961e55
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot removed S-blocked Status: Blocked on something else such as an RFC or other implementation work. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. S-waiting-on-concerns Status: Awaiting concerns to be addressed by the author S-waiting-on-documentation Status: Waiting on approved PRs to documentation before merging S-waiting-on-t-lang Status: Awaiting decision from T-lang labels Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. F-f16_and_f128 `#![feature(f16)]`, `#![feature(f128)]` I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-crater Status: Waiting on a crater run to be completed. T-lang Relevant to the language team T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

f32::from(<untyped float>) inference with f16 and f128