Skip to content

Commit 4099462

Browse files
committed
Update tests
1 parent c966e63 commit 4099462

5 files changed

Lines changed: 44 additions & 59 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <llvm/Support/ConvertUTF.h>
1010

1111
#include <format>
12+
#include <utility>
1213

1314
#include "compiler.h"
1415
#include "converter/converter_lib.h"
@@ -1060,7 +1061,7 @@ void Converter::ConvertLoopVariable(clang::VarDecl *decl,
10601061
}
10611062
} else {
10621063
{
1063-
PushAutorefReceiver autoref(*this, /*is_mut=*/false);
1064+
PushExplicitAutoref autoref(*this, /*is_mut=*/false);
10641065
Convert(range_init);
10651066
}
10661067
StrCat(std::format("[{}]", loop_var_name));
@@ -2224,20 +2225,22 @@ bool Converter::VisitConditionalOperator(clang::ConditionalOperator *expr) {
22242225
bool branch_is_addr =
22252226
expr->isLValue() && !isRValue() && !expr->getType()->isFunctionType();
22262227
{
2227-
PushBrace then_brace(*this);
2228+
PushBrace brace(*this);
22282229
if (branch_is_addr) {
22292230
StrCat(token::kRef, keyword_mut_);
2230-
autoref_receiver_ = false;
22312231
}
2232+
PushExplicitAutoref no_autoref(*this, branch_is_addr ? std::optional<bool>{}
2233+
: autoref_mut_);
22322234
Convert(expr->getTrueExpr());
22332235
}
22342236
StrCat(keyword::kElse);
22352237
{
2236-
PushBrace else_brace(*this);
2238+
PushBrace brace(*this);
22372239
if (branch_is_addr) {
22382240
StrCat(token::kRef, keyword_mut_);
2239-
autoref_receiver_ = false;
22402241
}
2242+
PushExplicitAutoref no_autoref(*this, branch_is_addr ? std::optional<bool>{}
2243+
: autoref_mut_);
22412244
Convert(expr->getFalseExpr());
22422245
}
22432246
return false;
@@ -2292,8 +2295,7 @@ bool Converter::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
22922295

22932296
if (decl->getType()->getAs<clang::ReferenceType>() && !isAddrOf() &&
22942297
!map_iter_decls_.contains(clang::dyn_cast<clang::VarDecl>(decl))) {
2295-
EmitMaybeWrappedDeref(std::move(str),
2296-
decl->getType().getNonReferenceType());
2298+
EmitDeref(std::move(str), decl->getType().getNonReferenceType());
22972299
SetValueFreshness(expr->getType());
22982300
return false;
22992301
}
@@ -2373,7 +2375,7 @@ bool Converter::ConvertCXXOperatorCallExpr(clang::CXXOperatorCallExpr *expr) {
23732375
is_mut = !method->isConst();
23742376
}
23752377
}
2376-
PushAutorefReceiver autoref(*this, is_mut);
2378+
PushExplicitAutoref autoref(*this, is_mut);
23772379
ConvertArraySubscript(expr->getArg(0), expr->getArg(1), expr->getType());
23782380
break;
23792381
}
@@ -2442,8 +2444,7 @@ bool Converter::VisitMemberExpr(clang::MemberExpr *expr) {
24422444
}
24432445

24442446
if (!isAddrOf() && member->getType()->isReferenceType()) {
2445-
EmitMaybeWrappedDeref(std::move(str),
2446-
member->getType().getNonReferenceType());
2447+
EmitDeref(std::move(str), member->getType().getNonReferenceType());
24472448
return false;
24482449
}
24492450

