Skip to content

Commit 9e5a1c0

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 9e5a1c0

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
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 {
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()

tests/arrays/src/arrays.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ static char *foo = "mystring";
66
void entry(const unsigned buffer_size, int buffer[const])
77
{
88
int arr[1][1] = { 1 };
9-
109
arr[0][0] += 9;
1110

11+
int arr2[16] = {};
12+
arr2[15] += 9;
13+
14+
struct {char* x; int y;} arr3[1] = {};
15+
arr3[0].y += 9;
16+
1217
int i = 0;
1318

1419
char abc[] = "abc";

0 commit comments

Comments
 (0)