Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Unhelpful "not a struct" error when typoing a type name when opening an impl block and using Self #88844

Closed
lopopolo opened this issue Sep 11, 2021 · 1 comment · Fixed by #88899
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

@lopopolo
Copy link
Contributor

lopopolo commented Sep 11, 2021

Given the following code:

extern crate regex; // 1.5.4

use std::iter::Enumerate;

pub struct CaptureIndices<'a, 'b> {
    name: &'b [u8],
    capture_names: Enumerate<regex::CaptureNames<'a>>
}

impl<'a, 'b> CapturesIndicies<'a, 'b> {
    pub(crate) fn with_name_and_iter(name: &'b [u8], iter: regex::CaptureNames<'a>) -> Self {
        Self {
            name,
            capture_names: iter.enumerate(),
        }
    }
}

fn main() {
    let r = regex::Regex::new("a").unwrap();
    CaptureIndicies::with_name_and_iter(b"", r.capture_names());
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1456a684a2c5ec1718ca127788c2a6a0

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0433]: failed to resolve: use of undeclared type `CaptureIndicies`
  --> src/main.rs:21:5
   |
21 |     CaptureIndicies::with_name_and_iter(b"", r.capture_names());
   |     ^^^^^^^^^^^^^^^ use of undeclared type `CaptureIndicies`

error[E0412]: cannot find type `CapturesIndicies` in this scope
  --> src/main.rs:10:14
   |
5  | pub struct CaptureIndices<'a, 'b> {
   | --------------------------------- similarly named struct `CaptureIndices` defined here
...
10 | impl<'a, 'b> CapturesIndicies<'a, 'b> {
   |              ^^^^^^^^^^^^^^^^ help: a struct with a similar name exists: `CaptureIndices`

error[E0071]: expected struct, variant or union type, found type error
  --> src/main.rs:12:9
   |
12 |         Self {
   |         ^^^^ not a struct

Some errors have detailed explanations: E0071, E0412, E0433.
For more information about an error, try `rustc --explain E0071`.
error: could not compile `playground` due to 3 previous errors

Ideally the output should look like:

the Self type not being a struct is caused by the typo in the impl target.

Ideally, the diagnostics could recover from "type error" to a suggested fix.

@lopopolo lopopolo 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 Sep 11, 2021
@lopopolo
Copy link
Contributor Author

As an aside, the fixed code in https://doc.rust-lang.org/stable/error-index.html#E0071 doesn't seem derived from the broken code example.

@lopopolo lopopolo changed the title Unhelpful "not a struct" error when typoing a struct name Unhelpful "not a struct" error when typoing a type name when opening an impl block and using Self Sep 11, 2021
@bors bors closed this as completed in 307f2dd Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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.

1 participant