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

The compiler could have been more precise about missing type annotation. #36495

Closed
kindlychung opened this issue Sep 15, 2016 · 1 comment
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kindlychung
Copy link
Contributor

Here is the code:

#[derive(Debug)]
enum Message {
    Quit,
    ChangeColor(i32, i32, i32),
    Move { x: i32, y: i32 },
    Write(String),
}
enum BoardGameTurn {
    Move { squares: i32 },
    Pass,
}
fn main() {
    let x: Message = Message::Move { x: 3, y: 4 };
//  let v = "hello world".split_whitespace();
//  let v: Vec<String> = "hello world".split_whitespace().map(|x| x.to_string()).collect();
    let v = "hello world".split_whitespace().map(|x| x.to_string()).collect();
//    let v = vec!["Hello".to_string(), "World".to_string()];
    fn write_str(s: &str) -> Message {
        Message::Write(s.to_string())
    }
    let v1: Vec<Message> = v.into_iter().map(Message::Write).collect();
    let y: BoardGameTurn = BoardGameTurn::Move { squares: 1 };
    for i in v1 {
        println!("{:?}", i);
    }
}

Compiler says:

cargo run
   Compiling rustcallc v0.1.0 (file:///Users/kaiyin/Documents/workspace-eclipse-neon/rustcallc)
error: the type of this value must be known in this context
  --> src/main.rs:21:28
   |
21 |     let v1: Vec<Message> = v.into_iter().map(Message::Write).collect();
   |                            ^^^^^^^^^^^^^

error: aborting due to previous error

error: Could not compile `rustcallc`.

The actual error is in this line:

    let v = "hello world".split_whitespace().map(|x| x.to_string()).collect();

which should have specified the type of v.

The compiler could have been more precise about the error.

@TimNN TimNN added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 15, 2016
@steveklabnik steveklabnik added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

Current output:

error[E0282]: type annotations needed
  --> src/main.rs:21:28
   |
16 |     let v = "hello world".split_whitespace().map(|x| x.to_string()).collect();
   |         - consider giving `v` a type
...
21 |     let v1: Vec<Message> = v.into_iter().map(Message::Write).collect();
   |                            ^ cannot infer type
   |
   = note: type must be known at this point

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. 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

5 participants