Skip to content
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

Too much large error span for sum() #43233

Closed
leonardo-m opened this issue Jul 14, 2017 · 3 comments
Closed

Too much large error span for sum() #43233

leonardo-m opened this issue Jul 14, 2017 · 3 comments

Comments

@leonardo-m
Copy link

This code:

fn sum_and_show(data: &[u32]) -> String {
    data.iter().sum().to_string()
}
fn main() {
    let s: String = sum_and_show(&[1, 2, 3]);
}

In rustc 1.20.0-nightly (b2c0707 2017-07-13) gives the error message:

error[E0619]: the type of this value must be known in this context
 --> ...\test.rs:2:5
  |
2 |     data.iter().sum().to_string()
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The error message doesn't point to the exact location of the problem. The correct code is:

data.iter().sum::<u32>().to_string()

So probably I'd like an error message like:

error[E0619]: the type of this value must be known in this context
 --> ...\test.rs:2:5
  |
2 |     data.iter().sum().to_string()
  |                 ^^^^^

Even better: I'd like Rustc to infer the right type for sum(), not require a type hint there and compile the original code.

@kennytm
Copy link
Member

kennytm commented Jul 14, 2017

I think rustc cannot confidently infer the type of sum(), because one could legally impl Sum<&u32> for MyType which produces a different to_string()

@zackmdavis
Copy link
Member

Maybe related? #42234

@Mark-Simulacrum
Copy link
Member

Going to go ahead and close in favor of #36598. Not, perhaps, an exact duplicate but I think it's the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants