File: mir_calls_to_shims.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (49 lines) | stat: -rw-r--r-- 1,255 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//@ run-pass
//@ needs-unwind

#![feature(fn_traits)]
#![feature(never_type)]

use std::panic;

fn foo(x: u32, y: u32) -> u32 { x/y }
fn foo_diverges() -> ! { panic!() }

fn test_fn_ptr<T>(mut t: T)
    where T: Fn(u32, u32) -> u32,
{
    let as_fn = <T as Fn<(u32, u32)>>::call;
    assert_eq!(as_fn(&t, (9, 3)), 3);
    let as_fn_mut = <T as FnMut<(u32, u32)>>::call_mut;
    assert_eq!(as_fn_mut(&mut t, (18, 3)), 6);
    let as_fn_once = <T as FnOnce<(u32, u32)>>::call_once;
    assert_eq!(as_fn_once(t, (24, 3)), 8);
}

fn assert_panics<F>(f: F) where F: FnOnce() {
    let f = panic::AssertUnwindSafe(f);
    let result = panic::catch_unwind(move || {
        f.0()
    });
    if let Ok(..) = result {
        panic!("diverging function returned");
    }
}

fn test_fn_ptr_panic<T>(mut t: T)
    where T: Fn() -> !
{
    let as_fn = <T as Fn<()>>::call;
    assert_panics(|| as_fn(&t, ()));
    let as_fn_mut = <T as FnMut<()>>::call_mut;
    assert_panics(|| as_fn_mut(&mut t, ()));
    let as_fn_once = <T as FnOnce<()>>::call_once;
    assert_panics(|| as_fn_once(t, ()));
}

fn main() {
    test_fn_ptr(foo);
    test_fn_ptr(foo as fn(u32, u32) -> u32);
    test_fn_ptr_panic(foo_diverges);
    test_fn_ptr_panic(foo_diverges as fn() -> !);
}