Skip to content

Segfault on calling method on owned trait object #7208

Closed
@benw

Description

@benw

This segfaults on the second call to fruit.eat():

trait Fruit {
  fn eat(&self);
}

struct Apple {
  x: int
}

impl Fruit for Apple {
  fn eat(&self) {
    println(fmt!("Crunching on apple %d", self.x));
  }
}

fn main() {
  let fruits: ~[ ~Fruit ] = ~[
    ~Apple{ x: 1 } as ~Fruit,
    ~Apple{ x: 2 } as ~Fruit,
    ~Apple{ x: 3 } as ~Fruit
  ];

  for fruits.each |fruit| {
    println(~"before");
    fruit.eat();
    println(~"after");
  }
}

Results:

> rustc --version
rustc 0.6
host: x86_64-apple-darwin
> rustc test.rs 
warning: no debug symbols in executable (-arch x86_64)
> ./test
before
Crunching on apple 1
after
before
Segmentation fault: 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions