Skip to content

Commit aecefd9

Browse files
committed
Fix implicit and explicit casts between u8/c_char
1 parent 90da3da commit aecefd9

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,8 +1241,13 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
12411241
expr->getType()->isPointerType()) {
12421242
Convert(expr->getSubExpr());
12431243
PushConversionKind push(*this, ConversionKind::Unboxed);
1244-
StrCat(std::format(".cast::<{}>().expect(\"ub:wrong type\")",
1245-
ConvertPointeeType(expr->getType())));
1244+
if (expr->getType()->getPointeeType()->isCharType()) {
1245+
StrCat(std::format(".reinterpret_cast::<{}>()",
1246+
ConvertPointeeType(expr->getType())));
1247+
} else {
1248+
StrCat(std::format(".cast::<{}>().expect(\"ub:wrong type\")",
1249+
ConvertPointeeType(expr->getType())));
1250+
}
12461251
return false;
12471252
} else if (expr->getType()->isVoidPointerType() &&
12481253
expr->getSubExpr()->getType()->isPointerType()) {
@@ -1253,9 +1258,18 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
12531258
} else if (expr->getSubExpr()->getType()->isPointerType() &&
12541259
!expr->getSubExpr()->isNullPointerConstant(
12551260
ctx_, clang::Expr::NPC_ValueDependentIsNull)) {
1256-
StrCat(std::format("({}.to_strong().as_pointer() as {})",
1257-
ToString(expr->getSubExpr()),
1258-
ToString(expr->getType())));
1261+
auto src_type = ToString(expr->getSubExpr()->getType());
1262+
auto tgt_type = ToString(expr->getType());
1263+
if (src_type == tgt_type) {
1264+
StrCat(std::format("({}.to_strong().as_pointer() as {})",
1265+
ToString(expr->getSubExpr()), tgt_type));
1266+
} else {
1267+
PushConversionKind push(*this, ConversionKind::Unboxed);
1268+
StrCat(std::format(
1269+
"({}.to_strong().as_pointer() as {}).reinterpret_cast::<{}>()",
1270+
ToString(expr->getSubExpr()), src_type,
1271+
ConvertPointeeType(expr->getType())));
1272+
}
12591273
return false;
12601274
}
12611275
return Converter::VisitExplicitCastExpr(expr);

0 commit comments

Comments
 (0)