Skip to content

Casting to a trait doesn't auto-coerce to a &T type #3794

@erickt

Description

@erickt

Here's an example:

https://gist.github.com/3907509

It seems that even if I produce a proper boxed type @T, rust 0.4 cannot coerce that type to a &T.


Felix is putting the contents of the above linked gist so that people using ghi can see them readily:

test.rs:

trait T {
  fn print(&self);
}

struct S {
  s: int,
}

impl S: T {
  fn print(&self) {
    io::println(fmt!("%?", self));
  }
}

fn print_t(t: &T) {
  t.print();
}

fn print_s(s: &S) {
  s.print();
}

fn main() {
  let s: @S = @S { s: 5 };
  print_s(s);
  let t: @T = s as @T;
  print_t(t);
}

output:

test.rs:27:12: 27:13 error: mismatched types: expected `&/T` but found `@T` (trait storage differs: expected & but found @)
test.rs:27 print_t(t);
^
error: aborting due to previous error

Metadata

Metadata

Assignees

No one assigned

    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