Inference failure with int inference variable
fn f<T>() -> T { panic!(); }
fn main() {
let mut a = f();
let b : u8 = a+a;
a = 1;
}
<anon>:4:18: 4:21 error: type mismatch resolving `<i32 as core::ops::Add>::Output == u8`:
expected i32,
found u8 [E0271]
<anon>:4 let b : u8 = a+a;
^~~
Given that the type of b is u8, the type of a is a builtin integer type, and the result of a+a is assigned to b, the type of a must be u8. Currently, this rule only applies "forwards": it only works if a is known to be a builtin integer type when the addition is being checked. However, type inference could be extended to apply it backwards as well.
I'm not sure if this is relevant in practical usage of Rust.
The code that currently applies this rule: https://github.com/rust-lang/rust/blob/a973e4cda5191040ca219fb4a4f8041e9b560301/src/librustc_typeck/check/op.rs#L130 .
eefriedman at 2015-07-07 00:28:39
Triage: still reproduces today.
Steve Klabnik at 2017-01-03 21:13:37
Triage: still reproduces today
Steve Klabnik at 2019-12-25 16:08:33
Triage: no change
Maayan Hanin at 2022-03-23 06:46:07