Skip to content

Commit 67d35b6

Browse files
committed
Skip cast if rust type does not need it
1 parent dd26518 commit 67d35b6

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,19 @@ void Converter::ConvertIntegralToBooleanCast(clang::ImplicitCastExpr *expr) {
17691769
}
17701770
}
17711771

1772+
bool Converter::IsSameRustType(clang::Expr *a, clang::Expr *b) {
1773+
auto get_converted_type_or_mapped_type = [&](clang::Expr *expr) {
1774+
if (!llvm::isa<clang::ImplicitCastExpr>(expr)) {
1775+
if (const auto *rule = Mapper::GetExprRule(expr)) {
1776+
return rule->return_type.type;
1777+
}
1778+
}
1779+
return GetUnsafeTypeAsString(expr->getType());
1780+
};
1781+
return get_converted_type_or_mapped_type(a) ==
1782+
get_converted_type_or_mapped_type(b);
1783+
}
1784+
17721785
bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
17731786
auto *sub_expr = expr->getSubExpr();
17741787
auto type = expr->getType();
@@ -1866,8 +1879,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
18661879
break;
18671880
}
18681881
// Skip cast if source and target map to the same Rust type.
1869-
if (GetUnsafeTypeAsString(sub_expr->getType()) ==
1870-
GetUnsafeTypeAsString(type)) {
1882+
if (IsSameRustType(sub_expr, expr)) {
18711883
Convert(sub_expr);
18721884
break;
18731885
}

cpp2rust/converter/converter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
685685

686686
TempMaterializationCtx CollectPrvalueToLRefArgs(clang::CallExpr *expr);
687687

688+
bool IsSameRustType(clang::Expr *a, clang::Expr *b);
689+
688690
private:
689691
void materializeTemplateSpecialization(clang::CXXRecordDecl *decl);
690692

0 commit comments

Comments
 (0)