Skip to content

Commit 47f1865

Browse files
committed
Update compile-fail tests to use the expected type to force the
closure kind, thereby detecting what happens if there are mismatches. Simply removing the `:` annotations caused most of these tests to pass or produce other errors, because the inference would convert the closure into a more appropriate kind. (The ability to override the inference by using the expected type is an important backdoor partly for this reason.)
1 parent 498595a commit 47f1865

12 files changed

+72
-64
lines changed

src/test/compile-fail/borrow-immutable-upvar-mutation.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,38 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(unboxed_closures, overloaded_calls)]
11+
#![feature(unboxed_closures)]
1212

1313
// Tests that we can't assign to or mutably borrow upvars from `Fn`
1414
// closures (issue #17780)
1515

1616
fn set(x: &mut usize) { *x = 5; }
1717

18+
fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
19+
fn to_fn_mut<A,F:FnMut<A>>(f: F) -> F { f }
20+
1821
fn main() {
1922
// By-ref captures
2023
{
2124
let mut x = 0us;
22-
let _f = |&:| x = 42; //~ ERROR cannot assign
25+
let _f = to_fn(|| x = 42); //~ ERROR cannot assign
2326

2427
let mut y = 0us;
25-
let _g = |&:| set(&mut y); //~ ERROR cannot borrow
28+
let _g = to_fn(|| set(&mut y)); //~ ERROR cannot borrow
2629

2730
let mut z = 0us;
28-
let _h = |&mut:| { set(&mut z); |&:| z = 42; }; //~ ERROR cannot assign
31+
let _h = to_fn_mut(|| { set(&mut z); to_fn(|| z = 42); }); //~ ERROR cannot assign
2932
}
33+
3034
// By-value captures
3135
{
3236
let mut x = 0us;
33-
let _f = move |&:| x = 42; //~ ERROR cannot assign
37+
let _f = to_fn(move || x = 42); //~ ERROR cannot assign
3438

3539
let mut y = 0us;
36-
let _g = move |&:| set(&mut y); //~ ERROR cannot borrow
40+
let _g = to_fn(move || set(&mut y)); //~ ERROR cannot borrow
3741

3842
let mut z = 0us;
39-
let _h = move |&mut:| { set(&mut z); move |&:| z = 42; }; //~ ERROR cannot assign
43+
let _h = to_fn_mut(move || { set(&mut z); to_fn(move || z = 42); }); //~ ERROR cannot assign
4044
}
4145
}

src/test/compile-fail/borrowck-move-by-capture.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(box_syntax)]
11+
#![feature(box_syntax,unboxed_closures)]
12+
13+
fn to_fn_mut<A,F:FnMut<A>>(f: F) -> F { f }
14+
fn to_fn_once<A,F:FnOnce<A>>(f: F) -> F { f }
1215

1316
pub fn main() {
1417
let bar = box 3;
15-
let _g = |&mut:| {
16-
let _h = move |:| -> isize { *bar }; //~ ERROR cannot move out of captured outer variable
17-
};
18+
let _g = to_fn_mut(|| {
19+
let _h = to_fn_once(move || -> isize { *bar }); //~ ERROR cannot move out of
20+
});
1821
}

src/test/compile-fail/cannot-mutate-captured-non-mut-var.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(unboxed_closures)]
12+
13+
fn to_fn_once<A,F:FnOnce<A>>(f: F) -> F { f }
14+
1115
fn main() {
1216
let x = 1;
13-
move|:| { x = 2; };
17+
to_fn_once(move|:| { x = 2; });
1418
//~^ ERROR: cannot assign to immutable captured outer variable
1519

1620
let s = std::old_io::stdin();
17-
move|:| { s.read_to_end(); };
21+
to_fn_once(move|:| { s.read_to_end(); });
1822
//~^ ERROR: cannot borrow immutable captured outer variable
1923
}

src/test/compile-fail/issue-11925.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(box_syntax)]
11+
#![feature(box_syntax, unboxed_closures)]
12+
13+
fn to_fn_once<A,F:FnOnce<A>>(f: F) -> F { f }
1214

1315
fn main() {
1416
let r = {
1517
let x = box 42;
16-
let f = move|:| &x; //~ ERROR: `x` does not live long enough
18+
let f = to_fn_once(move|| &x); //~ ERROR: `x` does not live long enough
1719
f()
1820
};
1921

src/test/compile-fail/issue-12127.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(box_syntax)]
11+
#![feature(box_syntax, unboxed_closures)]
1212

13+
fn to_fn_once<A,F:FnOnce<A>>(f: F) -> F { f }
1314
fn do_it(x: &isize) { }
1415

