Skip to content

Commit 427e4d4

Browse files
committed
Only apply const rewrite for lvalue context
1 parent d48981c commit 427e4d4

3 files changed

Lines changed: 8 additions & 7 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3019,28 +3019,28 @@ bool Converter::ConvertIncAndDec(clang::UnaryOperator *expr) {
30193019
auto *sub_expr = expr->getSubExpr();
30203020
switch (opcode) {
30213021
case clang::UO_PostInc: {
3022-
PushExprKind push(*this, ExprKind::RValue);
3022+
PushExprKind push(*this, ExprKind::LValue);
30233023
Convert(sub_expr);
30243024
StrCat(".postfix_inc()");
30253025
SetFresh();
30263026
return true;
30273027
}
30283028
case clang::UO_PostDec: {
3029-
PushExprKind push(*this, ExprKind::RValue);
3029+
PushExprKind push(*this, ExprKind::LValue);
30303030
Convert(sub_expr);
30313031
StrCat(".postfix_dec()");
30323032
SetFresh();
30333033
return true;
30343034
}
30353035
case clang::UO_PreInc: {
3036-
PushExprKind push(*this, ExprKind::RValue);
3036+
PushExprKind push(*this, ExprKind::LValue);
30373037
Convert(sub_expr);
30383038
StrCat(".prefix_inc()");
30393039
SetFresh();
30403040
return true;
30413041
}
30423042
case clang::UO_PreDec: {
3043-
PushExprKind push(*this, ExprKind::RValue);
3043+
PushExprKind push(*this, ExprKind::LValue);
30443044
Convert(sub_expr);
30453045
StrCat(".prefix_dec()");
30463046
SetFresh();
@@ -3383,7 +3383,8 @@ 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 (!clang::isa<clang::CXXThisExpr>(base->IgnoreParenImpCasts()) &&
3386+
if (!isRValue() &&
3387+
!clang::isa<clang::CXXThisExpr>(base->IgnoreParenImpCasts()) &&
33873388
object_type.isConstQualified()) {
33883389
if (isAddrOf()) {
33893390
StrCat(std::format("(&raw const {}).cast_mut()", str));

‎tests/unit/out/unsafe/class.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ unsafe fn main_0() -> i32 {
134134
assert!((unsafe { Counter::operator_eq(&mut *(&raw const c1).cast_mut(), &c2,) }));
135135
assert!((unsafe { Counter::operator_eq(&mut *(&raw const c2).cast_mut(), &c1,) }));
136136
assert!(((c1.calls) == (3)));
137-
assert!(((*(&raw const c2.calls).cast_mut()) == (2)));
137+
assert!(((c2.calls) == (2)));
138138
return 0;
139139
}
140140
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/copy_ctor.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ unsafe fn main_0() -> i32 {
244244
let ct: Tracked = <Tracked>::default();
245245
let mut t3: Tracked = Tracked::copy_from({ &ct });
246246
(unsafe { touch_1((&ct as *const Tracked)) });
247-
assert!(((*(&raw const ct.copied_from).cast_mut()) == (2)));
247+
assert!(((ct.copied_from) == (2)));
248248
return 0;
249249
}
250250
pub unsafe fn __cpp2rust_init_globals() {

0 commit comments

Comments
 (0)