Skip to content

Commit f2c0bf4

Browse files
committed
transpile: Call as_ptr on string literals instead of casting
1 parent 85b9d97 commit f2c0bf4

10 files changed

Lines changed: 29 additions & 29 deletions

c2rust-transpile/src/translator/pointers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'c> Translation<'c> {
131131
) = (arg_expr_kind, arg_is_macro)
132132
{
133133
if is_array_decay {
134-
ref_cast_pointee_ty = Some(mk().ident_ty("u8"));
134+
val = val.map(|val| mk().method_call_expr(val, "as_ptr", vec![]));
135135
} else {
136136
let size = self.ast_context.array_len(literal_cty.ctype) * element_size as usize;
137137
ref_cast_pointee_ty =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extern "C" {
1717
#[no_mangle]
1818
pub unsafe extern "C" fn call_printf() {
1919
printf(
20-
b"%d, %f\n\0" as *const u8 as *const ::core::ffi::c_char,
20+
b"%d, %f\n\0".as_ptr() as *const ::core::ffi::c_char,
2121
10 as ::core::ffi::c_int,
2222
1.5f64,
2323
);

c2rust-transpile/tests/snapshots/snapshots__transpile-linux@call_only_once.c.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ pub unsafe extern "C" fn assert_call_only_once() -> ::core::ffi::c_int {
3737
if called_only_once() != 0 {
3838
} else {
3939
__assert_fail(
40-
b"called_only_once()\0" as *const u8 as *const ::core::ffi::c_char,
40+
b"called_only_once()\0".as_ptr() as *const ::core::ffi::c_char,
4141
b"./tests/snapshots/os-specific/call_only_once.c\0"
42-
as *const u8 as *const ::core::ffi::c_char,
42+
.as_ptr() as *const ::core::ffi::c_char,
4343
12 as ::core::ffi::c_uint,
4444
__ASSERT_FUNCTION.as_ptr(),
4545
);

c2rust-transpile/tests/snapshots/snapshots__transpile-macos@call_only_once.c.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ unsafe extern "C" fn called_only_once() -> ::core::ffi::c_int {
2828
pub unsafe extern "C" fn assert_call_only_once() -> ::core::ffi::c_int {
2929
if (called_only_once() == 0) as ::core::ffi::c_int as ::core::ffi::c_long != 0 {
3030
__assert_rtn(
31-
b"assert_call_only_once\0" as *const u8 as *const ::core::ffi::c_char,
32-
b"call_only_once.c\0" as *const u8 as *const ::core::ffi::c_char,
31+
b"assert_call_only_once\0".as_ptr() as *const ::core::ffi::c_char,
32+
b"call_only_once.c\0".as_ptr() as *const ::core::ffi::c_char,
3333
12 as ::core::ffi::c_int,
34-
b"called_only_once()\0" as *const u8 as *const ::core::ffi::c_char,
34+
b"called_only_once()\0".as_ptr() as *const ::core::ffi::c_char,
3535
);
3636
} else {
3737
};

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub struct vastruct<'a> {
3939
#[no_mangle]
4040
pub unsafe extern "C" fn call_printf() {
4141
printf(
42-
b"%d, %f\n\0" as *const u8 as *const ::core::ffi::c_char,
42+
b"%d, %f\n\0".as_ptr() as *const ::core::ffi::c_char,
4343
10 as ::core::ffi::c_int,
4444
1.5f64,
4545
);
@@ -72,19 +72,19 @@ pub unsafe extern "C" fn my_printf(mut fmt: *const ::core::ffi::c_char, mut c2ru
7272
match *fmt as ::core::ffi::c_int {
7373
105 | 100 => {
7474
printf(
75-
b"%d\0" as *const u8 as *const ::core::ffi::c_char,
75+
b"%d\0".as_ptr() as *const ::core::ffi::c_char,
7676
ap.arg::<::core::ffi::c_int>(),
7777
);
7878
}
7979
102 => {
8080
printf(
81-
b"%f\0" as *const u8 as *const ::core::ffi::c_char,
81+
b"%f\0".as_ptr() as *const ::core::ffi::c_char,
8282
ap.arg::<::core::ffi::c_double>(),
8383
);
8484
}
8585
115 => {
8686
printf(
87-
b"%s\0" as *const u8 as *const ::core::ffi::c_char,
87+
b"%s\0".as_ptr() as *const ::core::ffi::c_char,
8888
ap.arg::<*mut ::core::ffi::c_char>(),
8989
);
9090
}
@@ -153,7 +153,7 @@ pub unsafe extern "C" fn restart_valist(mut fmt: *const ::core::ffi::c_char, mut
153153
#[no_mangle]
154154
pub unsafe extern "C" fn print_int(mut ap: *mut ::core::ffi::VaListImpl) {
155155
printf(
156-
b"%d\0" as *const u8 as *const ::core::ffi::c_char,
156+
b"%d\0".as_ptr() as *const ::core::ffi::c_char,
157157
(*ap).arg::<::core::ffi::c_int>(),
158158
);
159159
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub static mut static_char_array: [::core::ffi::c_char; 9] =
4040
unsafe { ::core::mem::transmute::<[u8; 9], [::core::ffi::c_char; 9]>(*b"mystring\0") };
4141
#[no_mangle]
4242
pub static mut static_char_ptr: *mut ::core::ffi::c_char =
43-
b"mystring\0" as *const u8 as *const ::core::ffi::c_char as *mut ::core::ffi::c_char;
43+
b"mystring\0".as_ptr() as *const ::core::ffi::c_char as *mut ::core::ffi::c_char;
4444
#[no_mangle]
4545
pub static mut static_void_ptr: *mut ::core::ffi::c_void =
4646
unsafe { &raw const static_char_array as *mut ::core::ffi::c_char as *mut ::core::ffi::c_void };
@@ -89,11 +89,11 @@ pub unsafe extern "C" fn entry() {
8989
&raw mut char_with_string as *mut ::core::ffi::c_char;
9090
let mut char_var_array_ptr: *mut [::core::ffi::c_char; 4] = &raw mut char_with_string;
9191
let mut const_char_lit_ptr: *const ::core::ffi::c_char =
92-
b"abc\0" as *const u8 as *const ::core::ffi::c_char;
92+
b"abc\0".as_ptr() as *const ::core::ffi::c_char;
9393
let mut const_char_lit_array_ptr: *const [::core::ffi::c_char; 4] =
9494
b"abc\0" as *const [u8; 4] as *const [::core::ffi::c_char; 4];
9595
let mut char_lit_ptr: *mut ::core::ffi::c_char =
96-
b"abc\0" as *const u8 as *const ::core::ffi::c_char as *mut ::core::ffi::c_char;
96+
b"abc\0".as_ptr() as *const ::core::ffi::c_char as *mut ::core::ffi::c_char;
9797
let mut char_lit_array_ptr: *mut [::core::ffi::c_char; 4] = b"abc\0" as *const [u8; 4]
9898
as *const [::core::ffi::c_char; 4]
9999
as *mut [::core::ffi::c_char; 4];

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub const C: C2Rust_Unnamed = 2;
2020
pub const B: C2Rust_Unnamed = 1;
2121
pub const A: C2Rust_Unnamed = 0;
2222
unsafe extern "C" fn side_effect() -> ::core::ffi::c_int {
23-
puts(b"the return of side effect\0" as *const u8 as *const ::core::ffi::c_char);
23+
puts(b"the return of side effect\0".as_ptr() as *const ::core::ffi::c_char);
2424
return 10 as ::core::ffi::c_int;
2525
}
2626
#[no_mangle]
@@ -44,7 +44,7 @@ pub unsafe extern "C" fn unary_with_side_effect() {
4444
side_effect();
4545
!side_effect();
4646
(side_effect() == 0) as ::core::ffi::c_int;
47-
(b"\0" as *const u8 as *const ::core::ffi::c_char).offset(side_effect() as isize)
47+
(b"\0".as_ptr() as *const ::core::ffi::c_char).offset(side_effect() as isize)
4848
as *const ::core::ffi::c_char;
4949
*arr[side_effect() as usize];
5050
arr[side_effect() as usize] = arr[side_effect() as usize].offset(1);
@@ -59,8 +59,8 @@ pub unsafe extern "C" fn compound_literal() {
5959
#[no_mangle]
6060
pub unsafe extern "C" fn statement_expr() {
6161
'_c2rust_label: {
62-
puts(b"should execute\0" as *const u8 as *const ::core::ffi::c_char);
62+
puts(b"should execute\0".as_ptr() as *const ::core::ffi::c_char);
6363
return;
6464
};
65-
puts(b"should be unreachable!\0" as *const u8 as *const ::core::ffi::c_char);
65+
puts(b"should be unreachable!\0".as_ptr() as *const ::core::ffi::c_char);
6666
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub unsafe extern "C" fn local_muts() {
101101
let mut indexing: ::core::ffi::c_char =
102102
NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize];
103103
let mut str_concatenation_ptr: *const ::core::ffi::c_char =
104-
b"hello hello world\0" as *const u8 as *const ::core::ffi::c_char;
104+
b"hello hello world\0".as_ptr() as *const ::core::ffi::c_char;
105105
let mut str_concatenation: [::core::ffi::c_char; 18] =
106106
::core::mem::transmute::<[u8; 18], [::core::ffi::c_char; 18]>(*b"hello hello world\0");
107107
let mut builtin: ::core::ffi::c_int =
@@ -172,7 +172,7 @@ pub unsafe extern "C" fn local_consts() {
172172
let conversion_cast: ::core::ffi::c_double = CONVERSION_CAST;
173173
let indexing: ::core::ffi::c_char = NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize];
174174
let str_concatenation_ptr: *const ::core::ffi::c_char =
175-
b"hello hello world\0" as *const u8 as *const ::core::ffi::c_char;
175+
b"hello hello world\0".as_ptr() as *const ::core::ffi::c_char;
176176
let str_concatenation: [::core::ffi::c_char; 18] =
177177
::core::mem::transmute::<[u8; 18], [::core::ffi::c_char; 18]>(*b"hello hello world\0");
178178
let builtin: ::core::ffi::c_int = (LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32;
@@ -246,7 +246,7 @@ static mut global_static_const_narrowing_cast: ::core::ffi::c_char =
246246
static mut global_static_const_conversion_cast: ::core::ffi::c_double = CONVERSION_CAST;
247247
static mut global_static_const_indexing: ::core::ffi::c_char = 0;
248248
static mut global_static_const_str_concatenation_ptr: *const ::core::ffi::c_char =
249-
b"hello hello world\0" as *const u8 as *const ::core::ffi::c_char;
249+
b"hello hello world\0".as_ptr() as *const ::core::ffi::c_char;
250250
static mut global_static_const_str_concatenation: [::core::ffi::c_char; 18] = unsafe {
251251
::core::mem::transmute::<[u8; 18], [::core::ffi::c_char; 18]>(*b"hello hello world\0")
252252
};
@@ -324,7 +324,7 @@ pub static mut global_const_conversion_cast: ::core::ffi::c_double = CONVERSION_
324324
pub static mut global_const_indexing: ::core::ffi::c_char = 0;
325325
#[no_mangle]
326326
pub static mut global_const_str_concatenation_ptr: *const ::core::ffi::c_char =
327-
b"hello hello world\0" as *const u8 as *const ::core::ffi::c_char;
327+
b"hello hello world\0".as_ptr() as *const ::core::ffi::c_char;
328328
#[no_mangle]
329329
pub static mut global_const_str_concatenation: [::core::ffi::c_char; 18] = unsafe {
330330
::core::mem::transmute::<[u8; 18], [::core::ffi::c_char; 18]>(*b"hello hello world\0")

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ input_file: c2rust-transpile/tests/snapshots/predefined.c
1515
pub unsafe extern "C" fn predefined() {
1616
let mut line: ::core::ffi::c_int = 2 as ::core::ffi::c_int;
1717
let mut file_name: *const ::core::ffi::c_char =
18-
b"predefined.c\0" as *const u8 as *const ::core::ffi::c_char;
18+
b"predefined.c\0".as_ptr() as *const ::core::ffi::c_char;
1919
let mut func: *const ::core::ffi::c_char =
20-
b"predefined\0" as *const u8 as *const ::core::ffi::c_char;
20+
b"predefined\0".as_ptr() as *const ::core::ffi::c_char;
2121
}
2222
#[no_mangle]
2323
pub unsafe extern "C" fn extension_operator() -> *const ::core::ffi::c_char {
24-
return b"const char *extension_operator()\0" as *const u8 as *const ::core::ffi::c_char;
24+
return b"const char *extension_operator()\0".as_ptr() as *const ::core::ffi::c_char;
2525
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct alpn_spec {
2424
}
2525
#[no_mangle]
2626
pub unsafe extern "C" fn ptr() {
27-
let mut _s: *const ::core::ffi::c_char = b"hello\0" as *const u8 as *const ::core::ffi::c_char;
27+
let mut _s: *const ::core::ffi::c_char = b"hello\0".as_ptr() as *const ::core::ffi::c_char;
2828
}
2929
#[no_mangle]
3030
pub unsafe extern "C" fn array_deduced_length() {
@@ -57,7 +57,7 @@ pub unsafe extern "C" fn int_array_extra_braces() {
5757
}
5858
#[no_mangle]
5959
pub unsafe extern "C" fn ptr_extra_braces() {
60-
let mut _s: *const ::core::ffi::c_char = b"hello\0" as *const u8 as *const ::core::ffi::c_char;
60+
let mut _s: *const ::core::ffi::c_char = b"hello\0".as_ptr() as *const ::core::ffi::c_char;
6161
}
6262
#[no_mangle]
6363
pub unsafe extern "C" fn array_extra_braces() {
@@ -67,7 +67,7 @@ pub unsafe extern "C" fn array_extra_braces() {
6767
#[no_mangle]
6868
pub unsafe extern "C" fn array_of_ptrs() {
6969
let mut _s: [*const ::core::ffi::c_char; 3] = [
70-
b"hello\0" as *const u8 as *const ::core::ffi::c_char,
70+
b"hello\0".as_ptr() as *const ::core::ffi::c_char,
7171
::core::ptr::null::<::core::ffi::c_char>(),
7272
::core::ptr::null::<::core::ffi::c_char>(),
7373
];

0 commit comments

Comments
 (0)