Skip to content

Type inference failure when f64 ops are not explicitly stated #122595

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

Open
attack68 opened this issue Mar 16, 2024 · 2 comments
Open

Type inference failure when f64 ops are not explicitly stated #122595

attack68 opened this issue Mar 16, 2024 · 2 comments
Labels
A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@attack68
Copy link

attack68 commented Mar 16, 2024

I tried this code:

use std::ops::Add;

fn some_op<T>(a: &f64, b: &T) -> T
where
    for <'a> &'a f64: Add<&'a T, Output = T>
    {
        let t = 2.5_f64;
        &(a + &t) + b
    }

#[derive(Debug)]
struct MyStruct {
    val: f64
}

impl Add<&MyStruct> for &f64 {
    type Output = MyStruct;
    fn add(self, other: &MyStruct) -> MyStruct {
        MyStruct {val: self + other.val}
    }
}

fn main() {
    let a = 1.5_f64;
    let b = MyStruct{val: 3.5_f64};
    println!("{:?}", some_op(&a, &b));
}

This code is valid due to the implementation of Add<&f64> for &f64 in the standard library.
The compiler reports this error:

error[E0308]: mismatched types
 --> src/main.rs:8:15
  |
3 | fn some_op<T>(a: &f64, b: &T) -> T
  |            - expected this type parameter
...
8 |         &(a + &t) + b
  |               ^^ expected `&T`, found `&f64`
  |
  = note: expected reference `&T`
             found reference `&f64`

error[E0369]: cannot add `&T` to `&T`
 --> src/main.rs:8:19
  |
8 |         &(a + &t) + b
  |         --------- ^ - &T
  |         |
  |         &T
  |
help: consider extending the `where` clause, but there might be an alternative better way to express this requirement
  |
5 |     for <'a> &'a f64: Add<&'a T, Output = T>, &T: Add<&T, Output = T>
  |                                             +++++++++++++++++++++++++

The expected type is not necessary and E0308 should not appear and finding f64 is correct.
If the following bound is added the code compiles without issue:

fn some_op<T>(a: &f64, b: &T) -> T
where
    for <'a> &'a f64: Add<&'a T, Output = T> + Add<&'a f64, Output = f64>

Meta

rustc --version --verbose:

1.76.0
Also tested nightly. 1.78.0
@attack68 attack68 added the C-bug Category: This is a bug. label Mar 16, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 16, 2024
@jieyouxu jieyouxu added A-inference Area: Type inference T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 16, 2024
@veera-sivarajan
Copy link
Contributor

@rustbot claim

@veera-sivarajan
Copy link
Contributor

Looks like this is a duplicate of #24066.

@veera-sivarajan veera-sivarajan removed their assignment Mar 25, 2024
@jieyouxu jieyouxu added T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 28, 2024
@bhdyaku bhdyaku marked this as a duplicate of #139661 Apr 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants