Skip to content

Commit 029981f

Browse files
committed
Fix comma operator for packs
1 parent 49d4ca8 commit 029981f

13 files changed

Lines changed: 345 additions & 63 deletions

‎cpp2rust/converter/converter.cpp‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2730,6 +2730,7 @@ void Converter::ConvertBinaryOperator(clang::BinaryOperator *expr) {
27302730
ConvertCast(lhs_type);
27312731
}
27322732
} else if (expr->isCommaOp()) {
2733+
PushBrace brace(*this);
27332734
{
27342735
PushExprKind push(*this, ExprKind::Void);
27352736
Convert(lhs);
@@ -3099,13 +3100,10 @@ bool Converter::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
30993100
}
31003101

31013102
bool Converter::VisitParenExpr(clang::ParenExpr *expr) {
3102-
// Comma operator becomes (A, B, C) -> { A; B; C }
3103-
if (auto *bin = clang::dyn_cast<clang::BinaryOperator>(expr->getSubExpr())) {
3104-
if (bin->isCommaOp()) {
3105-
PushBrace push(*this);
3106-
Convert(expr->getSubExpr());
3107-
return false;
3108-
}
3103+
if (auto *bin = clang::dyn_cast<clang::BinaryOperator>(expr->getSubExpr());
3104+
bin && (bin->isCommaOp() || (bin->isAssignmentOp() && isVoid()))) {
3105+
Convert(expr->getSubExpr());
3106+
return false;
31093107
}
31103108

31113109
{

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ pub fn for_test_0(n: i32) -> i32 {
2020
let w: Value<i32> = Rc::new(RefCell::new(0));
2121
'loop_: while ((*w.borrow()) < (*j.borrow())) {
2222
break;
23-
(*w.borrow_mut()) += 1;
24-
(*k.borrow_mut()) += 1;
25-
(*i.borrow_mut()) += (*k.borrow());
23+
{
24+
{
25+
(*w.borrow_mut()) += 1;
26+
(*k.borrow_mut()) += 1
27+
};
28+
(*i.borrow_mut()) += (*k.borrow())
29+
};
2630
}
2731
let __rhs = ((*x.borrow()) + 1);
2832
(*x.borrow_mut()) = __rhs;
29-
(*j.borrow_mut()) = (*i.borrow());
30-
(*i.borrow_mut()) += 1;
33+
{
34+
(*j.borrow_mut()) = (*i.borrow());
35+
(*i.borrow_mut()) += 1
36+
};
3137
}
3238
return (*x.borrow());
3339
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,30 @@ fn main_0() -> i32 {
1919
assert!(((*x.borrow()) == 2));
2020
assert!(((*y.borrow()) == 3));
2121
let z: Value<i32> = Rc::new(RefCell::new({
22-
1;
23-
2;
22+
{
23+
1;
24+
2
25+
};
2426
3
2527
}));
2628
assert!(((*z.borrow()) == 3));
2729
let counter: Value<i32> = Rc::new(RefCell::new(0));
2830
let w: Value<i32> = Rc::new(RefCell::new({
29-
(*counter.borrow_mut()).postfix_inc();
30-
(*counter.borrow_mut()).postfix_inc();
31+
{
32+
(*counter.borrow_mut()).postfix_inc();
33+
(*counter.borrow_mut()).postfix_inc()
34+
};
3135
(*counter.borrow())
3236
}));
3337
assert!(((*counter.borrow()) == 2));
3438
assert!(((*w.borrow()) == 2));
3539
let a: Value<i32> = Rc::new(RefCell::new(0));
3640
let b: Value<i32> = Rc::new(RefCell::new(0));
3741
if {
38-
(*a.borrow_mut()) = 1;
39-
(*b.borrow_mut()) = 2;
42+
{
43+
(*a.borrow_mut()) = 1;
44+
(*b.borrow_mut()) = 2
45+
};
4046
(((*a.borrow()) + (*b.borrow())) > 0)
4147
} {
4248
assert!(((*a.borrow()) == 1));

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,48 +41,48 @@ fn main_0() -> i32 {
4141
flags: Rc::new(RefCell::new(0_u8)),
4242
}));
4343
let ptr: Value<Ptr<Item>> = Rc::new(RefCell::new((item.as_pointer())));
44-
({
44+
{
4545
let rhs_0 =
4646
(((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) | (1 << 0)) as u8;
4747
(*(*(*ptr.borrow()).upgrade().deref()).flags.borrow_mut()) = rhs_0
48-
});
49-
({
48+
};
49+
{
5050
let rhs_0 =
5151
(((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) | (1 << 1)) as u8;
5252
(*(*(*ptr.borrow()).upgrade().deref()).flags.borrow_mut()) = rhs_0
53-
});
53+
};
5454
assert!((((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) == 3));
55-
({
55+
{
5656
let rhs_0 = (((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32)
5757
& ((!(1 << 0) as u8) as i32)) as u8;
5858
(*(*(*ptr.borrow()).upgrade().deref()).flags.borrow_mut()) = rhs_0
59-
});
59+
};
6060
assert!((((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) == 2));
6161
let bits: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::new([
6262
0_u8,
6363
<u8>::default(),
6464
<u8>::default(),
6565
<u8>::default(),
6666
])));
67-
({
67+
{
6868
let rhs_0 = (((*bits.borrow())[((5) / 8) as usize] as i32)
6969
| (((1 << ((5) & 7)) as u8) as i32)) as u8;
7070
(*bits.borrow_mut())[((5) / 8) as usize] = rhs_0
71-
});
72-
({
71+
};
72+
{
7373
let rhs_0 = (((*bits.borrow())[((13) / 8) as usize] as i32)
7474
| (((1 << ((13) & 7)) as u8) as i32)) as u8;
7575
(*bits.borrow_mut())[((13) / 8) as usize] = rhs_0
76-
});
76+
};
7777
assert!((((*bits.borrow())[(0) as usize] as i32) == 32));
7878
assert!((((*bits.borrow())[(1) as usize] as i32) == 32));
7979
assert!((((*bits.borrow())[(2) as usize] as i32) == 0));
8080
if (((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) != 0) {
81-
({
81+
{
8282
let rhs_0 = (((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32)
8383
& ((!(1 << 1) as u8) as i32)) as u8;
8484
(*(*(*ptr.borrow()).upgrade().deref()).flags.borrow_mut()) = rhs_0
85-
});
85+
};
8686
}
8787
assert!((((*(*(*ptr.borrow()).upgrade().deref()).flags.borrow()) as i32) == 0));
8888
return 0;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ fn main_0() -> i32 {
2222
.as_pointer()
2323
})
2424
.write((((*k.borrow()) as f64) / 2.0E+0));
25-
(*i.borrow_mut()).prefix_inc();
26-
(*k.borrow_mut()).prefix_dec();
25+
{
26+
(*i.borrow_mut()).prefix_inc();
27+
(*k.borrow_mut()).prefix_dec()
28+
};
2729
}
2830
let sum: Value<f64> = Rc::new(RefCell::new(0_f64));
2931
'loop_: for i in RefcountMapIter::begin(m.as_pointer()) {

0 commit comments

Comments
 (0)