Misleading error message for string parsing
Open
Nobody has claimed this yet.
A-diagnostics
C-bug
D-confusing
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let foo = "3";
let bar = foo.parse()?;
Ok(())
}
Current output
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `(): FromStr` is not satisfied
--> src/main.rs:5:19
|
5 | let bar = foo.parse()?;
| ^^^^^ the trait `FromStr` is not implemented for `()`
|
= help: the following other types implement trait `FromStr`:
bool
char
isize
i8
i16
i32
i64
i128
and 29 others
note: required by a bound in `core::str::<impl str>::parse`
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/mod.rs:2317:21
|
2317 | pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> {
| ^^^^^^^ required by this bound in `core::str::<impl str>::parse`
Desired output
A message clearly indicating that the problem is that bar or parse needs a type annotation.
Rationale and extra context
The following seems to work fine:
fn main() {
let foo = "3";
let bar = foo.parse().unwrap();
}
Error message:
Compiling playground v0.0.1 (/playground)
error[E0284]: type annotations needed
--> src/main.rs:3:9
|
3 | let bar = foo.parse().unwrap();
| ^^^ ----- type must be known at this point
|
= note: cannot satisfy `<_ as FromStr>::Err == _`
help: consider giving `bar` an explicit type
|
3 | let bar: /* Type */ = foo.parse().unwrap();
| ++++++++++++
Other cases
No response
Rust Version
rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: aarch64-apple-darwin
release: 1.73.0
LLVM version: 17.0.2
Anything else?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the diagnostic using the example in src/main.rs, focusing on the foo.parse() call and the bar binding. Compare it with the unwrap() example and investigate how the compiler reports missing type information; done means the error clearly points users toward adding a type annotation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100