Weird inference failure with unary minus

4c2a5a0
Opened by eefriedman at 2024-12-21 05:10:28
fn f<T>() -> T { panic!() }
fn main() {
    let a = f();
    let b = -a;
    let c : &i32 = &a;
}
<anon>:4:14: 4:15 error: the type of this value must be known in this context
<anon>:4     let b = -a;
                      ^
error: aborting due to previous error

As far as I can tell, there's enough information here for inference to succeed.

  1. a could be either &i32 or i32.

    Steven Allen at 2015-07-06 18:42:16

  2. I don't think that's relevant... this still doesn't work:

    fn f<T>() -> T { panic!() }
    fn main() {
        let a = f();
        let b = -a;
        let c : i32 = a;
    }
    

    And this works fine:

    fn f<T>() -> T { panic!() }
    fn main() {
        let a = f();
        let b = a-a;
        let c : i32 = a;
    }
    

    eefriedman at 2015-07-06 18:54:52

  3. So, sometimes we try to resolve methods (and, in this case, overloaded operators) "in the moment", and other times we allow that resolution to be deferred. In the case of sub, we are doing the latter -- but I'm not sure why unary minus is not. I'd have to go look at the code. I tentatively agree we could do better here, and that unary operators should work in the same way as binary ones.

    Niko Matsakis at 2015-07-06 21:12:13

  4. Triage: the issue still exists. Tested with rustc 1.9.0-nightly (339a409bf 2016-03-01).

    Oleksii Shmalko at 2016-03-10 13:50:00

  5. Triage: no change

    Steve Klabnik at 2018-10-31 16:01:02

  6. Triage: no change

    Maayan Hanin at 2022-03-22 10:44:54