Skip to content

Commit f62c210

Browse files
author
Hero
committed
add regression test for issue rust-lang#16223: fixed by NLL
1 parent 4b9b70c commit f62c210

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Diff for: src/test/ui/nll/issue-16223.rs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2018 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:55:29
18+
// |
19+
// 56 | lhs: SomeVariant::A(a),
20+
// | - value moved here
21+
// 57 | rhs: SomeVariant::B(b),
22+
// | ^ value used here after move
23+
// |
24+
// = note: move occurs because the value has type `A`, which does not implement the `Copy` trait
25+
26+
// must-compile-successfully
27+
28+
#![feature(nll)]
29+
#![feature(box_patterns)]
30+
31+
struct Root {
32+
boxed: Box<SetOfVariants>,
33+
}
34+
35+
struct SetOfVariants {
36+
lhs: SomeVariant,
37+
rhs: SomeVariant,
38+
}
39+
40+
enum SomeVariant {
41+
A(A),
42+
B(B),
43+
}
44+
45+
struct A(String);
46+
struct B(String);
47+
48+
fn main() {
49+
let root = Root {
50+
boxed: Box::new(SetOfVariants {
51+
lhs: SomeVariant::A(A(String::from("This is A"))),
52+
rhs: SomeVariant::B(B(String::from("This is B"))),
53+
}),
54+
};
55+
if let box SetOfVariants {
56+
lhs: SomeVariant::A(a),
57+
rhs: SomeVariant::B(b),
58+
} = root.boxed
59+
{
60+
println!("a = {}", a.0);
61+
println!("b = {}", b.0);
62+
}
63+
}

0 commit comments

Comments
 (0)