Skip to content

Spurious 'type annotations needed' error when a struct definition has an error #75331

Closed
@Aaron1011

Description

@Aaron1011
Member

The following code:

struct Foo {
    val: MissingType
}

fn make_it() {
    Foo { val: Default::default() };
}

produces the following errors:

error[E0412]: cannot find type `MissingType` in this scope
 --> src/lib.rs:2:10
  |
2 |     val: MissingType
  |          ^^^^^^^^^^^ not found in this scope

error[E0283]: type annotations needed
 --> src/lib.rs:6:16
  |
6 |     Foo { val: Default::default() };
  |                ^^^^^^^^^^^^^^^^ cannot infer type
  |
  = note: cannot satisfy `_: std::default::Default`
  = note: required by `std::default::Default::default`

We should suppress the 'type annotations needed' message, since it will likely be resolved by fixing the type of val.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.
on Aug 9, 2020
added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
on Aug 9, 2020
kpreid

kpreid commented on May 11, 2022

@kpreid
Contributor

I was just reviewing issues discussing “type annotations needed” and I think this might be very similar or duplicate to #68507 (where the field type is Option<MissingType> and the value is None, instead of involving a trait).

estebank

estebank commented on Dec 28, 2022

@estebank
Contributor

Current output:

error[E0412]: cannot find type `MissingType` in this scope
 --> src/lib.rs:2:10
  |
2 |     val: MissingType
  |          ^^^^^^^^^^^ not found in this scope

error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
 --> src/lib.rs:6:16
  |
6 |     Foo { val: Default::default() };
  |                ^^^^^^^^^^^^^^^^ cannot call associated function of trait
  |
help: use a fully-qualified path to a specific available implementation (1057 found)
  |
6 |     Foo { val: <std::io::Empty as Default>::default() };
  |                ++++++++++++++++++        +

The fully-qualified path suggestion isn't great. It's likely that val is of [type error], and we should hide the E0790 when that is the case.

added a commit that references this issue on Dec 31, 2022

Rollup merge of rust-lang#106302 - compiler-errors:terr-coerce-w-infe…

abeed83
added a commit that references this issue on Dec 31, 2022

Auto merge of rust-lang#106302 - compiler-errors:terr-coerce-w-infer,…

726bbfc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.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

      Participants

      @kpreid@Aaron1011@estebank@JohnTitor

      Issue actions

        Spurious 'type annotations needed' error when a struct definition has an error · Issue #75331 · rust-lang/rust