Skip to content

Unhelpful error message: E0283 ("type annotations needed") when proper solution is to remove where-constraint #73082

@graydon

Description

@graydon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions