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

inform user where to give a type annotation after a call to collect #47777

Closed
nikomatsakis opened this issue Jan 26, 2018 · 4 comments · Fixed by #48198
Closed

inform user where to give a type annotation after a call to collect #47777

nikomatsakis opened this issue Jan 26, 2018 · 4 comments · Fixed by #48198
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Jan 26, 2018

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

@nikomatsakis nikomatsakis added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 26, 2018
@estebank
Copy link
Contributor

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.

@cuviper cuviper added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jan 27, 2018
@nikomatsakis
Copy link
Contributor Author

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

pub fn need_type_info(&self, body_id: Option<hir::BodyId>, span: Span, ty: Ty<'tcx>) {

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:

type_error_struct!(self.tcx.sess, sp, ty, E0619,
"the type of this value must be known in this context")
.emit();

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);

@nikomatsakis nikomatsakis added the E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. label Jan 29, 2018
@csmoe
Copy link
Member

csmoe commented Jan 30, 2018

I’ll take this.

Manishearth added a commit to Manishearth/rust that referenced this issue Feb 19, 2018
…tebank

inform user where to give a type annotation

should resolve rust-lang#47777
previous pull request rust-lang#47982 was closed because of a mistaken rebase.
r? @estebank
@cengiz-io
Copy link
Contributor

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

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Feb 21, 2018
…tebank

inform user where to give a type annotation

should resolve rust-lang#47777
previous pull request rust-lang#47982 was closed because of a mistaken rebase.
r? @estebank
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
6 participants
@cuviper @cengiz-io @nikomatsakis @estebank @csmoe and others