Weird error with inference when a function doesn’t compile

85c227d
Opened by Dimitri Sabadie at 2021-06-09 14:39:12
fn foo(_: &[String]) -> Vec<std::std::string::String> {
    Vec::new()
}

fn main() {
    let x = foo(&Vec::new());
    foo(&x);
}

Here, the foo function is obviously wrong – std::std::string::String. However, it looks like rustc tried to typecheck the rest of the program and now thinks that x should have the type [String] – because of its use in foo. I guess the error message would be more explicit with x.as_slice() and that it has something to do with the auto deref coercion?

It results in two messages: the first one, correct, about the std::std thing. The second, about [String], which is really misleading.

  1. The errors (on stable):

       Compiling playground v0.0.1 (file:///playground)
    error[E0433]: failed to resolve. Could not find `std` in `std`
     --> src/main.rs:1:42
      |
    1 | fn foo(_: &[std::string::String]) -> Vec<std::std::string::String> {
      |                                          ^^^^^^^^^^^^^^^^^^^^^^^^ Could not find `std` in `std`
    
    error[E0277]: the trait bound `[std::string::String]: std::marker::Sized` is not satisfied
     --> src/main.rs:6:9
      |
    6 |     let x = foo(&Vec::new());
      |         ^ `[std::string::String]` does not have a constant size known at compile-time
      |
      = help: the trait `std::marker::Sized` is not implemented for `[std::string::String]`
      = note: all local variables must have a statically known size
    
    error: aborting due to 2 previous errors
    
    error: Could not compile `playground`.
    
    To learn more, run the command again with --verbose.
    

    Dimitri Sabadie at 2017-09-19 11:29:22

  2. Also, feel free to edit/rename the issue – I’m bad at naming that kind of issue, sorry for that.

    Dimitri Sabadie at 2017-09-19 11:30:11

  3. I don't see any issue with the error...

    Guillaume Gomez at 2017-09-19 16:02:59

  4. @GuillaumeGomez it sees x as a [String], while it’s a Vec<String>.

    Dimitri Sabadie at 2017-09-19 18:54:36

  5. Indeed. An unneeded coercion on the second error. Quite strange...

    Guillaume Gomez at 2017-09-20 11:20:09

  6. The second error that had weird types no longer appears on 1.52.1 stable, 1.53.0-beta7 beta, or 1.54.0 nightly (2021-06-08)

    Playground link

    asquared31415 at 2021-06-09 14:39:12