]> git.proxmox.com Git - rustc.git/blob - src/test/ui/mir/mir_calls_to_shims.rs
New upstream version 1.38.0+dfsg1
[rustc.git] / src / test / ui / mir / mir_calls_to_shims.rs
1 // run-pass
2 // ignore-wasm32-bare compiled with panic=abort by default
3
4 #![feature(fn_traits)]
5 #![feature(never_type)]
6
7 use std::panic;
8
9 fn foo(x: u32, y: u32) -> u32 { x/y }
10 fn foo_diverges() -> ! { panic!() }
11
12 fn test_fn_ptr<T>(mut t: T)
13 where T: Fn(u32, u32) -> u32,
14 {
15 let as_fn = <T as Fn<(u32, u32)>>::call;
16 assert_eq!(as_fn(&t, (9, 3)), 3);
17 let as_fn_mut = <T as FnMut<(u32, u32)>>::call_mut;
18 assert_eq!(as_fn_mut(&mut t, (18, 3)), 6);
19 let as_fn_once = <T as FnOnce<(u32, u32)>>::call_once;
20 assert_eq!(as_fn_once(t, (24, 3)), 8);
21 }
22
23 fn assert_panics<F>(f: F) where F: FnOnce() {
24 let f = panic::AssertUnwindSafe(f);
25 let result = panic::catch_unwind(move || {
26 f.0()
27 });
28 if let Ok(..) = result {
29 panic!("diverging function returned");
30 }
31 }
32
33 fn test_fn_ptr_panic<T>(mut t: T)
34 where T: Fn() -> !
35 {
36 let as_fn = <T as Fn<()>>::call;
37 assert_panics(|| as_fn(&t, ()));
38 let as_fn_mut = <T as FnMut<()>>::call_mut;
39 assert_panics(|| as_fn_mut(&mut t, ()));
40 let as_fn_once = <T as FnOnce<()>>::call_once;
41 assert_panics(|| as_fn_once(t, ()));
42 }
43
44 fn main() {
45 test_fn_ptr(foo);
46 test_fn_ptr(foo as fn(u32, u32) -> u32);
47 test_fn_ptr_panic(foo_diverges);
48 test_fn_ptr_panic(foo_diverges as fn() -> !);
49 }