Skip to content

rustc's help: try suggestions can be syntactically incorrect rust, even when suggesting specific rust code as an error solution #122569

Closed
@philjb

Description

@philjb

Discovered by a rust beginner...

Code

#[derive(Debug)]
enum MyError {
    MainError
}

fn main() -> Result<(), MyError> {
        let vec = vec!["one", "two", "three"];
        let list = vec.iter()
        .map(|s| s.strip_prefix("t"))
        .filter_map(Option::Some)
        .into()?;
        
        return Ok(());
}

Current output

error[E0283]: type annotations needed
  --> example.rs:11:10
   |
11 |         .into()?;
   |          ^^^^
   |
   = note: cannot satisfy `_: From<FilterMap<Map<std::slice::Iter<'_, &str>, {[email protected]:9:14: 9:17}>, fn(Option<&str>) -> Option<Option<&str>> {Option::<Option<&str>>::Some}>>`
   = note: required for `FilterMap<Map<std::slice::Iter<'_, &str>, {[email protected]:9:14: 9:17}>, fn(Option<&str>) -> Option<Option<&str>> {Option::<Option<&str>>::Some}>` to implement `Into<_>`
help: try using a fully qualified path to specify the expected types
   |
8  ~         let list = <FilterMap<Map<std::slice::Iter<'_, &str>, {[email protected]:9:14: 9:17}>, fn(Option<&str>) -> Option<Option<&str>> {Option::<Option<&str>>::Some}> as Into<T>>::into(vec.iter()
9  |         .map(|s| s.strip_prefix("t"))
10 ~         .filter_map(Option::Some))?;
   |

error: aborting due to 1 previous error

Desired output

Perhaps an output like for the similar error error[E0283]: type annotations needed would be better. It is also suggestion specific rust code but it is clear that this is pseudocode.

For example like:

help: consider giving `list` an explicit type
  |
8 |         let list: /* Type */ = self
  |                      ++++++++++++

Rationale and extra context

I expect rustc to generate at least syntactically correct rust if it is providing a suggestion of rust code, especially since rustc is the judge of syntax.

For many classes of errors, the help is prose or pseudocode and not a specific rust code suggestion. In this example a fully qualified path to specify the expected types is not valid rust.

The closure "type" with file line numbers is not a helpful suggestion.

Other cases

Here's the (sanitized) error i got while working on my production codebase. Notice it is an E0282 error instead of E0283 - I couldn't reproduce my production error exactly with my minimal example.

   Compiling myserviced v0.1.0 (/Volumes/wk/repo/project/crate/services/myserivced)
error[E0282]: type annotations needed
   --> services/myserviced/src/lib.rs:128:14
    |
128 |             .into()
    |              ^^^^
    |
help: try using a fully qualified path to specify the expected types
    |
122 ~         let file_list = <FilterMap<std::iter::Map<std::slice::Iter<'_, PathBuf>, {closure@services/myserviced/src/lib.rs:126:18: 126:21}>, fn(Result<&Path, StripPrefixError>) -> Option<&Path> {Result::<&Path, StripPrefixError>::ok}> as Into<T>>::into(self
123 |             .config
  ...
126 |             .map(|p| p.strip_prefix(&base))
127 ~             .filter_map(Result::ok))
    |

For more information about this error, try `rustc --explain E0282`.
error: could not compile `myserviced` (lib) due to 1 previous error

Rust Version

> rustc --version --verbose
rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: aarch64-apple-darwin
release: 1.76.0
LLVM version: 17.0.6

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions