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

where clause hides f64: Div<f64> impl #89910

Open
m-ou-se opened this issue Oct 15, 2021 · 4 comments
Open

where clause hides f64: Div<f64> impl #89910

m-ou-se opened this issue Oct 15, 2021 · 4 comments
Labels
A-trait-system Area: Trait 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

@m-ou-se
Copy link
Member

m-ou-se commented Oct 15, 2021

This code:

use std::ops::Div;

pub fn f<T>(_: T) -> f64
where
    f64: Div<T, Output = f64>,
{
    1.0 / 2.0
}

Results in this error:

error[E0308]: mismatched types
 --> src/lib.rs:7:11
  |
3 | pub fn f<T>(_: T) -> f64
  |          - this type parameter
...
7 |     1.0 / 2.0
  |           ^^^ expected type parameter `T`, found floating-point number
  |
  = note: expected type parameter `T`
                       found type `{float}`

But I expected it to compile fine, since f64: Div<f64, Output = f64> also exists unconditionally. It seems that the bound on f64 hides the existing f64: Div<f64>.

This seems to happen on all versions of Rust since 1.0.

@m-ou-se m-ou-se added A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Oct 15, 2021
@m-ou-se
Copy link
Member Author

m-ou-se commented Oct 15, 2021

Some more data points:

  • Div::div(1f64, 2f64) inside that function breaks with the same error.

  • Div::<f64>::div(1.0, 2.0) works.

@fbstj
Copy link
Contributor

fbstj commented Oct 16, 2021

playground link

  • it also happens similarly with std::ops::Add
  • it also happens if you constrain the RHS to 2.0f64 (found type 64)
  • it happens to every {float} / ? operation in the function body, not just the return value

@cuviper
Copy link
Member

cuviper commented Oct 17, 2021

I think this is part of what #41756 is talking about.

@cuviper
Copy link
Member

cuviper commented Oct 17, 2021

Chasing that conversation leads to a more direct dupe, #24066.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait 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

3 participants