Skip to content

Polymorphically creating traits barely works #7673

Closed
@alexcrichton

Description

@alexcrichton

I was confused by the following code:

fn main() {}

trait A {}
impl<T: 'static> A for T {}

fn owned1<T: 'static>(a: T) { ~a as ~A; }  // failed to find an implementation of trait ~A:Send for T
fn owned2<T: 'static>(a: ~T) { a as ~A; }  // failed to find an implementation of trait ~A:Send for T
fn owned3<T: 'static>(a: ~T) { ~a as ~A; } // cannot pack type `~~T`, which does not fulfill `Send`, as a trait bounded by Send

fn managed1<T: 'static>(a: T) { @a as @A; }  // failed to find an implementation of trait @A:'static for T
fn managed2<T: 'static>(a: @T) { a as @A; }  // failed to find an implementation of trait @A:'static for T
fn managed3<T: 'static>(a: @T) { @a as @A; } // only one that works

The only function which compiles is managed3. I was surprised that none of the others worked at all. For managed3, you're required to put a box in a box to get the trait out, while for all the others you just can't create the trait cast at all.

A very surprising result for me was that owned3 didn't work, even though its managed equivalent did work. The error message was different from owned1/2, but I still wasn't really able to decipher any of the errors or understand why they exist.

Are these all legitimate errors? Are some of these supposed to work but they aren't? If these are legitimate errors, I'm of the opinion that the error messages should be a bit more descriptive

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions