From d6bef1fdd04dc6a9dc0c19d778e8db27326fd82a Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 29 Jun 2026 15:00:10 +0100 Subject: [PATCH 1/2] Handle explicit cast of union member --- cpp2rust/converter/models/converter_refcount.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 72f8f143..627bda5d 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1310,9 +1310,17 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) { } else if (expr->getSubExpr()->getType()->isPointerType() && !expr->getSubExpr()->isNullPointerConstant( ctx_, clang::Expr::NPC_ValueDependentIsNull)) { - StrCat(std::format("({}.to_strong().as_pointer() as {})", - ToString(expr->getSubExpr()), - ToString(expr->getType()))); + auto src_pointee = expr->getSubExpr()->getType()->getPointeeType(); + auto dst_pointee = expr->getType()->getPointeeType(); + if (ctx_.hasSameUnqualifiedType(src_pointee, dst_pointee)) { + StrCat(std::format("({}.to_strong().as_pointer() as {})", + ToString(expr->getSubExpr()), + ToString(expr->getType()))); + } else { + StrCat(std::format("{}.reinterpret_cast::<{}>()", + ToString(expr->getSubExpr()), + ConvertPointeeType(expr->getType()))); + } return false; } return Converter::VisitExplicitCastExpr(expr); From 117fe7e10f590552fd3af9e0e869fedf0bc9742a Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 29 Jun 2026 15:47:10 +0100 Subject: [PATCH 2/2] Drop always false check --- cpp2rust/converter/models/converter_refcount.cpp | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 627bda5d..0da17bc5 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1310,17 +1310,9 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) { } else if (expr->getSubExpr()->getType()->isPointerType() && !expr->getSubExpr()->isNullPointerConstant( ctx_, clang::Expr::NPC_ValueDependentIsNull)) { - auto src_pointee = expr->getSubExpr()->getType()->getPointeeType(); - auto dst_pointee = expr->getType()->getPointeeType(); - if (ctx_.hasSameUnqualifiedType(src_pointee, dst_pointee)) { - StrCat(std::format("({}.to_strong().as_pointer() as {})", - ToString(expr->getSubExpr()), - ToString(expr->getType()))); - } else { - StrCat(std::format("{}.reinterpret_cast::<{}>()", - ToString(expr->getSubExpr()), - ConvertPointeeType(expr->getType()))); - } + StrCat(std::format("{}.reinterpret_cast::<{}>()", + ToString(expr->getSubExpr()), + ConvertPointeeType(expr->getType()))); return false; } return Converter::VisitExplicitCastExpr(expr);