Skip to content

Commit 892566f

Browse files
authored
Handle explicit cast of union member (#210)
Union members are translated using accessors: ```cpp union u { int a; } *(char *)&u.a = 0xFF; ``` becomes ```rs struct u { __bytes: Value<Box<[u8]>> } impl u { pub fn a() -> Ptr<i32> { ... } } // the accessor u.a().reinterpret_cast<u8>().write(0xFF) // Before this PR it did not compile: (u.a() as Ptr<u8>).write(0xFF) ``` This PR replaces the wrong `(u.a() as Ptr<u8>)` pattern with `u.a().reinterpret_cast<u8>()`
1 parent 36a7785 commit 892566f

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,9 +1313,9 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
13131313
} else if (expr->getSubExpr()->getType()->isPointerType() &&
13141314
!expr->getSubExpr()->isNullPointerConstant(
13151315
ctx_, clang::Expr::NPC_ValueDependentIsNull)) {
1316-
StrCat(std::format("({}.to_strong().as_pointer() as {})",
1316+
StrCat(std::format("{}.reinterpret_cast::<{}>()",
13171317
ToString(expr->getSubExpr()),
1318-
ToString(expr->getType())));
1318+
ConvertPointeeType(expr->getType())));
13191319
return false;
13201320
}
13211321
return Converter::VisitExplicitCastExpr(expr);

0 commit comments

Comments
 (0)