]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-overloaded-call.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-overloaded-call.rs
1 #![feature(fn_traits, unboxed_closures)]
2
3 use std::ops::{Fn, FnMut, FnOnce};
4
5 struct SFn {
6     x: isize,
7     y: isize,
8 }
9
10 impl Fn<(isize,)> for SFn {
11     extern "rust-call" fn call(&self, (z,): (isize,)) -> isize {
12         self.x * self.y * z
13     }
14 }
15
16 impl FnMut<(isize,)> for SFn {
17     extern "rust-call" fn call_mut(&mut self, args: (isize,)) -> isize { self.call(args) }
18 }
19
20 impl FnOnce<(isize,)> for SFn {
21     type Output = isize;
22     extern "rust-call" fn call_once(self, args: (isize,)) -> isize { self.call(args) }
23 }
24
25 struct SFnMut {
26     x: isize,
27     y: isize,
28 }
29
30 impl FnMut<(isize,)> for SFnMut {
31     extern "rust-call" fn call_mut(&mut self, (z,): (isize,)) -> isize {
32         self.x * self.y * z
33     }
34 }
35
36 impl FnOnce<(isize,)> for SFnMut {
37     type Output = isize;
38     extern "rust-call" fn call_once(mut self, args: (isize,)) -> isize { self.call_mut(args) }
39 }
40
41 struct SFnOnce {
42     x: String,
43 }
44
45 impl FnOnce<(String,)> for SFnOnce {
46     type Output = usize;
47
48     extern "rust-call" fn call_once(self, (z,): (String,)) -> usize {
49         self.x.len() + z.len()
50     }
51 }
52
53 fn f() {
54     let mut s = SFn {
55         x: 1,
56         y: 2,
57     };
58     let sp = &mut s;
59     s(3);   //~ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
60     use_mut(sp);
61 }
62 fn g() {
63     let s = SFnMut {
64         x: 1,
65         y: 2,
66     };
67     s(3);   //~ ERROR cannot borrow `s` as mutable, as it is not declared as mutable
68 }
69
70 fn h() {
71     let s = SFnOnce {
72         x: "hello".to_string(),
73     };
74     s(" world".to_string());
75     s(" world".to_string());    //~ ERROR use of moved value: `s`
76 }
77
78 fn main() {}
79
80 fn use_mut<T>(_: &mut T) { }