Skip to content

Commit 5087db0

Browse files
committed
Handle array to pointer decay for C literals
In C, as opposed to C++, string literals are char [] instead of const char [] so cast_mut should be added on ArrayToPointerDecay from char [] to char * instead of NoOp const char * to char *.
1 parent 198b11d commit 5087db0

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,11 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
17251725
case clang::CastKind::CK_ArrayToPointerDecay:
17261726
if (clang::isa<clang::StringLiteral>(sub_expr) ||
17271727
clang::isa<clang::PredefinedExpr>(sub_expr)) {
1728-
return Convert(sub_expr);
1728+
Convert(sub_expr);
1729+
if (IsConversionFromStringLiteralToCharPtr(expr)) {
1730+
StrCat(".cast_mut()");
1731+
}
1732+
return false;
17291733
}
17301734
// __va_list_tag [1] decays to __va_list_tag *. Just pass through by value
17311735
if (IsVaListType(sub_expr->getType())) {

cpp2rust/converter/converter_lib.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,12 @@ void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix) {
764764
}
765765

766766
bool IsConversionFromStringLiteralToCharPtr(clang::Expr *expr) {
767-
return clang::isa<clang::StringLiteral>(expr->IgnoreImplicit()) &&
768-
expr->getType()->isPointerType() &&
769-
expr->getType()->getPointeeType()->isCharType();
767+
if (clang::isa<clang::StringLiteral>(expr->IgnoreImplicit()) &&
768+
expr->getType()->isPointerType()) {
769+
auto pointee = expr->getType()->getPointeeType();
770+
return pointee->isCharType() && !pointee.isConstQualified();
771+
}
772+
return false;
770773
}
771774

772775
} // namespace cpp2rust

0 commit comments

Comments
 (0)