Skip to content

Commit

Permalink
liblibc: don't use int/uint for intptr_t/uintptr_t
Browse files Browse the repository at this point in the history
int/uint aren't considered FFI safe, replace them with the actual type they
represent (i64/u64 or i32/u32). This is a breaking change, but at most a cast
to `uint` or `int` needs to be added.

[breaking-change]
  • Loading branch information
emberian committed Aug 21, 2014
1 parent 0cffa32 commit 2dc2ac1
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 71 deletions.
37 changes: 19 additions & 18 deletions src/liblibc/lib.rs
Expand Up @@ -507,8 +507,8 @@ pub mod types {
pub mod c99 {
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intptr_t = int;
pub type uintptr_t = uint;
pub type intptr_t = i32;
pub type uintptr_t = u32;
}
#[cfg(target_arch = "x86")]
#[cfg(target_arch = "mips")]
Expand Down Expand Up @@ -702,8 +702,8 @@ pub mod types {
pub mod c99 {
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intptr_t = int;
pub type uintptr_t = uint;
pub type intptr_t = i64;
pub type uintptr_t = u64;
}
pub mod posix88 {
pub type off_t = i64;
Expand Down Expand Up @@ -911,8 +911,8 @@ pub mod types {
pub mod c99 {
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intptr_t = int;
pub type uintptr_t = uint;
pub type intptr_t = i64;
pub type uintptr_t = u64;
}
pub mod posix88 {
pub type off_t = i64;
Expand Down Expand Up @@ -1124,8 +1124,8 @@ pub mod types {
pub mod c99 {
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intptr_t = int;
pub type uintptr_t = uint;
pub type intptr_t = i64;
pub type uintptr_t = u64;
}
pub mod posix88 {
pub type off_t = i64;
Expand Down Expand Up @@ -1243,9 +1243,10 @@ pub mod types {
}

pub mod bsd44 {
use types::os::arch::c95::{c_char, c_int, c_uint, size_t, uintptr_t};
use types::os::arch::c95::{c_char, c_int, c_uint, size_t};
use types::os::arch::c99::uintptr_t;

pub type SOCKET = uint;
pub type SOCKET = uintptr_t;
pub type socklen_t = c_int;
pub type sa_family_t = u16;
pub type in_port_t = u16;
Expand Down Expand Up @@ -1356,8 +1357,8 @@ pub mod types {
pub mod c99 {
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intptr_t = int;
pub type uintptr_t = uint;
pub type intptr_t = i32;
pub type uintptr_t = u32;
}

pub mod posix88 {
Expand Down Expand Up @@ -1486,7 +1487,7 @@ pub mod types {
pub dwPageSize: DWORD,
pub lpMinimumApplicationAddress: LPVOID,
pub lpMaximumApplicationAddress: LPVOID,
pub dwActiveProcessorMask: uint,
pub dwActiveProcessorMask: uintptr_t,
pub dwNumberOfProcessors: DWORD,
pub dwProcessorType: DWORD,
pub dwAllocationGranularity: DWORD,
Expand Down Expand Up @@ -1720,8 +1721,8 @@ pub mod types {
pub mod c99 {
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intptr_t = int;
pub type uintptr_t = uint;
pub type intptr_t = i32;
pub type uintptr_t = u32;
}
pub mod posix88 {
pub type off_t = i64;
Expand Down Expand Up @@ -1821,8 +1822,8 @@ pub mod types {
pub mod c99 {
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intptr_t = int;
pub type uintptr_t = uint;
pub type intptr_t = i64;
pub type uintptr_t = u64;
}
pub mod posix88 {
pub type off_t = i64;
Expand Down Expand Up @@ -4401,7 +4402,7 @@ pub mod funcs {
pub fn glob(pattern: *const c_char,
flags: c_int,
errfunc: ::Nullable<extern "C" fn(epath: *const c_char,
errno: int) -> int>,
errno: c_int) -> int>,
pglob: *mut glob_t);
pub fn globfree(pglob: *mut glob_t);
}
Expand Down
1 change: 1 addition & 0 deletions src/libnative/io/c_unix.rs
Expand Up @@ -235,6 +235,7 @@ mod signal {
pub type sigset_t = u32;
#[cfg(target_os = "freebsd")]
#[cfg(target_os = "dragonfly")]
#[repr(C)]
pub struct sigset_t {
bits: [u32, ..4],
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/lint/builtin.rs
Expand Up @@ -369,9 +369,9 @@ impl LintPass for CTypes {

if !ty::is_ffi_safe(cx.tcx, tty) {
cx.span_lint(CTYPES, ty.span,
"found enum type without foreign-function-safe
"found type without foreign-function-safe
representation annotation in foreign module, consider \
adding a #[repr(...)] attribute to the enumeration");
adding a #[repr(...)] attribute to the type");
}
}
_ => ()
Expand Down
4 changes: 2 additions & 2 deletions src/librustuv/uvll.rs
Expand Up @@ -378,14 +378,14 @@ pub unsafe fn free_req(v: *mut c_void) {
#[test]
fn handle_sanity_check() {
unsafe {
assert_eq!(UV_HANDLE_TYPE_MAX as uint, rust_uv_handle_type_max());
assert_eq!(UV_HANDLE_TYPE_MAX as libc::uintptr_t, rust_uv_handle_type_max());
}
}

#[test]
fn request_sanity_check() {
unsafe {
assert_eq!(UV_REQ_TYPE_MAX as uint, rust_uv_req_type_max());
assert_eq!(UV_REQ_TYPE_MAX as libc::uintptr_t, rust_uv_req_type_max());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/os.rs
Expand Up @@ -59,7 +59,7 @@ use libc::c_char;
/// Get the number of cores available
pub fn num_cpus() -> uint {
unsafe {
return rust_get_num_cpus();
return rust_get_num_cpus() as uint;
}

extern {
Expand Down
6 changes: 3 additions & 3 deletions src/test/auxiliary/extern-crosscrate-source.rs
Expand Up @@ -24,17 +24,17 @@ pub mod rustrt {
}
}

pub fn fact(n: uint) -> uint {
pub fn fact(n: libc::uintptr_t) -> libc::uintptr_t {
unsafe {
println!("n = {}", n);
rustrt::rust_dbg_call(cb, n)
}
}

pub extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
if data == 1u {
if data == 1 {
data
} else {
fact(data - 1u) * data
fact(data - 1) * data
}
}
18 changes: 9 additions & 9 deletions src/test/compile-fail/issue-14309.rs
Expand Up @@ -11,19 +11,19 @@
#![deny(ctypes)]
#![allow(dead_code)]

struct A { //~ NOTE consider adding `#[repr(C)]` to this type
x: int
struct A {
x: i32
}

#[repr(C, packed)]
struct B {
x: int,
x: i32,
y: A
}

#[repr(C)]
struct C {
x: int
x: i32
}

type A2 = A;
Expand All @@ -37,13 +37,13 @@ struct D {
}

extern "C" {
fn foo(x: A); //~ ERROR found struct without FFI-safe representation used in FFI
fn bar(x: B); //~ ERROR FFI-safe
fn foo(x: A); //~ ERROR found type without foreign-function-safe
fn bar(x: B); //~ ERROR foreign-function-safe
fn baz(x: C);
fn qux(x: A2); //~ ERROR FFI-safe
fn quux(x: B2); //~ ERROR FFI-safe
fn qux(x: A2); //~ ERROR foreign-function-safe
fn quux(x: B2); //~ ERROR foreign-function-safe
fn corge(x: C2);
fn fred(x: D); //~ ERROR FFI-safe
fn fred(x: D); //~ ERROR foreign-function-safe
}

fn main() { }
6 changes: 3 additions & 3 deletions src/test/compile-fail/lint-ctypes-enum.rs
Expand Up @@ -18,9 +18,9 @@ enum T { E, F, G }

extern {
fn zf(x: Z);
fn uf(x: U);
fn bf(x: B); //~ ERROR found enum without FFI-safe
fn tf(x: T); //~ ERROR found enum without FFI-safe
fn uf(x: U); //~ ERROR found type without foreign-function-safe
fn bf(x: B); //~ ERROR found type without foreign-function-safe
fn tf(x: T); //~ ERROR found type without foreign-function-safe
}

pub fn main() { }
10 changes: 5 additions & 5 deletions src/test/run-pass/extern-call-deep.rs
Expand Up @@ -22,22 +22,22 @@ mod rustrt {
}

extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
if data == 1u {
if data == 1 {
data
} else {
count(data - 1u) + 1u
count(data - 1) + 1
}
}

fn count(n: uint) -> uint {
fn count(n: libc::uintptr_t) -> libc::uintptr_t {
unsafe {
println!("n = {}", n);
rustrt::rust_dbg_call(cb, n)
}
}

pub fn main() {
let result = count(1000u);
let result = count(1000);
println!("result = {}", result);
assert_eq!(result, 1000u);
assert_eq!(result, 1000);
}
10 changes: 5 additions & 5 deletions src/test/run-pass/extern-call-deep2.rs
Expand Up @@ -23,14 +23,14 @@ mod rustrt {
}

extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
if data == 1u {
if data == 1 {
data
} else {
count(data - 1u) + 1u
count(data - 1) + 1
}
}

fn count(n: uint) -> uint {
fn count(n: libc::uintptr_t) -> libc::uintptr_t {
unsafe {
println!("n = {}", n);
rustrt::rust_dbg_call(cb, n)
Expand All @@ -41,8 +41,8 @@ pub fn main() {
// Make sure we're on a task with small Rust stacks (main currently
// has a large stack)
task::spawn(proc() {
let result = count(1000u);
let result = count(1000);
println!("result = {}", result);
assert_eq!(result, 1000u);
assert_eq!(result, 1000);
});
}
10 changes: 5 additions & 5 deletions src/test/run-pass/extern-call-indirect.rs
Expand Up @@ -22,22 +22,22 @@ mod rustrt {
}

extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
if data == 1u {
if data == 1 {
data
} else {
fact(data - 1u) * data
fact(data - 1) * data
}
}

fn fact(n: uint) -> uint {
fn fact(n: libc::uintptr_t) -> libc::uintptr_t {
unsafe {
println!("n = {}", n);
rustrt::rust_dbg_call(cb, n)
}
}

pub fn main() {
let result = fact(10u);
let result = fact(10);
println!("result = {}", result);
assert_eq!(result, 3628800u);
assert_eq!(result, 3628800);
}
10 changes: 5 additions & 5 deletions src/test/run-pass/extern-call-scrub.rs
Expand Up @@ -27,14 +27,14 @@ mod rustrt {
}

extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
if data == 1u {
if data == 1 {
data
} else {
count(data - 1u) + count(data - 1u)
count(data - 1) + count(data - 1)
}
}

fn count(n: uint) -> uint {
fn count(n: libc::uintptr_t) -> libc::uintptr_t {
unsafe {
println!("n = {}", n);
rustrt::rust_dbg_call(cb, n)
Expand All @@ -45,8 +45,8 @@ pub fn main() {
// Make sure we're on a task with small Rust stacks (main currently
// has a large stack)
task::spawn(proc() {
let result = count(12u);
let result = count(12);
println!("result = {}", result);
assert_eq!(result, 2048u);
assert_eq!(result, 2048);
});
}
7 changes: 4 additions & 3 deletions src/test/run-pass/extern-crosscrate.rs
Expand Up @@ -11,16 +11,17 @@
//aux-build:extern-crosscrate-source.rs

extern crate externcallback;
extern crate libc;

fn fact(n: uint) -> uint {
fn fact(n: libc::uintptr_t) -> libc::uintptr_t {
unsafe {
println!("n = {}", n);
externcallback::rustrt::rust_dbg_call(externcallback::cb, n)
}
}

pub fn main() {
let result = fact(10u);
let result = fact(10);
println!("result = {}", result);
assert_eq!(result, 3628800u);
assert_eq!(result, 3628800);
}
10 changes: 5 additions & 5 deletions src/test/run-pass/extern-stress.rs
Expand Up @@ -26,24 +26,24 @@ mod rustrt {
}

extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
if data == 1u {
if data == 1 {
data
} else {
task::deschedule();
count(data - 1u) + count(data - 1u)
count(data - 1) + count(data - 1)
}
}

fn count(n: uint) -> uint {
fn count(n: libc::uintptr_t) -> libc::uintptr_t {
unsafe {
rustrt::rust_dbg_call(cb, n)
}
}

pub fn main() {
for _ in range(0, 100u) {
for _ in range(0u, 100) {
task::spawn(proc() {
assert_eq!(count(5u), 16u);
assert_eq!(count(5), 16);
});
}
}

1 comment on commit 2dc2ac1

@vks
Copy link
Contributor

@vks vks commented on 2dc2ac1 Aug 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation kind of suggests that uint/int are equivalent to uintptr_t/intptr_t. Why are they not FFI save? I think this should be documented somewhere.

Please sign in to comment.