1516
fn main() {
1617
let x = box 22;
17-
let f = move|:| do_it(&*x);
18-
(move|:| {
18+
let f = to_fn_once(move|| do_it(&*x));
19+
to_fn_once(move|| {
1920
f();
2021
f();
2122
//~^ ERROR: use of moved value: `f`

src/test/compile-fail/moves-based-on-type-move-out-of-closure-env-issue-1965.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(box_syntax)]
11+
#![feature(box_syntax, unboxed_closures)]
1212

1313
use std::usize;
1414

15+
fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
16+
1517
fn test(_x: Box<usize>) {}
1618

1719
fn main() {
1820
let i = box 3;
19-
let _f = |&:| test(i); //~ ERROR cannot move out
21+
let _f = to_fn(|| test(i)); //~ ERROR cannot move out
2022
}

src/test/compile-fail/unboxed-closer-non-implicit-copyable.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
#![feature(unboxed_closures)]
1212

13+
fn to_fn_once<A,F:FnOnce<A>>(f: F) -> F { f }
14+
1315
fn main() {
14-
let f = move|:| ();
16+
let f = to_fn_once(move|| ());
1517
f();
1618
f(); //~ ERROR use of moved value
1719
}

src/test/compile-fail/unboxed-closure-illegal-move.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,35 @@
1515
// if the upvar is captured by ref or the closure takes self by
1616
// reference.
1717

18+
fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
19+
fn to_fn_mut<A,F:FnMut<A>>(f: F) -> F { f }
20+
fn to_fn_once<A,F:FnOnce<A>>(f: F) -> F { f }
21+
1822
fn main() {
1923
// By-ref cases
2024
{
2125
let x = box 0us;
22-
let f = |&:| drop(x); //~ ERROR cannot move
26+
let f = to_fn(|| drop(x)); //~ ERROR cannot move
2327
}
2428
{
2529
let x = box 0us;
26-
let f = |&mut:| drop(x); //~ ERROR cannot move
30+
let f = to_fn_mut(|| drop(x)); //~ ERROR cannot move
2731
}
2832
{
2933
let x = box 0us;
30-
let f = |:| drop(x); // OK -- FnOnce
34+
let f = to_fn_once(|| drop(x)); // OK -- FnOnce
3135
}
3236
// By-value cases
3337
{
3438
let x = box 0us;
35-
let f = move |&:| drop(x); //~ ERROR cannot move
39+
let f = to_fn(move || drop(x)); //~ ERROR cannot move
3640
}
3741
{
3842
let x = box 0us;
39-
let f = move |&mut:| drop(x); //~ ERROR cannot move
43+
let f = to_fn_mut(move || drop(x)); //~ ERROR cannot move
4044
}
4145
{
4246
let x = box 0us;
43-
let f = move |:| drop(x); // this one is ok
47+
let f = to_fn_once(move || drop(x)); // this one is ok
4448
}
4549
}

src/test/compile-fail/unboxed-closures-mutate-upvar.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,56 @@
1212
// as `mut` through a closure. Also test that we CAN mutate a moved copy,
1313
// unless this is a `Fn` closure. Issue #16749.
1414

15+
#![feature(unboxed_closures)]
16+
1517
use std::mem;
1618

19+
fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
20+
fn to_fn_mut<A,F:FnMut<A>>(f: F) -> F { f }
21+
1722
fn a() {
1823
let n = 0u8;
19-
let mut f = |&mut:| { //~ ERROR closure cannot assign
24+
let mut f = to_fn_mut(|| { //~ ERROR closure cannot assign
2025
n += 1;
21-
};
26+
});
2227
}
2328

2429
fn b() {
2530
let mut n = 0u8;
26-
let mut f = |&mut:| {
31+
let mut f = to_fn_mut(|| {
2732
n += 1; // OK
28-
};
33+
});
2934
}
3035

3136
fn c() {
3237
let n = 0u8;
33-
let mut f = move |&mut:| {
38+
let mut f = to_fn_mut(move || {
3439
// If we just did a straight-forward desugaring, this would
3540
// compile, but we do something a bit more subtle, and hence
3641
// we get an error.
3742
n += 1; //~ ERROR cannot assign
38-
};
43+
});
3944
}
4045

4146
fn d() {
4247
let mut n = 0u8;
43-
let mut f = move |&mut:| {
48+
let mut f = to_fn_mut(move || {
4449
n += 1; // OK
45-
};
50+
});
4651
}
4752

4853
fn e() {
4954
let n = 0u8;
50-
let mut f = move |&:| {
55+
let mut f = to_fn(move || {
5156
n += 1; //~ ERROR cannot assign
52-
};
57+
});
5358
}
5459

5560
fn f() {
5661
let mut n = 0u8;
57-
let mut f = move |&:| {
62+
let mut f = to_fn(move || {
5863
n += 1; //~ ERROR cannot assign
59-
};
64+
});
6065
}
6166

6267
fn main() { }

src/test/compile-fail/unboxed-closures-static-call-wrong-trait.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
#![feature(unboxed_closures)]
1212

13+
fn to_fn_mut<A,F:FnMut<A>>(f: F) -> F { f }
14+
1315
fn main() {
14-
let mut_ = |&mut: x| x;
16+
let mut_ = to_fn_mut(|x| x);
1517
mut_.call((0, )); //~ ERROR does not implement any method in scope named `call`
1618
}
1719

src/test/compile-fail/unboxed-closures-vtable-mismatch.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212

1313
use std::ops::FnMut;
1414

15+
fn to_fn_mut<A,F:FnMut<A>>(f: F) -> F { f }
16+
1517
fn call_it<F:FnMut(isize,isize)->isize>(y: isize, mut f: F) -> isize {
1618
f(2, y)
1719
}
1820

1921
pub fn main() {
20-
let f = |&mut: x: usize, y: isize| -> isize { (x as isize) + y };
22+
let f = to_fn_mut(|x: usize, y: isize| -> isize { (x as isize) + y });
2123
let z = call_it(3, f);
2224
//~^ ERROR type mismatch
2325
//~| ERROR type mismatch

src/test/compile-fail/unboxed-closures-wrong-trait.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)