Skip to content

Error converting from i32 to f64 when type parameter implements From<f64> #73653

@djc

Description

@djc

I tried this code:

fn foo<T>(x: T, y: i32) -> f64 where f64: From<T> {
    f64::from(y)
}

I expected to see this happen: should compile and do the right thing.

Instead, this happened:

error[E0308]: mismatched types
 --> src/lib.rs:2:15
  |
1 | fn foo<T>(x: T, y: i32) -> f64 where f64: From<T> {
  |        - this type parameter
2 |     f64::from(y)
  |               ^ expected type parameter `T`, found `i32`
  |
  = note: expected type parameter `T`
                       found type `i32`
  = help: type parameters must be constrained to match other types
  = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

On the other hand, this code compiles fine:

fn foo<T>(_: T, y: i32) -> f64 {
    f64::from(y)
}

Meta

I was surprised about this, so after running this with 1.44.0 and 1.44.1 I checked older versions, but 1.39.0, 1.31.0 and 1.15.0 all give the same error. Seems likely that there is an issue about this already, but no clue how to look for it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions