|
9 | 9 | // It also validates that functions can be called through function pointers
|
10 | 10 | // through traits.
|
11 | 11 |
|
12 |
| -#![feature(no_core, lang_items, unboxed_closures, arbitrary_self_types)] |
| 12 | +#![feature(no_core, lang_items, intrinsics, unboxed_closures, arbitrary_self_types)] |
13 | 13 | #![crate_type = "lib"]
|
14 | 14 | #![no_core]
|
15 | 15 |
|
@@ -49,6 +49,10 @@ pub trait Fn<Args: Tuple>: FnOnce<Args> {
|
49 | 49 | extern "rust-call" fn call(&self, args: Args) -> Self::Output;
|
50 | 50 | }
|
51 | 51 |
|
| 52 | +extern "rust-intrinsic" { |
| 53 | + pub fn transmute<Src, Dst>(src: Src) -> Dst; |
| 54 | +} |
| 55 | + |
52 | 56 | pub static mut STORAGE_FOO: fn(&usize, &mut u32) -> Result<(), ()> = arbitrary_black_box;
|
53 | 57 | pub static mut STORAGE_BAR: u32 = 12;
|
54 | 58 |
|
@@ -87,3 +91,21 @@ pub extern "C" fn test() {
|
87 | 91 | STORAGE_FOO(&1, &mut buf);
|
88 | 92 | }
|
89 | 93 | }
|
| 94 | + |
| 95 | +// Validate that we can codegen transmutes between data ptrs and fn ptrs. |
| 96 | + |
| 97 | +// CHECK: define{{.+}}{{void \(\) addrspace\(1\)\*|ptr addrspace\(1\)}} @transmute_data_ptr_to_fn({{\{\}\*|ptr}}{{.*}} %x) |
| 98 | +#[no_mangle] |
| 99 | +pub unsafe fn transmute_data_ptr_to_fn(x: *const ()) -> fn() { |
| 100 | + // It doesn't matter precisely how this is codegenned (through memory or an addrspacecast), |
| 101 | + // as long as it doesn't cause a verifier error by using `bitcast`. |
| 102 | + transmute(x) |
| 103 | +} |
| 104 | + |
| 105 | +// CHECK: define{{.+}}{{\{\}\*|ptr}} @transmute_fn_ptr_to_data({{void \(\) addrspace\(1\)\*|ptr addrspace\(1\)}}{{.*}} %x) |
| 106 | +#[no_mangle] |
| 107 | +pub unsafe fn transmute_fn_ptr_to_data(x: fn()) -> *const () { |
| 108 | + // It doesn't matter precisely how this is codegenned (through memory or an addrspacecast), |
| 109 | + // as long as it doesn't cause a verifier error by using `bitcast`. |
| 110 | + transmute(x) |
| 111 | +} |
0 commit comments