Skip to content

Commit 4150cab

Browse files
committed
transpile: support CastKind::ArrayToPointerDecays in --translate-const-macros conservative
1 parent 3071fd7 commit 4150cab

File tree

4 files changed

+48
-57
lines changed

4 files changed

+48
-57
lines changed

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,6 @@ impl TypedAstContext {
649649
// TODO `f128` is not yet handled, as we should eventually
650650
// switch to the (currently unstable) `f128` primitive type (#1262).
651651
Binary(_, _, lhs, rhs, _, _) => is_const(lhs) && is_const(rhs),
652-
ImplicitCast(_, _, CastKind::ArrayToPointerDecay, _, _) => false, // TODO disabled for now as tests are broken
653652
// `as` casts are always `const`.
654653
ImplicitCast(_, expr, _, _, _) => is_const(expr),
655654
// `as` casts are always `const`.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ pub unsafe extern "C" fn size_of_const() -> std::ffi::c_int {
3131
return SIZE as std::ffi::c_int;
3232
}
3333
pub const SIZE: usize = unsafe { ::core::mem::size_of::<[std::ffi::c_int; 10]>() };
34+
pub const POS: [std::ffi::c_char; 3] =
35+
unsafe { *::core::mem::transmute::<&[u8; 3], &[std::ffi::c_char; 3]>(b"\"]\0") };
3436
#[no_mangle]
3537
pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut std::ffi::c_char) {
3638
memcpy(
3739
out as *mut std::ffi::c_void,
38-
b"\"]\0" as *const u8 as *const std::ffi::c_char as *const std::ffi::c_void,
40+
POS.as_ptr() as *const std::ffi::c_void,
3941
(::core::mem::size_of::<[std::ffi::c_char; 3]>() as size_t)
4042
.wrapping_div(::core::mem::size_of::<std::ffi::c_char>() as size_t)
4143
.wrapping_sub(1 as size_t)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ pub unsafe extern "C" fn size_of_const() -> std::ffi::c_int {
3232
return SIZE as std::ffi::c_int;
3333
}
3434
pub const SIZE: usize = unsafe { ::core::mem::size_of::<[std::ffi::c_int; 10]>() };
35+
pub const POS: [std::ffi::c_char; 3] =
36+
unsafe { *::core::mem::transmute::<&[u8; 3], &[std::ffi::c_char; 3]>(b"\"]\0") };
3537
#[no_mangle]
3638
pub unsafe extern "C" fn memcpy_str_literal(mut out: *mut std::ffi::c_char) {
3739
memcpy(
3840
out as *mut std::ffi::c_void,
39-
b"\"]\0" as *const u8 as *const std::ffi::c_char as *const std::ffi::c_void,
41+
POS.as_ptr() as *const std::ffi::c_void,
4042
(::core::mem::size_of::<[std::ffi::c_char; 3]>() as size_t)
4143
.wrapping_div(::core::mem::size_of::<std::ffi::c_char>() as size_t)
4244
.wrapping_sub(1 as size_t)

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

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub const LITERAL_INT: std::ffi::c_int = unsafe { 0xffff as std::ffi::c_int };
3636
pub const LITERAL_BOOL: std::ffi::c_int = unsafe { true_0 };
3737
pub const LITERAL_FLOAT: std::ffi::c_double = unsafe { 3.14f64 };
3838
pub const LITERAL_CHAR: std::ffi::c_int = unsafe { 'x' as i32 };
39+
pub const LITERAL_STR: [std::ffi::c_char; 6] =
40+
unsafe { *::core::mem::transmute::<&[u8; 6], &[std::ffi::c_char; 6]>(b"hello\0") };
3941
pub const LITERAL_STRUCT: S = unsafe {
4042
{
4143
let mut init = S {
@@ -48,6 +50,8 @@ pub const NESTED_INT: std::ffi::c_int = unsafe { LITERAL_INT };
4850
pub const NESTED_BOOL: std::ffi::c_int = unsafe { 1 as std::ffi::c_int };
4951
pub const NESTED_FLOAT: std::ffi::c_double = unsafe { LITERAL_FLOAT };
5052
pub const NESTED_CHAR: std::ffi::c_int = unsafe { 'x' as i32 };
53+
pub const NESTED_STR: [std::ffi::c_char; 6] =
54+
unsafe { *::core::mem::transmute::<&[u8; 6], &[std::ffi::c_char; 6]>(b"hello\0") };
5155
pub const NESTED_STRUCT: S = unsafe {
5256
{
5357
let mut init = S {
@@ -57,6 +61,12 @@ pub const NESTED_STRUCT: S = unsafe {
5761
}
5862
};
5963
pub const PARENS: std::ffi::c_int = unsafe { NESTED_INT * (LITERAL_CHAR + true_0) };
64+
pub const PTR_ARITHMETIC: *const std::ffi::c_char = unsafe {
65+
LITERAL_STR
66+
.as_ptr()
67+
.offset(5 as std::ffi::c_int as isize)
68+
.offset(-(3 as std::ffi::c_int as isize))
69+
};
6070
pub const WIDENING_CAST: std::ffi::c_int = unsafe { LITERAL_INT };
6171
pub const CONVERSION_CAST: std::ffi::c_int = unsafe { LITERAL_INT };
6272
pub const STMT_EXPR: std::ffi::c_float = unsafe {
@@ -82,10 +92,8 @@ pub unsafe extern "C" fn local_muts() {
8292
let mut literal_bool: bool = LITERAL_BOOL != 0;
8393
let mut literal_float: std::ffi::c_float = LITERAL_FLOAT as std::ffi::c_float;
8494
let mut literal_char: std::ffi::c_char = LITERAL_CHAR as std::ffi::c_char;
85-
let mut literal_str_ptr: *const std::ffi::c_char =
86-
b"hello\0" as *const u8 as *const std::ffi::c_char;
87-
let mut literal_str: [std::ffi::c_char; 6] =
88-
*::core::mem::transmute::<&[u8; 6], &mut [std::ffi::c_char; 6]>(b"hello\0");
95+
let mut literal_str_ptr: *const std::ffi::c_char = LITERAL_STR.as_ptr();
96+
let mut literal_str: [std::ffi::c_char; 6] = LITERAL_STR;
8997
let mut literal_array: [std::ffi::c_int; 3] = [
9098
1 as std::ffi::c_int,
9199
2 as std::ffi::c_int,
@@ -96,10 +104,8 @@ pub unsafe extern "C" fn local_muts() {
96104
let mut nested_bool: bool = NESTED_BOOL != 0;
97105
let mut nested_float: std::ffi::c_float = NESTED_FLOAT as std::ffi::c_float;
98106
let mut nested_char: std::ffi::c_char = NESTED_CHAR as std::ffi::c_char;
99-
let mut nested_str_ptr: *const std::ffi::c_char =
100-
b"hello\0" as *const u8 as *const std::ffi::c_char;
101-
let mut nested_str: [std::ffi::c_char; 6] =
102-
*::core::mem::transmute::<&[u8; 6], &mut [std::ffi::c_char; 6]>(b"hello\0");
107+
let mut nested_str_ptr: *const std::ffi::c_char = NESTED_STR.as_ptr();
108+
let mut nested_str: [std::ffi::c_char; 6] = NESTED_STR;
103109
let mut nested_array: [std::ffi::c_int; 3] = [
104110
1 as std::ffi::c_int,
105111
2 as std::ffi::c_int,
@@ -111,10 +117,7 @@ pub unsafe extern "C" fn local_muts() {
111117
(LITERAL_INT as std::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as std::ffi::c_double
112118
- true_0 as std::ffi::c_double) as std::ffi::c_float;
113119
let mut parens: std::ffi::c_int = PARENS;
114-
let mut ptr_arithmetic: *const std::ffi::c_char = (b"hello\0" as *const u8
115-
as *const std::ffi::c_char)
116-
.offset(5 as std::ffi::c_int as isize)
117-
.offset(-(3 as std::ffi::c_int as isize));
120+
let mut ptr_arithmetic: *const std::ffi::c_char = PTR_ARITHMETIC;
118121
let mut widening_cast: std::ffi::c_ulonglong = WIDENING_CAST as std::ffi::c_ulonglong;
119122
let mut narrowing_cast: std::ffi::c_char = LITERAL_INT as std::ffi::c_char;
120123
let mut conversion_cast: std::ffi::c_double = CONVERSION_CAST as std::ffi::c_double;
@@ -126,9 +129,10 @@ pub unsafe extern "C" fn local_muts() {
126129
let mut str_concatenation: [std::ffi::c_char; 18] =
127130
*::core::mem::transmute::<&[u8; 18], &mut [std::ffi::c_char; 18]>(b"hello hello world\0");
128131
let mut builtin: std::ffi::c_int = (LITERAL_INT as std::ffi::c_uint).leading_zeros() as i32;
129-
let mut ref_indexing: *const std::ffi::c_char =
130-
&*(b"hello\0" as *const u8 as *const std::ffi::c_char)
131-
.offset(LITERAL_FLOAT as std::ffi::c_int as isize) as *const std::ffi::c_char;
132+
let mut ref_indexing: *const std::ffi::c_char = &*NESTED_STR
133+
.as_ptr()
134+
.offset(LITERAL_FLOAT as std::ffi::c_int as isize)
135+
as *const std::ffi::c_char;
132136
let mut ref_struct: *const S = &mut LITERAL_STRUCT as *mut S;
133137
let mut ternary: std::ffi::c_int = if LITERAL_BOOL != 0 {
134138
1 as std::ffi::c_int
@@ -144,10 +148,8 @@ pub unsafe extern "C" fn local_consts() {
144148
let literal_bool: bool = LITERAL_BOOL != 0;
145149
let literal_float: std::ffi::c_float = LITERAL_FLOAT as std::ffi::c_float;
146150
let literal_char: std::ffi::c_char = LITERAL_CHAR as std::ffi::c_char;
147-
let literal_str_ptr: *const std::ffi::c_char =
148-
b"hello\0" as *const u8 as *const std::ffi::c_char;
149-
let literal_str: [std::ffi::c_char; 6] =
150-
*::core::mem::transmute::<&[u8; 6], &[std::ffi::c_char; 6]>(b"hello\0");
151+
let literal_str_ptr: *const std::ffi::c_char = LITERAL_STR.as_ptr();
152+
let literal_str: [std::ffi::c_char; 6] = LITERAL_STR;
151153
let literal_array: [std::ffi::c_int; 3] = [
152154
1 as std::ffi::c_int,
153155
2 as std::ffi::c_int,
@@ -158,10 +160,8 @@ pub unsafe extern "C" fn local_consts() {
158160
let nested_bool: bool = NESTED_BOOL != 0;
159161
let nested_float: std::ffi::c_float = NESTED_FLOAT as std::ffi::c_float;
160162
let nested_char: std::ffi::c_char = NESTED_CHAR as std::ffi::c_char;
161-
let nested_str_ptr: *const std::ffi::c_char =
162-
b"hello\0" as *const u8 as *const std::ffi::c_char;
163-
let nested_str: [std::ffi::c_char; 6] =
164-
*::core::mem::transmute::<&[u8; 6], &[std::ffi::c_char; 6]>(b"hello\0");
163+
let nested_str_ptr: *const std::ffi::c_char = NESTED_STR.as_ptr();
164+
let nested_str: [std::ffi::c_char; 6] = NESTED_STR;
165165
let nested_array: [std::ffi::c_int; 3] = [
166166
1 as std::ffi::c_int,
167167
2 as std::ffi::c_int,
@@ -173,10 +173,7 @@ pub unsafe extern "C" fn local_consts() {
173173
(LITERAL_INT as std::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as std::ffi::c_double
174174
- true_0 as std::ffi::c_double) as std::ffi::c_float;
175175
let parens: std::ffi::c_int = PARENS;
176-
let ptr_arithmetic: *const std::ffi::c_char = (b"hello\0" as *const u8
177-
as *const std::ffi::c_char)
178-
.offset(5 as std::ffi::c_int as isize)
179-
.offset(-(3 as std::ffi::c_int as isize));
176+
let ptr_arithmetic: *const std::ffi::c_char = PTR_ARITHMETIC;
180177
let widening_cast: std::ffi::c_ulonglong = WIDENING_CAST as std::ffi::c_ulonglong;
181178
let narrowing_cast: std::ffi::c_char = LITERAL_INT as std::ffi::c_char;
182179
let conversion_cast: std::ffi::c_double = CONVERSION_CAST as std::ffi::c_double;
@@ -188,9 +185,10 @@ pub unsafe extern "C" fn local_consts() {
188185
let str_concatenation: [std::ffi::c_char; 18] =
189186
*::core::mem::transmute::<&[u8; 18], &[std::ffi::c_char; 18]>(b"hello hello world\0");
190187
let builtin: std::ffi::c_int = (LITERAL_INT as std::ffi::c_uint).leading_zeros() as i32;
191-
let ref_indexing: *const std::ffi::c_char =
192-
&*(b"hello\0" as *const u8 as *const std::ffi::c_char)
193-
.offset(LITERAL_FLOAT as std::ffi::c_int as isize) as *const std::ffi::c_char;
188+
let ref_indexing: *const std::ffi::c_char = &*NESTED_STR
189+
.as_ptr()
190+
.offset(LITERAL_FLOAT as std::ffi::c_int as isize)
191+
as *const std::ffi::c_char;
194192
let ref_struct: *const S = &mut LITERAL_STRUCT as *mut S;
195193
let ternary: std::ffi::c_int = if LITERAL_BOOL != 0 {
196194
1 as std::ffi::c_int
@@ -205,10 +203,8 @@ static mut global_static_const_literal_bool: bool = LITERAL_BOOL != 0;
205203
static mut global_static_const_literal_float: std::ffi::c_float =
206204
LITERAL_FLOAT as std::ffi::c_float;
207205
static mut global_static_const_literal_char: std::ffi::c_char = LITERAL_CHAR as std::ffi::c_char;
208-
static mut global_static_const_literal_str_ptr: *const std::ffi::c_char =
209-
b"hello\0" as *const u8 as *const std::ffi::c_char;
210-
static mut global_static_const_literal_str: [std::ffi::c_char; 6] =
211-
unsafe { *::core::mem::transmute::<&[u8; 6], &[std::ffi::c_char; 6]>(b"hello\0") };
206+
static mut global_static_const_literal_str_ptr: *const std::ffi::c_char = LITERAL_STR.as_ptr();
207+
static mut global_static_const_literal_str: [std::ffi::c_char; 6] = LITERAL_STR;
212208
static mut global_static_const_literal_array: [std::ffi::c_int; 3] = [
213209
1 as std::ffi::c_int,
214210
2 as std::ffi::c_int,
@@ -219,10 +215,8 @@ static mut global_static_const_nested_int: std::ffi::c_int = NESTED_INT;
219215
static mut global_static_const_nested_bool: bool = NESTED_BOOL != 0;
220216
static mut global_static_const_nested_float: std::ffi::c_float = NESTED_FLOAT as std::ffi::c_float;
221217
static mut global_static_const_nested_char: std::ffi::c_char = NESTED_CHAR as std::ffi::c_char;
222-
static mut global_static_const_nested_str_ptr: *const std::ffi::c_char =
223-
b"hello\0" as *const u8 as *const std::ffi::c_char;
224-
static mut global_static_const_nested_str: [std::ffi::c_char; 6] =
225-
unsafe { *::core::mem::transmute::<&[u8; 6], &[std::ffi::c_char; 6]>(b"hello\0") };
218+
static mut global_static_const_nested_str_ptr: *const std::ffi::c_char = NESTED_STR.as_ptr();
219+
static mut global_static_const_nested_str: [std::ffi::c_char; 6] = NESTED_STR;
226220
static mut global_static_const_nested_array: [std::ffi::c_int; 3] = [
227221
1 as std::ffi::c_int,
228222
2 as std::ffi::c_int,
@@ -265,11 +259,9 @@ pub static mut global_const_literal_float: std::ffi::c_float = LITERAL_FLOAT as
265259
#[no_mangle]
266260
pub static mut global_const_literal_char: std::ffi::c_char = LITERAL_CHAR as std::ffi::c_char;
267261
#[no_mangle]
268-
pub static mut global_const_literal_str_ptr: *const std::ffi::c_char =
269-
b"hello\0" as *const u8 as *const std::ffi::c_char;
262+
pub static mut global_const_literal_str_ptr: *const std::ffi::c_char = LITERAL_STR.as_ptr();
270263
#[no_mangle]
271-
pub static mut global_const_literal_str: [std::ffi::c_char; 6] =
272-
unsafe { *::core::mem::transmute::<&[u8; 6], &[std::ffi::c_char; 6]>(b"hello\0") };
264+
pub static mut global_const_literal_str: [std::ffi::c_char; 6] = LITERAL_STR;
273265
#[no_mangle]
274266
pub static mut global_const_literal_array: [std::ffi::c_int; 3] = [
275267
1 as std::ffi::c_int,
@@ -287,11 +279,9 @@ pub static mut global_const_nested_float: std::ffi::c_float = NESTED_FLOAT as st
287279
#[no_mangle]
288280
pub static mut global_const_nested_char: std::ffi::c_char = NESTED_CHAR as std::ffi::c_char;
289281
#[no_mangle]
290-
pub static mut global_const_nested_str_ptr: *const std::ffi::c_char =
291-
b"hello\0" as *const u8 as *const std::ffi::c_char;
282+
pub static mut global_const_nested_str_ptr: *const std::ffi::c_char = NESTED_STR.as_ptr();
292283
#[no_mangle]
293-
pub static mut global_const_nested_str: [std::ffi::c_char; 6] =
294-
unsafe { *::core::mem::transmute::<&[u8; 6], &[std::ffi::c_char; 6]>(b"hello\0") };
284+
pub static mut global_const_nested_str: [std::ffi::c_char; 6] = NESTED_STR;
295285
#[no_mangle]
296286
pub static mut global_const_nested_array: [std::ffi::c_int; 3] = [
297287
1 as std::ffi::c_int,
@@ -429,13 +419,12 @@ pub unsafe extern "C" fn use_portable_type(mut len: uintptr_t) -> bool {
429419
return len <= (UINTPTR_MAX as uintptr_t).wrapping_div(2 as uintptr_t);
430420
}
431421
unsafe extern "C" fn run_static_initializers() {
432-
global_static_const_ptr_arithmetic = (b"hello\0" as *const u8 as *const std::ffi::c_char)
433-
.offset(5 as std::ffi::c_int as isize)
434-
.offset(-(3 as std::ffi::c_int as isize));
422+
global_static_const_ptr_arithmetic = PTR_ARITHMETIC;
435423
global_static_const_indexing =
436424
(*::core::mem::transmute::<&[u8; 6], &[std::ffi::c_char; 6]>(b"hello\0"))
437425
[LITERAL_FLOAT as std::ffi::c_int as usize];
438-
global_static_const_ref_indexing = &*(b"hello\0" as *const u8 as *const std::ffi::c_char)
426+
global_static_const_ref_indexing = &*NESTED_STR
427+
.as_ptr()
439428
.offset(LITERAL_FLOAT as std::ffi::c_int as isize)
440429
as *const std::ffi::c_char;
441430
global_static_const_ternary = if LITERAL_BOOL != 0 {
@@ -444,13 +433,12 @@ unsafe extern "C" fn run_static_initializers() {
444433
2 as std::ffi::c_int
445434
};
446435
global_static_const_member = LITERAL_STRUCT.i;
447-
global_const_ptr_arithmetic = (b"hello\0" as *const u8 as *const std::ffi::c_char)
448-
.offset(5 as std::ffi::c_int as isize)
449-
.offset(-(3 as std::ffi::c_int as isize));
436+
global_const_ptr_arithmetic = PTR_ARITHMETIC;
450437
global_const_indexing =
451438
(*::core::mem::transmute::<&[u8; 6], &[std::ffi::c_char; 6]>(b"hello\0"))
452439
[LITERAL_FLOAT as std::ffi::c_int as usize];
453-
global_const_ref_indexing = &*(b"hello\0" as *const u8 as *const std::ffi::c_char)
440+
global_const_ref_indexing = &*NESTED_STR
441+
.as_ptr()
454442
.offset(LITERAL_FLOAT as std::ffi::c_int as isize)
455443
as *const std::ffi::c_char;
456444
global_const_ternary = if LITERAL_BOOL != 0 {

0 commit comments

Comments
 (0)