Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3573,6 +3573,14 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {

StrCat(GetUnsafeTypeAsString(qual_type));
PushBrace brace(*this);
if (record->isUnion()) {
auto *field = expr->getInitializedFieldInUnion();
assert(field && expr->getNumInits() == 1);
StrCat(GetNamedDeclAsString(field), token::kColon);
ConvertFieldInit(field, expr->getInit(0));
SetFreshType(qual_type);
return false;
}
int i = 0;
for (const auto *field : record->fields()) {
StrCat(GetNamedDeclAsString(field), token::kColon);
Expand Down
15 changes: 15 additions & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,21 @@ bool ConverterRefCount::VisitInitListExpr(clang::InitListExpr *expr) {
return false;
}

if (record->isUnion()) {
auto *field = expr->getInitializedFieldInUnion();
assert(field && expr->getNumInits() == 1);
PushConversionKind push(*this, ConversionKind::Unboxed);
auto name = GetUnsafeTypeAsString(qual_type);
StrCat(std::format("{{ let __u: Value<{}> = Rc::new(RefCell::new({}));",
name, GetDefaultAsString(qual_type)));
StrCat(std::format("{}::{}(__u.as_pointer()).write(", name,
GetNamedDeclAsString(field)));
ConvertVarInit(field->getType(), expr->getInit(0));
StrCat("); Rc::try_unwrap(__u).ok().unwrap().into_inner() }");
computed_expr_type_ = ComputedExprType::FreshValue;
return false;
}

StrCat(GetUnsafeTypeAsString(qual_type));
{
PushBrace brace(*this);
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/out/refcount/union_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ fn main_0() -> i32 {
assert!(((basic::i(ru.clone()).read()) == 1065353216));
assert!(((pf.read()) == 1_f32));
assert!((((*buf.borrow())[(3) as usize] as i32) == 63));
let braced: Value<basic> = Rc::new(RefCell::new({
let __u: Value<basic> = Rc::new(RefCell::new(<basic>::default()));
basic::i(__u.as_pointer()).write(5);
Rc::try_unwrap(__u).ok().unwrap().into_inner()
}));
assert!(((basic::i(braced.as_pointer()).read()) == 5));
let copied: Value<basic> = Rc::new(RefCell::new({
let __u: Value<basic> = Rc::new(RefCell::new(<basic>::default()));
basic::i(__u.as_pointer()).write(7);
Rc::try_unwrap(__u).ok().unwrap().into_inner()
}));
assert!(((basic::i(copied.as_pointer()).read()) == 7));
{
let anon_0: Value<anon_0> = Rc::new(RefCell::new(<anon_0>::default()));
anon_0::ai(anon_0.as_pointer()).write(11);
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/out/unsafe/union_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ unsafe fn main_0() -> i32 {
assert!((((*ru).i) == (1065353216)));
assert!(((*pf) == (1_f32)));
assert!(((buf[(3) as usize] as i32) == (63)));
let mut braced: basic = basic { i: 5 };
assert!(((braced.i) == (5)));
let mut copied: basic = basic { i: 7 };
assert!(((copied.i) == (7)));
{
let mut anon_0: anon_0 = <anon_0>::default();
anon_0.ai = 11;
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/union_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ int main(void) {
assert(*pf == 1.0f);
assert(buf[3] == 0x3F);

union basic braced{5};
assert(braced.i == 5);
union basic copied = {7};
assert(copied.i == 7);

{
union {
int ai;
Expand Down
Loading