Skip to content

Commit c7275f1

Browse files
committed
Fix char array initialiation through init list
1 parent cee15a3 commit c7275f1

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,6 +2758,14 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
27582758
StrCat(token::kComma);
27592759
}
27602760
} else {
2761+
if (expr->getNumInits() == 1 && qual_type->isArrayType() &&
2762+
qual_type->getArrayElementTypeNoTypeQual()->isCharType() &&
2763+
clang::isa<clang::StringLiteral>(
2764+
expr->getInit(0)->IgnoreParenImpCasts())) {
2765+
auto *init = expr->getInit(0);
2766+
ConvertVarInit(init->getType(), init);
2767+
return false;
2768+
}
27612769
PushBracket bracket(*this);
27622770
for (auto *init : expr->inits()) {
27632771
ConvertVarInit(init->getType(), init);

tests/unit/string_literals.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@ int main() {
2222
foo_const("");
2323
foo_const(immutable_empty);
2424
foo_const(immutable_empty_arr);
25+
26+
const char inited_through_init_list[] = { "papanasi cu smantana" };
27+
foo_const(inited_through_init_list);
28+
2529
return 0;
2630
}

tests/unit/string_literals_c.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@ int main() {
3131
foo_const(immutable_empty);
3232
foo_const(mutable_empty_arr);
3333
foo_const(immutable_empty_arr);
34+
35+
const char inited_through_init_list[] = { "papanasi cu smantana" };
36+
foo_const(inited_through_init_list);
37+
3438
return 0;
3539
}

0 commit comments

Comments
 (0)