Skip to content

Commit a952787

Browse files
author
hyd-dev
committed
Improve tests
1 parent 160bc68 commit a952787

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

tests/compile-fail/function_calls/check_callback_abi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ extern "C" fn try_fn(_: *mut u8) {
66

77
fn main() {
88
unsafe {
9+
// Make sure we check the ABI when Miri itself invokes a function
10+
// as part of a shim implementation.
911
std::intrinsics::r#try( //~ ERROR calling a function with ABI C using caller ABI Rust
1012
std::mem::transmute::<extern "C" fn(*mut u8), _>(try_fn),
1113
std::ptr::null_mut(),
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
// revisions: no_cache cache
1+
// revisions: no_cache cache fn_ptr
22

33
#[no_mangle]
44
fn foo() {}
55

66
fn main() {
7+
#[cfg(any(cache, fn_ptr))]
8+
extern "Rust" {
9+
fn foo();
10+
}
11+
12+
#[cfg(fn_ptr)]
13+
unsafe { std::mem::transmute::<unsafe fn(), unsafe extern "C" fn()>(foo)() }
14+
//[fn_ptr]~^ ERROR calling a function with ABI Rust using caller ABI C
15+
16+
// `Instance` caching should not suppress ABI check.
717
#[cfg(cache)]
18+
unsafe { foo() }
19+
820
{
9-
// `Instance` caching should not suppress ABI check.
10-
extern "Rust" {
21+
#[cfg_attr(any(cache, fn_ptr), allow(clashing_extern_declarations))]
22+
extern "C" {
1123
fn foo();
1224
}
1325
unsafe { foo() }
26+
//[no_cache]~^ ERROR calling a function with ABI Rust using caller ABI C
27+
//[cache]~^^ ERROR calling a function with ABI Rust using caller ABI C
1428
}
15-
#[cfg_attr(cache, allow(clashing_extern_declarations))]
16-
extern "C" {
17-
fn foo();
18-
}
19-
unsafe { foo() }
20-
//[no_cache]~^ ERROR Undefined Behavior: calling a function with ABI Rust using caller ABI C
21-
//[cache]~^^ ERROR Undefined Behavior: calling a function with ABI Rust using caller ABI C
2229
}

tests/compile-fail/function_calls/exported_symbol_bad_unwind3.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![feature(unwind_attributes)]
2-
#![feature(c_unwind)] // make sure it doesn't insert abort-on-unwind for the `#[unwind(allowed)]` function
32

43
#[unwind(allowed)]
54
#[no_mangle]

tests/run-pass/function_calls/exported_symbol.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,42 @@ fn main() {
3232
extern "C" {
3333
fn foo() -> i32;
3434
}
35+
3536
assert_eq!(unsafe { foo() }, -1);
36-
assert_eq!(unsafe { foo() }, -1);
37+
3738
extern "Rust" {
3839
fn bar() -> i32;
3940
fn baz() -> i32;
4041
}
42+
4143
assert_eq!(unsafe { bar() }, -2);
4244
assert_eq!(unsafe { baz() }, -3);
45+
46+
#[allow(clashing_extern_declarations)]
47+
{
48+
extern "Rust" {
49+
fn foo() -> i32;
50+
}
51+
52+
assert_eq!(
53+
unsafe {
54+
std::mem::transmute::<unsafe fn() -> i32, unsafe extern "C" fn() -> i32>(foo)()
55+
},
56+
-1
57+
);
58+
59+
extern "C" {
60+
fn bar() -> i32;
61+
fn baz() -> i32;
62+
}
63+
64+
unsafe {
65+
let transmute = |f| {
66+
std::mem::transmute::<unsafe extern "C" fn() -> i32, unsafe fn() -> i32>(f)
67+
};
68+
assert_eq!(transmute(bar)(), -2);
69+
assert_eq!(transmute(baz)(), -3);
70+
}
71+
}
4372
}
4473
}

0 commit comments

Comments
 (0)