Skip to content

Commit 17ebc7a

Browse files
committed
simplify array being a string literal
1 parent 2498c99 commit 17ebc7a

File tree

1 file changed

+30
-40
lines changed

1 file changed

+30
-40
lines changed

c2rust-transpile/src/translator/literals.rs

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,6 @@ impl<'c> Translation<'c> {
173173
CTypeKind::ConstantArray(ty, n) => {
174174
// Convert all of the provided initializer values
175175

176-
// Need to check to see if the next item is a string literal,
177-
// if it is need to treat it as a declaration, rather than
178-
// an init list. https://github.com/GaloisInc/C2Rust/issues/40
179-
let mut is_string = false;
180-
181-
if ids.len() == 1 {
182-
let v = ids.first().unwrap();
183-
if let CExprKind::Literal(_, CLiteral::String { .. }) =
184-
self.ast_context.index(*v).kind
185-
{
186-
is_string = true;
187-
}
188-
}
189-
190176
let to_array_element = |id: &CExprId| -> TranslationResult<_> {
191177
self.convert_expr(ctx.used(), *id)?.result_map(|x| {
192178
// Array literals require all of their elements to be
@@ -208,38 +194,42 @@ impl<'c> Translation<'c> {
208194
})
209195
};
210196

211-
if is_string {
212-
let v = ids.first().unwrap();
213-
self.convert_expr(ctx.used(), *v)
214-
} else {
215-
match ids {
216-
[] => {
217-
// this was likely a C array of the form `int x[16] = {}`,
218-
// we'll emit that as [0; 16].
219-
let len = mk().lit_expr(mk().int_unsuffixed_lit(n as u128));
220-
Ok(self
221-
.implicit_default_expr(ty, ctx.is_static)?
222-
.map(|default_value| mk().repeat_expr(default_value, len)))
223-
}
224-
[single] => {
197+
match ids {
198+
[] => {
199+
// this was likely a C array of the form `int x[16] = {}`,
200+
// we'll emit that as [0; 16].
201+
let len = mk().lit_expr(mk().int_unsuffixed_lit(n as u128));
202+
Ok(self
203+
.implicit_default_expr(ty, ctx.is_static)?
204+
.map(|default_value| mk().repeat_expr(default_value, len)))
205+
}
206+
[single] => {
207+
// Need to check to see if the next item is a string literal,
208+
// if it is need to treat it as a declaration, rather than
209+
// an init list. https://github.com/GaloisInc/C2Rust/issues/40
210+
if let CExprKind::Literal(_, CLiteral::String { .. }) =
211+
self.ast_context.index(*single).kind
212+
{
213+
self.convert_expr(ctx.used(), *single)
214+
} else {
225215
// this was likely a C array of the form `int x[16] = { default }`,
226216
// we'll emit that as [default; 16].
227217
let len = mk().lit_expr(mk().int_unsuffixed_lit(n as u128));
228218
Ok(to_array_element(single)?
229219
.map(|default_value| mk().repeat_expr(default_value, len)))
230220
}
231-
[..] => {
232-
Ok(ids
233-
.iter()
234-
.map(to_array_element)
235-
.chain(
236-
// Pad out the array literal with default values to the desired size
237-
iter::repeat(self.implicit_default_expr(ty, ctx.is_static))
238-
.take(n - ids.len()),
239-
)
240-
.collect::<TranslationResult<WithStmts<_>>>()?
241-
.map(|vals| mk().array_expr(vals)))
242-
}
221+
}
222+
[..] => {
223+
Ok(ids
224+
.iter()
225+
.map(to_array_element)
226+
.chain(
227+
// Pad out the array literal with default values to the desired size
228+
iter::repeat(self.implicit_default_expr(ty, ctx.is_static))
229+
.take(n - ids.len()),
230+
)
231+
.collect::<TranslationResult<WithStmts<_>>>()?
232+
.map(|vals| mk().array_expr(vals)))
243233
}
244234
}
245235
}

0 commit comments

Comments
 (0)