|
| 1 | +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// Regression test for #16223: without NLL the `if let` construct together with |
| 12 | +// the nested box-structure of `Root` causes an unwanted collateral move. |
| 13 | + |
| 14 | +// The exact error prevented here is: |
| 15 | +// |
| 16 | +// error[E0382]: use of collaterally moved value: `(root.boxed.rhs as SomeVariant::B).0` |
| 17 | +// --> src/main.rs:30:75 |
| 18 | +// | |
| 19 | +// 30 | if let box SetOfVariants{ lhs: SomeVariant::A(a), rhs: SomeVariant::B(b) } = root.boxed { |
| 20 | +// | - value moved here ^ value used here after move |
| 21 | +// | |
| 22 | +// = note: move occurs because the value has type `A`, which does not implement the `Copy` trait |
| 23 | + |
| 24 | +// must-compile-successfully |
| 25 | + |
| 26 | +#![feature(nll)] |
| 27 | +#![feature(box_patterns)] |
| 28 | + |
| 29 | +struct Root { |
| 30 | + boxed: Box<SetOfVariants> |
| 31 | +} |
| 32 | + |
| 33 | +struct SetOfVariants { |
| 34 | + lhs: SomeVariant, |
| 35 | + rhs: SomeVariant |
| 36 | +} |
| 37 | + |
| 38 | +enum SomeVariant { |
| 39 | + A(A), |
| 40 | + B(B) |
| 41 | +} |
| 42 | + |
| 43 | +struct A(String); |
| 44 | +struct B(String); |
| 45 | + |
| 46 | +fn main() { |
| 47 | + let root = Root { |
| 48 | + boxed: Box::new( |
| 49 | + SetOfVariants{ |
| 50 | + lhs: SomeVariant::A(A(String::from("This is A"))), |
| 51 | + rhs: SomeVariant::B(B(String::from("This is B"))) |
| 52 | + } |
| 53 | + ) |
| 54 | + }; |
| 55 | + if let box SetOfVariants{ lhs: SomeVariant::A(a), rhs: SomeVariant::B(b) } = root.boxed { |
| 56 | + println!("a = {}", a.0); |
| 57 | + println!("b = {}", b.0); |
| 58 | + } |
| 59 | +} |
0 commit comments