Skip to content

Commit 9330bae

Browse files
committed
Fallout from changing fn traits to use inheritance rather than bridge
impls. This is a [breaking-change] (for gated code) in that when you implement `Fn` (`FnMut`) you must also implement `FnOnce`. This commit demonstrates how to fix it.
1 parent 3760113 commit 9330bae

28 files changed

+216
-59
lines changed

src/libcollectionstest/btree/set.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,21 @@ struct Counter<'a, 'b> {
4343
}
4444

4545
impl<'a, 'b, 'c> FnMut<(&'c i32,)> for Counter<'a, 'b> {
46-
type Output = bool;
47-
4846
extern "rust-call" fn call_mut(&mut self, (&x,): (&'c i32,)) -> bool {
4947
assert_eq!(x, self.expected[*self.i]);
5048
*self.i += 1;
5149
true
5250
}
5351
}
5452

53+
impl<'a, 'b, 'c> FnOnce<(&'c i32,)> for Counter<'a, 'b> {
54+
type Output = bool;
55+
56+
extern "rust-call" fn call_once(mut self, args: (&'c i32,)) -> bool {
57+
self.call_mut(args)
58+
}
59+
}
60+
5561
fn check<F>(a: &[i32], b: &[i32], expected: &[i32], f: F) where
5662
// FIXME Replace Counter with `Box<FnMut(_) -> _>`
5763
F: FnOnce(&BTreeSet<i32>, &BTreeSet<i32>, Counter) -> bool,

src/test/compile-fail/borrowck-overloaded-call.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,36 @@ struct SFn {
1818
}
1919

2020
impl Fn<(isize,)> for SFn {
21-
type Output = isize;
22-
2321
extern "rust-call" fn call(&self, (z,): (isize,)) -> isize {
2422
self.x * self.y * z
2523
}
2624
}
2725

26+
impl FnMut<(isize,)> for SFn {
27+
extern "rust-call" fn call_mut(&mut self, args: (isize,)) -> isize { self.call(args) }
28+
}
29+
30+
impl FnOnce<(isize,)> for SFn {
31+
type Output = isize;
32+
extern "rust-call" fn call_once(self, args: (isize,)) -> isize { self.call(args) }
33+
}
34+
2835
struct SFnMut {
2936
x: isize,
3037
y: isize,
3138
}
3239

3340
impl FnMut<(isize,)> for SFnMut {
34-
type Output = isize;
35-
3641
extern "rust-call" fn call_mut(&mut self, (z,): (isize,)) -> isize {
3742
self.x * self.y * z
3843
}
3944
}
4045

46+
impl FnOnce<(isize,)> for SFnMut {
47+
type Output = isize;
48+
extern "rust-call" fn call_once(mut self, args: (isize,)) -> isize { self.call_mut(args) }
49+
}
50+
4151
struct SFnOnce {
4252
x: String,
4353
}

src/test/compile-fail/coerce-unsafe-to-closure.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010

1111
fn main() {
1212
let x: Option<&[u8]> = Some("foo").map(std::mem::transmute);
13-
//~^ ERROR: is not implemented for the type
13+
//~^ ERROR E0277
14+
//~| ERROR E0277
1415
}

src/test/compile-fail/extern-wrong-value-type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ fn main() {
1818
let _x: extern "C" fn() = f; // OK
1919
is_fn(f);
2020
//~^ ERROR the trait `core::ops::Fn<()>` is not implemented for the type `extern "C" fn()
21-
//~| ERROR the trait `core::ops::Fn<()>` is not implemented for the type `extern "C" fn()
21+
//~| ERROR the trait `core::ops::FnOnce<()>` is not implemented for the type `extern "C" fn()
2222
}

src/test/compile-fail/feature-gate-unboxed-closures-manual-impls.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,21 @@
1818
struct Foo;
1919
impl Fn<()> for Foo {
2020
//~^ ERROR angle-bracket notation is not stable when used with the `Fn` family of traits
21-
type Output = ();
22-
23-
extern "rust-call" fn call(&self, args: ()) -> () {}
21+
extern "rust-call" fn call(self, args: ()) -> () {}
2422
}
2523
struct Foo1;
26-
impl Fn() for Foo1 {
24+
impl FnOnce() for Foo1 {
2725
//~^ ERROR associated type bindings are not allowed here
28-
29-
extern "rust-call" fn call(&self, args: ()) -> () {}
26+
extern "rust-call" fn call_once(self, args: ()) -> () {}
3027
}
3128
struct Bar;
3229
impl FnMut<()> for Bar {
3330
//~^ ERROR angle-bracket notation is not stable when used with the `Fn` family of traits
34-
type Output = ();
35-
3631
extern "rust-call" fn call_mut(&self, args: ()) -> () {}
3732
}
3833
struct Baz;
3934
impl FnOnce<()> for Baz {
4035
//~^ ERROR angle-bracket notation is not stable when used with the `Fn` family of traits
41-
type Output = ();
42-
4336
extern "rust-call" fn call_once(&self, args: ()) -> () {}
4437
}
4538

src/test/compile-fail/fn-trait-formatting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ fn main() {
3535

3636
needs_fn(1);
3737
//~^ ERROR `core::ops::Fn<(isize,)>`
38-
//~| ERROR `core::ops::Fn<(isize,)>`
38+
//~| ERROR `core::ops::FnOnce<(isize,)>`
3939
}

src/test/compile-fail/fn-variance-1.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ fn apply<T, F>(t: T, f: F) where F: FnOnce(T) {
1717
}
1818

1919
fn main() {
20-
apply(&3, takes_mut); //~ ERROR (values differ in mutability)
2120
apply(&3, takes_imm);
21+
apply(&3, takes_mut);
22+
//~^ ERROR (values differ in mutability)
23+
//~| ERROR (values differ in mutability)
2224

2325
apply(&mut 3, takes_mut);
24-
apply(&mut 3, takes_imm); //~ ERROR (values differ in mutability)
26+
apply(&mut 3, takes_imm);
27+
//~^ ERROR (values differ in mutability)
28+
//~| ERROR (values differ in mutability)
2529
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ struct Debuger<T> {
1616
x: T
1717
}
1818

19-
impl<T: fmt::Debug> ops::Fn<(),> for Debuger<T> {
19+
impl<T: fmt::Debug> ops::FnOnce<(),> for Debuger<T> {
2020
type Output = ();
21-
22-
fn call(&self, _args: ()) {
23-
//~^ ERROR `call` has an incompatible type for trait: expected "rust-call" fn, found "Rust" fn
21+
fn call_once(self, _args: ()) {
22+
//~^ ERROR `call_once` has an incompatible type for trait: expected "rust-call" fn, found "Rust" fn
2423
println!("{:?}", self.x);
2524
}
2625
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@
1313
struct Foo;
1414

1515
impl<'a, T> Fn<(&'a T,)> for Foo {
16+
extern "rust-call" fn call(&self, (_,): (T,)) {}
17+
//~^ ERROR: has an incompatible type for trait: expected &-ptr
18+
}
19+
20+
impl<'a, T> FnMut<(&'a T,)> for Foo {
21+
extern "rust-call" fn call_mut(&mut self, (_,): (T,)) {}
22+
//~^ ERROR: has an incompatible type for trait: expected &-ptr
23+
}
24+
25+
impl<'a, T> FnOnce<(&'a T,)> for Foo {
1626
type Output = ();
1727

18-
extern "rust-call" fn call(&self, (_,): (T,)) {}
28+
extern "rust-call" fn call_once(self, (_,): (T,)) {}
1929
//~^ ERROR: has an incompatible type for trait: expected &-ptr
2030
}
2131

src/test/compile-fail/overloaded-calls-bad.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ struct S {
1818
}
1919

2020
impl FnMut<(isize,)> for S {
21-
type Output = isize;
22-
2321
extern "rust-call" fn call_mut(&mut self, (z,): (isize,)) -> isize {
2422
self.x * self.y * z
2523
}
2624
}
2725

26+
impl FnOnce<(isize,)> for S {
27+
type Output = isize;
28+
extern "rust-call" fn call_once(mut self, (z,): (isize,)) -> isize {
29+
self.call_mut((z,))
30+
}
31+
}
32+
2833
fn main() {
2934
let mut s = S {
3035
x: 3,

0 commit comments

Comments
 (0)