Skip to content

Commit 5aba0a6

Browse files
committed
add ui tests for move expr
1 parent cac3426 commit 5aba0a6

12 files changed

Lines changed: 197 additions & 0 deletions

tests/ui/move-expr/borrow-only.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ check-pass
2+
#![allow(incomplete_features)]
3+
#![feature(move_expr)]
4+
5+
fn main() {
6+
let s = vec![1, 2, 3];
7+
let c = || {
8+
let t = &move(s);
9+
println!("{t:?}");
10+
};
11+
12+
c();
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![allow(incomplete_features)]
2+
#![feature(move_expr)]
3+
4+
fn main() {
5+
let c = {
6+
let x = 22;
7+
|| {
8+
let y = move(&x);
9+
//~^ ERROR `x` does not live long enough
10+
println!("{y:?}");
11+
}
12+
};
13+
14+
c();
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0597]: `x` does not live long enough
2+
--> $DIR/capture-reference.rs:8:26
3+
|
4+
LL | let c = {
5+
| - borrow later captured here by closure
6+
LL | let x = 22;
7+
| - binding `x` declared here
8+
LL | || {
9+
LL | let y = move(&x);
10+
| ^^ borrowed value does not live long enough
11+
...
12+
LL | };
13+
| - `x` dropped here while still borrowed
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0597`.

tests/ui/move-expr/copy-type.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ check-pass
2+
#![allow(incomplete_features)]
3+
#![feature(move_expr)]
4+
5+
fn main() {
6+
let x = 22;
7+
let c = || {
8+
let y = move(x);
9+
let z = x;
10+
assert_eq!(y + z, 44);
11+
};
12+
13+
c();
14+
c();
15+
}

tests/ui/move-expr/double-move.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![allow(incomplete_features)]
2+
#![feature(move_expr)]
3+
4+
fn main() {
5+
let x = vec![1, 2, 3];
6+
let _c = || {
7+
let y = move(x);
8+
let z = move(x);
9+
//~^ ERROR use of moved value: `x`
10+
drop(y);
11+
drop(z);
12+
};
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0382]: use of moved value: `x`
2+
--> $DIR/double-move.rs:8:22
3+
|
4+
LL | let x = vec![1, 2, 3];
5+
| - move occurs because `x` has type `Vec<i32>`, which does not implement the `Copy` trait
6+
LL | let _c = || {
7+
LL | let y = move(x);
8+
| - value moved here
9+
LL | let z = move(x);
10+
| ^ value used here after move
11+
|
12+
help: consider cloning the value if the performance cost is acceptable
13+
|
14+
LL | let y = move(x.clone());
15+
| ++++++++
16+
help: borrow this binding in the pattern to avoid moving the value
17+
|
18+
LL | let y = move(ref x);
19+
| +++
20+
21+
error: aborting due to 1 previous error
22+
23+
For more information about this error, try `rustc --explain E0382`.

tests/ui/move-expr/move-fnonce.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![allow(incomplete_features)]
2+
#![feature(move_expr)]
3+
4+
fn main() {
5+
let s = vec![1, 2, 3];
6+
let mut c = || {
7+
let t = move(s);
8+
println!("{t:?}");
9+
};
10+
11+
c();
12+
c();
13+
//~^ ERROR use of moved value: `c`
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0382]: use of moved value: `c`
2+
--> $DIR/move-fnonce.rs:12:5
3+
|
4+
LL | c();
5+
| --- `c` moved due to this call
6+
LL | c();
7+
| ^ value used here after move
8+
|
9+
note: closure cannot be invoked more than once because it moves the variable `__move_expr_0` out of its environment
10+
--> $DIR/move-fnonce.rs:7:17
11+
|
12+
LL | let t = move(s);
13+
| ^^^^^^^
14+
note: this value implements `FnOnce`, which causes it to be moved when called
15+
--> $DIR/move-fnonce.rs:11:5
16+
|
17+
LL | c();
18+
| ^
19+
20+
error: aborting due to 1 previous error
21+
22+
For more information about this error, try `rustc --explain E0382`.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ check-pass
2+
//@ ignore-test (#155050): currently ICEs instead of reporting a name-resolution error
3+
// FIXME(TaKO8Ki): Remove this ignore once closure-local names in `move(expr)` produce a real
4+
// diagnostic instead of hitting the current `Res::Err` ICE path.
5+
#![allow(incomplete_features)]
6+
#![feature(move_expr)]
7+
8+
fn main() {
9+
let _c = || {
10+
let x = 3;
11+
move(x);
12+
};
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ check-pass
2+
#![allow(incomplete_features)]
3+
#![feature(move_expr)]
4+
5+
fn main() {
6+
let x = String::from("hello");
7+
let outer = || {
8+
let inner = || move(x.clone());
9+
let y = inner();
10+
assert_eq!(y, "hello");
11+
assert_eq!(x, "hello");
12+
};
13+
14+
outer();
15+
}

0 commit comments

Comments
 (0)