Open
Description
Following up from #49391 @estebank
Not sure if the compiler can be clever about this.
Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=e30b2fc110718876664dd046492b4dc5
use std::fs::File;
use std::io::{self, prelude::*, BufReader};
fn char_count(text: &str, pattern: &str) -> usize {
text.len() + pattern.len()
}
fn main() -> io::Result<()> {
let rdr = BufReader::new(File::open("file")?);
// Compiler says: "doesn't have a size known at compile-time"
// Change to "collect::<Result<_, _>>()?" for fix.
let lines: Vec<_> = rdr.lines().collect()?;
let text = &lines[0];
let pattern = &lines[0];
// Removing below line, correctly triggers "cannot infer type"
let _x = char_count(text, pattern);
Ok(())
}
The current output is:
Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/main.rs:17:20
|
17 | let pattern = &lines[0];
| ^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= note: required because of the requirements on the impl of `Index<usize>` for `Vec<str>`
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/main.rs:14:16
|
14 | let lines: Vec<_> = rdr.lines().collect()?;
| ^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/main.rs:14:25
|
14 | let lines: Vec<_> = rdr.lines().collect()?;
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/main.rs:16:17
|
16 | let text = &lines[0];
| ^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= note: required because of the requirements on the impl of `Index<usize>` for `Vec<str>`
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
Ideally the output be:
Compiling playground v0.0.1 (/playground)
error[E0282]: type annotations needed for `Vec<_>`
--> src/main.rs:14:16
|
14 | let lines: Vec<_> = rdr.lines().collect()?;
| ----- ^^^^^^ cannot infer type
| |
| consider giving `lines` the explicit type `Vec<_>`, with the type parameters specified
error: aborting due to previous error
For more information about this error, try `rustc --explain E0282`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
This also will happen correctly when the function call taking the strings is not used. Apparently that function call put the compiler to the wrong direction what should be the correct fix to the code.