Skip to content

Commit 7c17bf8

Browse files
committed
Add tests
1 parent 38b4746 commit 7c17bf8

17 files changed

+357
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Check that using the parameter name in its type does not ICE.
2+
//@ edition:2018
3+
4+
#![feature(ergonomic_clones)]
5+
6+
fn main() {
7+
let _ = async use |x: x| x; //~ ERROR expected type
8+
let _ = async use |x: bool| -> x { x }; //~ ERROR expected type
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0573]: expected type, found local variable `x`
2+
--> $DIR/local-type.rs:7:27
3+
|
4+
LL | let _ = async use |x: x| x;
5+
| ^ not a type
6+
7+
error[E0573]: expected type, found local variable `x`
8+
--> $DIR/local-type.rs:8:36
9+
|
10+
LL | let _ = async use |x: bool| -> x { x };
11+
| ^ not a type
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0573`.

tests/ui/ergonomic-clones/closure/basic.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,18 @@ fn ergonomic_clone_closure_use_cloned() -> Foo {
4040
f
4141
}
4242

43+
fn ergonomic_clone_closure_copy() -> i32 {
44+
let i = 1;
45+
46+
let i1 = use || {
47+
i
48+
};
49+
50+
let i2 = use || {
51+
i
52+
};
53+
54+
i
55+
}
56+
4357
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(ergonomic_clones)]
2+
3+
fn get_closure() -> Box<dyn Fn() -> Vec<u8>> {
4+
let vec = vec![1u8, 2u8];
5+
6+
let closure = use || { //~ ERROR expected a closure
7+
vec
8+
};
9+
10+
Box::new(closure)
11+
}
12+
13+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
2+
--> $DIR/fn-once.rs:6:19
3+
|
4+
LL | let closure = use || {
5+
| ^^^^^^ this closure implements `FnOnce`, not `Fn`
6+
LL | vec
7+
| --- closure is `FnOnce` because it moves the variable `vec` out of its environment
8+
...
9+
LL | Box::new(closure)
10+
| ----------------- the requirement to implement `Fn` derives from here
11+
|
12+
= note: required for the cast from `Box<{closure@$DIR/fn-once.rs:6:19: 6:25}>` to `Box<(dyn Fn() -> Vec<u8> + 'static)>`
13+
14+
error: aborting due to 1 previous error
15+
16+
For more information about this error, try `rustc --explain E0525`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Check that using the parameter name in its type does not ICE.
2+
3+
#![feature(ergonomic_clones)]
4+
5+
fn main() {
6+
let _ = use |x: x| x; //~ ERROR expected type
7+
let _ = use |x: bool| -> x { x }; //~ ERROR expected type
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0573]: expected type, found local variable `x`
2+
--> $DIR/local-type.rs:6:21
3+
|
4+
LL | let _ = use |x: x| x;
5+
| ^ not a type
6+
7+
error[E0573]: expected type, found local variable `x`
8+
--> $DIR/local-type.rs:7:30
9+
|
10+
LL | let _ = use |x: bool| -> x { x };
11+
| ^ not a type
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0573`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ run-pass
2+
// Testing guarantees provided by once functions.
3+
4+
#![feature(ergonomic_clones)]
5+
6+
use std::sync::Arc;
7+
8+
fn foo<F: FnOnce()>(blk: F) {
9+
blk();
10+
}
11+
12+
pub fn main() {
13+
let x = Arc::new(true);
14+
foo(use || {
15+
assert!(*x);
16+
drop(x);
17+
});
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![feature(ergonomic_clones)]
2+
3+
fn parse1() {
4+
|| use {
5+
//~^ ERROR expected one of `async`, `|`, or `||`, found `{`
6+
};
7+
}
8+
9+
fn parse2() {
10+
move use || {
11+
//~^ ERROR expected one of `async`, `|`, or `||`, found keyword `use`
12+
};
13+
}
14+
15+
fn parse3() {
16+
use move || {
17+
//~^ ERROR expected identifier, found keyword `move`
18+
//~| ERROR expected one of `::`, `;`, or `as`, found `||`
19+
// FIXME ideally we should error like in the previous example
20+
};
21+
}
22+
23+
fn main() {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: expected one of `async`, `|`, or `||`, found `{`
2+
--> $DIR/parse.rs:4:12
3+
|
4+
LL | || use {
5+
| -- ^ expected one of `async`, `|`, or `||`
6+
| |
7+
| while parsing the body of this closure
8+
|
9+
help: you might have meant to open the body of the closure, instead of enclosing the closure in a block
10+
|
11+
LL ~ fn parse1()
12+
LL ~ || { use {
13+
|
14+
15+
error: expected one of `async`, `|`, or `||`, found keyword `use`
16+
--> $DIR/parse.rs:10:10
17+
|
18+
LL | move use || {
19+
| ^^^ expected one of `async`, `|`, or `||`
20+
21+
error: expected identifier, found keyword `move`
22+
--> $DIR/parse.rs:16:9
23+
|
24+
LL | use move || {
25+
| ^^^^ expected identifier, found keyword
26+
27+
error: expected one of `::`, `;`, or `as`, found `||`
28+
--> $DIR/parse.rs:16:14
29+
|
30+
LL | use move || {
31+
| ^^ expected one of `::`, `;`, or `as`
32+
33+
error: aborting due to 4 previous errors
34+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ compile-flags: -Zverbose-internals
2+
3+
#![feature(ergonomic_clones)]
4+
5+
fn to_fn_once<F: FnOnce()>(f: F) -> F {
6+
f
7+
}
8+
9+
fn f<T: std::fmt::Display>(y: T) {
10+
struct Foo<U: std::fmt::Display> {
11+
x: U,
12+
};
13+
14+
let foo = Foo { x: "x" };
15+
16+
let c = to_fn_once(use || {
17+
println!("{} {}", foo.x, y);
18+
});
19+
20+
c();
21+
c();
22+
//~^ ERROR use of moved value
23+
}
24+
25+
fn main() {
26+
f("S");
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0382]: use of moved value: `c`
2+
--> $DIR/print-verbose.rs:21:5
3+
|
4+
LL | let c = to_fn_once(use || {
5+
| - move occurs because `c` has type `{f<T>::{closure#0} closure_kind_ty=i32 closure_sig_as_fn_ptr_ty=extern "rust-call" fn(()) upvar_tys=(Foo<&'?9 str>, T)}`, which does not implement the `Copy` trait
6+
...
7+
LL | c();
8+
| --- `c` moved due to this call
9+
LL | c();
10+
| ^ value used here after move
11+
|
12+
note: this value implements `FnOnce`, which causes it to be moved when called
13+
--> $DIR/print-verbose.rs:20:5
14+
|
15+
LL | c();
16+
| ^
17+
18+
error: aborting due to 1 previous error
19+
20+
For more information about this error, try `rustc --explain E0382`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![feature(ergonomic_clones)]
2+
3+
fn to_fn_once<F: FnOnce()>(f: F) -> F {
4+
f
5+
}
6+
7+
fn f<T: std::fmt::Display>(y: T) {
8+
struct Foo<U: std::fmt::Display> {
9+
x: U,
10+
};
11+
12+
let foo = Foo { x: "x" };
13+
14+
let c = to_fn_once(use || {
15+
println!("{} {}", foo.x, y);
16+
});
17+
18+
c();
19+
c();
20+
//~^ ERROR use of moved value
21+
}
22+
23+
fn main() {
24+
f("S");
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0382]: use of moved value: `c`
2+
--> $DIR/print.rs:19:5
3+
|
4+
LL | let c = to_fn_once(use || {
5+
| - move occurs because `c` has type `{closure@$DIR/print.rs:14:24: 14:30}`, which does not implement the `Copy` trait
6+
...
7+
LL | c();
8+
| --- `c` moved due to this call
9+
LL | c();
10+
| ^ value used here after move
11+
|
12+
note: this value implements `FnOnce`, which causes it to be moved when called
13+
--> $DIR/print.rs:18:5
14+
|
15+
LL | c();
16+
| ^
17+
18+
error: aborting due to 1 previous error
19+
20+
For more information about this error, try `rustc --explain E0382`.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ edition:2021
2+
//@ check-pass
3+
4+
#![feature(closure_lifetime_binder)]
5+
#![feature(ergonomic_clones)]
6+
7+
fn main() {
8+
for<'a> use || -> () {};
9+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![feature(ergonomic_clones)]
2+
3+
fn parse1() {
4+
1.use!;
5+
//~^ ERROR: expected one of `.`, `;`, `?`, `}`, or an operator, found `!`
6+
}
7+
8+
fn parse2() {
9+
1.use!(2);
10+
//~^ ERROR: expected one of `.`, `;`, `?`, `}`, or an operator, found `!`
11+
}
12+
13+
fn parse3() {
14+
1.use 2;
15+
//~^ ERROR: expected one of `.`, `;`, `?`, `}`, or an operator, found `2`
16+
}
17+
18+
fn parse4() {
19+
1.use? 2;
20+
//~^ ERROR: expected one of `.`, `;`, `?`, `}`, or an operator, found `2`
21+
}
22+
23+
fn parse5() {
24+
1.use();
25+
//~^ ERROR: incorrect use of `use`
26+
}
27+
28+
fn parse6() {
29+
1.use(2);
30+
//~^ ERROR: expected function, found `{integer}` [E0618]
31+
}
32+
33+
fn parse7() {
34+
1.use { 2 };
35+
//~^ ERROR: expected one of `.`, `;`, `?`, `}`, or an operator, found `{`
36+
}
37+
38+
fn main() {}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `!`
2+
--> $DIR/parse.rs:4:10
3+
|
4+
LL | 1.use!;
5+
| ^ expected one of `.`, `;`, `?`, `}`, or an operator
6+
7+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `!`
8+
--> $DIR/parse.rs:9:10
9+
|
10+
LL | 1.use!(2);
11+
| ^ expected one of `.`, `;`, `?`, `}`, or an operator
12+
13+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `2`
14+
--> $DIR/parse.rs:14:11
15+
|
16+
LL | 1.use 2;
17+
| ^ expected one of `.`, `;`, `?`, `}`, or an operator
18+
19+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `2`
20+
--> $DIR/parse.rs:19:12
21+
|
22+
LL | 1.use? 2;
23+
| ^ expected one of `.`, `;`, `?`, `}`, or an operator
24+
25+
error: incorrect use of `use`
26+
--> $DIR/parse.rs:24:10
27+
|
28+
LL | 1.use();
29+
| ^^
30+
|
31+
help: `use` is not a method call, remove the parentheses
32+
|
33+
LL - 1.use();
34+
LL + 1.use;
35+
|
36+
37+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `{`
38+
--> $DIR/parse.rs:34:11
39+
|
40+
LL | 1.use { 2 };
41+
| ^ expected one of `.`, `;`, `?`, `}`, or an operator
42+
43+
error[E0618]: expected function, found `{integer}`
44+
--> $DIR/parse.rs:29:5
45+
|
46+
LL | 1.use(2);
47+
| ^^^^^---
48+
| |
49+
| call expression requires function
50+
51+
error: aborting due to 7 previous errors
52+
53+
For more information about this error, try `rustc --explain E0618`.

0 commit comments

Comments
 (0)