Skip to content

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

Closed
@lopopolo

Description

@lopopolo

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.

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