Skip to content

Commit 2a6cf14

Browse files
authored
Fix std::array default initializer generating empty Vec instead of sized Vec (#337)
`std::array<int, 3> v = {};` incorrectly generated `vec![]` (0 elements) instead of a 3-element vector with default values. ## Example ```cpp std::array<int, 3> v = {}; ``` Before: ```rust let mut v: Vec<i32> = vec![]; ``` After: ```rust let mut v: Vec<i32> = std::array::from_fn::<_, 3, _>(|_| Default::default()).to_vec(); ``` - Fixes #313 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Nuno Lopes<nuno.lopes@tecnico.ulisboa.pt>
1 parent 896e16e commit 2a6cf14

4 files changed

Lines changed: 8 additions & 6 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3102,11 +3102,11 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
31023102
} else if (qual_type->isRecordType()) {
31033103
const auto *record = qual_type->getAsRecordDecl();
31043104
if (record->getQualifiedNameAsString() == "std::array") {
3105-
StrCat("vec!");
31063105
if (auto init = clang::dyn_cast<clang::InitListExpr>(expr->getInit(0))) {
3106+
StrCat("vec!");
31073107
VisitInitListExpr(init);
31083108
} else {
3109-
StrCat("[]");
3109+
StrCat(GetArrayDefaultAsString(qual_type));
31103110
}
31113111
return false;
31123112
}

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,12 +1515,12 @@ bool ConverterRefCount::VisitInitListExpr(clang::InitListExpr *expr) {
15151515
if (qual_type->isRecordType()) {
15161516
const auto *record = qual_type->getAsRecordDecl();
15171517
if (record->getQualifiedNameAsString() == "std::array") {
1518-
StrCat("vec!");
15191518
if (auto init = clang::dyn_cast<clang::InitListExpr>(expr->getInit(0))) {
1519+
StrCat("vec!");
15201520
PushConversionKind push(*this, ConversionKind::Unboxed);
15211521
ConverterRefCount::VisitInitListExpr(init);
15221522
} else {
1523-
StrCat("[]");
1523+
StrCat(GetArrayDefaultAsString(qual_type));
15241524
}
15251525
computed_expr_type_ = ComputedExprType::FreshValue;
15261526
return false;

‎tests/unit/out/refcount/empty_array_init.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub fn main() {
1010
std::process::exit(main_0());
1111
}
1212
fn main_0() -> i32 {
13-
let vec_: Value<Vec<i32>> = Rc::new(RefCell::new(vec![]));
13+
let vec_: Value<Vec<i32>> = Rc::new(RefCell::new(
14+
std::array::from_fn::<_, 3, _>(|_| Default::default()).to_vec(),
15+
));
1416
return 0;
1517
}

‎tests/unit/out/unsafe/empty_array_init.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ pub fn main() {
1212
}
1313
}
1414
unsafe fn main_0() -> i32 {
15-
let mut vec_: Vec<i32> = vec![];
15+
let mut vec_: Vec<i32> = std::array::from_fn::<_, 3, _>(|_| Default::default()).to_vec();
1616
return 0;
1717
}

0 commit comments

Comments
 (0)