Skip to content

Commit 28d18fa

Browse files
committed
Unit test for the new implicit borrow and deref within the
guard expressions of matches (activated only when using new NLL mode). Review feedback: removed 27282 from filename. (The test still references it in a relevant comment in the file itself so that seemed like a reasonable compromise.)
1 parent a72790d commit 28d18fa

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2012-2014 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+
//revisions: ast mir
12+
//[mir] compile-flags: -Z borrowck=mir
13+
14+
#![feature(rustc_attrs)]
15+
16+
// Here is arielb1's basic example from rust-lang/rust#27282
17+
// that AST borrowck is flummoxed by:
18+
19+
fn should_reject_destructive_mutate_in_guard() {
20+
match Some(&4) {
21+
None => {},
22+
ref mut foo if {
23+
(|| { let bar = foo; bar.take() })();
24+
//[mir]~^ ERROR cannot move out of borrowed content [E0507]
25+
false } => { },
26+
Some(s) => std::process::exit(*s),
27+
}
28+
}
29+
30+
// Here below is a case that needs to keep working: we only use the
31+
// binding via immutable-borrow in the guard, and we mutate in the arm
32+
// body.
33+
fn allow_mutate_in_arm_body() {
34+
match Some(&4) {
35+
None => {},
36+
ref mut foo if foo.is_some() && false => { foo.take(); () }
37+
Some(s) => std::process::exit(*s),
38+
}
39+
}
40+
41+
// Here below is a case that needs to keep working: we only use the
42+
// binding via immutable-borrow in the guard, and we move into the arm
43+
// body.
44+
fn allow_move_into_arm_body() {
45+
match Some(&4) {
46+
None => {},
47+
mut foo if foo.is_some() && false => { foo.take(); () }
48+
Some(s) => std::process::exit(*s),
49+
}
50+
}
51+
52+
// Since this is a compile-fail test that is explicitly encoding the
53+
// different behavior of AST- vs MIR-borrowck where AST-borrowck does
54+
// not error, we need to use rustc_error to placate the test harness
55+
// that wants *some* error to occur.
56+
#[rustc_error]
57+
fn main() { //[ast]~ ERROR compilation successful
58+
should_reject_destructive_mutate_in_guard();
59+
allow_mutate_in_arm_body();
60+
allow_move_into_arm_body();
61+
}

0 commit comments

Comments
 (0)