-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Segfault when returning a boxed struct as a trait object from a match #20097
Comments
cc @nick29581, @luqmana |
I forgot to add that by explicitly casting, the crash goes away: let t: Box<Thing> = match true {
true => box Thing1 { name: "hello".to_string() } as Box<Thing>,
_ => box Thing2 { name: "hello".to_string() } as Box<Thing>,
}; |
+1, just ran into this same issue with today's nightly on Linux, with almost the exact same code and workaround. (I'm a little jealous, my
|
I believe that I am hitting this issue as well. Here is my repro: #![allow(dead_code)]
pub trait Foo : Send {
fn foo<'a>(me: Managed<'a, Self>);
}
pub trait BoxedFoo : Send {
fn foo_boxed(self: Box<Self>);
}
pub trait ToFoo<F: Foo> {
fn to_box(self) -> Box<BoxedFoo>;
}
/*
*
* ===== impl Foo for String =====
*
*/
impl Foo for String {
fn foo<'a>(me: Managed<'a, Self>) {
println!("foo: {}", me.take());
}
}
impl<F: Foo> BoxedFoo for F {
fn foo_boxed(self: Box<Self>) {
Foo::foo(Managed::heap(self));
}
}
impl<F: Foo> ToFoo<F> for F {
fn to_box(self) -> Box<BoxedFoo> {
box self
}
}
impl<'a, F: Foo> ToFoo<F> for Managed<'a, F> {
fn to_box(self) -> Box<BoxedFoo> {
match self.store {
Store::Heap(boxed) => boxed,
Store::Stack(opt) => box opt.take().unwrap(),
}
}
}
/*
*
* ===== Managed =====
*
*/
pub struct Managed<'a, T:'a> {
store: Store<'a, T>,
}
impl<'a, T> Managed<'a, T> {
fn heap(val: Box<T>) -> Managed<'a, T> {
Managed { store: Store::Heap(val) }
}
fn take(self) -> T {
match self.store {
Store::Heap(boxed) => *boxed,
Store::Stack(opt) => opt.take().expect("option is none"),
}
}
}
enum Store<'a, T:'a> {
Heap(Box<T>), // Callback is already heap allocated
Stack(&'a mut Option<T>), // Callback is stack allocated
}
fn do_foo<T: ToFoo<F>, F: Foo>(f: T) {
f.to_box().foo_boxed();
}
pub fn main() {
do_foo(Managed::heap(box "foo".to_string()));
} Backtrace:
Rustc:
|
Another workaround is to put the body of the match arm in
|
@kevinmehall I think that #21695 is a duplicate of this issue. 😈 |
Running this code yields various failures (SIGSEGV, "Illegal instruction: 4"):
LLDB has this to say:
and I'm reasonably up-to-date:
The text was updated successfully, but these errors were encountered: