]>
Commit | Line | Data |
---|---|---|
cc61c64b XL |
1 | // Copyright 2016 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 | ||
abe05a73 XL |
11 | // ignore-wasm32-bare compiled with panic=abort by default |
12 | ||
cc61c64b XL |
13 | #![feature(fn_traits)] |
14 | #![feature(never_type)] | |
15 | ||
16 | use std::panic; | |
17 | ||
18 | fn foo(x: u32, y: u32) -> u32 { x/y } | |
19 | fn foo_diverges() -> ! { panic!() } | |
20 | ||
21 | fn test_fn_ptr<T>(mut t: T) | |
22 | where T: Fn(u32, u32) -> u32, | |
23 | { | |
24 | let as_fn = <T as Fn<(u32, u32)>>::call; | |
25 | assert_eq!(as_fn(&t, (9, 3)), 3); | |
26 | let as_fn_mut = <T as FnMut<(u32, u32)>>::call_mut; | |
27 | assert_eq!(as_fn_mut(&mut t, (18, 3)), 6); | |
28 | let as_fn_once = <T as FnOnce<(u32, u32)>>::call_once; | |
29 | assert_eq!(as_fn_once(t, (24, 3)), 8); | |
30 | } | |
31 | ||
32 | fn assert_panics<F>(f: F) where F: FnOnce() { | |
33 | let f = panic::AssertUnwindSafe(f); | |
34 | let result = panic::catch_unwind(move || { | |
35 | f.0() | |
36 | }); | |
37 | if let Ok(..) = result { | |
38 | panic!("diverging function returned"); | |
39 | } | |
40 | } | |
41 | ||
42 | fn test_fn_ptr_panic<T>(mut t: T) | |
43 | where T: Fn() -> ! | |
44 | { | |
45 | let as_fn = <T as Fn<()>>::call; | |
46 | assert_panics(|| as_fn(&t, ())); | |
47 | let as_fn_mut = <T as FnMut<()>>::call_mut; | |
48 | assert_panics(|| as_fn_mut(&mut t, ())); | |
49 | let as_fn_once = <T as FnOnce<()>>::call_once; | |
50 | assert_panics(|| as_fn_once(t, ())); | |
51 | } | |
52 | ||
53 | fn main() { | |
54 | test_fn_ptr(foo); | |
55 | test_fn_ptr(foo as fn(u32, u32) -> u32); | |
56 | test_fn_ptr_panic(foo_diverges); | |
57 | test_fn_ptr_panic(foo_diverges as fn() -> !); | |
58 | } |