about summary refs log tree commit diff
path: root/src/test/ui/foreign
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2019-07-27 01:33:01 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2019-07-27 18:56:16 +0300
commit9be35f82c1abf2ecbab489bca9eca138ea648312 (patch)
tree69888506e34af447d9748c0d542de3ba1dd76210 /src/test/ui/foreign
parentca9faa52f5ada0054b1fa27d97aedf448afb059b (diff)
downloadrust-9be35f82c1abf2ecbab489bca9eca138ea648312.tar.gz
rust-9be35f82c1abf2ecbab489bca9eca138ea648312.zip
tests: Move run-pass tests without naming conflicts to ui
Diffstat (limited to 'src/test/ui/foreign')
-rw-r--r--src/test/ui/foreign/auxiliary/fn-abi.rs2
-rw-r--r--src/test/ui/foreign/auxiliary/foreign_lib.rs38
-rw-r--r--src/test/ui/foreign/foreign-call-no-runtime.rs55
-rw-r--r--src/test/ui/foreign/foreign-dupe.rs17
-rw-r--r--src/test/ui/foreign/foreign-fn-linkname.rs30
-rw-r--r--src/test/ui/foreign/foreign-fn-with-byval.rs32
-rw-r--r--src/test/ui/foreign/foreign-int-types.rs13
-rw-r--r--src/test/ui/foreign/foreign-mod-src/compiletest-ignore-dir0
-rw-r--r--src/test/ui/foreign/foreign-mod-src/inner.rs14
-rw-r--r--src/test/ui/foreign/foreign-mod-unused-const.rs12
-rw-r--r--src/test/ui/foreign/foreign-no-abi.rs22
-rw-r--r--src/test/ui/foreign/foreign-src/compiletest-ignore-dir0
-rw-r--r--src/test/ui/foreign/foreign-src/foreign.rs9
-rw-r--r--src/test/ui/foreign/foreign-truncated-arguments.rs20
-rw-r--r--src/test/ui/foreign/foreign2.rs30
15 files changed, 294 insertions, 0 deletions
diff --git a/src/test/ui/foreign/auxiliary/fn-abi.rs b/src/test/ui/foreign/auxiliary/fn-abi.rs
new file mode 100644
index 00000000000..25c9e1b4ca3
--- /dev/null
+++ b/src/test/ui/foreign/auxiliary/fn-abi.rs
@@ -0,0 +1,2 @@
+#[no_mangle]
+pub extern fn foo() {}
diff --git a/src/test/ui/foreign/auxiliary/foreign_lib.rs b/src/test/ui/foreign/auxiliary/foreign_lib.rs
new file mode 100644
index 00000000000..de6b0e2118a
--- /dev/null
+++ b/src/test/ui/foreign/auxiliary/foreign_lib.rs
@@ -0,0 +1,38 @@
+#![crate_name="foreign_lib"]
+
+#![feature(rustc_private)]
+
+pub mod rustrt {
+    extern crate libc;
+
+    #[link(name = "rust_test_helpers", kind = "static")]
+    extern {
+        pub fn rust_get_test_int() -> libc::intptr_t;
+    }
+}
+
+pub mod rustrt2 {
+    extern crate libc;
+
+    extern {
+        pub fn rust_get_test_int() -> libc::intptr_t;
+    }
+}
+
+pub mod rustrt3 {
+    // Different type, but same ABI (on all supported platforms).
+    // Ensures that we don't ICE or trigger LLVM asserts when
+    // importing the same symbol under different types.
+    // See https://github.com/rust-lang/rust/issues/32740.
+    extern {
+        pub fn rust_get_test_int() -> *const u8;
+    }
+}
+
+pub fn local_uses() {
+    unsafe {
+        let x = rustrt::rust_get_test_int();
+        assert_eq!(x, rustrt2::rust_get_test_int());
+        assert_eq!(x as *const _, rustrt3::rust_get_test_int());
+    }
+}
diff --git a/src/test/ui/foreign/foreign-call-no-runtime.rs b/src/test/ui/foreign/foreign-call-no-runtime.rs
new file mode 100644
index 00000000000..c6afa07ad05
--- /dev/null
+++ b/src/test/ui/foreign/foreign-call-no-runtime.rs
@@ -0,0 +1,55 @@
+// run-pass
+// ignore-emscripten no threads support
+
+#![feature(rustc_private)]
+
+extern crate libc;
+
+use std::mem;
+use std::thread;
+
+#[link(name = "rust_test_helpers", kind = "static")]
+extern {
+    fn rust_dbg_call(cb: extern "C" fn(libc::uintptr_t),
+                     data: libc::uintptr_t) -> libc::uintptr_t;
+}
+
+pub fn main() {
+    unsafe {
+        thread::spawn(move|| {
+            let i: isize = 100;
+            rust_dbg_call(callback_isize, mem::transmute(&i));
+        }).join().unwrap();
+
+        thread::spawn(move|| {
+            let i: i32 = 100;
+            rust_dbg_call(callback_i32, mem::transmute(&i));
+        }).join().unwrap();
+
+        thread::spawn(move|| {
+            let i: i64 = 100;
+            rust_dbg_call(callback_i64, mem::transmute(&i));
+        }).join().unwrap();
+    }
+}
+
+extern fn callback_isize(data: libc::uintptr_t) {
+    unsafe {
+        let data: *const isize = mem::transmute(data);
+        assert_eq!(*data, 100);
+    }
+}
+
+extern fn callback_i64(data: libc::uintptr_t) {
+    unsafe {
+        let data: *const i64 = mem::transmute(data);
+        assert_eq!(*data, 100);
+    }
+}
+
+extern fn callback_i32(data: libc::uintptr_t) {
+    unsafe {
+        let data: *const i32 = mem::transmute(data);
+        assert_eq!(*data, 100);
+    }
+}
diff --git a/src/test/ui/foreign/foreign-dupe.rs b/src/test/ui/foreign/foreign-dupe.rs
new file mode 100644
index 00000000000..3c9f0f583d4
--- /dev/null
+++ b/src/test/ui/foreign/foreign-dupe.rs
@@ -0,0 +1,17 @@
+// run-pass
+// aux-build:foreign_lib.rs
+// ignore-wasm32-bare no libc to test ffi with
+
+// Check that we can still call duplicated extern (imported) functions
+// which were declared in another crate. See issues #32740 and #32783.
+
+
+extern crate foreign_lib;
+
+pub fn main() {
+    unsafe {
+        let x = foreign_lib::rustrt::rust_get_test_int();
+        assert_eq!(x, foreign_lib::rustrt2::rust_get_test_int());
+        assert_eq!(x as *const _, foreign_lib::rustrt3::rust_get_test_int());
+    }
+}
diff --git a/src/test/ui/foreign/foreign-fn-linkname.rs b/src/test/ui/foreign/foreign-fn-linkname.rs
new file mode 100644
index 00000000000..1f048159064
--- /dev/null
+++ b/src/test/ui/foreign/foreign-fn-linkname.rs
@@ -0,0 +1,30 @@
+// run-pass
+// ignore-wasm32-bare no libc to test ffi with
+// ignore-sgx no libc
+
+#![feature(rustc_private)]
+
+extern crate libc;
+use std::ffi::CString;
+
+mod mlibc {
+    use libc::{c_char, size_t};
+
+    extern {
+        #[link_name = "strlen"]
+        pub fn my_strlen(str: *const c_char) -> size_t;
+    }
+}
+
+fn strlen(str: String) -> usize {
+    // C string is terminated with a zero
+    let s = CString::new(str).unwrap();
+    unsafe {
+        mlibc::my_strlen(s.as_ptr()) as usize
+    }
+}
+
+pub fn main() {
+    let len = strlen("Rust".to_string());
+    assert_eq!(len, 4);
+}
diff --git a/src/test/ui/foreign/foreign-fn-with-byval.rs b/src/test/ui/foreign/foreign-fn-with-byval.rs
new file mode 100644
index 00000000000..3a35599aa57
--- /dev/null
+++ b/src/test/ui/foreign/foreign-fn-with-byval.rs
@@ -0,0 +1,32 @@
+// run-pass
+#![allow(improper_ctypes)]
+
+// ignore-wasm32-bare no libc to test ffi with
+
+#[derive(Copy, Clone)]
+pub struct S {
+    x: u64,
+    y: u64,
+    z: u64,
+}
+
+#[link(name = "rust_test_helpers", kind = "static")]
+extern {
+    pub fn get_x(x: S) -> u64;
+    pub fn get_y(x: S) -> u64;
+    pub fn get_z(x: S) -> u64;
+}
+
+#[inline(never)]
+fn indirect_call(func: unsafe extern fn(s: S) -> u64, s: S) -> u64 {
+    unsafe {
+        func(s)
+    }
+}
+
+fn main() {
+    let s = S { x: 1, y: 2, z: 3 };
+    assert_eq!(s.x, indirect_call(get_x, s));
+    assert_eq!(s.y, indirect_call(get_y, s));
+    assert_eq!(s.z, indirect_call(get_z, s));
+}
diff --git a/src/test/ui/foreign/foreign-int-types.rs b/src/test/ui/foreign/foreign-int-types.rs
new file mode 100644
index 00000000000..66296574d7d
--- /dev/null
+++ b/src/test/ui/foreign/foreign-int-types.rs
@@ -0,0 +1,13 @@
+// run-pass
+#![forbid(improper_ctypes)]
+#![allow(dead_code)]
+
+mod xx {
+    extern {
+        pub fn strlen(str: *const u8) -> usize;
+        pub fn foo(x: isize, y: usize);
+    }
+}
+
+fn main() {
+}
diff --git a/src/test/ui/foreign/foreign-mod-src/compiletest-ignore-dir b/src/test/ui/foreign/foreign-mod-src/compiletest-ignore-dir
new file mode 100644
index 00000000000..e69de29bb2d
--- /dev/null
+++ b/src/test/ui/foreign/foreign-mod-src/compiletest-ignore-dir
diff --git a/src/test/ui/foreign/foreign-mod-src/inner.rs b/src/test/ui/foreign/foreign-mod-src/inner.rs
new file mode 100644
index 00000000000..cf484878b07
--- /dev/null
+++ b/src/test/ui/foreign/foreign-mod-src/inner.rs
@@ -0,0 +1,14 @@
+// run-pass
+
+
+
+pub fn main() {
+    let f = "Makefile";
+    let s = rustrt.str_buf(f);
+    let buf = libc.malloc(1024);
+    let fd = libc.open(s, 0, 0);
+    libc.read(fd, buf, 1024);
+    libc.write(1, buf, 1024);
+    libc.close(fd);
+    libc.free(buf);
+}
diff --git a/src/test/ui/foreign/foreign-mod-unused-const.rs b/src/test/ui/foreign/foreign-mod-unused-const.rs
new file mode 100644
index 00000000000..d9efbe00e52
--- /dev/null
+++ b/src/test/ui/foreign/foreign-mod-unused-const.rs
@@ -0,0 +1,12 @@
+// run-pass
+#![allow(dead_code)]
+// pretty-expanded FIXME #23616
+
+mod foo {
+    extern {
+        pub static errno: u32;
+    }
+}
+
+pub fn main() {
+}
diff --git a/src/test/ui/foreign/foreign-no-abi.rs b/src/test/ui/foreign/foreign-no-abi.rs
new file mode 100644
index 00000000000..2f33fb47656
--- /dev/null
+++ b/src/test/ui/foreign/foreign-no-abi.rs
@@ -0,0 +1,22 @@
+// run-pass
+// ABI is cdecl by default
+
+// ignore-wasm32-bare no libc to test ffi with
+// pretty-expanded FIXME #23616
+
+#![feature(rustc_private)]
+
+mod rustrt {
+    extern crate libc;
+
+    #[link(name = "rust_test_helpers", kind = "static")]
+    extern {
+        pub fn rust_get_test_int() -> libc::intptr_t;
+    }
+}
+
+pub fn main() {
+    unsafe {
+        rustrt::rust_get_test_int();
+    }
+}
diff --git a/src/test/ui/foreign/foreign-src/compiletest-ignore-dir b/src/test/ui/foreign/foreign-src/compiletest-ignore-dir
new file mode 100644
index 00000000000..e69de29bb2d
--- /dev/null
+++ b/src/test/ui/foreign/foreign-src/compiletest-ignore-dir
diff --git a/src/test/ui/foreign/foreign-src/foreign.rs b/src/test/ui/foreign/foreign-src/foreign.rs
new file mode 100644
index 00000000000..47016ad6ce7
--- /dev/null
+++ b/src/test/ui/foreign/foreign-src/foreign.rs
@@ -0,0 +1,9 @@
+// run-pass
+
+
+
+pub fn main() {
+    libc.puts(rustrt.str_buf("hello, extern world 1"));
+    libc.puts(rustrt.str_buf("hello, extern world 2"));
+    libc.puts(rustrt.str_buf("hello, extern world 3"));
+}
diff --git a/src/test/ui/foreign/foreign-truncated-arguments.rs b/src/test/ui/foreign/foreign-truncated-arguments.rs
new file mode 100644
index 00000000000..c61c2b587b6
--- /dev/null
+++ b/src/test/ui/foreign/foreign-truncated-arguments.rs
@@ -0,0 +1,20 @@
+// run-pass
+// compile-flags: -O
+// Regression test for https://github.com/rust-lang/rust/issues/33868
+
+#[repr(C)]
+pub struct S {
+    a: u32,
+    b: f32,
+    c: u32
+}
+
+#[no_mangle]
+#[inline(never)]
+pub extern "C" fn test(s: S) -> u32 {
+    s.c
+}
+
+fn main() {
+    assert_eq!(test(S{a: 0, b: 0.0, c: 42}), 42);
+}
diff --git a/src/test/ui/foreign/foreign2.rs b/src/test/ui/foreign/foreign2.rs
new file mode 100644
index 00000000000..c1ab57776f6
--- /dev/null
+++ b/src/test/ui/foreign/foreign2.rs
@@ -0,0 +1,30 @@
+// run-pass
+#![allow(dead_code)]
+// ignore-wasm32-bare no libc to test ffi with
+// pretty-expanded FIXME #23616
+
+#![feature(rustc_private)]
+
+extern crate libc;
+
+mod bar {
+    extern {}
+}
+
+mod zed {
+    extern {}
+}
+
+mod mlibc {
+    use libc::{c_int, c_void, size_t, ssize_t};
+
+    extern {
+        pub fn write(fd: c_int, buf: *const c_void, count: size_t) -> ssize_t;
+    }
+}
+
+mod baz {
+    extern {}
+}
+
+pub fn main() { }