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

Some Rust Box operation is internally supported by rustc #777

Open
shaobo-he opened this issue Feb 3, 2022 · 1 comment
Open

Some Rust Box operation is internally supported by rustc #777

shaobo-he opened this issue Feb 3, 2022 · 1 comment

Comments

@shaobo-he
Copy link
Contributor

Consider this program,

// An example from `The Little MLer` chapter 3
#[macro_use]
extern crate smack;
use smack::*;
//#[derive(Debug)]
enum Pizza {
    Crust,
    Cheese(Box<Pizza>),
    Onion(Box<Pizza>),
    Anchovy(Box<Pizza>),
    Sausage(Box<Pizza>),
}
fn make_plain_pizza() -> Pizza {
    Pizza::Crust
}
fn cheese_it_up(pizza: Pizza) -> Pizza {
    Pizza::Cheese(Box::new(pizza))
}
fn add_salt(pizza: Pizza) -> Pizza {
    Pizza::Anchovy(Box::new(pizza))
}
fn remove_anchovy(pizza: Pizza) -> Pizza {
    match pizza {
        Pizza::Crust => pizza,
        Pizza::Cheese(p) => Pizza::Cheese(Box::new(remove_anchovy(*p))),
        Pizza::Onion(p) => Pizza::Onion(Box::new(remove_anchovy(*p))),
        Pizza::Anchovy(p) => remove_anchovy(*p),
        Pizza::Sausage(p) => Pizza::Sausage(Box::new(remove_anchovy(*p))),
    }
}
fn contains_anchovy(pizza: &Pizza) -> bool {
    match pizza {
        Pizza::Crust => false,
        Pizza::Cheese(p) => contains_anchovy(&*p),
        Pizza::Onion(p) => contains_anchovy(&*p),
        Pizza::Anchovy(p) => true,
        Pizza::Sausage(p) => contains_anchovy(&*p),
    }
}
fn main() {
    println!("Hello, world!");
    let salt_double_cheese_pizza = cheese_it_up(add_salt(
                                       cheese_it_up(make_plain_pizza())));
    //println!("{:?}", salt_double_cheese_pizza);
    //assert!(contains_anchovy(&remove_anchovy(salt_double_cheese_pizza)));
    smack::assert!(contains_anchovy(&remove_anchovy(salt_double_cheese_pizza)));
}

SMACK's Box implementation fails to type check with the following message,

error[E0507]: cannot move out of dereference of `smack::Box<Pizza>`
  --> ml.rs:25:67
   |
25 |         Pizza::Cheese(p) => Pizza::Cheese(Box::new(remove_anchovy(*p))),
   |                                                                   ^^ move occurs because value has type `Pizza`, which does not implement the `Copy` trait

It seems *Box<T> is not (cannot be) desugared into a call to deref but instead is treated uniquely by rustc.

This blog post discusses this issue: https://manishearth.github.io/blog/2017/01/10/rust-tidbits-box-is-special/

@zvonimir
Copy link
Member

Can we fix this somehow?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants