diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 5ce8d433f4..7ddc0a8b6b 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1917,6 +1917,11 @@ bool Converter::ConvertSpecialCall(clang::CallExpr *expr) { return true; } + if (IsTransparentStdCall(ctx_, expr)) { + Convert(expr->getArg(0)); + return true; + } + // The remaining calls are converted as rules say, if there is one. if (Mapper::Contains(ctx_, expr)) { return false; @@ -1928,11 +1933,6 @@ bool Converter::ConvertSpecialCall(clang::CallExpr *expr) { return true; } - if (IsTransparentStdCall(expr)) { - Convert(expr->getArg(0)); - return true; - } - if (auto *opcall = clang::dyn_cast(expr); opcall && !IsUserOperatorCall(opcall)) { ConvertCXXOperatorCallExpr(opcall); @@ -3090,7 +3090,8 @@ void Converter::ConvertGenericBinaryOperator(clang::BinaryOperator *expr) { } bool Converter::IsReferenceType(const clang::Expr *expr) const { - const auto *e = IgnoreTransparentStdCall(expr->IgnoreCasts())->IgnoreCasts(); + const auto *e = + IgnoreTransparentStdCall(ctx_, expr->IgnoreCasts())->IgnoreCasts(); if (const auto *call = clang::dyn_cast(e)) { return !clang::isa(call) && GetReturnTypeOfFunction(call)->isReferenceType(); diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index f503959168..959cd4fcb8 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -2459,34 +2459,31 @@ std::optional GetOperandImplicitConversionTarget( return std::nullopt; } -bool IsTransparentStdCall(const clang::CallExpr *expr) { - const auto *callee = expr->getDirectCallee(); - if (!callee) { - return false; - } - switch (callee->getBuiltinID()) { - case clang::Builtin::BImove: - case clang::Builtin::BImove_if_noexcept: - case clang::Builtin::BIforward: - case clang::Builtin::BIforward_like: - case clang::Builtin::BIas_const: - return true; - default: +bool IsTransparentStdCall(clang::ASTContext &ctx, const clang::CallExpr *expr) { + auto *rule = RuleRegistry::GetExprRule(ctx, expr); + if (!rule || rule->body.size() != 1 || + !RuleRegistry::ReturnsReference(expr) || + clang::isa(expr) || expr->getNumArgs() == 0) { return false; } + auto *ph = std::get_if(&rule->body[0]); + return ph && ph->n == 0 && + ctx.hasSameUnqualifiedType(expr->getArg(0)->getType(), + expr->getType()); } -const clang::Expr *IgnoreTransparentStdCall(const clang::Expr *expr) { +const clang::Expr *IgnoreTransparentStdCall(clang::ASTContext &ctx, + const clang::Expr *expr) { if (const auto *call = clang::dyn_cast(expr->IgnoreParenImpCasts()); - call && IsTransparentStdCall(call)) { + call && IsTransparentStdCall(ctx, call)) { return call->getArg(0); } return expr; } -bool IsTemporaryObject(const clang::Expr *expr) { - const auto *operand = IgnoreTransparentStdCall(expr); +bool IsTemporaryObject(clang::ASTContext &ctx, const clang::Expr *expr) { + const auto *operand = IgnoreTransparentStdCall(ctx, expr); if (operand != expr) { return !operand->isGLValue(); } diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index 944316271a..5169caadaa 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -403,11 +403,12 @@ bool IsConstantEvaluatedQuery(const clang::CallExpr *expr); bool HasConstantEvaluatedInit(const clang::ASTContext &ctx, const clang::VarDecl *decl); -bool IsTransparentStdCall(const clang::CallExpr *expr); +bool IsTransparentStdCall(clang::ASTContext &ctx, const clang::CallExpr *expr); -const clang::Expr *IgnoreTransparentStdCall(const clang::Expr *expr); +const clang::Expr *IgnoreTransparentStdCall(clang::ASTContext &ctx, + const clang::Expr *expr); -bool IsTemporaryObject(const clang::Expr *expr); +bool IsTemporaryObject(clang::ASTContext &ctx, const clang::Expr *expr); bool ContainsVAArgExpr(const clang::Stmt *stmt); diff --git a/cpp2rust/converter/models/boxed_vars.cpp b/cpp2rust/converter/models/boxed_vars.cpp index 62fbaf87f2..938fbd1ee5 100644 --- a/cpp2rust/converter/models/boxed_vars.cpp +++ b/cpp2rust/converter/models/boxed_vars.cpp @@ -192,6 +192,9 @@ class AddressTakenVisitor } bool VisitCallExpr(clang::CallExpr *expr) { + if (IsTransparentStdCall(ctx_, expr)) { + return true; + } AddRuleArgs(expr, expr->getArgs(), expr->getNumArgs()); return true; } @@ -305,7 +308,7 @@ class AddressTakenVisitor } void VisitCallExpr(const clang::CallExpr *expr) { - if (IsTransparentStdCall(expr)) { + if (IsTransparentStdCall(owner_.ctx_, expr)) { Traverse(expr->getArg(0)); } else { VisitExpr(expr); @@ -392,7 +395,8 @@ class AddressTakenVisitor // An element of an array or vector, or a field of a struct, is accessed // without making a pointer to it. void AddValueUse(const clang::Expr *expr, bool write = false) { - expr = IgnoreNoOpCasts(IgnoreTransparentStdCall(expr)->IgnoreParens()); + expr = + IgnoreNoOpCasts(IgnoreTransparentStdCall(ctx_, expr)->IgnoreParens()); if (auto *op = clang::dyn_cast(expr); op && op->isPrefix() && op->isIncrementDecrementOp()) { AddValueUse(op->getSubExpr(), /*write=*/true); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index dd14dbc3eb..bc3979fadb 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -3165,7 +3165,7 @@ std::string ConverterRefCount::ConvertMappedMethodCall( auto arg_idx = receiver_ph->n; auto *arg = args[arg_idx]; if (auto *call = clang::dyn_cast(arg->IgnoreCasts()); - call && IsTransparentStdCall(call)) { + call && IsTransparentStdCall(ctx_, call)) { arg = call->getArg(0); } @@ -3249,7 +3249,7 @@ void ConverterRefCount::SetUFCSReceiver(clang::Expr *base, bool is_arrow, } return; } - if (IsTemporaryObject(base) && base->getType()->isRecordType() && + if (IsTemporaryObject(ctx_, base) && base->getType()->isRecordType() && !IsReferenceType(base->IgnoreImplicit())) { if (retain_bound_temporaries_ && full_expression_temporaries_) { ufcs_receiver_ = token::kRef + ConvertTemporaryPointer(base, {}); diff --git a/rules/move/src.cpp b/rules/move/src.cpp new file mode 100644 index 0000000000..187d73713a --- /dev/null +++ b/rules/move/src.cpp @@ -0,0 +1,16 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +#include + +template T1 &&f2(typename std::remove_reference::type &&a0) { + return std::forward(std::move(a0)); +} + +template T1 &&f3(typename std::remove_reference::type &a0) { + return std::forward(a0); +} + +template typename std::remove_reference::type &&f5(T1 &&a0) { + return std::move(std::forward(a0)); +} diff --git a/rules/move/src_cxx14.cpp b/rules/move/src_cxx14.cpp new file mode 100644 index 0000000000..12100c23d8 --- /dev/null +++ b/rules/move/src_cxx14.cpp @@ -0,0 +1,8 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +#include + +template const T1 &f6(T1 &a0) { + return std::move_if_noexcept(a0); +} diff --git a/rules/move/src_cxx23.cpp b/rules/move/src_cxx23.cpp new file mode 100644 index 0000000000..89922685f2 --- /dev/null +++ b/rules/move/src_cxx23.cpp @@ -0,0 +1,8 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +#include + +template auto &&f7(T2 &&a0) { + return std::forward_like(std::forward(a0)); +} diff --git a/rules/move/tgt_refcount.rs b/rules/move/tgt_refcount.rs new file mode 100644 index 0000000000..a13a1081f5 --- /dev/null +++ b/rules/move/tgt_refcount.rs @@ -0,0 +1,24 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use libcc2rs::*; + +fn f2(a0: Ptr) -> Ptr { + a0 +} + +fn f3(a0: Ptr) -> Ptr { + a0 +} + +fn f5(a0: Ptr) -> Ptr { + a0 +} + +fn f6(a0: Ptr) -> Ptr { + a0 +} + +fn f7(a0: Ptr) -> Ptr { + a0 +} diff --git a/rules/move/tgt_unsafe.rs b/rules/move/tgt_unsafe.rs new file mode 100644 index 0000000000..7be2b6a8a9 --- /dev/null +++ b/rules/move/tgt_unsafe.rs @@ -0,0 +1,22 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +unsafe fn f2(a0: *mut T1) -> *mut T1 { + a0 +} + +unsafe fn f3(a0: *mut T1) -> *mut T1 { + a0 +} + +unsafe fn f5(a0: *mut T1) -> *mut T1 { + a0 +} + +unsafe fn f6(a0: *const T1) -> *const T1 { + a0 +} + +unsafe fn f7(a0: *mut T2) -> *mut T2 { + a0 +} diff --git a/rules/utility/src_cxx17.cpp b/rules/utility/src_cxx17.cpp new file mode 100644 index 0000000000..6f2ad49a35 --- /dev/null +++ b/rules/utility/src_cxx17.cpp @@ -0,0 +1,9 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +#include +#include + +template std::add_const_t &f7(T1 &a0) { + return std::as_const(a0); +} diff --git a/rules/utility/tgt_refcount.rs b/rules/utility/tgt_refcount.rs new file mode 100644 index 0000000000..267dce1f5c --- /dev/null +++ b/rules/utility/tgt_refcount.rs @@ -0,0 +1,8 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use libcc2rs::*; + +fn f7(a0: Ptr) -> Ptr { + a0 +} diff --git a/rules/utility/tgt_unsafe.rs b/rules/utility/tgt_unsafe.rs new file mode 100644 index 0000000000..1595049e7c --- /dev/null +++ b/rules/utility/tgt_unsafe.rs @@ -0,0 +1,6 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +unsafe fn f7(a0: *const T1) -> *const T1 { + a0 +} diff --git a/tests/std/utilities/utility/forward-helpers.cpp b/tests/std/utilities/utility/forward-helpers.cpp new file mode 100644 index 0000000000..b5bfe03017 --- /dev/null +++ b/tests/std/utilities/utility/forward-helpers.cpp @@ -0,0 +1,136 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include +#include +#include + +// N4950 [forward]/3: forward(t) returns static_cast(t). +namespace p3_forward { +void test() { + int value = 1; + int forwarded = std::forward(std::move(value)); + assert(forwarded == 1); + int &lvalue = std::forward(value); + lvalue = 2; + assert(value == 2); +} +} // namespace p3_forward + +// N4950 [forward]/6: forward_like(x) forwards x as an rvalue when T is not +// an lvalue reference, and as an lvalue otherwise. +namespace p6_forward_like { +struct Movable { + int value; + Movable(int value) : value(value) {} + Movable(const Movable &other) : value(other.value) {} + Movable(Movable &&other) : value(other.value) { other.value = 0; } +}; + +struct Holder { + Movable member{1}; +}; + +template Movable take(Self &&self) { + return Movable(std::forward_like(self.member)); +} + +void test() { + Holder holder; + Movable copied = take(holder); + assert(copied.value == 1 && holder.member.value == 1); + Movable moved = take(std::move(holder)); + assert(moved.value == 1 && holder.member.value == 0); +} +} // namespace p6_forward_like + +// N4950 [forward]/9: move(t) returns static_cast&&>(t). +namespace p9_move { +struct Movable { + int value; + Movable(int value) : value(value) {} + Movable(Movable &&other) : value(other.value) { other.value = 0; } +}; + +void test() { + int number = 1; + int moved = std::move(number); + assert(moved == 1 && number == 1); + + int target = 2; + int *pointer = ⌖ + int *moved_pointer = std::move(pointer); + assert(moved_pointer == &target && pointer == &target); + + Movable source(3); + Movable destination = std::move(source); + assert(destination.value == 3 && source.value == 0); + + std::vector strings; + std::string text = "text"; + strings.push_back(std::move(text)); + assert(strings[0] == "text"); +} +} // namespace p9_move + +// N4950 [forward]/10: forward passes an lvalue to A(const A&), which copies, +// and an rvalue to A(A&&), which moves. +namespace p10_forward_in_template { +struct A { + int value; + int copies = 0; + int moves = 0; + A(int value) : value(value) {} + A(const A &other) : value(other.value), copies(other.copies + 1) {} + A(A &&other) : value(other.value), moves(other.moves + 1) { other.value = 0; } +}; + +template A make(T &&source) { return A(std::forward(source)); } + +void test() { + A a(1); + A copied = make(a); + assert(copied.value == 1 && copied.copies == 1 && copied.moves == 0); + assert(a.value == 1); + A moved = make(std::move(a)); + assert(moved.value == 1 && moved.copies == 0 && moved.moves == 1); + assert(a.value == 0); +} +} // namespace p10_forward_in_template + +// N4950 [forward]/11: move_if_noexcept(x) returns std::move(x), as a const +// lvalue when the move constructor may throw and there is a copy constructor. +namespace p11_move_if_noexcept { +struct NoexceptMove { + int value; + NoexceptMove(int value) : value(value) {} + NoexceptMove(const NoexceptMove &other) : value(other.value) {} + NoexceptMove(NoexceptMove &&other) noexcept : value(other.value) { + other.value = 0; + } +}; + +struct ThrowingMove { + int value; + ThrowingMove(int value) : value(value) {} + ThrowingMove(const ThrowingMove &other) : value(other.value) {} + ThrowingMove(ThrowingMove &&other) : value(other.value) { other.value = 0; } +}; + +void test() { + NoexceptMove moved_from(1); + NoexceptMove moved(std::move_if_noexcept(moved_from)); + assert(moved.value == 1 && moved_from.value == 0); + ThrowingMove copied_from(2); + ThrowingMove copied(std::move_if_noexcept(copied_from)); + assert(copied.value == 2 && copied_from.value == 2); +} +} // namespace p11_move_if_noexcept + +int main() { + p3_forward::test(); + p6_forward_like::test(); + p9_move::test(); + p10_forward_in_template::test(); + p11_move_if_noexcept::test(); + return 0; +} diff --git a/tests/std/utilities/utility/out/refcount/forward-helpers.rs b/tests/std/utilities/utility/out/refcount/forward-helpers.rs new file mode 100644 index 0000000000..1d0a87853c --- /dev/null +++ b/tests/std/utilities/utility/out/refcount/forward-helpers.rs @@ -0,0 +1,306 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn test_0() { + let value: Value = Rc::new(RefCell::new(1)); + let mut forwarded: i32 = (*value.borrow()); + assert!((forwarded == 1)); + let lvalue: Ptr = value.as_pointer(); + lvalue.write(2); + assert!(({ ((*value.borrow()) == 2) })); +} +#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct p6_forward_like_Movable { + #[offset(0)] + pub value: i32, +} +impl p6_forward_like_Movable { + pub fn new(mut value: i32) -> Self { + Self { value: value } + } + pub fn copy_from(other: Ptr) -> Self { + Self { + value: other.with(|__s| __s.value), + } + } + pub fn move_from(other: Ptr) -> Self { + let __this: p6_forward_like_Movable = Self { + value: other.with(|__s| __s.value), + }; + field!(other, value).write(0); + __this + } +} +impl Clone for p6_forward_like_Movable { + fn clone(&self) -> Self { + let __src: Value = + Rc::new(RefCell::new(p6_forward_like_Movable { + value: self.value.clone(), + })); + p6_forward_like_Movable::copy_from(__src.as_pointer()) + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor)] +#[byte_size(4)] +pub struct p6_forward_like_Holder { + #[offset(0)] + #[byte_size(4)] + pub member: p6_forward_like_Movable, +} +impl p6_forward_like_Holder { + pub fn move_from(_a0: Ptr) -> Self { + Self { + member: p6_forward_like_Movable::move_from({ field_ptr!(_a0, member) }), + } + } +} +impl Default for p6_forward_like_Holder { + fn default() -> Self { + { + p6_forward_like_Holder { + member: p6_forward_like_Movable::new({ 1 }), + } + } + } +} +pub fn take_1(self_: Ptr) -> p6_forward_like_Movable { + return p6_forward_like_Movable::copy_from({ field_ptr!(self_, member) }); +} +pub fn take_2(self_: Ptr) -> p6_forward_like_Movable { + return p6_forward_like_Movable::move_from({ field_ptr!(self_, member) }); +} +pub fn test_3() { + let holder: Value = + Rc::new(RefCell::new(::default())); + let mut copied: p6_forward_like_Movable = ({ take_1(holder.as_pointer()) }); + assert!(({ (copied.value == 1) && ({ (*holder.borrow()).member.value } == 1) })); + let mut moved: p6_forward_like_Movable = ({ take_2(holder.as_pointer()) }); + assert!(({ (moved.value == 1) && ({ (*holder.borrow()).member.value } == 0) })); +} +#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct p9_move_Movable { + #[offset(0)] + pub value: i32, +} +impl p9_move_Movable { + pub fn new(mut value: i32) -> Self { + Self { value: value } + } + pub fn move_from(other: Ptr) -> Self { + let __this: p9_move_Movable = Self { + value: other.with(|__s| __s.value), + }; + field!(other, value).write(0); + __this + } +} +pub fn test_4() { + let mut number: i32 = 1; + let mut moved: i32 = number; + assert!((moved == 1) && (number == 1)); + let target: Value = Rc::new(RefCell::new(2)); + let mut pointer: Ptr = (target.as_pointer()); + let mut moved_pointer: Ptr = (pointer).clone(); + assert!( + ({ (moved_pointer).clone() } == { (target.as_pointer()) }) + && ({ (pointer).clone() } == { (target.as_pointer()) }) + ); + let source: Value = Rc::new(RefCell::new(p9_move_Movable::new({ 3 }))); + let mut destination: p9_move_Movable = p9_move_Movable::move_from({ source.as_pointer() }); + assert!(({ (destination.value == 3) && ({ (*source.borrow()).value } == 0) })); + let strings: Value>> = Rc::new(RefCell::new(Vec::new())); + let mut text: Vec = { + let mut __bytes = Ptr::::from_string_literal(b"text").to_c_bytes(); + __bytes.push(0); + __bytes + }; + { + let __a1 = std::mem::replace(&mut text, vec![0]); + (*strings.borrow_mut()).push(__a1) + }; + assert!( + ({ + match (strings.decay().offset(0_usize),) { + (__tmp_0,) => Ptr::::from_string_literal(b"text").with_c_str(|__s| { + (*__tmp_0.upgrade().deref()) + [..(*__tmp_0.upgrade().deref()).len().saturating_sub(1)] + == *__s + }), + } + }) + ); +} +#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom)] +#[cpp_from(i32, new, value)] +#[byte_size(12)] +pub struct p10_forward_in_template_A { + #[offset(0)] + pub value: i32, + #[offset(4)] + pub copies: i32, + #[offset(8)] + pub moves: i32, +} +impl p10_forward_in_template_A { + pub fn new(mut value: i32) -> Self { + Self { + value: value, + copies: 0, + moves: 0, + } + } + pub fn copy_from(other: Ptr) -> Self { + Self { + value: other.with(|__s| __s.value), + copies: (other.with(|__s| __s.copies) + 1), + moves: 0, + } + } + pub fn move_from(other: Ptr) -> Self { + let __this: p10_forward_in_template_A = Self { + value: other.with(|__s| __s.value), + copies: 0, + moves: (other.with(|__s| __s.moves) + 1), + }; + field!(other, value).write(0); + __this + } +} +impl Clone for p10_forward_in_template_A { + fn clone(&self) -> Self { + let __src: Value = + Rc::new(RefCell::new(p10_forward_in_template_A { + value: self.value.clone(), + copies: self.copies.clone(), + moves: self.moves.clone(), + })); + p10_forward_in_template_A::copy_from(__src.as_pointer()) + } +} +impl Default for p10_forward_in_template_A { + fn default() -> Self { + { + p10_forward_in_template_A { + value: 0_i32, + copies: 0, + moves: 0, + } + } + } +} +pub fn make_5(source: Ptr) -> p10_forward_in_template_A { + return p10_forward_in_template_A::copy_from({ (source).clone() }); +} +pub fn make_6(source: Ptr) -> p10_forward_in_template_A { + return p10_forward_in_template_A::move_from({ (source).clone() }); +} +pub fn test_7() { + let a: Value = + Rc::new(RefCell::new(p10_forward_in_template_A::new({ 1 }))); + let mut copied: p10_forward_in_template_A = ({ make_5(a.as_pointer()) }); + assert!(((copied.value == 1) && (copied.copies == 1)) && (copied.moves == 0)); + assert!(({ ({ (*a.borrow()).value } == 1) })); + let mut moved: p10_forward_in_template_A = ({ make_6(a.as_pointer()) }); + assert!(((moved.value == 1) && (moved.copies == 0)) && (moved.moves == 1)); + assert!(({ ({ (*a.borrow()).value } == 0) })); +} +#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct p11_move_if_noexcept_NoexceptMove { + #[offset(0)] + pub value: i32, +} +impl p11_move_if_noexcept_NoexceptMove { + pub fn new(mut value: i32) -> Self { + Self { value: value } + } + pub fn copy_from(other: Ptr) -> Self { + Self { + value: other.with(|__s| __s.value), + } + } + pub fn move_from(other: Ptr) -> Self { + let __this: p11_move_if_noexcept_NoexceptMove = Self { + value: other.with(|__s| __s.value), + }; + field!(other, value).write(0); + __this + } +} +impl Clone for p11_move_if_noexcept_NoexceptMove { + fn clone(&self) -> Self { + let __src: Value = + Rc::new(RefCell::new(p11_move_if_noexcept_NoexceptMove { + value: self.value.clone(), + })); + p11_move_if_noexcept_NoexceptMove::copy_from(__src.as_pointer()) + } +} +#[derive(Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, CppFrom, Default)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct p11_move_if_noexcept_ThrowingMove { + #[offset(0)] + pub value: i32, +} +impl p11_move_if_noexcept_ThrowingMove { + pub fn new(mut value: i32) -> Self { + Self { value: value } + } + pub fn copy_from(other: Ptr) -> Self { + Self { + value: other.with(|__s| __s.value), + } + } + pub fn move_from(other: Ptr) -> Self { + let __this: p11_move_if_noexcept_ThrowingMove = Self { + value: other.with(|__s| __s.value), + }; + field!(other, value).write(0); + __this + } +} +impl Clone for p11_move_if_noexcept_ThrowingMove { + fn clone(&self) -> Self { + let __src: Value = + Rc::new(RefCell::new(p11_move_if_noexcept_ThrowingMove { + value: self.value.clone(), + })); + p11_move_if_noexcept_ThrowingMove::copy_from(__src.as_pointer()) + } +} +pub fn test_8() { + let moved_from: Value = + Rc::new(RefCell::new(p11_move_if_noexcept_NoexceptMove::new({ 1 }))); + let mut moved: p11_move_if_noexcept_NoexceptMove = + p11_move_if_noexcept_NoexceptMove::move_from({ moved_from.as_pointer() }); + assert!(({ (moved.value == 1) && ({ (*moved_from.borrow()).value } == 0) })); + let copied_from: Value = + Rc::new(RefCell::new(p11_move_if_noexcept_ThrowingMove::new({ 2 }))); + let mut copied: p11_move_if_noexcept_ThrowingMove = + p11_move_if_noexcept_ThrowingMove::copy_from({ copied_from.as_pointer() }); + assert!(({ (copied.value == 2) && ({ (*copied_from.borrow()).value } == 2) })); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + ({ test_3() }); + ({ test_4() }); + ({ test_7() }); + ({ test_8() }); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/utilities/utility/out/refcount/utility.as.const.rs b/tests/std/utilities/utility/out/refcount/utility.as.const.rs new file mode 100644 index 0000000000..4eb930e32e --- /dev/null +++ b/tests/std/utilities/utility/out/refcount/utility.as.const.rs @@ -0,0 +1,49 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(4)] +pub struct p1_as_const_Counter { + #[offset(0)] + pub value: i32, +} +pub fn test_0() { + let number: Value = Rc::new(RefCell::new(3)); + let ref_: Ptr = number.as_pointer(); + assert!(({ (ref_).clone() } == { (number.as_pointer()) })); + (*number.borrow_mut()) = 4; + assert!(((ref_.read()) == 4)); + assert!(({ ((*number.borrow()) == 4) })); + let mut pointer: Ptr = (number.as_pointer()); + assert!(({ (pointer).clone() } == { (number.as_pointer()) })); + let counter: Value = + Rc::new(RefCell::new(p1_as_const_Counter { value: 5 })); + assert!((({ p1_as_const_CounterImpl::get_2(&counter.as_pointer(),) }) == 5)); + assert!((({ p1_as_const_CounterImpl::get_1(&counter.as_pointer(),) }) == 6)); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + return 0; +} +pub trait p1_as_const_CounterImpl { + fn get_1(&self) -> i32; + fn get_2(&self) -> i32; +} +impl p1_as_const_CounterImpl for Ptr { + fn get_1(&self) -> i32 { + return ((*self).with(|__s| __s.value) + 1); + } + fn get_2(&self) -> i32 { + return (*self).with(|__s| __s.value); + } +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/utilities/utility/out/unsafe/forward-helpers.rs b/tests/std/utilities/utility/out/unsafe/forward-helpers.rs new file mode 100644 index 0000000000..0789113dbd --- /dev/null +++ b/tests/std/utilities/utility/out/unsafe/forward-helpers.rs @@ -0,0 +1,294 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn test_0() { + let mut value: i32 = 1; + let mut forwarded: i32 = value; + assert!(((forwarded) == (1))); + let lvalue: *mut i32 = &mut value; + (*lvalue) = 2; + assert!(((value) == (2))); +} +#[repr(C)] +#[derive(VaArg, FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct p6_forward_like_Movable { + pub value: i32, +} +impl p6_forward_like_Movable { + pub unsafe fn new(mut value: i32) -> Self { + let mut this = Self { value: value }; + this + } + pub unsafe fn copy_from(other: *const p6_forward_like_Movable) -> Self { + let mut this = Self { + value: (*other).value, + }; + this + } + pub unsafe fn move_from(other: *mut p6_forward_like_Movable) -> Self { + let mut this = Self { + value: (*other).value, + }; + (*other).value = 0; + this + } +} +impl Clone for p6_forward_like_Movable { + fn clone(&self) -> Self { + unsafe { p6_forward_like_Movable::copy_from(self as *const p6_forward_like_Movable) } + } +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, MoveCtorUnsafe)] +pub struct p6_forward_like_Holder { + pub member: p6_forward_like_Movable, +} +impl p6_forward_like_Holder { + pub unsafe fn move_from(_a0: *mut p6_forward_like_Holder) -> Self { + let mut this = Self { + member: p6_forward_like_Movable::move_from({ &mut (*_a0).member }), + }; + this + } +} +impl Default for p6_forward_like_Holder { + fn default() -> Self { + unsafe { + p6_forward_like_Holder { + member: p6_forward_like_Movable::new({ 1 }), + } + } + } +} +pub unsafe fn take_1(self_: *mut p6_forward_like_Holder) -> p6_forward_like_Movable { + return p6_forward_like_Movable::copy_from({ &(*self_).member }); +} +pub unsafe fn take_2(self_: *mut p6_forward_like_Holder) -> p6_forward_like_Movable { + return p6_forward_like_Movable::move_from({ &mut (*self_).member }); +} +pub unsafe fn test_3() { + let mut holder: p6_forward_like_Holder = ::default(); + let mut copied: p6_forward_like_Movable = (unsafe { take_1(&mut holder) }); + assert!(((copied.value) == (1)) && ((holder.member.value) == (1))); + let mut moved: p6_forward_like_Movable = (unsafe { take_2(&mut holder) }); + assert!(((moved.value) == (1)) && ((holder.member.value) == (0))); +} +#[repr(C)] +#[derive(FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct p9_move_Movable { + pub value: i32, +} +impl p9_move_Movable { + pub unsafe fn new(mut value: i32) -> Self { + let mut this = Self { value: value }; + this + } + pub unsafe fn move_from(other: *mut p9_move_Movable) -> Self { + let mut this = Self { + value: (*other).value, + }; + (*other).value = 0; + this + } +} +pub unsafe fn test_4() { + let mut number: i32 = 1; + let mut moved: i32 = number; + assert!(((moved) == (1)) && ((number) == (1))); + let mut target: i32 = 2; + let mut pointer: *mut i32 = (&mut target as *mut i32); + let mut moved_pointer: *mut i32 = pointer; + assert!( + ((moved_pointer) == (&mut target as *mut i32)) && ((pointer) == (&mut target as *mut i32)) + ); + let mut source: p9_move_Movable = p9_move_Movable::new({ 3 }); + let mut destination: p9_move_Movable = p9_move_Movable::move_from({ &mut source }); + assert!(((destination.value) == (3)) && ((source.value) == (0))); + let mut strings: Vec> = Vec::new(); + let mut text: Vec = std::slice::from_raw_parts( + c"text".as_ptr(), + (0..).take_while(|&i| *c"text".as_ptr().add(i) != 0).count() + 1, + ) + .to_vec(); + { + let __a1 = std::mem::replace(&mut text, vec![0]); + strings.push(__a1) + }; + assert!( + strings[(0_usize)] == { + std::slice::from_raw_parts( + c"text".as_ptr(), + (0..).take_while(|&i| *c"text".as_ptr().add(i) != 0).count() + 1, + ) + .to_vec() + } + ); +} +#[repr(C)] +#[derive(VaArg, FnPtrArg, MoveCtorUnsafe, CppFromUnsafe)] +#[cpp_from(i32, new, value)] +pub struct p10_forward_in_template_A { + pub value: i32, + pub copies: i32, + pub moves: i32, +} +impl p10_forward_in_template_A { + pub unsafe fn new(mut value: i32) -> Self { + let mut this = Self { + value: value, + copies: 0, + moves: 0, + }; + this + } + pub unsafe fn copy_from(other: *const p10_forward_in_template_A) -> Self { + let mut this = Self { + value: (*other).value, + copies: (((*other).copies) + (1)), + moves: 0, + }; + this + } + pub unsafe fn move_from(other: *mut p10_forward_in_template_A) -> Self { + let mut this = Self { + value: (*other).value, + copies: 0, + moves: (((*other).moves) + (1)), + }; + (*other).value = 0; + this + } +} +impl Clone for p10_forward_in_template_A { + fn clone(&self) -> Self { + unsafe { p10_forward_in_template_A::copy_from(self as *const p10_forward_in_template_A) } + } +} +impl Default for p10_forward_in_template_A { + fn default() -> Self { + unsafe { + p10_forward_in_template_A { + value: 0_i32, + copies: 0, + moves: 0, + } + } + } +} +pub unsafe fn make_5(source: *mut p10_forward_in_template_A) -> p10_forward_in_template_A { + return p10_forward_in_template_A::copy_from({ source }); +} +pub unsafe fn make_6(source: *mut p10_forward_in_template_A) -> p10_forward_in_template_A { + return p10_forward_in_template_A::move_from({ source }); +} +pub unsafe fn test_7() { + let mut a: p10_forward_in_template_A = p10_forward_in_template_A::new({ 1 }); + let mut copied: p10_forward_in_template_A = (unsafe { make_5(&mut a) }); + assert!((((copied.value) == (1)) && ((copied.copies) == (1))) && ((copied.moves) == (0))); + assert!(((a.value) == (1))); + let mut moved: p10_forward_in_template_A = (unsafe { make_6(&mut a) }); + assert!((((moved.value) == (1)) && ((moved.copies) == (0))) && ((moved.moves) == (1))); + assert!(((a.value) == (0))); +} +#[repr(C)] +#[derive(VaArg, FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct p11_move_if_noexcept_NoexceptMove { + pub value: i32, +} +impl p11_move_if_noexcept_NoexceptMove { + pub unsafe fn new(mut value: i32) -> Self { + let mut this = Self { value: value }; + this + } + pub unsafe fn copy_from(other: *const p11_move_if_noexcept_NoexceptMove) -> Self { + let mut this = Self { + value: (*other).value, + }; + this + } + pub unsafe fn move_from(other: *mut p11_move_if_noexcept_NoexceptMove) -> Self { + let mut this = Self { + value: (*other).value, + }; + (*other).value = 0; + this + } +} +impl Clone for p11_move_if_noexcept_NoexceptMove { + fn clone(&self) -> Self { + unsafe { + p11_move_if_noexcept_NoexceptMove::copy_from( + self as *const p11_move_if_noexcept_NoexceptMove, + ) + } + } +} +#[repr(C)] +#[derive(VaArg, FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, Default)] +#[cpp_from(i32, new, value)] +pub struct p11_move_if_noexcept_ThrowingMove { + pub value: i32, +} +impl p11_move_if_noexcept_ThrowingMove { + pub unsafe fn new(mut value: i32) -> Self { + let mut this = Self { value: value }; + this + } + pub unsafe fn copy_from(other: *const p11_move_if_noexcept_ThrowingMove) -> Self { + let mut this = Self { + value: (*other).value, + }; + this + } + pub unsafe fn move_from(other: *mut p11_move_if_noexcept_ThrowingMove) -> Self { + let mut this = Self { + value: (*other).value, + }; + (*other).value = 0; + this + } +} +impl Clone for p11_move_if_noexcept_ThrowingMove { + fn clone(&self) -> Self { + unsafe { + p11_move_if_noexcept_ThrowingMove::copy_from( + self as *const p11_move_if_noexcept_ThrowingMove, + ) + } + } +} +pub unsafe fn test_8() { + let mut moved_from: p11_move_if_noexcept_NoexceptMove = + p11_move_if_noexcept_NoexceptMove::new({ 1 }); + let mut moved: p11_move_if_noexcept_NoexceptMove = + p11_move_if_noexcept_NoexceptMove::move_from({ &mut moved_from }); + assert!(((moved.value) == (1)) && ((moved_from.value) == (0))); + let mut copied_from: p11_move_if_noexcept_ThrowingMove = + p11_move_if_noexcept_ThrowingMove::new({ 2 }); + let mut copied: p11_move_if_noexcept_ThrowingMove = + p11_move_if_noexcept_ThrowingMove::copy_from({ &copied_from }); + assert!(((copied.value) == (2)) && ((copied_from.value) == (2))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_0() }); + (unsafe { test_3() }); + (unsafe { test_4() }); + (unsafe { test_7() }); + (unsafe { test_8() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/utilities/utility/out/unsafe/utility.as.const.rs b/tests/std/utilities/utility/out/unsafe/utility.as.const.rs new file mode 100644 index 0000000000..2b0a9d7af5 --- /dev/null +++ b/tests/std/utilities/utility/out/unsafe/utility.as.const.rs @@ -0,0 +1,45 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct p1_as_const_Counter { + pub value: i32, +} +impl p1_as_const_Counter { + pub unsafe fn get_1(&mut self) -> i32 { + return ((self.value) + (1)); + } + pub unsafe fn get_2(&self) -> i32 { + return self.value; + } +} +pub unsafe fn test_0() { + let mut number: i32 = 3; + let ref_: *const i32 = &number; + assert!(((ref_) == ((&mut number as *mut i32).cast_const()))); + number = 4; + assert!(((*ref_) == (4))); + assert!(((number) == (4))); + let mut pointer: *mut i32 = (&mut number as *mut i32); + assert!(((pointer) == (&mut number as *mut i32))); + let mut counter: p1_as_const_Counter = p1_as_const_Counter { value: 5 }; + assert!(((unsafe { p1_as_const_Counter::get_2(&counter,) }) == (5))); + assert!(((unsafe { p1_as_const_Counter::get_1(&mut counter,) }) == (6))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_0() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/utilities/utility/utility.as.const.cpp b/tests/std/utilities/utility/utility.as.const.cpp new file mode 100644 index 0000000000..f2e5f32da7 --- /dev/null +++ b/tests/std/utilities/utility/utility.as.const.cpp @@ -0,0 +1,34 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include +#include + +// N4950 [utility.as.const]/1: as_const(t) returns t. +namespace p1_as_const { +struct Counter { + int value; + int get() { return value + 1; } + int get() const { return value; } +}; + +void test() { + int number = 3; + const int &ref = std::as_const(number); + assert(&ref == &number); + number = 4; + assert(ref == 4); + assert(std::as_const(number) == 4); + + int *pointer = &number; + assert(std::as_const(pointer) == &number); + + Counter counter{5}; + assert(std::as_const(counter).get() == 5); + assert(counter.get() == 6); +} +} // namespace p1_as_const + +int main() { + p1_as_const::test(); + return 0; +} diff --git a/tests/unit/as_const.cpp b/tests/unit/as_const.cpp index b5d806b2ff..001ecd42c9 100644 --- a/tests/unit/as_const.cpp +++ b/tests/unit/as_const.cpp @@ -1,5 +1,6 @@ #include #include +#include #include enum Overload {