Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
philjb opened this issue Mar 15, 2024 · 1 comment · Fixed by #122799
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@philjb
Copy link

philjb commented Mar 15, 2024

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>, {closure@example.rs:9:14: 9:17}>, fn(Option<&str>) -> Option<Option<&str>> {Option::<Option<&str>>::Some}>>`
   = note: required for `FilterMap<Map<std::slice::Iter<'_, &str>, {closure@example.rs: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>, {closure@example.rs: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

@philjb philjb added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 15, 2024
@compiler-errors
Copy link
Member

estebank added a commit to estebank/rust that referenced this issue Mar 20, 2024
…thod call

```
error[E0283]: type annotations needed
  --> $DIR/into-inference-needs-type.rs:12:10
   |
LL |         .into()?;
   |          ^^^^
   |
   = note: cannot satisfy `_: From<...>`
   = note: required for `FilterMap<...>` to implement `Into<_>`
help: try using a fully qualified path to specify the expected types
   |
LL ~     let list = <FilterMap<Map<std::slice::Iter<'_, &str>, _>, _> as Into<T>>::into(vec
LL |         .iter()
LL |         .map(|s| s.strip_prefix("t"))
LL ~         .filter_map(Option::Some))?;
   |
```

Fix rust-lang#122569.
estebank added a commit to estebank/rust that referenced this issue Mar 21, 2024
…thod call

```
error[E0283]: type annotations needed
  --> $DIR/into-inference-needs-type.rs:12:10
   |
LL |         .into()?;
   |          ^^^^
   |
   = note: cannot satisfy `_: From<...>`
   = note: required for `FilterMap<...>` to implement `Into<_>`
help: try using a fully qualified path to specify the expected types
   |
LL ~     let list = <FilterMap<Map<std::slice::Iter<'_, &str>, _>, _> as Into<T>>::into(vec
LL |         .iter()
LL |         .map(|s| s.strip_prefix("t"))
LL ~         .filter_map(Option::Some))?;
   |
```

Fix rust-lang#122569.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 21, 2024
Replace closures with `_` when suggesting fully qualified path for method call

```
error[E0283]: type annotations needed
  --> $DIR/into-inference-needs-type.rs:12:10
   |
LL |         .into()?;
   |          ^^^^
   |
   = note: cannot satisfy `_: From<...>`
   = note: required for `FilterMap<...>` to implement `Into<_>`
help: try using a fully qualified path to specify the expected types
   |
LL ~     let list = <FilterMap<Map<std::slice::Iter<'_, &str>, _>, _> as Into<T>>::into(vec
LL |         .iter()
LL |         .map(|s| s.strip_prefix("t"))
LL ~         .filter_map(Option::Some))?;
   |
```

Fix rust-lang#122569.
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Mar 21, 2024
Replace closures with `_` when suggesting fully qualified path for method call

```
error[E0283]: type annotations needed
  --> $DIR/into-inference-needs-type.rs:12:10
   |
LL |         .into()?;
   |          ^^^^
   |
   = note: cannot satisfy `_: From<...>`
   = note: required for `FilterMap<...>` to implement `Into<_>`
help: try using a fully qualified path to specify the expected types
   |
LL ~     let list = <FilterMap<Map<std::slice::Iter<'_, &str>, _>, _> as Into<T>>::into(vec
LL |         .iter()
LL |         .map(|s| s.strip_prefix("t"))
LL ~         .filter_map(Option::Some))?;
   |
```

Fix rust-lang#122569.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 21, 2024
Replace closures with `_` when suggesting fully qualified path for method call

```
error[E0283]: type annotations needed
  --> $DIR/into-inference-needs-type.rs:12:10
   |
LL |         .into()?;
   |          ^^^^
   |
   = note: cannot satisfy `_: From<...>`
   = note: required for `FilterMap<...>` to implement `Into<_>`
help: try using a fully qualified path to specify the expected types
   |
LL ~     let list = <FilterMap<Map<std::slice::Iter<'_, &str>, _>, _> as Into<T>>::into(vec
LL |         .iter()
LL |         .map(|s| s.strip_prefix("t"))
LL ~         .filter_map(Option::Some))?;
   |
```

Fix rust-lang#122569.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 21, 2024
Replace closures with `_` when suggesting fully qualified path for method call

```
error[E0283]: type annotations needed
  --> $DIR/into-inference-needs-type.rs:12:10
   |
LL |         .into()?;
   |          ^^^^
   |
   = note: cannot satisfy `_: From<...>`
   = note: required for `FilterMap<...>` to implement `Into<_>`
help: try using a fully qualified path to specify the expected types
   |
LL ~     let list = <FilterMap<Map<std::slice::Iter<'_, &str>, _>, _> as Into<T>>::into(vec
LL |         .iter()
LL |         .map(|s| s.strip_prefix("t"))
LL ~         .filter_map(Option::Some))?;
   |
```

Fix rust-lang#122569.
@bors bors closed this as completed in 5fae665 Mar 21, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 21, 2024
Rollup merge of rust-lang#122799 - estebank:issue-122569, r=fee1-dead

Replace closures with `_` when suggesting fully qualified path for method call

```
error[E0283]: type annotations needed
  --> $DIR/into-inference-needs-type.rs:12:10
   |
LL |         .into()?;
   |          ^^^^
   |
   = note: cannot satisfy `_: From<...>`
   = note: required for `FilterMap<...>` to implement `Into<_>`
help: try using a fully qualified path to specify the expected types
   |
LL ~     let list = <FilterMap<Map<std::slice::Iter<'_, &str>, _>, _> as Into<T>>::into(vec
LL |         .iter()
LL |         .map(|s| s.strip_prefix("t"))
LL ~         .filter_map(Option::Some))?;
   |
```

Fix rust-lang#122569.
flip1995 pushed a commit to flip1995/rust that referenced this issue Apr 4, 2024
…thod call

```
error[E0283]: type annotations needed
  --> $DIR/into-inference-needs-type.rs:12:10
   |
LL |         .into()?;
   |          ^^^^
   |
   = note: cannot satisfy `_: From<...>`
   = note: required for `FilterMap<...>` to implement `Into<_>`
help: try using a fully qualified path to specify the expected types
   |
LL ~     let list = <FilterMap<Map<std::slice::Iter<'_, &str>, _>, _> as Into<T>>::into(vec
LL |         .iter()
LL |         .map(|s| s.strip_prefix("t"))
LL ~         .filter_map(Option::Some))?;
   |
```

Fix rust-lang#122569.
flip1995 pushed a commit to flip1995/rust that referenced this issue Apr 4, 2024
Replace closures with `_` when suggesting fully qualified path for method call

```
error[E0283]: type annotations needed
  --> $DIR/into-inference-needs-type.rs:12:10
   |
LL |         .into()?;
   |          ^^^^
   |
   = note: cannot satisfy `_: From<...>`
   = note: required for `FilterMap<...>` to implement `Into<_>`
help: try using a fully qualified path to specify the expected types
   |
LL ~     let list = <FilterMap<Map<std::slice::Iter<'_, &str>, _>, _> as Into<T>>::into(vec
LL |         .iter()
LL |         .map(|s| s.strip_prefix("t"))
LL ~         .filter_map(Option::Some))?;
   |
```

Fix rust-lang#122569.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants