Skip to content

Cannot refer to associated type of generics of the same trait (illegal recursive type) #18655

@kennytm

Description

@kennytm

This code:

#![feature(associated_types)]

trait Factory {
    type Product;
    fn create(&self) -> <Self as Factory>::Product;
}

impl Factory for f64 {
    type Product = f64;
    fn create(&self) -> f64 { *self * *self }
}

impl<A: Factory, B: Factory> Factory for (A, B) {
    type Product = (<A as Factory>::Product, <B as Factory>::Product); // line 14
    fn create(&self) -> (<A as Factory>::Product, <B as Factory>::Product) {
        let (ref a, ref b) = *self;
        (a.create(), b.create())
    }
}

fn main() {
    println!("{}", (4.0f64, 5.0f64).create());
    // ^ should print (16, 25)
}

Failed to compile with error:

1.rs:14:20: 14:70 error: illegal recursive type; insert an enum or struct in the cycle, if this is desired
1.rs:14     type Product = (<A as Factory>::Product, <B as Factory>::Product);
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I think this should be allowed.

Metadata

Metadata

Assignees

No one assigned

    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