Skip to content

Commit 0d08cae

Browse files
committed
transpile: Pass override_ty to init lists and their elements
1 parent b1ed8e2 commit 0d08cae

4 files changed

Lines changed: 7 additions & 8 deletions

File tree

c2rust-transpile/src/translator/literals.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ impl<'c> Translation<'c> {
172172
// Convert all of the provided initializer values
173173

174174
let to_array_element = |id: CExprId| -> TranslationResult<_> {
175-
self.convert_expr(ctx.used(), id, None)?.result_map(|x| {
175+
let val = self.convert_expr(ctx.used(), id, Some(CQualTypeId::new(ty)))?;
176+
val.result_map(|x| {
176177
// Array literals require all of their elements to be
177178
// the correct type; they will not use implicit casts to
178179
// change mut to const. This becomes a problem when an
@@ -236,7 +237,7 @@ impl<'c> Translation<'c> {
236237
// * `ptr_extra_braces`
237238
// * `array_of_ptrs`
238239
// * `array_of_arrays`
239-
self.convert_expr(ctx.used(), single, None)
240+
self.convert_expr(ctx.used(), single, Some(ty))
240241
}
241242
&[single] if is_zero_literal(single) && n > 1 => {
242243
// This was likely a C array of the form `int x[16] = { 0 }`.
@@ -271,7 +272,7 @@ impl<'c> Translation<'c> {
271272
}
272273
ref kind if kind.is_scalar() => {
273274
if let Some(&first) = ids.first() {
274-
self.convert_expr(ctx.used(), first, None)
275+
self.convert_expr(ctx.used(), first, Some(ty))
275276
} else {
276277
self.implicit_default_expr(ctx.used(), ty.ctype)
277278
}

c2rust-transpile/src/translator/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3932,7 +3932,7 @@ impl<'c> Translation<'c> {
39323932
}
39333933

39343934
InitList(ty, ref ids, opt_union_field_id, _) => {
3935-
self.convert_init_list(ctx, ty, ids, opt_union_field_id)
3935+
self.convert_init_list(ctx, override_ty.unwrap_or(ty), ids, opt_union_field_id)
39363936
}
39373937

39383938
ImplicitValueInit(ty) => self.implicit_default_expr(ctx, ty.ctype),

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ pub unsafe extern "C" fn entry() {
7171
'e' as i32 as ::core::ffi::c_char,
7272
'f' as i32 as ::core::ffi::c_char,
7373
];
74-
let mut char_with_ints: [::core::ffi::c_char; 2] =
75-
[1 as ::core::ffi::c_int as ::core::ffi::c_char, 0];
74+
let mut char_with_ints: [::core::ffi::c_char; 2] = [1 as ::core::ffi::c_char, 0];
7675
let mut char_with_init: [::core::ffi::c_char; 5] =
7776
::core::mem::transmute::<[u8; 5], [::core::ffi::c_char; 5]>(*b"abcd\0");
7877
let mut char_too_long: [::core::ffi::c_char; 3] =

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ pub unsafe extern "C" fn entry() {
7171
'e' as i32 as ::core::ffi::c_char,
7272
'f' as i32 as ::core::ffi::c_char,
7373
];
74-
let mut char_with_ints: [::core::ffi::c_char; 2] =
75-
[1 as ::core::ffi::c_int as ::core::ffi::c_char, 0];
74+
let mut char_with_ints: [::core::ffi::c_char; 2] = [1 as ::core::ffi::c_char, 0];
7675
let mut char_with_init: [::core::ffi::c_char; 5] =
7776
::core::mem::transmute::<[u8; 5], [::core::ffi::c_char; 5]>(*b"abcd\0");
7877
let mut char_too_long: [::core::ffi::c_char; 3] =

0 commit comments

Comments
 (0)