Skip to content

Commit 3ee8ea5

Browse files
committed
FIx default for std::array
1 parent cfb6ea8 commit 3ee8ea5

4 files changed

Lines changed: 6 additions & 10 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -739,12 +739,6 @@ bool Converter::TypeDerivesDefault(clang::QualType qual_type) {
739739
return false;
740740
}
741741

742-
// Types that need a custom array initializer (e.g., std::array, which is
743-
// translated to Vec)
744-
if (!GetArrayDefaultAsString(qual_type).empty()) {
745-
return false;
746-
}
747-
748742
// libc types do not implement Default
749743
if (auto record = qual_type->getAsRecordDecl()) {
750744
if (ctx_.getSourceManager().isInSystemHeader(record->getLocation()) &&
@@ -3637,7 +3631,7 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
36373631
StrCat("vec!");
36383632
VisitInitListExpr(init);
36393633
} else {
3640-
StrCat(GetArrayDefaultAsString(qual_type));
3634+
StrCat(GetDefaultAsString(qual_type));
36413635
}
36423636
SetFreshType(qual_type);
36433637
return false;

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,7 @@ bool ConverterRefCount::VisitInitListExpr(clang::InitListExpr *expr) {
18881888
PushConversionKind push(*this, ConversionKind::Unboxed);
18891889
ConverterRefCount::VisitInitListExpr(init);
18901890
} else {
1891-
StrCat(GetArrayDefaultAsString(qual_type));
1891+
StrCat(GetDefaultAsString(qual_type));
18921892
}
18931893
computed_expr_type_ = ComputedExprType::FreshValue;
18941894
return false;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ pub fn main() {
1111
std::process::exit(main_0());
1212
}
1313
fn main_0() -> i32 {
14-
let vec_: Value<Vec<i32>> = Rc::new(RefCell::new());
14+
let vec_: Value<Vec<i32>> = Rc::new(RefCell::new(Rc::new(RefCell::new(
15+
std::array::from_fn::<_, 3_u64, _>(|_| <i32>::default()).to_vec(),
16+
))));
1517
return 0;
1618
}
1719
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn main() {
1313
}
1414
}
1515
unsafe fn main_0() -> i32 {
16-
let mut vec_: Vec<i32> = std::array::from_fn::<_, 3, _>(|_| Default::default()).to_vec();
16+
let mut vec_: Vec<i32> = std::array::from_fn::<_, 3_u64, _>(|_| <i32>::default()).to_vec();
1717
return 0;
1818
}
1919
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)