Skip to content

size_t is translated to libc::c_ulong instead of libc::size_t #870

@Dante-Broggi

Description

@Dante-Broggi

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 to usize/isize by default is that there are corner cases where the change is not sound.
For example, C long generally matches isize on Linux but not on Windows, where long is always i32. There are a few C types that we can always convert, like size_t and intptr_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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions