Skip to content

Commit bcab56f

Browse files
committed
Use const receiver since we do the mut cast at usage time
1 parent 427e4d4 commit bcab56f

2 files changed

Lines changed: 2 additions & 19 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3383,9 +3383,7 @@ bool Converter::VisitMemberExpr(clang::MemberExpr *expr) {
33833383
auto *base = expr->getBase();
33843384
auto object_type = expr->isArrow() ? base->getType()->getPointeeType()
33853385
: base->getType().getNonReferenceType();
3386-
if (!isRValue() &&
3387-
!clang::isa<clang::CXXThisExpr>(base->IgnoreParenImpCasts()) &&
3388-
object_type.isConstQualified()) {
3386+
if (!isRValue() && object_type.isConstQualified()) {
33893387
if (isAddrOf()) {
33903388
StrCat(std::format("(&raw const {}).cast_mut()", str));
33913389
computed_expr_type_ = ComputedExprType::FreshPointer;
@@ -3443,22 +3441,12 @@ void Converter::SetUFCSReceiver(clang::Expr *base, bool is_arrow,
34433441
}
34443442
Buffer buf(*this);
34453443
PushExprKind push(*this, ExprKind::LValue);
3446-
auto object_type = is_arrow ? base->getType()->getPointeeType()
3447-
: base->getType().getNonReferenceType();
3448-
bool cast_mut =
3449-
MethodNeedsMutableReceiver(method) && object_type.isConstQualified();
34503444
StrCat(MethodNeedsMutableReceiver(method) ? "&mut" : "&");
3451-
if (cast_mut) {
3452-
StrCat("*(&raw const");
3453-
}
34543445
if (is_arrow) {
34553446
ConvertArrow(base);
34563447
} else {
34573448
Convert(base);
34583449
}
3459-
if (cast_mut) {
3460-
StrCat(").cast_mut()");
3461-
}
34623450
ufcs_receiver_ = std::move(buf).str();
34633451
}
34643452

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,7 @@ bool IsRValueConvertingConstructor(const clang::CXXConstructorDecl *ctor) {
606606
}
607607

608608
bool MethodNeedsMutableReceiver(const clang::CXXMethodDecl *method) {
609-
if (!method->isConst()) {
610-
return true;
611-
}
612-
return std::any_of(method->getParent()->field_begin(),
613-
method->getParent()->field_end(),
614-
[](const clang::FieldDecl *f) { return f->isMutable(); });
609+
return !method->isConst();
615610
}
616611

617612
bool IsPassThroughConstructor(const clang::CXXConstructorDecl *ctor) {

0 commit comments

Comments
 (0)