-
Notifications
You must be signed in to change notification settings - Fork 273
Description
Currently c2rust translates the typedef of size_t
to unsigned long
in <stdlib.h>
and thus embeds it's size into the type.
It could translate size_t
to libc::size_t
or usize
which would more directly encode the source, while preserving semantics.
As mentioned in #20 many integer types differ across platforms but this does not apply to size_t
:
The big issue with changing
libc
types tousize
/isize
by default is that there are corner cases where the change is not sound.
For example, Clong
generally matchesisize
on Linux but not on Windows, wherelong
is alwaysi32
. There are a few C types that we can always convert, likesize_t
andintptr_t
.
Originally posted by @ahomescu in #20 (comment)
Presumably any integer typedef named size_t
is intended to be libc::size_t
, but an option could be provided.
For Example:
#include <stdlib.h>;
size_t x = 0;
intptr_t y = 0;
Translates to:
#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
#![register_tool(c2rust)]
#![feature(register_tool)]
pub type size_t = libc::c_ulong;
pub type __intptr_t = libc::c_long;
#[no_mangle]
pub static mut x: size_t = 0 as libc::c_int as size_t;
#[no_mangle]
pub static mut y: __intptr_t = 0 as libc::c_int as __intptr_t;
Instead of:
#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
#![register_tool(c2rust)]
#![feature(register_tool)]
pub type size_t = libc::size_t; // or use libc::size_t;
pub type __intptr_t = libc::intptr_t;
#[no_mangle]
pub static mut x: size_t = 0 as libc::c_int as size_t;
#[no_mangle]
pub static mut y: __intptr_t = 0 as libc::c_int as __intptr_t;