Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
erickt opened this issue Oct 17, 2012 · 8 comments
Closed

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

erickt opened this issue Oct 17, 2012 · 8 comments
Labels
A-trait-system Area: Trait system A-type-system Area: Type system

Comments

@erickt
Copy link
Contributor

erickt commented Oct 17, 2012

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
@nikomatsakis
Copy link
Contributor

Is this fixed?

@catamorphism
Copy link
Contributor

Not yet (tested in d2ad028 )

@catamorphism
Copy link
Contributor

I have a fix. Running tests.

@ghost ghost assigned catamorphism Dec 13, 2012
@catamorphism
Copy link
Contributor

Pending review: #4178

@nikomatsakis
Copy link
Contributor

Not critical for 0.6; removing milestone

@catamorphism
Copy link
Contributor

Nominating for milestone 5 (production-ready)

@metajack
Copy link
Contributor

Still a problem, but here is the updated test case:

use std::io;

trait T {
  fn print(&self);
}

struct S {
  s: int,
}

impl T for S {
  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);
}

which produces

issue3974.rs:29:10: 29:11 error: mismatched types: expected `&T<no-bounds>` but found `@T:'static` (trait storage differs: expected & but found @)
issue3974.rs:29   print_t(t);
                          ^

@graydon
Copy link
Contributor

graydon commented Jul 11, 2013

accepted for production-ready milestone

@huonw huonw closed this as completed in 12099ce Sep 3, 2013
RalfJung pushed a commit to RalfJung/rust that referenced this issue Aug 10, 2024
RalfJung pushed a commit to RalfJung/rust that referenced this issue Aug 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

5 participants