Skip to content

Commit 511a6a2

Browse files
committed
Add tests
1 parent 6a8cc22 commit 511a6a2

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
@@ -39,4 +39,18 @@ fn ergonomic_clone_closure_use_cloned() -> Foo {
3939
f
4040
}
4141

42+
fn ergonomic_clone_closure_copy() -> i32 {
43+
let i = 1;
44+
45+
let i1 = use || {
46+
i
47+
};
48+
49+
let i2 = use || {
50+
i
51+
};
52+
53+
i
54+
}
55+
4256
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+

0 commit comments

Comments
 (0)