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+
}
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ known-bug: unknown
2+
// This test currently ICEs, need fix
3+
4+
#![feature(ergonomic_clones)]
5+
6+
fn main() {
7+
let mut my_var = false;
8+
let callback = use || {
9+
my_var = true;
10+
};
11+
callback();
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ known-bug: unknown
2+
//@ run-pass
3+
// This is not correctly parsing the nested use
4+
5+
#![feature(ergonomic_clones)]
6+
7+
use std::clone::UseCloned;
8+
9+
#[derive(Clone)]
10+
struct Foo;
11+
12+
impl UseCloned for Foo {}
13+
14+
fn work(_: Box<Foo>) {}
15+
fn foo<F:FnOnce()>(_: F) {}
16+
17+
pub fn main() {
18+
let a = Box::new(Foo);
19+
foo(use|| { foo(use|| { work(a) }) });
20+
use || { use || { use || { Foo } } };
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: unused closure that must be used
2+
--> $DIR/nested.rs:18:3
3+
|
4+
LL | move || { move || { move || { Foo } } };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: closures are lazy and do nothing unless called
8+
= note: `#[warn(unused_must_use)]` on by default
9+
10+
warning: 1 warning emitted
11+
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 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`.

0 commit comments

Comments
 (0)