Skip to content

Commit a45c2a6

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

4 files changed

Lines changed: 12 additions & 12 deletions

File tree

c2rust-transpile/src/translator/literals.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,13 @@ impl<'c> Translation<'c> {
168168
opt_union_field_id: Option<CFieldId>,
169169
) -> TranslationResult<WithStmts<Box<Expr>>> {
170170
match self.ast_context.resolve_type(ty.ctype).kind {
171-
CTypeKind::ConstantArray(ty, n) => {
171+
CTypeKind::ConstantArray(element_ty, n) => {
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 =
176+
self.convert_expr(ctx.used(), id, Some(CQualTypeId::new(element_ty)))?;
177+
val.result_map(|x| {
176178
// Array literals require all of their elements to be
177179
// the correct type; they will not use implicit casts to
178180
// change mut to const. This becomes a problem when an
@@ -207,7 +209,7 @@ impl<'c> Translation<'c> {
207209
// * the element type of the array being `CTypeKind::Char` (w/o this, `array_of_arrays` is included)
208210
// * the expr kind being a string literal (`CExprKind::Literal` of a `CLiteral::String`).
209211
let is_string_literal = |id: CExprId| {
210-
let ty_kind = &self.ast_context.resolve_type(ty).kind;
212+
let ty_kind = &self.ast_context.resolve_type(element_ty).kind;
211213
let expr_kind = &self.ast_context.index(id).kind;
212214
let is_char_array = matches!(*ty_kind, CTypeKind::Char);
213215
let is_str_literal =
@@ -227,7 +229,7 @@ impl<'c> Translation<'c> {
227229
// This was likely a C array of the form `int x[16] = {}`.
228230
// We'll emit that as [0; 16].
229231
let len = mk().lit_expr(mk().int_unsuffixed_lit(n as u128));
230-
let zeroed = self.implicit_default_expr(ctx, ty)?;
232+
let zeroed = self.implicit_default_expr(ctx, element_ty)?;
231233
Ok(zeroed.map(|default_value| mk().repeat_expr(default_value, len)))
232234
}
233235
&[single] if is_string_literal(single) => {
@@ -236,7 +238,7 @@ impl<'c> Translation<'c> {
236238
// * `ptr_extra_braces`
237239
// * `array_of_ptrs`
238240
// * `array_of_arrays`
239-
self.convert_expr(ctx.used(), single, None)
241+
self.convert_expr(ctx.used(), single, Some(ty))
240242
}
241243
&[single] if is_zero_literal(single) && n > 1 => {
242244
// This was likely a C array of the form `int x[16] = { 0 }`.
@@ -252,7 +254,7 @@ impl<'c> Translation<'c> {
252254
.map(to_array_element)
253255
.chain(
254256
// Pad out the array literal with default values to the desired size
255-
iter::repeat(self.implicit_default_expr(ctx, ty))
257+
iter::repeat(self.implicit_default_expr(ctx, element_ty))
256258
.take(n - ids.len()),
257259
)
258260
.collect::<TranslationResult<WithStmts<_>>>()?
@@ -271,7 +273,7 @@ impl<'c> Translation<'c> {
271273
}
272274
ref kind if kind.is_scalar() => {
273275
if let Some(&first) = ids.first() {
274-
self.convert_expr(ctx.used(), first, None)
276+
self.convert_expr(ctx.used(), first, Some(ty))
275277
} else {
276278
self.implicit_default_expr(ctx.used(), ty.ctype)
277279
}

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)