Skip to content

Commit a5b008c

Browse files
committed
Test move-into-Fn/FnMut errors too
1 parent 6a24abb commit a5b008c

File tree

3 files changed

+530
-2
lines changed

3 files changed

+530
-2
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
#![feature(nll)]
12+
13+
#[derive(Clone)]
14+
enum Either {
15+
One(X),
16+
Two(X),
17+
}
18+
19+
#[derive(Clone)]
20+
struct X(Y);
21+
22+
#[derive(Clone)]
23+
struct Y;
24+
25+
fn consume_fn<F: Fn()>(_f: F) { }
26+
27+
fn consume_fnmut<F: FnMut()>(_f: F) { }
28+
29+
pub fn main() { }
30+
31+
fn move_into_fn() {
32+
let e = Either::One(X(Y));
33+
let mut em = Either::One(X(Y));
34+
35+
let x = X(Y);
36+
37+
// -------- move into Fn --------
38+
39+
consume_fn(|| {
40+
let X(_t) = x;
41+
//~^ ERROR cannot move
42+
//~| HELP consider borrowing here
43+
//~| SUGGESTION &x
44+
if let Either::One(_t) = e { }
45+
//~^ ERROR cannot move
46+
//~| HELP consider borrowing here
47+
//~| SUGGESTION &e
48+
while let Either::One(_t) = e { }
49+
//~^ ERROR cannot move
50+
//~| HELP consider borrowing here
51+
//~| SUGGESTION &e
52+
match e {
53+
//~^ ERROR cannot move
54+
//~| HELP consider borrowing here
55+
//~| SUGGESTION &e
56+
Either::One(_t)
57+
| Either::Two(_t) => (),
58+
}
59+
match e {
60+
//~^ ERROR cannot move
61+
//~| HELP consider borrowing here
62+
//~| SUGGESTION &e
63+
Either::One(_t) => (),
64+
Either::Two(ref _t) => (),
65+
// FIXME: should suggest removing `ref` too
66+
}
67+
68+
let X(mut _t) = x;
69+
//~^ ERROR cannot move
70+
//~| HELP consider borrowing here
71+
//~| SUGGESTION &x
72+
if let Either::One(mut _t) = em { }
73+
//~^ ERROR cannot move
74+
//~| HELP consider borrowing here
75+
//~| SUGGESTION &em
76+
while let Either::One(mut _t) = em { }
77+
//~^ ERROR cannot move
78+
//~| HELP consider borrowing here
79+
//~| SUGGESTION &em
80+
match em {
81+
//~^ ERROR cannot move
82+
//~| HELP consider borrowing here
83+
//~| SUGGESTION &em
84+
Either::One(mut _t)
85+
| Either::Two(mut _t) => (),
86+
}
87+
match em {
88+
//~^ ERROR cannot move
89+
//~| HELP consider borrowing here
90+
//~| SUGGESTION &em
91+
Either::One(mut _t) => (),
92+
Either::Two(ref _t) => (),
93+
// FIXME: should suggest removing `ref` too
94+
}
95+
});
96+
}
97+
98+
fn move_into_fnmut() {
99+
let e = Either::One(X(Y));
100+
let mut em = Either::One(X(Y));
101+
102+
let x = X(Y);
103+
104+
// -------- move into FnMut --------
105+
106+
consume_fnmut(|| {
107+
let X(_t) = x;
108+
//~^ ERROR cannot move
109+
//~| HELP consider borrowing here
110+
//~| SUGGESTION &x
111+
if let Either::One(_t) = e { }
112+
//~^ ERROR cannot move
113+
//~| HELP consider borrowing here
114+
//~| SUGGESTION &e
115+
while let Either::One(_t) = e { }
116+
//~^ ERROR cannot move
117+
//~| HELP consider borrowing here
118+
//~| SUGGESTION &e
119+
match e {
120+
//~^ ERROR cannot move
121+
//~| HELP consider borrowing here
122+
//~| SUGGESTION &e
123+
Either::One(_t)
124+
| Either::Two(_t) => (),
125+
}
126+
match e {
127+
//~^ ERROR cannot move
128+
//~| HELP consider borrowing here
129+
//~| SUGGESTION &e
130+
Either::One(_t) => (),
131+
Either::Two(ref _t) => (),
132+
// FIXME: should suggest removing `ref` too
133+
}
134+
135+
let X(mut _t) = x;
136+
//~^ ERROR cannot move
137+
//~| HELP consider borrowing here
138+
//~| SUGGESTION &x
139+
if let Either::One(mut _t) = em { }
140+
//~^ ERROR cannot move
141+
//~| HELP consider borrowing here
142+
//~| SUGGESTION &em
143+
while let Either::One(mut _t) = em { }
144+
//~^ ERROR cannot move
145+
//~| HELP consider borrowing here
146+
//~| SUGGESTION &em
147+
match em {
148+
//~^ ERROR cannot move
149+
//~| HELP consider borrowing here
150+
//~| SUGGESTION &em
151+
Either::One(mut _t)
152+
| Either::Two(mut _t) => (),
153+
}
154+
match em {
155+
//~^ ERROR cannot move
156+
//~| HELP consider borrowing here
157+
//~| SUGGESTION &em
158+
Either::One(mut _t) => (),
159+
Either::Two(ref _t) => (),
160+
// FIXME: should suggest removing `ref` too
161+
}
162+
match em {
163+
//~^ ERROR cannot move
164+
//~| HELP consider borrowing here
165+
//~| SUGGESTION &em
166+
Either::One(mut _t) => (),
167+
Either::Two(ref mut _t) => (),
168+
// FIXME: should suggest removing `ref` too
169+
}
170+
});
171+
}

0 commit comments

Comments
 (0)