Skip to content

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

Open
@m-ou-se

Description

@m-ou-se
Member

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.

Activity

added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
C-bugCategory: This is a bug.
on Oct 15, 2021
m-ou-se

m-ou-se commented on Oct 15, 2021

@m-ou-se
MemberAuthor

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

fbstj commented on Oct 16, 2021

@fbstj
Contributor

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

cuviper commented on Oct 17, 2021

@cuviper
Member

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

cuviper

cuviper commented on Oct 17, 2021

@cuviper
Member

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler 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

        Participants

        @fbstj@cuviper@m-ou-se

        Issue actions

          `where` clause hides `f64: Div<f64>` impl · Issue #89910 · rust-lang/rust