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 error message: E0283 ("type annotations needed") when proper solution is to remove where-constraint #73082

Closed
graydon opened this issue Jun 7, 2020 · 3 comments

Comments

@graydon
Copy link
Contributor

graydon commented Jun 7, 2020

I ran into an issue with serde derive(Deserialize) on a struct that had type parameter that already implemented Deserialize. The error message suggests adding a type annotation to help infer the type of the type parameter; when in fact (as I learned at some length later while experimenting with the macro-expanded code) the correct solution is to remove a redundant where-clause in the generated impl.

The following minimal (not-using-serde) example shows the problem:

trait Deserialize<'a> {}
trait DeserializeOwned: for<'a> Deserialize<'a> {}
trait Mine : DeserializeOwned {}

// The following line, if un-commented:
//
// impl <'a, M:Mine> Deserialize<'a> for M where M: Deserialize<'a> {}
//
// Gives error message "type annotations needed", whereas the actual
// solution is to _delete_ the redundant where-clause and compile
// the following smaller version:

impl <'a, M:Mine> Deserialize<'a> for M {}

fn main()
{
    
}                                                                                                                                                         

(Playground)

rustc 1.44.0-nightly (1406186 2020-04-10)
binary: rustc
commit-hash: 1406186
commit-date: 2020-04-10
host: x86_64-unknown-linux-gnu
release: 1.44.0-nightly
LLVM version: 9.0

@dtolnay
Copy link
Member

dtolnay commented Jun 7, 2020

The reason the misleading "type annotations needed" error is present in the first place is a longstanding compiler bug: #41617. Other than that, what you originally wrote (impl<'a, M: Mine> Deserialize<'a> for M where M: Deserialize<'a>) is supposed to compile.

@graydon
Copy link
Contributor Author

graydon commented Jun 7, 2020

Aha, fascinating! Thanks for the information, I guess this can be closed as redundant?

@jonas-schievink
Copy link
Contributor

Closing in favor of #41617 then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants