diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index 9054b08681..b0e4d28133 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -2990,7 +2990,7 @@ impl<'c> Translation<'c> { } _ => { // Non function null ptrs provide enough information to skip - // type annotations; ie `= 0 as *const MyStruct;` + // type annotations; ie `= ::core::ptr::null::();` if initializer.is_none() { return false; } @@ -3086,17 +3086,37 @@ impl<'c> Translation<'c> { return Ok(mk().path_expr(vec!["None"])); } - let pointee = self + let pointer_qty = self .ast_context .get_pointee_qual_type(type_id) .ok_or_else(|| TranslationError::generic("null_ptr requires a pointer"))?; - let ty = self.convert_type(type_id)?; - let mut zero = mk().lit_expr(mk().int_unsuffixed_lit(0)); - if is_static && !pointee.qualifiers.is_const { - let ty_ = self.convert_pointee_type(pointee.ctype)?; - zero = mk().cast_expr(zero, mk().ptr_ty(ty_)); + + let func = if pointer_qty.qualifiers.is_const + // static variable initializers aren't able to use null_mut + // TODO: Rust 1.83: Allowed, so this can be removed. + || is_static + { + "null" + } else { + "null_mut" + }; + let pointee_ty = self.convert_pointee_type(pointer_qty.ctype)?; + let type_args = mk().angle_bracketed_args(vec![pointee_ty.clone()]); + let mut val = mk().call_expr( + mk().abs_path_expr(vec![ + mk().path_segment("core"), + mk().path_segment("ptr"), + mk().path_segment_with_args(func, type_args), + ]), + vec![], + ); + + // TODO: Rust 1.83: Remove. + if is_static && !pointer_qty.qualifiers.is_const { + val = mk().cast_expr(val, mk().mutbl().ptr_ty(pointee_ty)); } - Ok(mk().cast_expr(zero, ty)) + + Ok(val) } fn addr_lhs( diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap index 8e15e172ef..720d38b207 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-aarch64@vm_x86.c.snap @@ -28,11 +28,11 @@ pub unsafe extern "C" fn VM_CallCompiled( mut args: *mut ::core::ffi::c_int, ) -> ::core::ffi::c_int { let mut stack: [byte; 271] = [0; 271]; - let mut entryPoint: *mut ::core::ffi::c_void = 0 as *mut ::core::ffi::c_void; + let mut entryPoint: *mut ::core::ffi::c_void = ::core::ptr::null_mut::<::core::ffi::c_void>(); let mut programStack: ::core::ffi::c_int = 0; let mut stackOnEntry: ::core::ffi::c_int = 0; - let mut image: *mut byte = 0 as *mut byte; - let mut opStack: *mut ::core::ffi::c_int = 0 as *mut ::core::ffi::c_int; + let mut image: *mut byte = ::core::ptr::null_mut::(); + let mut opStack: *mut ::core::ffi::c_int = ::core::ptr::null_mut::<::core::ffi::c_int>(); let mut opStackOfs: ::core::ffi::c_int = 0; let mut arg: ::core::ffi::c_int = 0; let mut currentVM: *mut vm_t = vm; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap index 3893b0277f..9b967f6d04 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile-x86_64@vm_x86.c.snap @@ -30,11 +30,11 @@ pub unsafe extern "C" fn VM_CallCompiled( mut args: *mut ::core::ffi::c_int, ) -> ::core::ffi::c_int { let mut stack: [byte; 271] = [0; 271]; - let mut entryPoint: *mut ::core::ffi::c_void = 0 as *mut ::core::ffi::c_void; + let mut entryPoint: *mut ::core::ffi::c_void = ::core::ptr::null_mut::<::core::ffi::c_void>(); let mut programStack: ::core::ffi::c_int = 0; let mut stackOnEntry: ::core::ffi::c_int = 0; - let mut image: *mut byte = 0 as *mut byte; - let mut opStack: *mut ::core::ffi::c_int = 0 as *mut ::core::ffi::c_int; + let mut image: *mut byte = ::core::ptr::null_mut::(); + let mut opStack: *mut ::core::ffi::c_int = ::core::ptr::null_mut::<::core::ffi::c_int>(); let mut opStackOfs: ::core::ffi::c_int = 0; let mut arg: ::core::ffi::c_int = 0; let mut currentVM: *mut vm_t = vm; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap index 126703a4d3..cc09026f03 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@alloca.c.snap @@ -19,8 +19,8 @@ pub unsafe extern "C" fn alloca_sum( mut val2: ::core::ffi::c_int, ) -> ::core::ffi::c_int { let mut alloca_allocations: Vec> = Vec::new(); - let mut alloca1: *mut ::core::ffi::c_int = 0 as *mut ::core::ffi::c_int; - let mut alloca2: *mut ::core::ffi::c_int = 0 as *mut ::core::ffi::c_int; + let mut alloca1: *mut ::core::ffi::c_int = ::core::ptr::null_mut::<::core::ffi::c_int>(); + let mut alloca2: *mut ::core::ffi::c_int = ::core::ptr::null_mut::<::core::ffi::c_int>(); if TRUE != 0 { alloca_allocations.push(::std::vec::from_elem( 0, diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.snap index b8eb61d0cc..032271ecd5 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@arrays.c.snap @@ -51,7 +51,7 @@ pub unsafe extern "C" fn entry() { let mut int_too_short: [::core::ffi::c_int; 16] = [0 as ::core::ffi::c_int; 16]; int_too_short[15 as ::core::ffi::c_int as usize] += 9 as ::core::ffi::c_int; let mut struct_init_too_short: [C2RustUnnamed_0; 1] = [C2RustUnnamed_0 { - x: 0 as *mut ::core::ffi::c_char, + x: ::core::ptr::null_mut::<::core::ffi::c_char>(), y: 0, }; 1]; struct_init_too_short[0 as ::core::ffi::c_int as usize].y += 9 as ::core::ffi::c_int; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.snap index c4251bcb09..db8be5bc49 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@empty_init.c.snap @@ -19,7 +19,7 @@ pub struct Scope { #[no_mangle] pub static mut scope: *mut Scope = &{ let mut init = Scope { - next: 0 as *const Scope as *mut Scope, + next: ::core::ptr::null::() as *mut Scope, }; init } as *const Scope as *mut Scope; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap index 2125fc3142..4941db5813 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap @@ -38,7 +38,7 @@ pub unsafe extern "C" fn unary_without_side_effect() { } #[no_mangle] pub unsafe extern "C" fn unary_with_side_effect() { - let mut arr: [*mut ::core::ffi::c_char; 1] = [0 as *mut ::core::ffi::c_char]; + let mut arr: [*mut ::core::ffi::c_char; 1] = [::core::ptr::null_mut::<::core::ffi::c_char>()]; -side_effect(); side_effect(); !side_effect(); diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap index fc5349c45b..64e751aafa 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap @@ -240,7 +240,7 @@ static mut global_static_const_mixed_arithmetic: ::core::ffi::c_float = - true_0 as ::core::ffi::c_double) as ::core::ffi::c_float; static mut global_static_const_parens: ::core::ffi::c_int = PARENS; static mut global_static_const_ptr_arithmetic: *const ::core::ffi::c_char = - 0 as *const ::core::ffi::c_char; + ::core::ptr::null::<::core::ffi::c_char>(); static mut global_static_const_widening_cast: ::core::ffi::c_ulonglong = WIDENING_CAST; static mut global_static_const_narrowing_cast: ::core::ffi::c_char = LITERAL_INT as ::core::ffi::c_char; @@ -254,7 +254,7 @@ static mut global_static_const_str_concatenation: [::core::ffi::c_char; 18] = un static mut global_static_const_builtin: ::core::ffi::c_int = (LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32; static mut global_static_const_ref_indexing: *const ::core::ffi::c_char = - 0 as *const ::core::ffi::c_char; + ::core::ptr::null::<::core::ffi::c_char>(); static mut global_static_const_ref_struct: *const S = &LITERAL_STRUCT as *const S as *mut S; static mut global_static_const_ternary: ::core::ffi::c_int = 0; 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 = pub static mut global_const_parens: ::core::ffi::c_int = PARENS; #[no_mangle] pub static mut global_const_ptr_arithmetic: *const ::core::ffi::c_char = - 0 as *const ::core::ffi::c_char; + ::core::ptr::null::<::core::ffi::c_char>(); #[no_mangle] pub static mut global_const_widening_cast: ::core::ffi::c_ulonglong = WIDENING_CAST; #[no_mangle] @@ -335,7 +335,7 @@ pub static mut global_const_builtin: ::core::ffi::c_int = (LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32; #[no_mangle] pub static mut global_const_ref_indexing: *const ::core::ffi::c_char = - 0 as *const ::core::ffi::c_char; + ::core::ptr::null::<::core::ffi::c_char>(); #[no_mangle] pub static mut global_const_ref_struct: *const S = &LITERAL_STRUCT as *const S as *mut S; #[no_mangle] @@ -363,7 +363,7 @@ pub unsafe extern "C" fn reference_define() -> ::core::ffi::c_int { #[no_mangle] pub static mut fns: fn_ptrs = { let mut init = fn_ptrs { - v: 0 as *const ::core::ffi::c_void as *mut ::core::ffi::c_void, + v: ::core::ptr::null::<::core::ffi::c_void>() as *mut ::core::ffi::c_void, fn1: None, fn2: None, }; diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.snap index c72a2d4bc7..2a7959eec5 100644 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.snap +++ b/c2rust-transpile/tests/snapshots/snapshots__transpile@str_init.c.snap @@ -68,8 +68,8 @@ pub unsafe extern "C" fn array_extra_braces() { pub unsafe extern "C" fn array_of_ptrs() { let mut _s: [*const ::core::ffi::c_char; 3] = [ b"hello\0" as *const u8 as *const ::core::ffi::c_char, - 0 as *const ::core::ffi::c_char, - 0 as *const ::core::ffi::c_char, + ::core::ptr::null::<::core::ffi::c_char>(), + ::core::ptr::null::<::core::ffi::c_char>(), ]; } #[no_mangle]