@@ -223,11 +223,13 @@ impl<'c> Translation<'c> {
223223 opt_union_field_id : Option < CFieldId > ,
224224 ) -> TranslationResult < WithStmts < Box < Expr > > > {
225225 match self . ast_context . resolve_type ( ty. ctype ) . kind {
226- CTypeKind :: ConstantArray ( ty , n) => {
226+ CTypeKind :: ConstantArray ( element_ty , n) => {
227227 // Convert all of the provided initializer values
228228
229229 let to_array_element = |id : CExprId | -> TranslationResult < _ > {
230- self . convert_expr ( ctx. used ( ) , id, None ) ?. try_map ( |x| {
230+ let val =
231+ self . convert_expr ( ctx. used ( ) , id, Some ( CQualTypeId :: new ( element_ty) ) ) ?;
232+ val. try_map ( |x| {
231233 // Array literals require all of their elements to be
232234 // the correct type; they will not use implicit casts to
233235 // change mut to const. This becomes a problem when an
@@ -262,7 +264,7 @@ impl<'c> Translation<'c> {
262264 // * the element type of the array being `CTypeKind::Char` (w/o this, `array_of_arrays` is included)
263265 // * the expr kind being a string literal (`CExprKind::Literal` of a `CLiteral::String`).
264266 let is_string_literal = |id : CExprId | {
265- let ty_kind = & self . ast_context . resolve_type ( ty ) . kind ;
267+ let ty_kind = & self . ast_context . resolve_type ( element_ty ) . kind ;
266268 let expr_kind = & self . ast_context . index ( id) . kind ;
267269 let is_char_array = matches ! ( * ty_kind, CTypeKind :: Char ) ;
268270 let is_str_literal =
@@ -282,7 +284,7 @@ impl<'c> Translation<'c> {
282284 // This was likely a C array of the form `int x[16] = {}`.
283285 // We'll emit that as [0; 16].
284286 let len = mk ( ) . lit_expr ( mk ( ) . int_unsuffixed_lit ( n as u128 ) ) ;
285- let zeroed = self . implicit_default_expr ( ctx, ty ) ?;
287+ let zeroed = self . implicit_default_expr ( ctx, element_ty ) ?;
286288 Ok ( zeroed. map ( |default_value| mk ( ) . repeat_expr ( default_value, len) ) )
287289 }
288290 & [ single] if is_string_literal ( single) => {
@@ -291,7 +293,7 @@ impl<'c> Translation<'c> {
291293 // * `ptr_extra_braces`
292294 // * `array_of_ptrs`
293295 // * `array_of_arrays`
294- self . convert_expr ( ctx. used ( ) , single, None )
296+ self . convert_expr ( ctx. used ( ) , single, Some ( ty ) )
295297 }
296298 & [ single] if is_zero_literal ( single) && n > 1 => {
297299 // This was likely a C array of the form `int x[16] = { 0 }`.
@@ -307,7 +309,7 @@ impl<'c> Translation<'c> {
307309 . map ( to_array_element)
308310 . chain (
309311 // Pad out the array literal with default values to the desired size
310- iter:: repeat ( self . implicit_default_expr ( ctx, ty ) )
312+ iter:: repeat ( self . implicit_default_expr ( ctx, element_ty ) )
311313 . take ( n - ids. len ( ) ) ,
312314 )
313315 . collect :: < TranslationResult < WithStmts < _ > > > ( ) ?
@@ -326,7 +328,7 @@ impl<'c> Translation<'c> {
326328 }
327329 ref kind if kind. is_scalar ( ) => {
328330 if let Some ( & first) = ids. first ( ) {
329- self . convert_expr ( ctx. used ( ) , first, None )
331+ self . convert_expr ( ctx. used ( ) , first, Some ( ty ) )
330332 } else {
331333 self . implicit_default_expr ( ctx. used ( ) , ty. ctype )
332334 }
0 commit comments