]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/borrowck-overloaded-call.rs
Auto merge of #55657 - davidtwco:issue-55651, r=pnkfelix
[rust.git] / src / test / ui / borrowck / borrowck-overloaded-call.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(fn_traits, unboxed_closures)]
12
13 use std::ops::{Fn, FnMut, FnOnce};
14
15 struct SFn {
16     x: isize,
17     y: isize,
18 }
19
20 impl Fn<(isize,)> for SFn {
21     extern "rust-call" fn call(&self, (z,): (isize,)) -> isize {
22         self.x * self.y * z
23     }
24 }
25
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
35 struct SFnMut {
36     x: isize,
37     y: isize,
38 }
39
40 impl FnMut<(isize,)> for SFnMut {
41     extern "rust-call" fn call_mut(&mut self, (z,): (isize,)) -> isize {
42         self.x * self.y * z
43     }
44 }
45
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
51 struct SFnOnce {
52     x: String,
53 }
54
55 impl FnOnce<(String,)> for SFnOnce {
56     type Output = usize;
57
58     extern "rust-call" fn call_once(self, (z,): (String,)) -> usize {
59         self.x.len() + z.len()
60     }
61 }
62
63 fn f() {
64     let mut s = SFn {
65         x: 1,
66         y: 2,
67     };
68     let sp = &mut s;
69     s(3);   //~ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
70     use_mut(sp);
71 }
72 fn g() {
73     let s = SFnMut {
74         x: 1,
75         y: 2,
76     };
77     s(3);   //~ ERROR cannot borrow immutable local variable `s` as mutable
78 }
79
80 fn h() {
81     let s = SFnOnce {
82         x: "hello".to_string(),
83     };
84     s(" world".to_string());
85     s(" world".to_string());    //~ ERROR use of moved value: `s`
86 }
87
88 fn main() {}
89
90 fn use_mut<T>(_: &mut T) { }