Skip to content
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

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

Open
djc opened this issue Jun 23, 2020 · 3 comments
Open

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

djc opened this issue Jun 23, 2020 · 3 comments
Labels
A-typesystem Area: The type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@djc
Copy link
Contributor

djc commented Jun 23, 2020

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.

@djc djc added the C-bug Category: This is a bug. label Jun 23, 2020
@tesuji
Copy link
Contributor

tesuji commented Jun 23, 2020

I don't know if we can bound on primitive types like f64. But if you bound T by Into trait, this should compile:

fn foo<T>(x: T, y: i32) -> f64 where T: Into<f64>;

@lcnr
Copy link
Contributor

lcnr commented Jun 23, 2020

Might be related to #34979 (comment)

I think this is caused by winnowing somehow incorrectly discarding a relevant predicate.

cc @eddyb @nikomatsakis for good measure

@lcnr lcnr added the A-typesystem Area: The type system label Jun 23, 2020
@nikomatsakis
Copy link
Contributor

Yeah, I agree with @lcnr's diagnosis. This is an example of a known limitation with the current trait solver, sort of a sub-issue of #41756 or some other similar issues. I'm not sure that we have any plans to fix this apart from transitioning over to chalk.

@JohnTitor JohnTitor added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants