Skip to content

suggestion for "type annotations needed" should be explicit about how to specify the generic argument #147313

@shepmaster

Description

@shepmaster

Code

fn usage() -> Result<(), ()> {
    "".parse().unwrap()?;
    Ok(())
}

Current output

error[E0284]: type annotations needed
 --> src/lib.rs:2:8
  |
2 |     "".parse().unwrap()?;
  |        ^^^^^ cannot infer type of the type parameter `F` declared on the method `parse`
  |
  = note: cannot satisfy `<_ as FromStr>::Err == _`
help: consider specifying the generic argument
  |
2 |     "".parse::<F>().unwrap()?;
  |             +++++

Desired output

error[E0284]: type annotations needed
 --> src/lib.rs:2:8
  |
2 |     "".parse().unwrap()?;
  |        ^^^^^ cannot infer type of the type parameter `F` declared on the method `parse`
  |
  = note: cannot satisfy `<_ as FromStr>::Err == _`
help: consider specifying a concrete type for the generic type `F`
  |
2 |     "".parse::<SomeConcreteType>().unwrap()?;
  |             ++++++++++++++++++++

I'm not tied to any specific wording here, just something to indicate that F is a generic type and that (except in very rare cases!) using F in the turbofish isn't going to be the right type.

Rust Version

rustc 1.92.0-nightly (5c7ae0c7e 2025-10-02)
binary: rustc
commit-hash: 5c7ae0c7ed184c603e5224604a9f33ca0e8e0b36
commit-date: 2025-10-02
host: aarch64-apple-darwin
release: 1.92.0-nightly
LLVM version: 21.1.2

Anything else?

Discovered while giving training to newcomers to Rust.

May be related to #128585.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions