Skip to content

Support builtin bounds on associated types and make them Sized by default #17921

Closed
@kennytm

Description

@kennytm

(Moved from http://discuss.rust-lang.org/t/make-associated-types-sized-by-default/615)

Currently this program:

#![feature(associated_types)]
trait A {
    type T;
    fn foo(&self) -> <Self as A>::T;
    fn bar(&self) -> <Self as A>::T { self.foo() }
}
fn main() {}

Fails to compile as:

1.rs:5:39: 5:49 error: cannot move a value of type T: the size of T cannot be statically determined [E0161]
1.rs:5     fn bar(&self) -> <Self as A>::T { self.foo() }
                                             ^~~~~~~~~~

According to RFC 59 we should be able to fix it by (currently unimplemented)

trait A {
    type T : Sized;
    ...
}

But,

  1. Shouldn't associated types be Sized by default? As associated types are to be used to replace "output" type parameters, it is more convenient to be Sized.

  2. If associated types are Sized by default, what should be the syntax then if we want to allow unsized associated types? The generic spelling currently suggests something like

    type Sized? T;
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions