@@ -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 }
0 commit comments