Skip to content

Commit 1ee81b4

Browse files
authored
Fix bool vector indexing (#573)
1 parent a908bf0 commit 1ee81b4

4 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎cpp2rust/converter/rules/expr_matcher.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <clang/AST/DeclCXX.h>
77
#include <clang/AST/DeclTemplate.h>
88
#include <clang/AST/ExprCXX.h>
9+
#include <clang/AST/IgnoreExpr.h>
910
#include <clang/Basic/OperatorKinds.h>
1011
#include <clang/Lex/Lexer.h>
1112
#include <clang/Sema/EnterExpressionEvaluationContext.h>
@@ -160,7 +161,9 @@ std::vector<clang::Expr *> getOperands(clang::ASTContext &ctx,
160161
operands.assign(op->arg_begin(), op->arg_end());
161162
if (!operands.empty() &&
162163
llvm::isa_and_nonnull<clang::CXXMethodDecl>(op->getDirectCallee())) {
163-
operands[0] = operands[0]->IgnoreParenImpCasts();
164+
operands[0] =
165+
clang::IgnoreExprNodes(operands[0], clang::IgnoreParensSingleStep,
166+
clang::IgnoreImplicitCastsSingleStep);
164167
}
165168
} else if (auto call = llvm::dyn_cast<clang::CXXMemberCallExpr>(expr)) {
166169
operands.push_back(getObject(

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ fn main_0() -> i32 {
2626
(r2).clone()
2727
};
2828
assert!(v1.decay().offset(0_usize as isize).read());
29+
{
30+
v1.decay().offset(0_usize as isize).write(false);
31+
v1.decay().offset(0_usize as isize)
32+
};
33+
assert!(!(v1.decay().offset(0_usize as isize).read()));
2934
return 0;
3035
}
3136
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ unsafe fn main_0() -> i32 {
2828
r2
2929
};
3030
assert!(*(&mut (v1)[0_usize as usize] as *mut bool));
31+
{
32+
*(&mut (v1)[0_usize as usize] as *mut bool) = false;
33+
(&mut (v1)[0_usize as usize] as *mut bool)
34+
};
35+
assert!(!(*(&mut (v1)[0_usize as usize] as *mut bool)));
3136
return 0;
3237
}
3338
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/vector_bool_reference.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ int main() {
1111
auto r2 = *it;
1212
r2 = true;
1313
assert(v1[0]);
14+
v1[0] = false;
15+
assert(!v1[0]);
1416
return 0;
1517
}

0 commit comments

Comments
 (0)