Skip to content

Commit a56b3ee

Browse files
committed
emit zeroed arrays as [0; N]
an array like `int x[16] = {}` was emitted as `[0, 0, 0, 0, ...]`, but is not emitted as `[0; 16]`.
1 parent 68de84c commit a56b3ee

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

c2rust-transpile/src/translator/literals.rs

+8
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ impl<'c> Translation<'c> {
190190
if is_string {
191191
let v = ids.first().unwrap();
192192
self.convert_expr(ctx.used(), *v)
193+
} else if ids.len() == 0 && ty.is_integral_type() || ty.is_floating_type() {
194+
// this was likely a C array of the form `int x[16] = {}`,
195+
// we'll emit that as [0; 16].
196+
let len = mk().lit_expr(mk().int_unsuffixed_lit(n as u128));
197+
self.implicit_default_expr(ty, ctx.is_static)?
198+
.and_then(|default_value| {
199+
Ok(WithStmts::new_val(mk().repeat_expr(default_value, len)))
200+
})
193201
} else {
194202
Ok(ids
195203
.iter()

0 commit comments

Comments
 (0)