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

Error refers to the wrong span of code #90321

Closed
cubetastic33 opened this issue Oct 26, 2021 · 2 comments
Closed

Error refers to the wrong span of code #90321

cubetastic33 opened this issue Oct 26, 2021 · 2 comments
Labels
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.

Comments

@cubetastic33
Copy link

Given the following code:

use rand::prelude::*;
use std::collections::HashSet;

fn main() {
    let mut foo = vec![String::new(); 10];
    let bar: HashSet<_> = "bar".chars().collect();
    let mut fubar: HashSet<_> = "fubar".chars().collect();
    let baz = vec![(String::new(), bar)];
    let mut rng = thread_rng();
    
    let choice = baz.choose(&mut rng).unwrap();
    foo.push(choice.0.clone());
    
    fubar = fubar.difference(&choice.1).collect();
    
    // Uncomment to get the correct error in nightly
    // foo = vec![String::new(); 10];
    foo.shuffle(&mut rng);
}

The error reported by rust nightly/beta is:

error[E0277]: a value of type `HashSet<char>` cannot be built from an iterator over elements of type `&char`
  --> src/main.rs:18:17
   |
18 |     foo.shuffle(&mut rng);
   |         ------- ^^^^^^^^ value of type `HashSet<char>` cannot be built from `std::iter::Iterator<Item=&char>`
   |         |
   |         required by a bound introduced by this call
   |
   = help: the trait `FromIterator<&char>` is not implemented for `HashSet<char>`

For more information about this error, try `rustc --explain E0277`.

Playground

But the error clearly doesn't belong there - it's supposed to be on line 14. Rust stable shows the correct error, and re-assigning to foo just before the shuffle makes the correct error show on nightly and beta as well.

This is what the error should be:

error[E0277]: a value of type `HashSet<char>` cannot be built from an iterator over elements of type `&char`
  --> src/main.rs:14:41
   |
14 |     fubar = fubar.difference(&choice.1).collect();
   |                                         ^^^^^^^ value of type `HashSet<char>` cannot be built from `std::iter::Iterator<Item=&char>`
   |
   = help: the trait `FromIterator<&char>` is not implemented for `HashSet<char>`

For more information about this error, try `rustc --explain E0277`.
@cubetastic33 cubetastic33 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 Oct 26, 2021
@EliseZeroTwo
Copy link
Contributor

I was not able to reproduce this on latest nightly, it seems to have been fixed by #90181

@cubetastic33
Copy link
Author

Yup, I think it's fixed

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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants