Skip to content

Commit c3abec5

Browse files
authored
Increment and decrement return a lvalue (#638)
1 parent 83d5770 commit c3abec5

92 files changed

Lines changed: 951 additions & 228 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cpp2rust/converter/converter.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3136,6 +3136,11 @@ bool Converter::ConvertIncAndDec(clang::UnaryOperator *expr) {
31363136
MarkFresh();
31373137
return true;
31383138
}
3139+
bool deref_prefix = expr->isPrefix() && !isVoid();
3140+
PushParen paren(*this, deref_prefix);
3141+
if (deref_prefix) {
3142+
StrCat(token::kStar);
3143+
}
31393144
{
31403145
PushExprKind push(*this, ExprKind::LValue);
31413146
Convert(expr->getSubExpr());

‎cpp2rust/converter/models/boxed_vars.cpp‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ class AddressTakenVisitor
255255
}
256256

257257
bool VisitUnaryOperator(clang::UnaryOperator *expr) {
258-
if (expr->isIncrementDecrementOp()) {
258+
if (expr->isIncrementDecrementOp() &&
259+
(expr->isPostfix() || expr->isPRValue())) {
259260
AddValueUse(expr->getSubExpr(), /*write=*/true);
260261
}
261262
return true;
@@ -392,6 +393,11 @@ class AddressTakenVisitor
392393
// without making a pointer to it.
393394
void AddValueUse(const clang::Expr *expr, bool write = false) {
394395
expr = IgnoreNoOpCasts(IgnoreTransparentStdCall(expr)->IgnoreParens());
396+
if (auto *op = clang::dyn_cast<clang::UnaryOperator>(expr);
397+
op && op->isPrefix() && op->isIncrementDecrementOp()) {
398+
AddValueUse(op->getSubExpr(), /*write=*/true);
399+
return;
400+
}
395401
// The value of a conditional lvalue is the value of either operand; it is
396402
// written through a pointer to the operand.
397403
if (auto *cond = clang::dyn_cast<clang::ConditionalOperator>(expr)) {
@@ -423,6 +429,14 @@ class AddressTakenVisitor
423429
if (auto *cleanups = clang::dyn_cast<clang::ExprWithCleanups>(expr)) {
424430
expr = cleanups->getSubExpr()->IgnoreParens();
425431
}
432+
if (auto *op = clang::dyn_cast<clang::UnaryOperator>(expr);
433+
op && op->isIncrementDecrementOp()) {
434+
AddValueUse(op->getSubExpr(), /*write=*/true);
435+
} else if (auto *comma = clang::dyn_cast<clang::BinaryOperator>(expr);
436+
comma && comma->getOpcode() == clang::BO_Comma) {
437+
AddDiscardedUse(comma->getRHS());
438+
}
439+
426440
auto *binary = clang::dyn_cast<clang::BinaryOperator>(expr);
427441
if (!binary) {
428442
return;

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -925,22 +925,51 @@ bool ConverterRefCount::ConvertIncAndDec(clang::UnaryOperator *expr) {
925925
if (expr->isPrefix() && isAddrOf()) {
926926
auto ptr = ConvertFreshObject(sub_expr);
927927
auto name = NewTempName();
928-
StrCat(
929-
std::format("{{ let {0} = {1}; {0}.with_mut(|__v| __v.{2}()); {0} }}",
930-
name, ptr, method));
928+
StrCat(std::format(
929+
"{{ let {0} = {1}; {0}.with_mut(|__v| {{ __v.{2}(); }}); {0} }}", name,
930+
ptr, method));
931931
MarkFresh();
932932
return true;
933933
}
934934

935+
bool is_prefix = expr->isPrefix();
935936
auto str = ConvertLValue(sub_expr);
936937
if (!pending_deref_.empty()) {
937-
StrCat(pending_deref_.take(), ".with_mut(|__v| __v.", method, "())");
938+
bool fresh = pending_deref_.is_fresh();
939+
auto ptr = pending_deref_.take();
940+
if (!is_prefix) {
941+
StrCat(ptr, ".with_mut(|__v| __v.", method, "())");
942+
} else if (isVoid()) {
943+
StrCat(ptr, ".with_mut(|__v| { __v.", method, "(); })");
944+
} else if (isLValue()) {
945+
auto name = NewTempName();
946+
pending_deref_.set(std::format("{{ let {0} = {1}{2}; {0}.with_mut(|__v| "
947+
"{{ __v.{3}(); }}); {0} }}",
948+
name, ptr, fresh ? "" : ".clone()",
949+
method),
950+
/*fresh=*/true);
951+
return true;
952+
} else {
953+
StrCat(ptr, ".with_mut(|__v| __v.", method, "().clone())");
954+
}
938955
} else if (!isVoid() &&
939956
clang::isa<clang::MemberExpr>(sub_expr->IgnoreParens()) &&
940957
!IsUnboxedPlace(sub_expr)) {
941958
// The struct is borrowed in a block, which ends the borrow before the
942959
// result is used, e.g., to write through a pointer into the struct.
943-
StrCat(std::format("{{ {}.{}() }}", str, method));
960+
if (!is_prefix) {
961+
StrCat(std::format("{{ {}.{}() }}", str, method));
962+
} else if (isLValue()) {
963+
StrCat(std::format("(*{}.{}())", str, method));
964+
} else {
965+
StrCat(std::format("{{ {}.{}().clone() }}", str, method));
966+
}
967+
} else if (is_prefix && !isVoid()) {
968+
StrCat(std::format("(*{}.{}())", str, method));
969+
if (!isLValue()) {
970+
SetValueFreshness(expr->getType());
971+
return true;
972+
}
944973
} else {
945974
StrCat(str, '.', method, "()");
946975
}

‎docs/src/runtime/inc-dec.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ statement, so the `inc` and `dec` modules define one trait per operator form:
66

77
```rust
88
pub trait PostfixInc { fn postfix_inc(&mut self) -> Self; }
9-
pub trait PrefixInc { fn prefix_inc(&mut self) -> Self; }
9+
pub trait PrefixInc { fn prefix_inc(&mut self) -> &mut Self; }
1010
pub trait PostfixDec { fn postfix_dec(&mut self) -> Self; }
11-
pub trait PrefixDec { fn prefix_dec(&mut self) -> Self; }
11+
pub trait PrefixDec { fn prefix_dec(&mut self) -> &mut Self; }
1212
```
1313

1414
Each method updates the value in place and returns what the C expression
1515
evaluates to: the postfix forms return a copy of the old value, the prefix forms
16-
the new one.
16+
a mutable reference to the updated operand, which is an lvalue in C++.
1717

1818
```c
1919
int x = 0;

‎libcc2rs/src/dec.rs‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ macro_rules! postfix_wrap_dec_impl {
3232
}
3333

3434
pub trait PrefixDec {
35-
fn prefix_dec(&mut self) -> Self;
35+
fn prefix_dec(&mut self) -> &mut Self;
3636
}
3737

3838
macro_rules! prefix_nowrap_dec_impl {
3939
($($type:ty),*) => {
4040
$(impl PrefixDec for $type {
4141
#[inline]
42-
fn prefix_dec(&mut self) -> Self {
42+
fn prefix_dec(&mut self) -> &mut Self {
4343
*self -= 1 as $type;
44-
*self
44+
self
4545
}
4646
})*
4747
};
@@ -51,9 +51,9 @@ macro_rules! prefix_wrap_dec_impl {
5151
($($type:ty),*) => {
5252
$(impl PrefixDec for $type {
5353
#[inline]
54-
fn prefix_dec(&mut self) -> Self {
54+
fn prefix_dec(&mut self) -> &mut Self {
5555
*self = self.wrapping_sub(1 as $type);
56-
*self
56+
self
5757
}
5858
})*
5959
};
@@ -98,22 +98,22 @@ pub trait UnsafePrefixDec {
9898
/// # Safety
9999
/// This function decrements a pointer and returns the new value.
100100
/// The caller must ensure the pointer is valid and doesn't underflow.
101-
unsafe fn prefix_dec(&mut self) -> Self;
101+
unsafe fn prefix_dec(&mut self) -> &mut Self;
102102
}
103103

104104
impl<T> UnsafePrefixDec for *const T {
105105
#[inline]
106-
unsafe fn prefix_dec(&mut self) -> Self {
106+
unsafe fn prefix_dec(&mut self) -> &mut Self {
107107
unsafe { *self = self.offset(-1) }
108-
*self
108+
self
109109
}
110110
}
111111

112112
impl<T> UnsafePrefixDec for *mut T {
113113
#[inline]
114-
unsafe fn prefix_dec(&mut self) -> Self {
114+
unsafe fn prefix_dec(&mut self) -> &mut Self {
115115
unsafe { *self = self.offset(-1) }
116-
*self
116+
self
117117
}
118118
}
119119

@@ -131,7 +131,7 @@ mod tests {
131131
#[test]
132132
fn test_prefix_dec() {
133133
let mut x = 10;
134-
assert_eq!(x.prefix_dec(), 9);
134+
assert_eq!(*x.prefix_dec(), 9);
135135
assert_eq!(x, 9);
136136
}
137137

@@ -152,7 +152,7 @@ mod tests {
152152
let y = &x as *const i32;
153153
let mut z = &x as *const i32;
154154
unsafe {
155-
assert_eq!(z.prefix_dec(), y.offset(-1));
155+
assert_eq!(*z.prefix_dec(), y.offset(-1));
156156
assert_eq!(z, y.offset(-1));
157157
}
158158
}

‎libcc2rs/src/inc.rs‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ macro_rules! postfix_wrap_inc_impl {
3232
}
3333

3434
pub trait PrefixInc {
35-
fn prefix_inc(&mut self) -> Self;
35+
fn prefix_inc(&mut self) -> &mut Self;
3636
}
3737

3838
macro_rules! prefix_nowrap_inc_impl {
3939
($($type:ty),*) => {
4040
$(impl PrefixInc for $type {
4141
#[inline]
42-
fn prefix_inc(&mut self) -> Self {
42+
fn prefix_inc(&mut self) -> &mut Self {
4343
*self += 1 as $type;
44-
*self
44+
self
4545
}
4646
})*
4747
};
@@ -51,9 +51,9 @@ macro_rules! prefix_wrap_inc_impl {
5151
($($type:ty),*) => {
5252
$(impl PrefixInc for $type {
5353
#[inline]
54-
fn prefix_inc(&mut self) -> Self {
54+
fn prefix_inc(&mut self) -> &mut Self {
5555
*self = self.wrapping_add(1 as $type);
56-
*self
56+
self
5757
}
5858
})*
5959
};
@@ -98,22 +98,22 @@ pub trait UnsafePrefixInc {
9898
/// # Safety
9999
/// This function increments a pointer and returns the new value.
100100
/// The caller must ensure the pointer is valid and doesn't overflow.
101-
unsafe fn prefix_inc(&mut self) -> Self;
101+
unsafe fn prefix_inc(&mut self) -> &mut Self;
102102
}
103103

104104
impl<T> UnsafePrefixInc for *const T {
105105
#[inline]
106-
unsafe fn prefix_inc(&mut self) -> Self {
106+
unsafe fn prefix_inc(&mut self) -> &mut Self {
107107
unsafe { *self = self.offset(1) }
108-
*self
108+
self
109109
}
110110
}
111111

112112
impl<T> UnsafePrefixInc for *mut T {
113113
#[inline]
114-
unsafe fn prefix_inc(&mut self) -> Self {
114+
unsafe fn prefix_inc(&mut self) -> &mut Self {
115115
unsafe { *self = self.offset(1) }
116-
*self
116+
self
117117
}
118118
}
119119

@@ -131,7 +131,7 @@ mod tests {
131131
#[test]
132132
fn test_prefix_inc() {
133133
let mut x = 10;
134-
assert_eq!(x.prefix_inc(), 11);
134+
assert_eq!(*x.prefix_inc(), 11);
135135
assert_eq!(x, 11);
136136
}
137137

@@ -152,7 +152,7 @@ mod tests {
152152
let y = &x as *const i32;
153153
let mut z = &x as *const i32;
154154
unsafe {
155-
assert_eq!(z.prefix_inc(), y.offset(1));
155+
assert_eq!(*z.prefix_inc(), y.offset(1));
156156
assert_eq!(z, y.offset(1));
157157
}
158158
}

‎libcc2rs/src/rc.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,17 +1444,17 @@ impl<T> PostfixDec for Ptr<T> {
14441444

14451445
impl<T> PrefixInc for Ptr<T> {
14461446
#[inline]
1447-
fn prefix_inc(&mut self) -> Self {
1447+
fn prefix_inc(&mut self) -> &mut Self {
14481448
self.offset = self.offset.wrapping_add(self.elem_step());
1449-
self.clone()
1449+
self
14501450
}
14511451
}
14521452

14531453
impl<T> PrefixDec for Ptr<T> {
14541454
#[inline]
1455-
fn prefix_dec(&mut self) -> Self {
1455+
fn prefix_dec(&mut self) -> &mut Self {
14561456
self.offset = self.offset.wrapping_sub(self.elem_step());
1457-
self.clone()
1457+
self
14581458
}
14591459
}
14601460

0 commit comments

Comments
 (0)