Skip to content

Missing Type Annotations Error Lacks Suggestions #88284

Closed
@jamesmunns

Description

@jamesmunns

Given the following code:

struct Foo {
    inner: u32,
}

struct Bar {
    inner: u32,
}

#[derive(Clone, Copy)]
struct Baz {
    inner: u32,
}

impl From<Baz> for Bar {
    fn from(other: Baz) -> Self {
        Self {
            inner: other.inner,
        }
    }
}

impl From<Baz> for Foo {
    fn from(other: Baz) -> Self {
        Self {
            inner: other.inner,
        }
    }
}

fn main() {
    let x: Baz = Baz { inner: 42 };

    // DOESN'T Compile: Multiple options!
    let y = x.into();

    let y_1: Foo = x.into();
    let y_2: Bar = x.into();

    let z_1 = Foo::from(y_1);
    let z_2 = Bar::from(y_2);

    // No type annotations needed, the compiler KNOWS the type must be `Foo`!
    let m = magic_foo(x);
}

fn magic_foo(arg: Baz) -> Foo {
    arg.into()
}

The current output is:

error[E0282]: type annotations needed
  --> src/main.rs:34:9
   |
34 |     let y = x.into();
   |         ^ consider giving `y` a type

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.

Ideally the output should look like:

IMO the output could include:

  • A reminder of the explicit type syntax (e.g. let y: TYPE = x.into())
    • This is well suggested in rustc --explain E0282, but it might be helpful in the rustc output as well
  • A suggestion of potential candidate (e.g. in this case: Foo or Bar)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceA-trait-systemArea: Trait systemD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-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