-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Regression in type inference from Iterator.sum #25094
Comments
/cc @nikomatsakis |
|
Testing with the shiny new Default Type Parameter Fallback: #![feature(default_type_parameter_fallback)]
#![feature(iter_arith)]
fn main() {
let a = [1, 2, 3, 4, 5];
let it = a.iter();
assert_eq!(it.sum(), 15);
}
(example from the Iterator::sum documentation) |
@mitaa It appears that your example currently fails with the same exact message as OP: https://is.gd/eA5RJ0 |
Nominating for compiler team discussion since this is a regression that slipped through a while back. |
Copying over #36201 to close that: fn total1(a: &[u32]) -> u32 { // OK
a.iter().sum()
}
fn total3(a: &[u32]) -> u32 { // Error
a.iter().sum() + 5u32
} fn f() -> f64 { // Ok
0.0f64 + vec![1.0f64].into_iter().sum::<f64>()
}
fn f2() -> f64 { // Error
0.0f64 + vec![1.0f64].into_iter().sum()
} |
I believe this changed when |
I'm inclined to close myself. |
@nikomatsakis fn total3(a: &[u32]) -> u32 { // Error
a.iter().sum() + 5u32
} Everything is u32. So why doesn't this work? |
I'm having a hard time understanding why it's inappropriate to infer
Rust says:
The passive voice in "type annotations are needed" begs the question, "by whom???" |
Because other things are addable to |
Similarly, there are many |
Closing since this is behaving as expected, for better or worse. =) |
Oh, just some additional notes: the problem that @jimblandy highlighted is basically a dup of #20297 (so in some sense you could consider this issue a dup of that issue). You can see the effect of fn main() {
let v: Vec<usize> = vec![1,2,3,4];
assert_eq!(10_usize, v.iter().sum());
} |
@nikomatsakis it's weird that if you swap the arguments, inference works again. Is that ok? |
It wasn't available when I was first working on this code, it is now, so hooray! Also implemented as sum::<f64> since the type inference doesn't quite work in this context. Related discussion to this issue can be found on github here: rust-lang/rust#25094
Stumbled upon this bug via #36201; I appreciate (now) why "the sum type ... must be specified and cannot be inferred from context", but the error message seems like it could still be more helpful. In particular, could it suggest a likely annotation, e.g. "Did you mean FWIW in 1.36.0 the error message from the
|
The following used to work (around the time where
AdditiveIterator
still existed):http://is.gd/2jQlp7
The error message:
The text was updated successfully, but these errors were encountered: