Fallback {float}
to f32
when f32: From<{float}>
and add impl From<f16> for f32
by beetrees · Pull Request #139087 · rust-lang/rust (original) (raw)
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
rustbot added S-waiting-on-review
Status: Awaiting review from the assignee but also interested parties.
Relevant to the compiler team, which will review and decide on the PR/issue.
Relevant to the library team, which will review and decide on the PR/issue.
labels
Relevant to the language team, which will review and decide on the PR/issue.
Relevant to the library API team, which will review and decide on the PR/issue.
Relevant to the types team, which will review and decide on the PR/issue.
Nominated for discussion during a lang team meeting.
`#![feature(f16)]`, `#![feature(f128)]`
labels
bors added a commit to rust-lang-ci/rust that referenced this pull request
Fallback {float}
to f32
when f32: From<{float}>
and add impl From<f16> for f32
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 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