Rust: inform user where to give a type annotation after a call to collect

Created on 26 Jan 2018  路  4Comments  路  Source: rust-lang/rust

UPDATE: Mentoring instructions below.


We are not giving a very good error message in this example:

fn main() {
    let mut dirty_list = (0..5).collect();
    dirty_list.pop();
}

We say:

error[E0619]: the type of this value must be known in this context
 --> src/main.rs:3:5
  |
3 |     dirty_list.pop();
  |     ^^^^^^^^^^^^^^^^

It'd be nice if we at least informed the user that they ought to annotate the type of dirty_list. (Indeed, I know we used to have a bug on this, and I thought we were doing so...?)

It'd be nicer still if we recognized that there is a call to collect and were able to say something like "you need to specify the kind of collection".

cc @estebank

A-diagnostics C-enhancement E-mentor T-compiler

Most helpful comment

I鈥檒l take this.

All 4 comments

The only thing I'd add is that there are two cases, one where this error happens with a let binding, like in this case, where suggesting giving a type to the binding might be less surprising, and every other case, from having multiple collect to being a return, where the only place to add the type is in the method call itself.

Those two cases are already handled by the code that seeks to give hints. That code lives in this unfortunately uncommented method:

https://github.com/rust-lang/rust/blob/70f7d5842f29d4900f24420b030f144d21f3c5fc/src/librustc/infer/error_reporting/need_type_info.rs#L89

I believe that the idea is that it reports an error that the type ty must be resolved; the error occurs at the given span, and the body_id should be the id of the enclosing function body.

Probably all we have to do is to call this function from here:

https://github.com/rust-lang/rust/blob/70f7d5842f29d4900f24420b030f144d21f3c5fc/src/librustc_typeck/check/mod.rs#L4969-L4971

As a bonus, we can remove E0619, which would be subsumed into the existing error code. The call would look something like:

self.need_type_info(Some(self.body_id), sp, ty);

I鈥檒l take this.

@csmoe hi! don't hesitate to ask anything related to need_type_info to me. I've spent some time around that function :rofl:

Was this page helpful?
0 / 5 - 0 ratings