]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-16739.rs
Rollup merge of #53110 - Xanewok:save-analysis-remap-path, r=nrc
[rust.git] / src / test / run-pass / issue-16739.rs
1 // Copyright 2014 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(box_syntax)]
12 #![feature(unboxed_closures, fn_traits)]
13
14 // Test that unboxing shim for calling rust-call ABI methods through a
15 // trait box works and does not cause an ICE.
16
17 struct Foo { foo: u32 }
18
19 impl FnMut<()> for Foo {
20     extern "rust-call" fn call_mut(&mut self, _: ()) -> u32 { self.foo }
21 }
22
23 impl FnOnce<()> for Foo {
24     type Output = u32;
25     extern "rust-call" fn call_once(mut self, _: ()) -> u32 { self.call_mut(()) }
26 }
27
28 /////////////////////////////////////////////////////////////////////////
29
30 impl FnMut<(u32,)> for Foo {
31     extern "rust-call" fn call_mut(&mut self, (x,): (u32,)) -> u32 { self.foo + x }
32 }
33
34 impl FnOnce<(u32,)> for Foo {
35     type Output = u32;
36     extern "rust-call" fn call_once(mut self, args: (u32,)) -> u32 { self.call_mut(args) }
37 }
38
39 /////////////////////////////////////////////////////////////////////////
40
41 impl FnMut<(u32,u32)> for Foo {
42     extern "rust-call" fn call_mut(&mut self, (x, y): (u32, u32)) -> u32 { self.foo + x + y }
43 }
44
45 impl FnOnce<(u32,u32)> for Foo {
46     type Output = u32;
47     extern "rust-call" fn call_once(mut self, args: (u32,u32)) -> u32 { self.call_mut(args) }
48 }
49
50 fn main() {
51     let mut f = box Foo { foo: 42 } as Box<FnMut() -> u32>;
52     assert_eq!(f.call_mut(()), 42);
53
54     let mut f = box Foo { foo: 40 } as Box<FnMut(u32) -> u32>;
55     assert_eq!(f.call_mut((2,)), 42);
56
57     let mut f = box Foo { foo: 40 } as Box<FnMut(u32, u32) -> u32>;
58     assert_eq!(f.call_mut((1, 1)), 42);
59 }