Skip to content

If a function requires f64: Mul<C>, then 1f64 * 1f64 becomes a type error #46697

@ExpHP

Description

@ExpHP
use ::std::ops::Mul;

fn multiply_two_floats<C>()
where f64: Mul<C>,
{
    let _ = 1.0_f64 * 1.0_f64;
}
   Compiling playground v0.0.1 (file:///playground)
error[E0308]: mismatched types
 --> src/main.rs:7:23
  |
7 |     let _ = 1.0_f64 * 1.0_f64;
  |                       ^^^^^^^ expected type parameter, found f64
  |
  = note: expected type `C`
             found type `f64`

I've been playing around with this since yesterday and it looks and feels like the sort of thing that would usually be a known limitation (if that makes sense)... except that I cannot seem to locate any existing discussion on it. Here's some more properties of it:

Adding f64: Mul<f64> + Mul<C> does not resolve the issue.

It is unrelated to operators. (if you swap out Add for some other trait and use a.method(b), the issue persists)

UFCS still works:

use ::std::ops::Mul;

fn multiply_two_floats<C>()
where f64: Mul<C>,
{
    <f64 as Mul<f64>>::mul(1.0_f64, 1.0_f64); // ok
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions