Skip to content

Commit 4ae5c83

Browse files
committed
transpile: Translate null pointers with ptr::null[_mut]
1 parent bf97325 commit 4ae5c83

File tree

9 files changed

+46
-26
lines changed

9 files changed

+46
-26
lines changed

c2rust-transpile/src/translator/mod.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,7 +2990,7 @@ impl<'c> Translation<'c> {
29902990
}
29912991
_ => {
29922992
// Non function null ptrs provide enough information to skip
2993-
// type annotations; ie `= 0 as *const MyStruct;`
2993+
// type annotations; ie `= ::core::ptr::null::<MyStruct>();`
29942994
if initializer.is_none() {
29952995
return false;
29962996
}
@@ -3086,17 +3086,37 @@ impl<'c> Translation<'c> {
30863086
return Ok(mk().path_expr(vec!["None"]));
30873087
}
30883088

3089-
let pointee = self
3089+
let pointer_qty = self
30903090
.ast_context
30913091
.get_pointee_qual_type(type_id)
30923092
.ok_or_else(|| TranslationError::generic("null_ptr requires a pointer"))?;
3093-
let ty = self.convert_type(type_id)?;
3094-
let mut zero = mk().lit_expr(mk().int_unsuffixed_lit(0));
3095-
if is_static && !pointee.qualifiers.is_const {
3096-
let ty_ = self.convert_pointee_type(pointee.ctype)?;
3097-
zero = mk().cast_expr(zero, mk().ptr_ty(ty_));
3093+
3094+
let func = if pointer_qty.qualifiers.is_const
3095+
// static variable initializers aren't able to use null_mut
3096+
// TODO: Rust 1.83: Allowed, so this can be removed.
3097+
|| is_static
3098+
{
3099+
"null"
3100+
} else {
3101+
"null_mut"
3102+
};
3103+
let pointee_ty = self.convert_pointee_type(pointer_qty.ctype)?;
3104+
let type_args = mk().angle_bracketed_args(vec![pointee_ty.clone()]);
3105+
let mut val = mk().call_expr(
3106+
mk().abs_path_expr(vec![
3107+
mk().path_segment("core"),
3108+
mk().path_segment("ptr"),
3109+
mk().path_segment_with_args(func, type_args),
3110+
]),
3111+
vec![],
3112+
);
3113+
3114+
// TODO: Rust 1.83: Remove.
3115+
if is_static && !pointer_qty.qualifiers.is_const {
3116+
val = mk().cast_expr(val, mk().mutbl().ptr_ty(pointee_ty));
30983117
}
3099-
Ok(mk().cast_expr(zero, ty))
3118+
3119+
Ok(val)
31003120
}
31013121

31023122
fn addr_lhs(

c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ pub unsafe extern "C" fn VM_CallCompiled(
2828
mut args: *mut ::core::ffi::c_int,
2929
) -> ::core::ffi::c_int {
3030
let mut stack: [byte; 271] = [0; 271];
31-
let mut entryPoint: *mut ::core::ffi::c_void = 0 as *mut ::core::ffi::c_void;
31+
let mut entryPoint: *mut ::core::ffi::c_void = ::core::ptr::null_mut::<::core::ffi::c_void>();
3232
let mut programStack: ::core::ffi::c_int = 0;
3333
let mut stackOnEntry: ::core::ffi::c_int = 0;
34-
let mut image: *mut byte = 0 as *mut byte;
35-
let mut opStack: *mut ::core::ffi::c_int = 0 as *mut ::core::ffi::c_int;
34+
let mut image: *mut byte = ::core::ptr::null_mut::<byte>();
35+
let mut opStack: *mut ::core::ffi::c_int = ::core::ptr::null_mut::<::core::ffi::c_int>();
3636
let mut opStackOfs: ::core::ffi::c_int = 0;
3737
let mut arg: ::core::ffi::c_int = 0;
3838
let mut currentVM: *mut vm_t = vm;

c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ pub unsafe extern "C" fn VM_CallCompiled(
3030
mut args: *mut ::core::ffi::c_int,
3131
) -> ::core::ffi::c_int {
3232
let mut stack: [byte; 271] = [0; 271];
33-
let mut entryPoint: *mut ::core::ffi::c_void = 0 as *mut ::core::ffi::c_void;
33+
let mut entryPoint: *mut ::core::ffi::c_void = ::core::ptr::null_mut::<::core::ffi::c_void>();
3434
let mut programStack: ::core::ffi::c_int = 0;
3535
let mut stackOnEntry: ::core::ffi::c_int = 0;
36-
let mut image: *mut byte = 0 as *mut byte;
37-
let mut opStack: *mut ::core::ffi::c_int = 0 as *mut ::core::ffi::c_int;
36+
let mut image: *mut byte = ::core::ptr::null_mut::<byte>();
37+
let mut opStack: *mut ::core::ffi::c_int = ::core::ptr::null_mut::<::core::ffi::c_int>();
3838
let mut opStackOfs: ::core::ffi::c_int = 0;
3939
let mut arg: ::core::ffi::c_int = 0;
4040
let mut currentVM: *mut vm_t = vm;

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ pub unsafe extern "C" fn alloca_sum(
1919
mut val2: ::core::ffi::c_int,
2020
) -> ::core::ffi::c_int {
2121
let mut alloca_allocations: Vec<Vec<u8>> = Vec::new();
22-
let mut alloca1: *mut ::core::ffi::c_int = 0 as *mut ::core::ffi::c_int;
23-
let mut alloca2: *mut ::core::ffi::c_int = 0 as *mut ::core::ffi::c_int;
22+
let mut alloca1: *mut ::core::ffi::c_int = ::core::ptr::null_mut::<::core::ffi::c_int>();
23+
let mut alloca2: *mut ::core::ffi::c_int = ::core::ptr::null_mut::<::core::ffi::c_int>();
2424
if TRUE != 0 {
2525
alloca_allocations.push(::std::vec::from_elem(
2626
0,

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub unsafe extern "C" fn entry() {
5151
let mut int_too_short: [::core::ffi::c_int; 16] = [0 as ::core::ffi::c_int; 16];
5252
int_too_short[15 as ::core::ffi::c_int as usize] += 9 as ::core::ffi::c_int;
5353
let mut struct_init_too_short: [C2RustUnnamed_0; 1] = [C2RustUnnamed_0 {
54-
x: 0 as *mut ::core::ffi::c_char,
54+
x: ::core::ptr::null_mut::<::core::ffi::c_char>(),
5555
y: 0,
5656
}; 1];
5757
struct_init_too_short[0 as ::core::ffi::c_int as usize].y += 9 as ::core::ffi::c_int;

c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct Scope {
1919
#[no_mangle]
2020
pub static mut scope: *mut Scope = &{
2121
let mut init = Scope {
22-
next: 0 as *const Scope as *mut Scope,
22+
next: ::core::ptr::null::<Scope>() as *mut Scope,
2323
};
2424
init
2525
} as *const Scope as *mut Scope;

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub unsafe extern "C" fn unary_without_side_effect() {
3838
}
3939
#[no_mangle]
4040
pub unsafe extern "C" fn unary_with_side_effect() {
41-
let mut arr: [*mut ::core::ffi::c_char; 1] = [0 as *mut ::core::ffi::c_char];
41+
let mut arr: [*mut ::core::ffi::c_char; 1] = [::core::ptr::null_mut::<::core::ffi::c_char>()];
4242
-side_effect();
4343
side_effect();
4444
!side_effect();

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ static mut global_static_const_mixed_arithmetic: ::core::ffi::c_float =
240240
- true_0 as ::core::ffi::c_double) as ::core::ffi::c_float;
241241
static mut global_static_const_parens: ::core::ffi::c_int = PARENS;
242242
static mut global_static_const_ptr_arithmetic: *const ::core::ffi::c_char =
243-
0 as *const ::core::ffi::c_char;
243+
::core::ptr::null::<::core::ffi::c_char>();
244244
static mut global_static_const_widening_cast: ::core::ffi::c_ulonglong = WIDENING_CAST;
245245
static mut global_static_const_narrowing_cast: ::core::ffi::c_char =
246246
LITERAL_INT as ::core::ffi::c_char;
@@ -254,7 +254,7 @@ static mut global_static_const_str_concatenation: [::core::ffi::c_char; 18] = un
254254
static mut global_static_const_builtin: ::core::ffi::c_int =
255255
(LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32;
256256
static mut global_static_const_ref_indexing: *const ::core::ffi::c_char =
257-
0 as *const ::core::ffi::c_char;
257+
::core::ptr::null::<::core::ffi::c_char>();
258258
static mut global_static_const_ref_struct: *const S = &LITERAL_STRUCT as *const S as *mut S;
259259
static mut global_static_const_ternary: ::core::ffi::c_int = 0;
260260
static mut global_static_const_member: ::core::ffi::c_int = 0;
@@ -313,7 +313,7 @@ pub static mut global_const_mixed_arithmetic: ::core::ffi::c_float =
313313
pub static mut global_const_parens: ::core::ffi::c_int = PARENS;
314314
#[no_mangle]
315315
pub static mut global_const_ptr_arithmetic: *const ::core::ffi::c_char =
316-
0 as *const ::core::ffi::c_char;
316+
::core::ptr::null::<::core::ffi::c_char>();
317317
#[no_mangle]
318318
pub static mut global_const_widening_cast: ::core::ffi::c_ulonglong = WIDENING_CAST;
319319
#[no_mangle]
@@ -335,7 +335,7 @@ pub static mut global_const_builtin: ::core::ffi::c_int =
335335
(LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32;
336336
#[no_mangle]
337337
pub static mut global_const_ref_indexing: *const ::core::ffi::c_char =
338-
0 as *const ::core::ffi::c_char;
338+
::core::ptr::null::<::core::ffi::c_char>();
339339
#[no_mangle]
340340
pub static mut global_const_ref_struct: *const S = &LITERAL_STRUCT as *const S as *mut S;
341341
#[no_mangle]
@@ -363,7 +363,7 @@ pub unsafe extern "C" fn reference_define() -> ::core::ffi::c_int {
363363
#[no_mangle]
364364
pub static mut fns: fn_ptrs = {
365365
let mut init = fn_ptrs {
366-
v: 0 as *const ::core::ffi::c_void as *mut ::core::ffi::c_void,
366+
v: ::core::ptr::null::<::core::ffi::c_void>() as *mut ::core::ffi::c_void,
367367
fn1: None,
368368
fn2: None,
369369
};

c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ pub unsafe extern "C" fn array_extra_braces() {
6868
pub unsafe extern "C" fn array_of_ptrs() {
6969
let mut _s: [*const ::core::ffi::c_char; 3] = [
7070
b"hello\0" as *const u8 as *const ::core::ffi::c_char,
71-
0 as *const ::core::ffi::c_char,
72-
0 as *const ::core::ffi::c_char,
71+
::core::ptr::null::<::core::ffi::c_char>(),
72+
::core::ptr::null::<::core::ffi::c_char>(),
7373
];
7474
}
7575
#[no_mangle]

0 commit comments

Comments
 (0)