-
Notifications
You must be signed in to change notification settings - Fork 13.3k
cannot infer type
when expression is logically negated in closure
#106138
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
Comments
@rustbot claim |
emm, this is because if |
It appears to happen with other unary operators as well, but never with binary operators: pub fn bools(x: &Vec<bool>) {
let binary = |i, a: &Vec<bool>| {
a[i] && a[i+1] // ok
};
let unary = |i, a: &Vec<bool>| {
!a[i] // cannot infer type
};
binary(0, x);
unary(0, x);
}
pub fn ints(x: &Vec<isize>) {
let binary = |i, a: &Vec<isize>| {
a[i] + a[i+1] // ok
};
let unary = |i, a: &Vec<isize>| {
-a[i] // cannot infer type
};
binary(0, x);
unary(0, x);
}
|
I can not figure out a proper fix for this issue right now. |
Hi! I’m pretty new to Rust, but I think I just encountered a bug in the compiler.
For some reason, Rust cannot “infer” the type of
!a[i]
in this code:But when you replace
!a[i]
with justa[i]
, then it compiles without error. So the error only occurs when there is a logical negation.While narrowing this down, I also noticed that the error message appears to be a bit misleading: It looks like the compiler actually fails to infer the type of
i
, not!a[i]
, as the error goes away when I add a type annotation fori
. Here is a more elaborate example, combining all of my three test cases:Run it on Rust Playground
Reproduces on Stable, Beta and Nightly.
The text was updated successfully, but these errors were encountered: