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

A move-through-deref is confusing rustc somehow #40439

Closed
jonnydee opened this issue Mar 11, 2017 · 1 comment
Closed

A move-through-deref is confusing rustc somehow #40439

jonnydee opened this issue Mar 11, 2017 · 1 comment

Comments

@jonnydee
Copy link

The compiler complains with an error but the code is supposed to be accepted:

I tried this code:

#[derive(Debug)]
enum BinTree {
    Node {
        lhs: Box<BinTree>,
        rhs: Box<BinTree>,
    },
    Leaf,
}

fn print_tree(bt: BinTree) {
    println!("{:?}", bt);
}

fn main() {
    let t = Box::new(BinTree::Node {
        lhs: Box::new(BinTree::Leaf),
        rhs: Box::new(BinTree::Leaf),
    });

    match *t {
        BinTree::Node { lhs: l, rhs: r } => print_tree(*l),
        _ => println!("Other"),
    }
}

I expected to see this happen:

The compiler should accept this code as it does when I change it to:

let t = *t; match t { ... }

The compiler also has no problem, if I change the match arm to:

BinTree::Node { lhs: l, .. } => print_tree(*l), // Note the '..' here.

Instead, this happened:

The compiler rejects this code with the following error message:

rustc 1.15.1 (021bd294c 2017-02-08)
error[E0382]: use of collaterally moved value: `(t:BinTree::Node).rhs`
  --> <anon>:21:38
   |
21 |         BinTree::Node { lhs: l, rhs: r } => print_tree(*l),
   |                              -       ^ value used here after move
   |                              |
   |                              value moved here
   |
   = note: move occurs because `(t:BinTree::Node).lhs` has type `Box<BinTree>`, which does not implement the `Copy` trait

error: aborting due to previous error

Meta

rustc --version --verbose:

rustc 1.15.0 (10893a9a3 2017-01-19)
binary: rustc
commit-hash: 10893a9a349cdd423f2490a6984acb5b3b7c8046
commit-date: 2017-01-19
host: x86_64-apple-darwin
release: 1.15.0
LLVM version: 3.9
@jonnydee
Copy link
Author

jonnydee commented Mar 11, 2017

A very nice guy on the IRC pointed out that this bug seems to be related to #30104, #34859 and #30564. So I close it again. Sorry for any inconvenience.

Using the following code works:

match { *t } { ... } // Note the use of curly braces here.

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

1 participant