]> git.lizzy.rs Git - rust.git/blob - src/test/ui/issues/issue-16739.rs
Rollup merge of #98801 - joshtriplett:file-create-new, r=thomcc
[rust.git] / src / test / ui / issues / issue-16739.rs
1 // run-pass
2 #![feature(unboxed_closures, fn_traits)]
3
4 // Test that unboxing shim for calling rust-call ABI methods through a
5 // trait box works and does not cause an ICE.
6
7 struct Foo { foo: u32 }
8
9 impl FnMut<()> for Foo {
10     extern "rust-call" fn call_mut(&mut self, _: ()) -> u32 { self.foo }
11 }
12
13 impl FnOnce<()> for Foo {
14     type Output = u32;
15     extern "rust-call" fn call_once(mut self, _: ()) -> u32 { self.call_mut(()) }
16 }
17
18 impl FnMut<(u32,)> for Foo {
19     extern "rust-call" fn call_mut(&mut self, (x,): (u32,)) -> u32 { self.foo + x }
20 }
21
22 impl FnOnce<(u32,)> for Foo {
23     type Output = u32;
24     extern "rust-call" fn call_once(mut self, args: (u32,)) -> u32 { self.call_mut(args) }
25 }
26
27 impl FnMut<(u32,u32)> for Foo {
28     extern "rust-call" fn call_mut(&mut self, (x, y): (u32, u32)) -> u32 { self.foo + x + y }
29 }
30
31 impl FnOnce<(u32,u32)> for Foo {
32     type Output = u32;
33     extern "rust-call" fn call_once(mut self, args: (u32,u32)) -> u32 { self.call_mut(args) }
34 }
35
36 fn main() {
37     let mut f = Box::new(Foo { foo: 42 }) as Box<dyn FnMut() -> u32>;
38     assert_eq!(f.call_mut(()), 42);
39
40     let mut f = Box::new(Foo { foo: 40 }) as Box<dyn FnMut(u32) -> u32>;
41     assert_eq!(f.call_mut((2,)), 42);
42
43     let mut f = Box::new(Foo { foo: 40 }) as Box<dyn FnMut(u32, u32) -> u32>;
44     assert_eq!(f.call_mut((1, 1)), 42);
45 }