Skip to content

Specialization on associated type #85228

@vandenheuvel

Description

@vandenheuvel

Specializing on an associated type doesn't yet work.

#![feature(specialization)]

trait Tr {
    type X;
    fn f(&self) -> Self::X;
}

// Works
// impl<T> Tr for T {
//     type X = i8;
//     fn f(&self) -> Self::X { 1_i8 }
// }

// Works
// impl<T> Tr for T {
//     type X = i8;
//     
//     default fn f(&self) -> Self::X {
//         1_i8
//     }
// }

// Doesn't work
impl<T> Tr for T {
    default type X = i8;
    
    default fn f(&self) -> Self::X {
        1_i8
    }
}

Error:

error[E0308]: mismatched types
  --> src/lib.rs:28:5
   |
25 |     default type X = i8;
   |     -------------------- expected this associated type
26 |     
27 |     default fn f(&self) -> Self::X {
   |                            ------- expected `<T as Tr>::X` because of return type
28 |         1_i8
   |         ^^^^ expected associated type, found `i8`
   |
   = note: expected associated type `<T as Tr>::X`
                         found type `i8`

error: aborting due to previous error; 1 warning emitted

On 1.54.0-nightly (2021-05-11 5c029265465301fe9cb3). Playground.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-specializationArea: Trait impl specializationC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.F-specialization`#![feature(specialization)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    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