Skip to content

Commit 890e2c2

Browse files
committed
Fix compound assignment type casts
1 parent 0a50db5 commit 890e2c2

11 files changed

Lines changed: 37 additions & 23 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2274,7 +2274,8 @@ void Converter::ConvertBinaryOperator(clang::BinaryOperator *expr) {
22742274
if (auto *cmpd_assign_op =
22752275
llvm::dyn_cast<clang::CompoundAssignOperator>(expr);
22762276
expr->isCompoundAssignmentOp() &&
2277-
lhs_type != cmpd_assign_op->getComputationResultType()) {
2277+
GetUnsafeTypeAsString(lhs_type) !=
2278+
GetUnsafeTypeAsString(cmpd_assign_op->getComputationResultType())) {
22782279
auto computation_result_type = cmpd_assign_op->getComputationResultType();
22792280
if (IsUnsignedArithOp(cmpd_assign_op)) {
22802281
Convert(lhs);

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ ConverterRefCount::PushUnboxedIfSimple::PushUnboxedIfSimple(
109109
: ConversionKind::FullRefCount);
110110
}
111111

112+
std::string
113+
ConverterRefCount::GetSafeTypeAsString(clang::QualType qual_type) const {
114+
std::string type_as_string;
115+
ConverterRefCount converter(type_as_string, ctx_);
116+
converter.Convert(qual_type);
117+
return std::string(Trim(type_as_string));
118+
}
119+
112120
std::string ConverterRefCount::BoxType(std::string &&str) const {
113121
switch (getConversionKind()) {
114122
case ConversionKind::Unboxed:
@@ -1362,7 +1370,8 @@ void ConverterRefCount::ConvertBinaryOperator(clang::BinaryOperator *expr) {
13621370
std::string_view opcode_as_string = expr->getOpcodeStr();
13631371

13641372
if (auto *assign = llvm::dyn_cast<clang::CompoundAssignOperator>(expr);
1365-
assign && lhs_type != assign->getComputationResultType()) {
1373+
assign && GetSafeTypeAsString(lhs_type) !=
1374+
GetSafeTypeAsString(assign->getComputationResultType())) {
13661375
auto computation_result_type = assign->getComputationResultType();
13671376
StrCat(keyword::kLet, "rhs_0", token::kAssign);
13681377
if (IsUnsignedArithOp(assign)) {

‎cpp2rust/converter/models/converter_refcount.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ class ConverterRefCount final : public Converter {
242242
std::string ConvertPtrType(clang::QualType type);
243243
std::string ConvertPointeeType(clang::QualType ptr_type) override;
244244

245+
std::string GetSafeTypeAsString(clang::QualType qual_type) const;
246+
245247
/// The kind of conversion that should be performed.
246248
enum class ConversionKind : uint8_t {
247249
Unboxed,

‎tests/unit/out/refcount/auto.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ fn main_0() -> i32 {
1919
let sum: Value<i32> = Rc::new(RefCell::new(0));
2020
'loop_: for mut elem in v.as_pointer() as Ptr<i32> {
2121
let elem: Value<i32> = Rc::new(RefCell::new(elem.read().clone()));
22-
let rhs_0 = (((*sum.borrow()) as i32) + (*elem.borrow())) as i32;
23-
(*sum.borrow_mut()) = rhs_0;
22+
(*sum.borrow_mut()) += (*elem.borrow());
2423
}
2524
return (*sum.borrow());
2625
}

‎tests/unit/out/refcount/compound_assign_ref.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ pub fn main() {
1212
fn main_0() -> i32 {
1313
let v: Value<Vec<i32>> = Rc::new(RefCell::new(Vec::new()));
1414
(*v.borrow_mut()).push(10);
15-
let rhs_0 = ((((v.as_pointer() as Ptr<i32>).read()) as i32) + 5) as i32;
16-
(v.as_pointer() as Ptr<i32>).write(rhs_0);
15+
{
16+
let _ptr = (v.as_pointer() as Ptr<i32>).clone();
17+
_ptr.write(_ptr.read() + 5)
18+
};
1719
assert!((((v.as_pointer() as Ptr<i32>).read()) == 15));
1820
return ((v.as_pointer() as Ptr<i32>).read());
1921
}

‎tests/unit/out/refcount/foreach_mut.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ fn main_0() -> i32 {
2424
(*sum.borrow_mut()) += (*x.borrow());
2525
}
2626
'loop_: for mut x in v1.as_pointer() as Ptr<i32> {
27-
let rhs_0 = (((x.read()) as i32) + 10) as i32;
28-
x.write(rhs_0);
27+
{
28+
let _ptr = x.clone();
29+
_ptr.write(_ptr.read() + 10)
30+
};
2931
}
3032
'loop_: for mut x in v1.as_pointer() as Ptr<i32> {
3133
let __rhs = (x.read());
@@ -49,8 +51,10 @@ fn main_0() -> i32 {
4951
}
5052
'loop_: for mut p in v2.as_pointer() as Ptr<Ptr<i32>> {
5153
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(p.read().clone()));
52-
let rhs_0 = ((((*p.borrow()).read()) as i32) + 5) as i32;
53-
(*p.borrow()).write(rhs_0);
54+
{
55+
let _ptr = (*p.borrow()).clone();
56+
_ptr.write(_ptr.read() + 5)
57+
};
5458
}
5559
'loop_: for mut p in v2.as_pointer() as Ptr<Ptr<i32>> {
5660
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(p.read().clone()));

‎tests/unit/out/refcount/unique_ptr_deref_ops.rs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ pub fn main() {
1111
}
1212
fn main_0() -> i32 {
1313
let p: Value<Option<Value<i32>>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(10)))));
14-
let rhs_0 = (((*(*p.borrow()).as_ref().unwrap().borrow()) as i32) + 5) as i32;
15-
(*(*p.borrow_mut()).as_ref().unwrap().borrow_mut()) = rhs_0;
16-
let rhs_0 = (((*(*p.borrow()).as_ref().unwrap().borrow()) as i32) - 3) as i32;
17-
(*(*p.borrow_mut()).as_ref().unwrap().borrow_mut()) = rhs_0;
18-
let rhs_0 = (((*(*p.borrow()).as_ref().unwrap().borrow()) as i32) * 2) as i32;
19-
(*(*p.borrow_mut()).as_ref().unwrap().borrow_mut()) = rhs_0;
14+
(*(*p.borrow_mut()).as_ref().unwrap().borrow_mut()) += 5;
15+
(*(*p.borrow_mut()).as_ref().unwrap().borrow_mut()) -= 3;
16+
(*(*p.borrow_mut()).as_ref().unwrap().borrow_mut()) *= 2;
2017
let q: Value<Option<Value<i32>>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(1)))));
2118
let sum: Value<i32> = Rc::new(RefCell::new(
2219
((*(*p.borrow()).as_ref().unwrap().borrow()) + (*(*q.borrow()).as_ref().unwrap().borrow())),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ unsafe fn main_0() -> i32 {
2121
let mut sum: i32 = 0;
2222
'loop_: for elem in 0..(v.len()) {
2323
let mut elem = v[elem].clone();
24-
sum = ((sum as i32) + elem) as i32;
24+
sum += elem;
2525
}
2626
return sum;
2727
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn main() {
1414
unsafe fn main_0() -> i32 {
1515
let mut v: Vec<i32> = Vec::new();
1616
v.push(10);
17-
((v).first_mut().unwrap()) = ((((v).first_mut().unwrap()) as i32) + 5) as i32;
17+
(*((v).first_mut().unwrap())) += 5;
1818
assert!(((*((v).first_mut().unwrap())) == (15)));
1919
return (*((v).first_mut().unwrap()));
2020
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ unsafe fn main_0() -> i32 {
2727
}
2828
'loop_: for x in 0..(v1.len()) {
2929
let mut x = v1.as_mut_ptr().add(x);
30-
(*x) = (((*x) as i32) + 10) as i32;
30+
(*x) += 10;
3131
}
3232
'loop_: for x in 0..(v1.len()) {
3333
let mut x = v1.as_ptr().add(x);
@@ -47,7 +47,7 @@ unsafe fn main_0() -> i32 {
4747
}
4848
'loop_: for p in 0..(v2.len()) {
4949
let mut p = v2[p].clone();
50-
(*p) = (((*p) as i32) + 5) as i32;
50+
(*p) += 5;
5151
}
5252
'loop_: for p in 0..(v2.len()) {
5353
let mut p = v2[p].clone();

0 commit comments

Comments
 (0)