Skip to content

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

Closed
@jonnydee

Description

@jonnydee

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

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