Skip to content

Commit 2737662

Browse files
committed
Add tests
1 parent 1a64b48 commit 2737662

22 files changed

+382
-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//@ known-bug: unknown
2+
// This test currently ICEs, need fix
3+
4+
#![feature(ergonomic_clones)]
5+
#![allow(warnings)]
6+
7+
fn closure_expecting_bound<F>(_: F)
8+
where
9+
F: FnOnce(&u32),
10+
{
11+
}
12+
13+
fn expect_bound_supply_named<'x>() {
14+
let mut f: Option<&u32> = None;
15+
16+
// Here we give a type annotation that `x` should be free. We get
17+
// an error because of that.
18+
closure_expecting_bound(use |x: &'x u32| {
19+
//~^ ERROR lifetime may not live long enough
20+
//~| ERROR lifetime may not live long enough
21+
22+
// Borrowck doesn't get a chance to run, but if it did it should error
23+
// here.
24+
f = Some(x);
25+
});
26+
}
27+
28+
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ run-rustfix
2+
3+
// Point at the captured immutable outer variable
4+
5+
#![feature(ergonomic_clones)]
6+
7+
fn foo(mut f: Box<dyn FnMut()>) {
8+
f();
9+
}
10+
11+
fn main() {
12+
let mut y = true;
13+
foo(Box::new(use || y = !y) as Box<_>);
14+
//~^ ERROR cannot assign to `y`, as it is not declared as mutable
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ run-rustfix
2+
3+
// Point at the captured immutable outer variable
4+
5+
#![feature(ergonomic_clones)]
6+
7+
fn foo(mut f: Box<dyn FnMut()>) {
8+
f();
9+
}
10+
11+
fn main() {
12+
let y = true;
13+
foo(Box::new(use || y = !y) as Box<_>);
14+
//~^ ERROR cannot assign to `y`, as it is not declared as mutable
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0594]: cannot assign to `y`, as it is not declared as mutable
2+
--> $DIR/immutable-outer-variable.rs:13:25
3+
|
4+
LL | foo(Box::new(use || y = !y) as Box<_>);
5+
| ^^^^^^ cannot assign
6+
|
7+
help: consider changing this to be mutable
8+
|
9+
LL | let mut y = true;
10+
| +++
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0594`.
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+
}

0 commit comments

Comments
 (0)