@@ -169,11 +169,13 @@ impl<'c> Translation<'c> {
169169 opt_union_field_id : Option < CFieldId > ,
170170 ) -> TranslationResult < WithStmts < Box < Expr > > > {
171171 match self . ast_context . resolve_type ( ty. ctype ) . kind {
172- CTypeKind :: ConstantArray ( ty , n) => {
172+ CTypeKind :: ConstantArray ( element_ty , n) => {
173173 // Convert all of the provided initializer values
174174
175175 let to_array_element = |id : CExprId | -> TranslationResult < _ > {
176- self . convert_expr ( ctx. used ( ) , id, None ) ?. result_map ( |x| {
176+ let val =
177+ self . convert_expr ( ctx. used ( ) , id, Some ( CQualTypeId :: new ( element_ty) ) ) ?;
178+ val. result_map ( |x| {
177179 // Array literals require all of their elements to be
178180 // the correct type; they will not use implicit casts to
179181 // change mut to const. This becomes a problem when an
@@ -208,7 +210,7 @@ impl<'c> Translation<'c> {
208210 // * the element type of the array being `CTypeKind::Char` (w/o this, `array_of_arrays` is included)
209211 // * the expr kind being a string literal (`CExprKind::Literal` of a `CLiteral::String`).
210212 let is_string_literal = |id : CExprId | {
211- let ty_kind = & self . ast_context . resolve_type ( ty ) . kind ;
213+ let ty_kind = & self . ast_context . resolve_type ( element_ty ) . kind ;
212214 let expr_kind = & self . ast_context . index ( id) . kind ;
213215 let is_char_array = matches ! ( * ty_kind, CTypeKind :: Char ) ;
214216 let is_str_literal =
@@ -228,7 +230,7 @@ impl<'c> Translation<'c> {
228230 // This was likely a C array of the form `int x[16] = {}`.
229231 // We'll emit that as [0; 16].
230232 let len = mk ( ) . lit_expr ( mk ( ) . int_unsuffixed_lit ( n as u128 ) ) ;
231- let zeroed = self . implicit_default_expr ( ctx, ty ) ?;
233+ let zeroed = self . implicit_default_expr ( ctx, element_ty ) ?;
232234 Ok ( zeroed. map ( |default_value| mk ( ) . repeat_expr ( default_value, len) ) )
233235 }
234236 & [ single] if is_string_literal ( single) => {
@@ -237,7 +239,7 @@ impl<'c> Translation<'c> {
237239 // * `ptr_extra_braces`
238240 // * `array_of_ptrs`
239241 // * `array_of_arrays`
240- self . convert_expr ( ctx. used ( ) , single, None )
242+ self . convert_expr ( ctx. used ( ) , single, Some ( ty ) )
241243 }
242244 & [ single] if is_zero_literal ( single) && n > 1 => {
243245 // This was likely a C array of the form `int x[16] = { 0 }`.
@@ -253,7 +255,7 @@ impl<'c> Translation<'c> {
253255 . map ( to_array_element)
254256 . chain (
255257 // Pad out the array literal with default values to the desired size
256- iter:: repeat ( self . implicit_default_expr ( ctx, ty ) )
258+ iter:: repeat ( self . implicit_default_expr ( ctx, element_ty ) )
257259 . take ( n - ids. len ( ) ) ,
258260 )
259261 . collect :: < TranslationResult < WithStmts < _ > > > ( ) ?
@@ -272,7 +274,7 @@ impl<'c> Translation<'c> {
272274 }
273275 ref kind if kind. is_scalar ( ) => {
274276 if let Some ( & first) = ids. first ( ) {
275- self . convert_expr ( ctx. used ( ) , first, None )
277+ self . convert_expr ( ctx. used ( ) , first, Some ( ty ) )
276278 } else {
277279 self . implicit_default_expr ( ctx. used ( ) , ty. ctype )
278280 }
0 commit comments