From f7257fbfb858f7adcb1259f96beea35dcffc4bda Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 15:58:06 +0000 Subject: [PATCH 1/3] Pass pointer arguments bound to reference rule parameters by address --- cpp2rust/converter/converter.cpp | 12 ++--- cpp2rust/converter/models/boxed_vars.cpp | 4 +- cpp2rust/converter/rules/registry.cpp | 18 ++++++++ cpp2rust/converter/rules/registry.h | 5 +++ cpp2rust/converter/translation_rule.cpp | 15 ++++--- cpp2rust/converter/translation_rule.h | 6 +-- .../algorithms/alg.sorting/alg.min.max.cpp | 31 +++++++++++++ .../containers/associative/map/map.access.cpp | 44 +++++++++++++++++++ tests/std/utilities/utility/utility.swap.cpp | 23 ++++++++++ 9 files changed, 143 insertions(+), 15 deletions(-) create mode 100644 tests/std/algorithms/alg.sorting/alg.min.max.cpp create mode 100644 tests/std/containers/associative/map/map.access.cpp create mode 100644 tests/std/utilities/utility/utility.swap.cpp diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index d1a32a9e08..f742b71f61 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -5560,7 +5560,8 @@ std::string Converter::GetMappedAsString(clang::Expr *expr, clang::Expr **args, // The rule takes the value of a transparent union. auto *arg = IgnoreTransparentUnion(all_args[i]); auto type = arg->getType(); - bool by_value = arg->isPRValue() && !tgt_ir->takesAddress(ctx_, i, arg); + bool by_value = + arg->isPRValue() && !tgt_ir->takesAddress(ctx_, expr, i, arg); if (!by_value && arg->isPRValue()) { arg = new (ctx_) clang::MaterializeTemporaryExpr(type, arg, false); } @@ -5655,7 +5656,9 @@ std::string Converter::ConvertIRFragment( assert(arg_idx < all_args.size()); auto *arg = all_args[arg_idx]; bool is_receiver = HasReceiver(expr) && arg_idx == 0; - bool is_cpp_ptr = arg->getType()->isPointerType(); + bool cpp_ptr = arg->getType()->isPointerType(); + bool is_cpp_ptr = + cpp_ptr && RuleRegistry::ParamTakesPointerValue(expr, arg_idx); PlaceholderCtx ph_ctx{ .arg_idx = arg_idx, @@ -5668,9 +5671,8 @@ std::string Converter::ConvertIRFragment( .is_cpp_ptr = is_cpp_ptr, // Whatever its spelling, a pointer in C++ is converted as one. .maps_to_rust_ptr = - !is_cpp_ptr && - RuleRegistry::MapsToPointer( - ctx_, arg->IgnoreParenNoopCasts(ctx_)->getType()), + !cpp_ptr && RuleRegistry::MapsToPointer( + ctx_, arg->IgnoreParenNoopCasts(ctx_)->getType()), .declared_in_rule_as_rust_ptr = RuleRegistry::ParamIsPointer(ctx_, expr, arg_idx), .is_index_base = ph->is_index_base, diff --git a/cpp2rust/converter/models/boxed_vars.cpp b/cpp2rust/converter/models/boxed_vars.cpp index 3934c5a434..1cce4c1a4f 100644 --- a/cpp2rust/converter/models/boxed_vars.cpp +++ b/cpp2rust/converter/models/boxed_vars.cpp @@ -293,7 +293,7 @@ class AddressTakenVisitor // borrowed, unless the rule takes a pointer to them, other than to a // temporary copy of them, or they are bound to references. A pointer // parameter receives the value of an argument that is itself translated to a - // pointer. + // pointer, unless the rule binds a reference to that argument. void AddRuleArgs(clang::Expr *expr, clang::Expr **args, unsigned num_args) { auto *rule = RuleRegistry::GetExprRule(ctx_, expr); if (!rule) { @@ -317,7 +317,7 @@ class AddressTakenVisitor TraverseStmt(init); continue; } - if (!rule->takesAddress(ctx_, i, arg) || + if (!rule->takesAddress(ctx_, expr, i, arg) || rule->takesTemporaryAddress(ctx_, expr, i, arg)) { AddValueUse(arg); } diff --git a/cpp2rust/converter/rules/registry.cpp b/cpp2rust/converter/rules/registry.cpp index ef867d4b97..059acbe475 100644 --- a/cpp2rust/converter/rules/registry.cpp +++ b/cpp2rust/converter/rules/registry.cpp @@ -562,6 +562,24 @@ bool ParamIsPointer(clang::ASTContext &ctx, const clang::Expr *expr, return rule->params.at(index).is_pointer(); } +bool ParamTakesPointerValue(const clang::Expr *expr, unsigned index) { + if (HasReceiver(const_cast(expr)) && index == 0) { + return true; + } + auto match = findExpr(expr); + assert(match.rule_decl && "expression must have a translation rule"); + auto decl = match.rule_decl->decl; + auto function = decl->getAsFunction(); + auto type = + function ? function->getType() + : llvm::cast(decl)->getType()->getPointeeType(); + auto proto = type->getAs(); + assert(proto && "expression rule is neither a function nor a function " + "pointer"); + return index >= proto->getNumParams() || + !proto->getParamType(index)->isReferenceType(); +} + clang::Expr *BuildParamConversion(clang::ASTContext &ctx, const clang::Expr *expr, unsigned index, clang::Expr *arg) { diff --git a/cpp2rust/converter/rules/registry.h b/cpp2rust/converter/rules/registry.h index f25617ad4b..d3d39c7be3 100644 --- a/cpp2rust/converter/rules/registry.h +++ b/cpp2rust/converter/rules/registry.h @@ -57,6 +57,11 @@ bool ReturnsReference(const clang::Expr *expr); bool ParamIsPointer(clang::ASTContext &ctx, const clang::Expr *expr, unsigned index); +// A pointer argument is passed to a pointer parameter as its value, unless +// the rule's C++ parameter is a reference, which is bound to the argument +// itself; the receiver of a call through a pointer is that pointer. +bool ParamTakesPointerValue(const clang::Expr *expr, unsigned index); + clang::Expr *BuildParamConversion(clang::ASTContext &ctx, const clang::Expr *expr, unsigned index, clang::Expr *arg); diff --git a/cpp2rust/converter/translation_rule.cpp b/cpp2rust/converter/translation_rule.cpp index bc82a38bd3..ee304a6f95 100644 --- a/cpp2rust/converter/translation_rule.cpp +++ b/cpp2rust/converter/translation_rule.cpp @@ -332,14 +332,19 @@ ExprRule::argsToBind(clang::ASTContext &ctx, const clang::Expr *expr, return bind; } -bool ExprRule::takesAddress(clang::ASTContext &ctx, unsigned i, - const clang::Expr *arg) const { +bool ExprRule::takesAddress(clang::ASTContext &ctx, const clang::Expr *expr, + unsigned i, const clang::Expr *arg) const { // An init pack forwards references to its arguments, not their values. if (i >= params.size()) { return usesInit(); } - return params[i].is_pointer() && !arg->getType()->isPointerType() && - !RuleRegistry::MapsToPointer(ctx, arg->getType()); + if (!params[i].is_pointer()) { + return false; + } + if (arg->getType()->isPointerType()) { + return !RuleRegistry::ParamTakesPointerValue(expr, i); + } + return !RuleRegistry::MapsToPointer(ctx, arg->getType()); } bool ExprRule::takesTemporaryAddress(clang::ASTContext &ctx, @@ -347,7 +352,7 @@ bool ExprRule::takesTemporaryAddress(clang::ASTContext &ctx, const clang::Expr *arg) const { auto *call = clang::dyn_cast(expr); unsigned first_arg = clang::isa(expr); - return call && i >= first_arg && takesAddress(ctx, i, arg) && + return call && i >= first_arg && takesAddress(ctx, expr, i, arg) && GetRefBindingTempType(ctx, call, i - first_arg).has_value(); } diff --git a/cpp2rust/converter/translation_rule.h b/cpp2rust/converter/translation_rule.h index d5be83aa90..99ebaae628 100644 --- a/cpp2rust/converter/translation_rule.h +++ b/cpp2rust/converter/translation_rule.h @@ -138,9 +138,9 @@ struct ExprRule { std::vector argsToBind(clang::ASTContext &ctx, const clang::Expr *expr, llvm::ArrayRef args) const; - // Whether the body takes a pointer to arg, its argument i, which is not a - // pointer. - bool takesAddress(clang::ASTContext &ctx, unsigned i, + // Whether the body takes a pointer to arg, argument i of expr, rather than + // its value. + bool takesAddress(clang::ASTContext &ctx, const clang::Expr *expr, unsigned i, const clang::Expr *arg) const; // Whether the body takes a pointer to a temporary copy of arg, argument i diff --git a/tests/std/algorithms/alg.sorting/alg.min.max.cpp b/tests/std/algorithms/alg.sorting/alg.min.max.cpp new file mode 100644 index 0000000000..cbeb42cc88 --- /dev/null +++ b/tests/std/algorithms/alg.sorting/alg.min.max.cpp @@ -0,0 +1,31 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include + +// N4950 [alg.min.max]/2: min returns the smaller value. +namespace p2_min_pointers { +void test() { + int values[2] = {2, 1}; + int *low = &values[0]; + int *high = &values[1]; + assert(std::min(low, high) == low); + assert(std::min(high, low) == low); +} +} // namespace p2_min_pointers + +// N4950 [alg.min.max]/10: max returns the larger value. +namespace p10_max_pointers { +void test() { + int values[2] = {2, 1}; + int *low = &values[0]; + int *high = &values[1]; + assert(std::max(low, high) == high); + assert(std::max(high, low) == high); +} +} // namespace p10_max_pointers + +int main() { + p2_min_pointers::test(); + p10_max_pointers::test(); + return 0; +} diff --git a/tests/std/containers/associative/map/map.access.cpp b/tests/std/containers/associative/map/map.access.cpp new file mode 100644 index 0000000000..f9f4126789 --- /dev/null +++ b/tests/std/containers/associative/map/map.access.cpp @@ -0,0 +1,44 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include + +// N4950 [map.access]/1: operator[](const key_type& x) is equivalent to +// try_emplace(x).first->second. +namespace p1_subscript_pointer_lvalue { +void test() { + int first = 0, second = 0; + std::map counts; + int *key = &first; + counts[key] = 1; + key = &second; + counts[key] = 2; + counts[key] += 3; + assert(counts.size() == 2); + assert(counts[key] == 5); + key = &first; + assert(counts[key] == 1); + assert(first == 0 && second == 0); +} +} // namespace p1_subscript_pointer_lvalue + +// N4950 [map.access]/2: operator[](key_type&& x) is equivalent to +// try_emplace(std::move(x)).first->second. +namespace p2_subscript_pointer_rvalue { +void test() { + int first = 0, second = 0; + std::map counts; + counts[&first] = 1; + counts[&second] = 2; + counts[&second] += 3; + assert(counts.size() == 2); + assert(counts[&first] == 1); + assert(counts[&second] == 5); + assert(first == 0 && second == 0); +} +} // namespace p2_subscript_pointer_rvalue + +int main() { + p1_subscript_pointer_lvalue::test(); + p2_subscript_pointer_rvalue::test(); + return 0; +} diff --git a/tests/std/utilities/utility/utility.swap.cpp b/tests/std/utilities/utility/utility.swap.cpp new file mode 100644 index 0000000000..0b530618c8 --- /dev/null +++ b/tests/std/utilities/utility/utility.swap.cpp @@ -0,0 +1,23 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include + +// N4950 [utility.swap]/3: swap(a, b) exchanges the values stored in two +// locations. +namespace p3_swap_pointers { +void test() { + int first = 1, second = 2; + int *p = &first, *q = &second; + std::swap(p, q); + assert(p == &second && q == &first); + assert(first == 1 && second == 2); + int *null = nullptr; + std::swap(null, p); + assert(null == &second && p == nullptr); +} +} // namespace p3_swap_pointers + +int main() { + p3_swap_pointers::test(); + return 0; +} From ccaea334151359e7bfc3a64c84d9daabe7989e92 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 21:17:16 +0000 Subject: [PATCH 2/3] Update tests --- .../algorithms/alg.sorting/alg.min.max.cpp | 31 ------------- .../containers/associative/map/map.access.cpp | 44 ------------------- tests/std/utilities/utility/utility.swap.cpp | 23 ---------- tests/unit/map.cpp | 15 +++++++ tests/unit/min_max_ptr2ptr.cpp | 8 ++++ tests/unit/swap_malloc.cpp | 9 ++++ 6 files changed, 32 insertions(+), 98 deletions(-) delete mode 100644 tests/std/algorithms/alg.sorting/alg.min.max.cpp delete mode 100644 tests/std/containers/associative/map/map.access.cpp delete mode 100644 tests/std/utilities/utility/utility.swap.cpp diff --git a/tests/std/algorithms/alg.sorting/alg.min.max.cpp b/tests/std/algorithms/alg.sorting/alg.min.max.cpp deleted file mode 100644 index cbeb42cc88..0000000000 --- a/tests/std/algorithms/alg.sorting/alg.min.max.cpp +++ /dev/null @@ -1,31 +0,0 @@ -// ADDITIONAL_COMPILE_FLAGS: -std=c++23 -#include -#include - -// N4950 [alg.min.max]/2: min returns the smaller value. -namespace p2_min_pointers { -void test() { - int values[2] = {2, 1}; - int *low = &values[0]; - int *high = &values[1]; - assert(std::min(low, high) == low); - assert(std::min(high, low) == low); -} -} // namespace p2_min_pointers - -// N4950 [alg.min.max]/10: max returns the larger value. -namespace p10_max_pointers { -void test() { - int values[2] = {2, 1}; - int *low = &values[0]; - int *high = &values[1]; - assert(std::max(low, high) == high); - assert(std::max(high, low) == high); -} -} // namespace p10_max_pointers - -int main() { - p2_min_pointers::test(); - p10_max_pointers::test(); - return 0; -} diff --git a/tests/std/containers/associative/map/map.access.cpp b/tests/std/containers/associative/map/map.access.cpp deleted file mode 100644 index f9f4126789..0000000000 --- a/tests/std/containers/associative/map/map.access.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// ADDITIONAL_COMPILE_FLAGS: -std=c++23 -#include -#include - -// N4950 [map.access]/1: operator[](const key_type& x) is equivalent to -// try_emplace(x).first->second. -namespace p1_subscript_pointer_lvalue { -void test() { - int first = 0, second = 0; - std::map counts; - int *key = &first; - counts[key] = 1; - key = &second; - counts[key] = 2; - counts[key] += 3; - assert(counts.size() == 2); - assert(counts[key] == 5); - key = &first; - assert(counts[key] == 1); - assert(first == 0 && second == 0); -} -} // namespace p1_subscript_pointer_lvalue - -// N4950 [map.access]/2: operator[](key_type&& x) is equivalent to -// try_emplace(std::move(x)).first->second. -namespace p2_subscript_pointer_rvalue { -void test() { - int first = 0, second = 0; - std::map counts; - counts[&first] = 1; - counts[&second] = 2; - counts[&second] += 3; - assert(counts.size() == 2); - assert(counts[&first] == 1); - assert(counts[&second] == 5); - assert(first == 0 && second == 0); -} -} // namespace p2_subscript_pointer_rvalue - -int main() { - p1_subscript_pointer_lvalue::test(); - p2_subscript_pointer_rvalue::test(); - return 0; -} diff --git a/tests/std/utilities/utility/utility.swap.cpp b/tests/std/utilities/utility/utility.swap.cpp deleted file mode 100644 index 0b530618c8..0000000000 --- a/tests/std/utilities/utility/utility.swap.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// ADDITIONAL_COMPILE_FLAGS: -std=c++23 -#include -#include - -// N4950 [utility.swap]/3: swap(a, b) exchanges the values stored in two -// locations. -namespace p3_swap_pointers { -void test() { - int first = 1, second = 2; - int *p = &first, *q = &second; - std::swap(p, q); - assert(p == &second && q == &first); - assert(first == 1 && second == 2); - int *null = nullptr; - std::swap(null, p); - assert(null == &second && p == nullptr); -} -} // namespace p3_swap_pointers - -int main() { - p3_swap_pointers::test(); - return 0; -} diff --git a/tests/unit/map.cpp b/tests/unit/map.cpp index caa288ecd8..5a330ae8c7 100644 --- a/tests/unit/map.cpp +++ b/tests/unit/map.cpp @@ -147,5 +147,20 @@ int main() { k = 0; const auto &value_0 = m.at(k); assert(m.size() + x1 + x2 + x3 + x4 + x5 + value_0 == 21); + + int pk1 = 0, pk2 = 0; + std::map ptr_counts; + int *ptr_key = &pk1; + ptr_counts[ptr_key] = 1; + ptr_key = &pk2; + ptr_counts[ptr_key] = 2; + ptr_counts[ptr_key] += 3; + assert(ptr_counts.size() == 2); + assert(ptr_counts[ptr_key] == 5); + ptr_key = &pk1; + assert(ptr_counts[ptr_key] == 1); + ptr_counts[&pk2] += 1; + assert(ptr_counts[&pk2] == 6); + assert(pk1 == 0 && pk2 == 0); return 0; } diff --git a/tests/unit/min_max_ptr2ptr.cpp b/tests/unit/min_max_ptr2ptr.cpp index fbd0523ce8..f30ebd4b93 100644 --- a/tests/unit/min_max_ptr2ptr.cpp +++ b/tests/unit/min_max_ptr2ptr.cpp @@ -9,5 +9,13 @@ int main() { int r1 = std::max(**ppa, **ppb); int r2 = std::min(**ppa, **ppb); assert(r1 + r2 == 30); + + int values[2] = {2, 1}; + int *low = &values[0]; + int *high = &values[1]; + assert(std::min(low, high) == low); + assert(std::min(high, low) == low); + assert(std::max(low, high) == high); + assert(std::max(high, low) == high); return 0; } diff --git a/tests/unit/swap_malloc.cpp b/tests/unit/swap_malloc.cpp index eb6515e014..807fee5694 100644 --- a/tests/unit/swap_malloc.cpp +++ b/tests/unit/swap_malloc.cpp @@ -32,6 +32,15 @@ int main() { std::swap(halves[0], halves[3]); assert(words[0] == 0x01020506 && words[1] == 0x03040708); + int s1 = 1, s2 = 2; + int *sp = &s1, *sq = &s2; + std::swap(sp, sq); + assert(sp == &s2 && sq == &s1); + assert(s1 == 1 && s2 == 2); + int *null_ptr = nullptr; + std::swap(null_ptr, sp); + assert(null_ptr == &s2 && sp == nullptr); + free(other); free(buf); return 0; From d21a54a2fa93a7bc94012a0114f66593102ede44 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 21:20:42 +0000 Subject: [PATCH 3/3] Update tests --- cpp2rust/converter/rules/registry.h | 3 -- tests/unit/out/refcount/map.rs | 34 +++++++++++++++ tests/unit/out/refcount/min_max_ptr2ptr.rs | 51 ++++++++++++++++++++++ tests/unit/out/refcount/swap_malloc.rs | 20 +++++++++ tests/unit/out/unsafe/map.rs | 18 ++++++++ tests/unit/out/unsafe/min_max_ptr2ptr.rs | 31 +++++++++++++ tests/unit/out/unsafe/swap_malloc.rs | 10 +++++ 7 files changed, 164 insertions(+), 3 deletions(-) diff --git a/cpp2rust/converter/rules/registry.h b/cpp2rust/converter/rules/registry.h index d3d39c7be3..441080387f 100644 --- a/cpp2rust/converter/rules/registry.h +++ b/cpp2rust/converter/rules/registry.h @@ -57,9 +57,6 @@ bool ReturnsReference(const clang::Expr *expr); bool ParamIsPointer(clang::ASTContext &ctx, const clang::Expr *expr, unsigned index); -// A pointer argument is passed to a pointer parameter as its value, unless -// the rule's C++ parameter is a reference, which is bound to the argument -// itself; the receiver of a call through a pointer is that pointer. bool ParamTakesPointerValue(const clang::Expr *expr, unsigned index); clang::Expr *BuildParamConversion(clang::ASTContext &ctx, diff --git a/tests/unit/out/refcount/map.rs b/tests/unit/out/refcount/map.rs index c4ed1ab17e..18a58fc26d 100644 --- a/tests/unit/out/refcount/map.rs +++ b/tests/unit/out/refcount/map.rs @@ -758,6 +758,40 @@ fn main_0() -> i32 { == 21_usize) }) ); + let pk1: Value = Rc::new(RefCell::new(0)); + let pk2: Value = Rc::new(RefCell::new(0)); + let ptr_counts: Value, (Value>, Value)>> = + Rc::new(RefCell::new(Map::new())); + let ptr_key: Value> = Rc::new(RefCell::new((pk1.as_pointer()))); + Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()).write(1); + (*ptr_key.borrow_mut()) = (pk2.as_pointer()); + Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()).write(2); + { + Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()) + .with_mut(|__v| *__v = *__v + 3) + }; + assert!(({ ((*ptr_counts.borrow()).len() == 2_usize) })); + assert!(((Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()).read()) == 5)); + (*ptr_key.borrow_mut()) = (pk1.as_pointer()); + assert!(((Map::subscript(ptr_counts.as_pointer(), ptr_key.as_pointer()).read()) == 1)); + { + Map::subscript_move( + ptr_counts.as_pointer(), + Rc::new(RefCell::new((pk2.as_pointer()))).as_pointer(), + |__k| cpp_move!(__k), + ) + .with_mut(|__v| *__v = *__v + 1) + }; + assert!( + ((Map::subscript_move( + ptr_counts.as_pointer(), + Rc::new(RefCell::new((pk2.as_pointer()))).as_pointer(), + |__k| cpp_move!(__k) + ) + .read()) + == 6) + ); + assert!(({ ((*pk1.borrow()) == 0) && ((*pk2.borrow()) == 0) })); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/min_max_ptr2ptr.rs b/tests/unit/out/refcount/min_max_ptr2ptr.rs index 2d1445b659..0008a79e02 100644 --- a/tests/unit/out/refcount/min_max_ptr2ptr.rs +++ b/tests/unit/out/refcount/min_max_ptr2ptr.rs @@ -30,6 +30,57 @@ fn main_0() -> i32 { } .read()); assert!(((r1 + r2) == 30)); + let values: Value> = Rc::new(RefCell::new(Box::new([2, 1]))); + let low: Value> = Rc::new(RefCell::new((values.decay().offset(0)))); + let high: Value> = Rc::new(RefCell::new((values.decay().offset(1)))); + assert!( + ({ + ({ + (if CppLess::less(&high.as_pointer(), &low.as_pointer()) { + high.as_pointer() + } else { + low.as_pointer() + } + .read()) + } == { (*low.borrow()).clone() }) + }) + ); + assert!( + ({ + ({ + (if CppLess::less(&low.as_pointer(), &high.as_pointer()) { + low.as_pointer() + } else { + high.as_pointer() + } + .read()) + } == { (*low.borrow()).clone() }) + }) + ); + assert!( + ({ + ({ + (if CppLess::less(&low.as_pointer(), &high.as_pointer()) { + high.as_pointer() + } else { + low.as_pointer() + } + .read()) + } == { (*high.borrow()).clone() }) + }) + ); + assert!( + ({ + ({ + (if CppLess::less(&high.as_pointer(), &low.as_pointer()) { + low.as_pointer() + } else { + high.as_pointer() + } + .read()) + } == { (*high.borrow()).clone() }) + }) + ); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/swap_malloc.rs b/tests/unit/out/refcount/swap_malloc.rs index 5b1fa7ac6d..41675a026d 100644 --- a/tests/unit/out/refcount/swap_malloc.rs +++ b/tests/unit/out/refcount/swap_malloc.rs @@ -48,6 +48,26 @@ fn main_0() -> i32 { && ((*words.borrow())[(1) as usize] == 50595592_u32) }) ); + let s1: Value = Rc::new(RefCell::new(1)); + let s2: Value = Rc::new(RefCell::new(2)); + let sp: Value> = Rc::new(RefCell::new((s1.as_pointer()))); + let sq: Value> = Rc::new(RefCell::new((s2.as_pointer()))); + cpp_swap!(sp.as_pointer(), sq.as_pointer()); + assert!( + ({ + ({ (*sp.borrow()).clone() } == { (s2.as_pointer()) }) + && ({ (*sq.borrow()).clone() } == { (s1.as_pointer()) }) + }) + ); + assert!(({ ((*s1.borrow()) == 1) && ((*s2.borrow()) == 2) })); + let null_ptr: Value> = Rc::new(RefCell::new(Ptr::::null())); + cpp_swap!(null_ptr.as_pointer(), sp.as_pointer()); + assert!( + ({ + ({ (*null_ptr.borrow()).clone() } == { (s2.as_pointer()) }) + && ((*sp.borrow()).is_null()) + }) + ); libcc2rs::free_refcount((other).to_any()); libcc2rs::free_refcount((buf).to_any()); return 0; diff --git a/tests/unit/out/unsafe/map.rs b/tests/unit/out/unsafe/map.rs index 1d21b475ef..ab736789ea 100644 --- a/tests/unit/out/unsafe/map.rs +++ b/tests/unit/out/unsafe/map.rs @@ -239,6 +239,24 @@ unsafe fn main_0() -> i32 { .wrapping_add(((*value_0) as usize))) == (21_usize)) ); + let mut pk1: i32 = 0; + let mut pk2: i32 = 0; + let mut ptr_counts: UnsafeMap<*mut i32, (*mut i32, i32)> = UnsafeMap::new(); + let mut ptr_key: *mut i32 = (&mut pk1 as *mut i32); + (*ptr_counts.subscript(&ptr_key)) = 1; + ptr_key = (&mut pk2 as *mut i32); + (*ptr_counts.subscript(&ptr_key)) = 2; + (*ptr_counts.subscript(&ptr_key)) += 3; + assert!(((ptr_counts.len()) == (2_usize))); + assert!(((*ptr_counts.subscript(&ptr_key)) == (5))); + ptr_key = (&mut pk1 as *mut i32); + assert!(((*ptr_counts.subscript(&ptr_key)) == (1))); + (*ptr_counts.subscript_move(&mut (&mut pk2 as *mut i32), |__k| cpp_move_unsafe!(__k))) += 1; + assert!( + ((*ptr_counts.subscript_move(&mut (&mut pk2 as *mut i32), |__k| cpp_move_unsafe!(__k))) + == (6)) + ); + assert!(((pk1) == (0)) && ((pk2) == (0))); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/min_max_ptr2ptr.rs b/tests/unit/out/unsafe/min_max_ptr2ptr.rs index cb32ecdc9a..ee7b858bcd 100644 --- a/tests/unit/out/unsafe/min_max_ptr2ptr.rs +++ b/tests/unit/out/unsafe/min_max_ptr2ptr.rs @@ -30,6 +30,37 @@ unsafe fn main_0() -> i32 { if *__b < *__a { __b } else { __a } }); assert!((((r1) + (r2)) == (30))); + let mut values: [i32; 2] = [2, 1]; + let mut low: *mut i32 = (&mut values[(0) as usize] as *mut i32); + let mut high: *mut i32 = (&mut values[(1) as usize] as *mut i32); + assert!( + ((*{ + let __a: *const _ = &mut low; + let __b: *const _ = &mut high; + if *__b < *__a { __b } else { __a } + }) == (low)) + ); + assert!( + ((*{ + let __a: *const _ = &mut high; + let __b: *const _ = &mut low; + if *__b < *__a { __b } else { __a } + }) == (low)) + ); + assert!( + ((*{ + let __a: *const _ = &mut low; + let __b: *const _ = &mut high; + if *__a < *__b { __b } else { __a } + }) == (high)) + ); + assert!( + ((*{ + let __a: *const _ = &mut high; + let __b: *const _ = &mut low; + if *__a < *__b { __b } else { __a } + }) == (high)) + ); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/swap_malloc.rs b/tests/unit/out/unsafe/swap_malloc.rs index 57de449334..4a19284675 100644 --- a/tests/unit/out/unsafe/swap_malloc.rs +++ b/tests/unit/out/unsafe/swap_malloc.rs @@ -45,6 +45,16 @@ unsafe fn main_0() -> i32 { let mut halves: *mut u16 = (words.as_mut_ptr() as *mut u16); cpp_swap_unsafe!(halves.offset((0) as isize), halves.offset((3) as isize)); assert!(((words[(0) as usize]) == (16909574_u32)) && ((words[(1) as usize]) == (50595592_u32))); + let mut s1: i32 = 1; + let mut s2: i32 = 2; + let mut sp: *mut i32 = (&mut s1 as *mut i32); + let mut sq: *mut i32 = (&mut s2 as *mut i32); + cpp_swap_unsafe!(&mut sp, &mut sq); + assert!(((sp) == (&mut s2 as *mut i32)) && ((sq) == (&mut s1 as *mut i32))); + assert!(((s1) == (1)) && ((s2) == (2))); + let mut null_ptr: *mut i32 = std::ptr::null_mut(); + cpp_swap_unsafe!(&mut null_ptr, &mut sp); + assert!(((null_ptr) == (&mut s2 as *mut i32)) && ((sp).is_null())); libcc2rs::free_unsafe((other as *mut ::libc::c_void)); libcc2rs::free_unsafe((buf as *mut ::libc::c_void)); return 0;