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

Dereferencing boxed tuple doesn't compile properly. #30564

Closed
Mokosha opened this issue Dec 26, 2015 · 9 comments
Closed

Dereferencing boxed tuple doesn't compile properly. #30564

Mokosha opened this issue Dec 26, 2015 · 9 comments
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug.

Comments

@Mokosha
Copy link
Contributor

Mokosha commented Dec 26, 2015

I think I've encountered a bug, as was described in this stack overflow question. In particular, I would expect the syntax in the following code to be allowed:

#[derive(Clone, PartialEq, Debug)]
enum Foo {
  Base,
  Branch(Box<(Foo, Foo)>)
}

fn do_something(f: Foo) -> Foo {
  match f {
    Foo::Base => Foo::Base,
    Foo::Branch(pair) => {
      let (f1, f2) = *pair;  // ... What do I do here?
      if f2 == Foo::Base { f1 } else { f2 }
   }
  }
}

fn main() {
  let foo = Foo::Base;
  println!("Foo: {:?}", do_something(foo));
}

Instead, I need to do something like

let p = *pair;
let (f1, f2) = p;

Meta

I tried this on the latest nightly release of rustc using the rust playground and it still doesn't work. I'm still somewhat new with rust so let me know if this isn't a bug!

@leoyvens
Copy link
Contributor

#30104 seems related.

@Aatch
Copy link
Contributor

Aatch commented Dec 27, 2015

It is related to #30104. Looks like an issue with how moves and pattern matching interact. One issue is that Box<T> is the only pointer type you can actually do this with and we don't want to special case any more than it already is, so this (and the other issue) may have to wait for work on bringing library smart-pointers up to parity with Box<T>.

@gamazeps
Copy link
Contributor

gamazeps commented Jan 7, 2016

A simpler example could be:

    let a = Box::new(("foo".to_owned(), "bar".to_owned()));
    let (b, c) = *a;

This has to be fixed with:

    let a = Box::new(("foo".to_owned(), "bar".to_owned()));
    let tmp = *a;
    let (b, c) = tmp;

@aidanhs
Copy link
Member

aidanhs commented Oct 17, 2016

Or

let a = Box::new(("foo".to_owned(), "bar".to_owned()));
let (b, c) = {*a};

@mbrubeck
Copy link
Contributor

One of the duplicate issues notes that this behavior was introduced as a consequence of rust-lang/rfcs#130.

@jonhoo
Copy link
Contributor

jonhoo commented Oct 31, 2017

Is this a dupe of #16223?

@arielb1
Copy link
Contributor

arielb1 commented Oct 31, 2017

@jonhoo

This isn't strictly defined by rust-lang/rfcs#130, but it's a variant of the same issue. We want a test that this has stable behavior between ast & mir borrowck https://github.com/rust-lang/rust/blob/5d4d09daf2e8e46839647d4e72b1cbefebad6ece/src/test/compile-fail/borrowck-box-insensitivity.rs

@arielb1 arielb1 closed this as completed Oct 31, 2017
@Mokosha
Copy link
Contributor Author

Mokosha commented Oct 31, 2017

Sorry -- just to clarify: this is the intended behavior?

@jonhoo
Copy link
Contributor

jonhoo commented Nov 1, 2017

@arielb1 I'm not sure I follow. Unless I missed it, the linked .rs does not have any test case similar to the case mentioned above. I guess maybe

let _y = a.y; //~ ERROR use of partially moved
, though that being an error already sounds questionable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

10 participants