Skip to content

Commit d6bef1f

Browse files
committed
Handle explicit cast of union member
1 parent 4b1deff commit d6bef1f

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,9 +1310,17 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
13101310
} else if (expr->getSubExpr()->getType()->isPointerType() &&
13111311
!expr->getSubExpr()->isNullPointerConstant(
13121312
ctx_, clang::Expr::NPC_ValueDependentIsNull)) {
1313-
StrCat(std::format("({}.to_strong().as_pointer() as {})",
1314-
ToString(expr->getSubExpr()),
1315-
ToString(expr->getType())));
1313+
auto src_pointee = expr->getSubExpr()->getType()->getPointeeType();
1314+
auto dst_pointee = expr->getType()->getPointeeType();
1315+
if (ctx_.hasSameUnqualifiedType(src_pointee, dst_pointee)) {
1316+
StrCat(std::format("({}.to_strong().as_pointer() as {})",
1317+
ToString(expr->getSubExpr()),
1318+
ToString(expr->getType())));
1319+
} else {
1320+
StrCat(std::format("{}.reinterpret_cast::<{}>()",
1321+
ToString(expr->getSubExpr()),
1322+
ConvertPointeeType(expr->getType())));
1323+
}
13161324
return false;
13171325
}
13181326
return Converter::VisitExplicitCastExpr(expr);

0 commit comments

Comments
 (0)