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

Type inference failure on From::from tries to blame Iterator::collect #78055

Closed
saethlin opened this issue Oct 17, 2020 · 2 comments · Fixed by #78111
Closed

Type inference failure on From::from tries to blame Iterator::collect #78055

saethlin opened this issue Oct 17, 2020 · 2 comments · Fixed by #78111
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@saethlin
Copy link
Member

This code:

use rand::Rng;
use std::net::Ipv4Addr;

fn main() {
    let rng = rand::thread_rng();
    let ips: Vec<_> = (0..100_000).map(|_| Ipv4Addr::from(rng.gen())).collect();
}

Produces:

error[E0283]: type annotations needed for `Vec<Ipv4Addr>`
 --> src/main.rs:6:44
  |
6 |     let ips: Vec<_> = (0..100_000).map(|_| Ipv4Addr::from(rng.gen())).collect();
  |         ---                                ^^^^^^^^^^^^^^ cannot infer type for struct `Ipv4Addr`
  |         |
  |         consider giving `ips` the explicit type `Vec<Ipv4Addr>`, where the type parameter `Ipv4Addr` is specified
  |
  = note: cannot satisfy `Ipv4Addr: From<_>`
  = note: required by `from`

At least following the suggestion doesn't result in the error message going any farther off the rails, but it does look quite silly:

error[E0283]: type annotations needed for `Vec<Ipv4Addr>`
 --> src/main.rs:6:51
  |
6 |     let ips: Vec<Ipv4Addr> = (0..100_000).map(|_| Ipv4Addr::from(rng.gen())).collect();
  |         ---                                       ^^^^^^^^^^^^^^ cannot infer type for struct `Ipv4Addr`
  |         |
  |         consider giving `ips` the explicit type `Vec<Ipv4Addr>`, where the type parameter `Ipv4Addr` is specified
  |
  = note: cannot satisfy `Ipv4Addr: From<_>`
  = note: required by `from`

Diagnostics pasted from rustc 1.49.0-nightly (e3051d8c2 2020-10-16), but same behavior on stable 1.47.0.

@tesuji
Copy link
Contributor

tesuji commented Oct 18, 2020

The error message should point to rng.gen() instead.
The correct code would be:

use rand::Rng;
use std::net::Ipv4Addr;

fn main() {
    let mut rng = rand::thread_rng();
    let ips: Vec<_> = (0..100_000)
        .map(|_| Ipv4Addr::from(rng.gen::<u32>()))
        .collect();
}

@rustbot modify labels: A-inference

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 18, 2020
@SNCPlay42
Copy link
Contributor

A similar thing happened in #77982

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 A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants