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

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

Closed
Aaron1011 opened this issue Aug 9, 2020 · 2 comments · Fixed by #106302
Closed

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

Aaron1011 opened this issue Aug 9, 2020 · 2 comments · Fixed by #106302
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Aaron1011
Copy link
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.

@Aaron1011 Aaron1011 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. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. labels Aug 9, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Aug 9, 2020
@kpreid
Copy link
Contributor

kpreid commented May 11, 2022

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
Copy link
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.

Noratrieb added a commit to Noratrieb/rust that referenced this issue Dec 31, 2022
…r, r=estebank

Suppress errors due to TypeError not coercing with inference variables

Fixes rust-lang#75331
Fixes rust-lang#68507
Fixes rust-lang#82323

cc `@estebank`
@bors bors closed this as completed in 726bbfc Dec 31, 2022
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. 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.

4 participants