Fallback {float} to f32 when f32: From<{float}> and add impl From<f16> for f32#139087
Fallback {float} to f32 when f32: From<{float}> and add impl From<f16> for f32#139087beetrees wants to merge 4 commits intorust-lang:mainfrom
{float} to f32 when f32: From<{float}> and add impl From<f16> for f32#139087Conversation
|
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
ef59666 to
bb79c28
Compare
This comment has been minimized.
This comment has been minimized.
bb79c28 to
afe5b7f
Compare
This comment has been minimized.
This comment has been minimized.
afe5b7f to
f4cb5a7
Compare
|
@bors try |
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
|
☀️ Try build successful - checks-actions |
|
@craterbot check |
|
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🚨 Report generation of 🆘 Can someone from the infra team check in on this? @rust-lang/infra |
|
@craterbot retry-report |
|
🛠️ Generation of the report for ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🎉 Experiment
|
This comment has been minimized.
This comment has been minimized.
cacf239 to
c3ca875
Compare
|
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. |
This comment has been minimized.
This comment has been minimized.
c3ca875 to
17b8efa
Compare
|
Based on #t-types > `From<f16> for f32` FCW lint, the types team wants to add the fallback from For now I've kept the lint, which should make this pattern slowly disappear. |
This comment has been minimized.
This comment has been minimized.
17b8efa to
8a10d58
Compare
8a10d58 to
48aefdc
Compare
48aefdc to
635510c
Compare
|
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). |
|
@bors try |
This comment has been minimized.
This comment has been minimized.
Fallback `{float}` to `f32` when `f32: From<{float}>` and add `impl From<f16> for f32`
|
@craterbot check |
|
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
View all comments
Currently, the following code compiles:
This is because the only
From<{float}>impl forf32is currentlyFrom<f32>. However, onceimpl From<f16> for f32is added this is no longer the case. This would cause the float literal to fallback tof64, subsequently causing a type error asf32does not implementFrom<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 addimpl From<f16> for f32was 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 usingf32as the fallback type for{float}when there is a trait predicate off32: From<{float}>. This allows addingimpl From<f16> for f32without 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 thef32fallback 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 thef32fallback 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 thef32fallback, the second addsimpl From<f16> for f32, and the third adds a FCW lint for thef32fallback. I think this falls under the types team, sor? 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