@@ -2471,19 +2472,18 @@ void Converter::ConvertMemberExpr(clang::MemberExpr *expr) {
24712472
auto *base = expr->getBase();
24722473
bool base_is_this = clang::isa<clang::CXXThisExpr>(base->IgnoreCasts());
24732474
PushExprKind push(*this, isLValue() ? ExprKind::LValue : ExprKind::RValue);
2474-
std::optional<PushAutorefReceiver> autoref;
2475-
if (auto *method = clang::dyn_cast<clang::CXXMethodDecl>(member);
2476-
method && !method->isStatic() && isCallee() && !base_is_this) {
2477-
autoref.emplace(*this, !method->isConst());
2478-
}
2475+
auto *method = clang::dyn_cast<clang::CXXMethodDecl>(member);
2476+
PushExplicitAutoref autoref(*this, method && !method->isStatic() &&
2477+
isCallee() && !base_is_this
2478+
? std::optional{!method->isConst()}
2479+
: autoref_mut_);
24792480
if (expr->isArrow() && !base_is_this) {
24802481
ConvertArrow(base);
24812482
} else {
24822483
Convert(base);
24832484
}
24842485

2485-
if (auto *method = clang::dyn_cast<clang::CXXMethodDecl>(member);
2486-
method && IsOverloadedMethod(method)) {
2486+
if (method && IsOverloadedMethod(method)) {
24872487
StrCat(token::kDot);
24882488
StrCat(GetOverloadedFunctionName(method));
24892489
} else {
@@ -3217,15 +3217,14 @@ void Converter::ConvertPointerOffset(clang::Expr *base, clang::Expr *idx,
32173217

32183218
void Converter::ConvertArraySubscript(clang::Expr *base, clang::Expr *idx,
32193219
clang::QualType type) {
3220-
bool is_unique_ptr = IsUniquePtr(base->getType());
3221-
if (is_unique_ptr) {
3222-
autoref_receiver_ = false;
3223-
}
3224-
Convert(base->IgnoreImplicit());
3225-
if (is_unique_ptr) {
3220+
if (IsUniquePtr(base->getType())) {
3221+
PushExplicitAutoref no_autoref(*this, std::nullopt);
3222+
Convert(base->IgnoreImplicit());
32263223
StrCat(".as_mut().unwrap()");
3224+
} else {
3225+
Convert(base->IgnoreImplicit());
32273226
}
3228-
autoref_receiver_ = false;
3227+
PushExplicitAutoref no_autoref(*this, std::nullopt);
32293228
PushBracket bracket(*this);
32303229
{
32313230
PushParen paren(*this);
@@ -3531,26 +3530,19 @@ void Converter::ConvertAddrOf(clang::Expr *expr, clang::QualType pointer_type) {
35313530
}
35323531
}
35333532

3534-
void Converter::EmitMaybeWrappedDeref(std::string inner,
3535-
clang::QualType pointee_type) {
3536-
bool wrap = autoref_receiver_;
3537-
bool wrap_mut = autoref_receiver_mut_;
3538-
autoref_receiver_ = false;
3533+
void Converter::EmitDeref(std::string inner, clang::QualType pointee_type) {
3534+
auto wrap = std::exchange(autoref_mut_, std::nullopt);
3535+
PushParen outer(*this, wrap.has_value());
35393536
if (wrap) {
3540-
StrCat("(", wrap_mut ? "&mut " : "&");
3541-
}
3542-
{
3543-
PushParen paren(*this);
3544-
StrCat(GetPointerDerefPrefix(pointee_type), std::move(inner));
3545-
}
3546-
if (wrap) {
3547-
StrCat(")");
3537+
StrCat(*wrap ? "&mut" : "&");
35483538
}
3539+
PushParen paren(*this);
3540+
StrCat(GetPointerDerefPrefix(pointee_type), std::move(inner));
35493541
}
35503542

35513543
void Converter::ConvertDeref(clang::Expr *expr) {
35523544
if (!isAddrOf()) {
3553-
EmitMaybeWrappedDeref(ToString(expr), expr->getType()->getPointeeType());
3545+
EmitDeref(ToString(expr), expr->getType()->getPointeeType());
35543546
} else {
35553547
Convert(expr);
35563548
}

cpp2rust/converter/converter.h

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
474474

475475
virtual void ConvertDeref(clang::Expr *expr);
476476

477-
void EmitMaybeWrappedDeref(std::string inner, clang::QualType pointee_type);
477+
void EmitDeref(std::string inner, clang::QualType pointee_type);
478478

479479
virtual void ConvertArrow(clang::Expr *expr);
480480

@@ -517,23 +517,16 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
517517
clang::ASTContext &ctx_;
518518
clang::FunctionDecl *curr_function_ = nullptr;
519519
bool in_function_formals_ = false;
520-
bool autoref_receiver_ = false;
521-
bool autoref_receiver_mut_ = false;
520+
std::optional<bool> autoref_mut_;
522521

523-
struct PushAutorefReceiver {
522+
struct PushExplicitAutoref {
524523
Converter &c;
525-
bool prev_active;
526-
bool prev_mut;
527-
PushAutorefReceiver(Converter &c, bool is_mut)
528-
: c(c), prev_active(c.autoref_receiver_),
529-
prev_mut(c.autoref_receiver_mut_) {
530-
c.autoref_receiver_ = true;
531-
c.autoref_receiver_mut_ = is_mut;
532-
}
533-
~PushAutorefReceiver() {
534-
c.autoref_receiver_ = prev_active;
535-
c.autoref_receiver_mut_ = prev_mut;
524+
std::optional<bool> prev;
525+
PushExplicitAutoref(Converter &c, std::optional<bool> v)
526+
: c(c), prev(c.autoref_mut_) {
527+
c.autoref_mut_ = v;
536528
}
529+
~PushExplicitAutoref() { c.autoref_mut_ = prev; }
537530
};
538531
std::stack<clang::Expr *> curr_for_inc_;
539532
std::stack<clang::QualType> curr_init_type_;

tests/lit/lit/formats/Cpp2RustTest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self):
2929
self.regex_translation_fail = re.compile(r"//\s*translation-fail\s*(?::\s*(.*))?$", re.MULTILINE)
3030
self.regex_nondet_result = re.compile(r"//\s*nondet-result\s*(?::\s*(.*))?$", re.MULTILINE)
3131
self.rust_version = read_rust_version()
32-
os.environ['RUSTFLAGS'] = '-Awarnings -A dangerous-implicit-autorefs'
32+
os.environ['RUSTFLAGS'] = '-Awarnings'
3333

3434
def updateExpected(self, generated, expected_path):
3535
os.makedirs(os.path.dirname(expected_path), exist_ok=True)

tests/unit/out/unsafe/vector2.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ pub unsafe fn fn_0(v: *mut Vec<i32>, mut v3: Vec<i32>) {
1414
v2.push(0);
1515
v2.push(1);
1616
v2.push(3);
17-
x = (*v)[(2_u64) as usize];
17+
x = (&mut (*v))[(2_u64) as usize];
1818
v2[(0_u64) as usize] = 1;
1919
(if true { &mut v3 } else { &mut (*v) })[(0_u64) as usize] = 7;
2020
v2 = (*v).clone();
21-
(*v4)[(1_u64) as usize] = 13;
21+
(&mut (*v4))[(1_u64) as usize] = 13;
2222
assert!(((x) == (6)));
2323
assert!(((*((*v).first_mut().unwrap())) == (4)));
24-
assert!((((*v)[(1_u64) as usize]) == (5)));
25-
assert!((((*v)[(2_u64) as usize]) == (6)));
24+
assert!((((&mut (*v))[(1_u64) as usize]) == (5)));
25+
assert!((((&mut (*v))[(2_u64) as usize]) == (6)));
2626
assert!(((*((*v).last_mut().unwrap())) == (20)));
2727
assert!(((v2[(0_u64) as usize]) == (4)));
2828
assert!(((v2[(1_u64) as usize]) == (5)));

tests/unit/out/unsafe/vector3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ unsafe fn main_0() -> i32 {
3535
'loop_: for v2 in 0..(v.len()) {
3636
let mut v2 = v.as_mut_ptr().add(v2);
3737
'loop_: for i in 0..((*v2).len()) {
38-
let mut i = (*v2)[i].clone();
38+
let mut i = (&(*v2))[i].clone();
3939
printf(b"%d\n\0".as_ptr() as *const i8, ((i) + (3)));
4040
}
4141
}

0 commit comments

Comments
 (0)