@@ -219,11 +219,13 @@ impl<'c> Translation<'c> {
219219 opt_union_field_id : Option < CFieldId > ,
220220 ) -> TranslationResult < WithStmts < Box < Expr > > > {
221221 match self . ast_context . resolve_type ( ty. ctype ) . kind {
222- CTypeKind :: ConstantArray ( ty , n) => {
222+ CTypeKind :: ConstantArray ( element_ty , n) => {
223223 // Convert all of the provided initializer values
224224
225225 let to_array_element = |id : CExprId | -> TranslationResult < _ > {
226- self . convert_expr ( ctx. used ( ) , id, None ) ?. try_map ( |x| {
226+ let val =
227+ self . convert_expr ( ctx. used ( ) , id, Some ( CQualTypeId :: new ( element_ty) ) ) ?;
228+ val. try_map ( |x| {
227229 // Array literals require all of their elements to be
228230 // the correct type; they will not use implicit casts to
229231 // change mut to const. This becomes a problem when an
@@ -258,7 +260,7 @@ impl<'c> Translation<'c> {
258260 // * the element type of the array being `CTypeKind::Char` (w/o this, `array_of_arrays` is included)
259261 // * the expr kind being a string literal (`CExprKind::Literal` of a `CLiteral::String`).
260262 let is_string_literal = |id : CExprId | {
261- let ty_kind = & self . ast_context . resolve_type ( ty ) . kind ;
263+ let ty_kind = & self . ast_context . resolve_type ( element_ty ) . kind ;
262264 let expr_kind = & self . ast_context . index ( id) . kind ;
263265 let is_char_array = matches ! ( * ty_kind, CTypeKind :: Char ) ;
264266 let is_str_literal =
@@ -278,7 +280,7 @@ impl<'c> Translation<'c> {
278280 // This was likely a C array of the form `int x[16] = {}`.
279281 // We'll emit that as [0; 16].
280282 let len = mk ( ) . lit_expr ( mk ( ) . int_unsuffixed_lit ( n as u128 ) ) ;
281- let zeroed = self . implicit_default_expr ( ctx, ty ) ?;
283+ let zeroed = self . implicit_default_expr ( ctx, element_ty ) ?;
282284 Ok ( zeroed. map ( |default_value| mk ( ) . repeat_expr ( default_value, len) ) )
283285 }
284286 & [ single] if is_string_literal ( single) => {
@@ -287,7 +289,7 @@ impl<'c> Translation<'c> {
287289 // * `ptr_extra_braces`
288290 // * `array_of_ptrs`
289291 // * `array_of_arrays`
290- self . convert_expr ( ctx. used ( ) , single, None )
292+ self . convert_expr ( ctx. used ( ) , single, Some ( ty ) )
291293 }
292294 & [ single] if is_zero_literal ( single) && n > 1 => {
293295 // This was likely a C array of the form `int x[16] = { 0 }`.
@@ -303,7 +305,7 @@ impl<'c> Translation<'c> {
303305 . map ( to_array_element)
304306 . chain (
305307 // Pad out the array literal with default values to the desired size
306- iter:: repeat ( self . implicit_default_expr ( ctx, ty ) )
308+ iter:: repeat ( self . implicit_default_expr ( ctx, element_ty ) )
307309 . take ( n - ids. len ( ) ) ,
308310 )
309311 . collect :: < TranslationResult < WithStmts < _ > > > ( ) ?
@@ -322,7 +324,7 @@ impl<'c> Translation<'c> {
322324 }
323325 ref kind if kind. is_scalar ( ) => {
324326 if let Some ( & first) = ids. first ( ) {
325- self . convert_expr ( ctx. used ( ) , first, None )
327+ self . convert_expr ( ctx. used ( ) , first, Some ( ty ) )
326328 } else {
327329 self . implicit_default_expr ( ctx. used ( ) , ty. ctype )
328330 }
0 commit comments