Skip to content

Feature associated_type_defaults fails to resolve type equality #41868

Closed
@chrivers

Description

@chrivers

As far as I can tell, this is a legitimate bug, and I have not been able to find any similar reports on github.

I have a project where I have greatly simplified my codebase using associated_type_defaults. However, it seems like the type checker can't figure out that the literal type of the default case is the same as the default case. I have condensed my problem to the following test-case.

I feel both variants should definitely be valid, since the default is clearly Self. Have I misunderstood something, or is this a bug?

#![feature(associated_type_defaults)]
// rustc 1.19.0-nightly (f1140a331 2017-05-08)
pub struct Foo;

pub trait CanDecode {
    type Output = Self;
    fn read(rdr: &mut Foo) -> Option<Self::Output>;
}

impl CanDecode for u8 {
    fn read(rdr: &mut Foo) -> Option<Self::Output> { Some(42) }
}

impl CanDecode for u16 {
    fn read(rdr: &mut Foo) -> Option<u16> { Some(17) }
}

/*
error[E0053]: method `read` has an incompatible type for trait
  --> yo_dawg_i_heard_you_like_traits.rs:14:31
   |
6  |     fn read(rdr: &mut Foo) -> Option<Self::Output>;
   |                               -------------------- type in trait
...
14 |     fn read(rdr: &mut Foo) -> Option<u16> { Some(17) }
   |                               ^^^^^^^^^^^ expected associated type, found u16
   |
   = note: expected type `fn(&mut Foo) -> std::option::Option<<u16 as CanDecode>::Output>`
              found type `fn(&mut Foo) -> std::option::Option<u16>`

error: aborting due to previous error
*/

Metadata

Metadata

Labels

A-trait-systemArea: Trait systemA-type-systemArea: Type systemC-bugCategory: This is a bug.F-associated_type_defaults`#![feature(associated_type_defaults)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions