Skip to content

Commit a378f8f

Browse files
committed
Avoid mutable Values in refcount
1 parent 1f8a629 commit a378f8f

10 files changed

Lines changed: 54 additions & 36 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,9 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
473473
auto *method_or_null =
474474
curr_function_ ? clang::dyn_cast<clang::CXXMethodDecl>(curr_function_)
475475
: nullptr;
476-
if (hoisted_decls_.contains(decl) && !qual_type->isReferenceType()) {
477-
StrCat("mut");
478-
} else if (!qual_type.isConstQualified() && !qual_type->isReferenceType() &&
479-
((method_or_null == nullptr) || !method_or_null->isVirtual()) &&
480-
!IsGlobalVar(decl)) {
476+
if (!qual_type.isConstQualified() && !qual_type->isReferenceType() &&
477+
((method_or_null == nullptr) || !method_or_null->isVirtual()) &&
478+
!IsGlobalVar(decl)) {
481479
StrCat(keyword_mut_);
482480
}
483481

@@ -520,13 +518,18 @@ void Converter::ConvertVarDeclInitializer(clang::VarDecl *decl) {
520518
}
521519
}
522520

521+
void Converter::EmitHoistedInArmAssignment(clang::VarDecl *decl) {
522+
if (!decl->hasInit()) {
523+
return;
524+
}
525+
StrCat(GetNamedDeclAsString(decl), token::kAssign);
526+
ConvertVarInit(decl->getType(), decl->getInit());
527+
StrCat(token::kSemiColon);
528+
}
529+
523530
void Converter::ConvertVarDecl(clang::VarDecl *decl) {
524531
if (hoisted_decls_.contains(decl)) {
525-
if (decl->hasInit()) {
526-
StrCat(GetNamedDeclAsString(decl), token::kAssign);
527-
ConvertVarInit(decl->getType(), decl->getInit());
528-
StrCat(token::kSemiColon);
529-
}
532+
EmitHoistedInArmAssignment(decl);
530533
return;
531534
}
532535
if (!ConvertVarDeclSkipInit(decl)) {

cpp2rust/converter/converter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
9292

9393
void ConvertVarDecl(clang::VarDecl *decl);
9494

95+
virtual void EmitHoistedInArmAssignment(clang::VarDecl *decl);
96+
9597
void ConvertVarDeclInitializer(clang::VarDecl *decl);
9698

9799
virtual void ConvertGlobalVarDecl(clang::VarDecl *decl);

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,17 @@ bool ConverterRefCount::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
628628
return Converter::ConvertVarDeclSkipInit(decl);
629629
}
630630

631+
void ConverterRefCount::EmitHoistedInArmAssignment(clang::VarDecl *decl) {
632+
if (!decl->hasInit()) {
633+
return;
634+
}
635+
PushConversionKind push(*this, ConversionKind::Unboxed);
636+
StrCat(token::kStar, GetNamedDeclAsString(decl), ".borrow_mut()",
637+
token::kAssign);
638+
Convert(decl->getInit());
639+
StrCat(token::kSemiColon);
640+
}
641+
631642
void ConverterRefCount::ConvertGlobalVarDecl(clang::VarDecl *decl) {
632643
StrCat("thread_local!");
633644
{

cpp2rust/converter/models/converter_refcount.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class ConverterRefCount final : public Converter {
5959

6060
bool ConvertVarDeclSkipInit(clang::VarDecl *decl) override;
6161

62+
void EmitHoistedInArmAssignment(clang::VarDecl *decl) override;
63+
6264
bool ConvertLambdaVarDecl(clang::VarDecl *decl) override;
6365

6466
bool VisitDeclRefExpr(clang::DeclRefExpr *expr) override;

tests/unit/out/refcount/goto_aggregate_default.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,26 @@ impl ByteRepr for Point {
2525
}
2626
pub fn agg_0(n: i32) -> i32 {
2727
let n: Value<i32> = Rc::new(RefCell::new(n));
28-
let mut buf40: Value<Box<[u8]>> = Rc::new(RefCell::new(
28+
let buf40: Value<Box<[u8]>> = Rc::new(RefCell::new(
2929
(0..40).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
3030
));
31-
let mut buf256: Value<Box<[u8]>> = Rc::new(RefCell::new(
31+
let buf256: Value<Box<[u8]>> = Rc::new(RefCell::new(
3232
(0..256).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
3333
));
34-
let mut arr64: Value<Box<[i32]>> = Rc::new(RefCell::new(
34+
let arr64: Value<Box<[i32]>> = Rc::new(RefCell::new(
3535
(0..64).map(|_| <i32>::default()).collect::<Box<[i32]>>(),
3636
));
37-
let mut longs: Value<Box<[i64]>> = Rc::new(RefCell::new(
37+
let longs: Value<Box<[i64]>> = Rc::new(RefCell::new(
3838
(0..33).map(|_| <i64>::default()).collect::<Box<[i64]>>(),
3939
));
40-
let mut p: Value<Point> = <Value<Point>>::default();
41-
let mut ptr: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::<i32>::null()));
42-
let mut fp: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::null()));
43-
let mut file: Value<Ptr<::std::fs::File>> = Rc::new(RefCell::new(Ptr::null()));
44-
let mut total: Value<i32> = <Value<i32>>::default();
40+
let p: Value<Point> = <Value<Point>>::default();
41+
let ptr: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::<i32>::null()));
42+
let fp: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::null()));
43+
let file: Value<Ptr<::std::fs::File>> = Rc::new(RefCell::new(Ptr::null()));
44+
let total: Value<i32> = <Value<i32>>::default();
4545
goto_block!({
4646
'__entry: {
47-
total = Rc::new(RefCell::new(0));
47+
*total.borrow_mut() = 0;
4848
if ((((*n.borrow()) < 0) as i32) != 0) {
4949
goto!('out);
5050
}

tests/unit/out/refcount/goto_backward.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn retry_0(n: i32) -> i32 {
1010
let n: Value<i32> = Rc::new(RefCell::new(n));
11-
let mut count: Value<i32> = <Value<i32>>::default();
12-
let mut acc: Value<i32> = <Value<i32>>::default();
11+
let count: Value<i32> = <Value<i32>>::default();
12+
let acc: Value<i32> = <Value<i32>>::default();
1313
goto_block!({
1414
'__entry: {
15-
count = Rc::new(RefCell::new(0));
16-
acc = Rc::new(RefCell::new(0));
15+
*count.borrow_mut() = 0;
16+
*acc.borrow_mut() = 0;
1717
}
1818
'again: {
1919
(*count.borrow_mut()) += 1;

tests/unit/out/refcount/goto_cleanup.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn early_0(n: i32) -> i32 {
1010
let n: Value<i32> = Rc::new(RefCell::new(n));
11-
let mut ret: Value<i32> = <Value<i32>>::default();
11+
let ret: Value<i32> = <Value<i32>>::default();
1212
goto_block!({
1313
'__entry: {
14-
ret = Rc::new(RefCell::new(0));
14+
*ret.borrow_mut() = 0;
1515
if ((((*n.borrow()) < 0) as i32) != 0) {
1616
(*ret.borrow_mut()) = -1_i32;
1717
goto!('out);
@@ -26,10 +26,10 @@ pub fn early_0(n: i32) -> i32 {
2626
}
2727
pub fn from_loop_1(n: i32) -> i32 {
2828
let n: Value<i32> = Rc::new(RefCell::new(n));
29-
let mut ret: Value<i32> = <Value<i32>>::default();
29+
let ret: Value<i32> = <Value<i32>>::default();
3030
goto_block!({
3131
'__entry: {
32-
ret = Rc::new(RefCell::new(0));
32+
*ret.borrow_mut() = 0;
3333
let i: Value<i32> = Rc::new(RefCell::new(0));
3434
'loop_: while ((((*i.borrow()) < (*n.borrow())) as i32) != 0) {
3535
if ((((*i.borrow()) == 3) as i32) != 0) {
@@ -49,10 +49,10 @@ pub fn from_loop_1(n: i32) -> i32 {
4949
}
5050
pub fn from_switch_2(n: i32) -> i32 {
5151
let n: Value<i32> = Rc::new(RefCell::new(n));
52-
let mut ret: Value<i32> = <Value<i32>>::default();
52+
let ret: Value<i32> = <Value<i32>>::default();
5353
goto_block!({
5454
'__entry: {
55-
ret = Rc::new(RefCell::new(0));
55+
*ret.borrow_mut() = 0;
5656
'switch: {
5757
let __match_cond = (*n.borrow());
5858
match __match_cond {

tests/unit/out/refcount/goto_multi_label.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn classify_0(n: i32) -> i32 {
1010
let n: Value<i32> = Rc::new(RefCell::new(n));
11-
let mut ret: Value<i32> = <Value<i32>>::default();
11+
let ret: Value<i32> = <Value<i32>>::default();
1212
goto_block!({
1313
'__entry: {
14-
ret = Rc::new(RefCell::new(0));
14+
*ret.borrow_mut() = 0;
1515
if ((((*n.borrow()) < 0) as i32) != 0) {
1616
goto!('error);
1717
}

tests/unit/out/refcount/goto_nested_label.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ pub fn scan_0(n: i32) -> i32 {
1111
let total: Value<i32> = Rc::new(RefCell::new(0));
1212
let i: Value<i32> = Rc::new(RefCell::new(0));
1313
'loop_: while ((((*i.borrow()) < (*n.borrow())) as i32) != 0) {
14-
let mut j: Value<i32> = <Value<i32>>::default();
14+
let j: Value<i32> = <Value<i32>>::default();
1515
goto_block!({
1616
'__entry: {
17-
j = Rc::new(RefCell::new(0));
17+
*j.borrow_mut() = 0;
1818
'loop_: while ((((*j.borrow()) < 10) as i32) != 0) {
1919
if ((((*j.borrow()) == 5) as i32) != 0) {
2020
goto!('next);

tests/unit/out/refcount/goto_switch_fallthrough.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn sm_0(n: i32) -> i32 {
1010
let n: Value<i32> = Rc::new(RefCell::new(n));
11-
let mut ret: Value<i32> = <Value<i32>>::default();
11+
let ret: Value<i32> = <Value<i32>>::default();
1212
goto_block!({
1313
'__entry: {
14-
ret = Rc::new(RefCell::new(0));
14+
*ret.borrow_mut() = 0;
1515
switch!(match (*n.borrow()) {
1616
v if v == 0 => {
1717
(*ret.borrow_mut()) += 1;

0 commit comments

Comments
 (0)