diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 8a42f8627c..dcaa4ba7f0 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -2802,6 +2802,18 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) { } // A cast to a reference type only rebinds the operand, it converts nothing if (type->isReferenceType()) { + if (auto to = type.getNonReferenceType(); + !isAddrOf() && sub_expr->isLValue() && !to.isConstQualified() && + sub_expr->getType().isConstQualified() && + ctx_.hasSameUnqualifiedType(to, sub_expr->getType())) { + std::string object; + { + ScopedValue no_autoref(autoref_mut_, std::nullopt); + object = ToString(sub_expr); + } + EmitDeref(std::format("(&raw const {}).cast_mut()", object), to); + return false; + } const char *suffix = isAddrOf() ? GetConstCastSuffix(type.getNonReferenceType(), sub_expr->getType()) diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 7ea46fdacf..dd4083b021 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1556,6 +1556,12 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) { EmitFresh(GetDefaultAsString(expr->getType())); return false; } + if (auto type = expr->getTypeAsWritten(); + type->isReferenceType() && + ctx_.hasSameUnqualifiedType(type.getNonReferenceType(), + expr->getSubExpr()->getType())) { + return Convert(expr->getSubExpr()); + } if (expr->getCastKind() == clang::CK_PointerToIntegral || expr->getCastKind() == clang::CK_IntegralToPointer) { std::string dst_type; diff --git a/tests/std/expr/expr.compound/expr.cast.cpp b/tests/std/expr/expr.compound/expr.cast.cpp new file mode 100644 index 0000000000..18efff2624 --- /dev/null +++ b/tests/std/expr/expr.compound/expr.cast.cpp @@ -0,0 +1,29 @@ +#include +#include + +// N4950 [expr.cast]/4: The conversions performed by +// (4.1) a const_cast (7.6.1.11), +namespace p4_cast_notation_as_const_cast { +void set(int &r) { r = 7; } + +void take(int &&r) { r = 8; } + +void test() { + int v = 1; + const int &cr = v; + set((int &)cr); + assert(v == 7); + take((int &&)cr); + assert(v == 8); + + std::vector vec{1}; + const std::vector &cvec = vec; + ((std::vector &)cvec).push_back(2); + assert(vec.size() == 2); +} +} // namespace p4_cast_notation_as_const_cast + +int main() { + p4_cast_notation_as_const_cast::test(); + return 0; +} diff --git a/tests/std/expr/expr.compound/expr.post/expr.const.cast.cpp b/tests/std/expr/expr.compound/expr.post/expr.const.cast.cpp new file mode 100644 index 0000000000..094a59d4ce --- /dev/null +++ b/tests/std/expr/expr.compound/expr.post/expr.const.cast.cpp @@ -0,0 +1,41 @@ +#include +#include + +// N4950 [expr.const.cast]/4: For two object types T1 and T2, if a pointer to T1 +// can be explicitly converted to the type "pointer to T2" using a const_cast, +// then the following conversions can also be made: +// (4.1) an lvalue of type T1 can be explicitly converted to an lvalue of type +// T2 using the cast const_cast; +// (4.2) a glvalue of type T1 can be explicitly converted to an xvalue of type +// T2 using the cast const_cast; +namespace p4_reference_const_cast { +struct Pair { + int x; +}; + +void take(int &&r) { r = 8; } + +void test() { + int v = 1; + const int &cr = v; + const_cast(cr) = 2; + assert(v == 2); + take(const_cast(cr)); + assert(v == 8); + + Pair pair{1}; + const Pair &cpair = pair; + const_cast(cpair).x = 2; + assert(pair.x == 2); + + std::vector vec{1}; + const std::vector &cvec = vec; + const_cast &>(cvec).push_back(2); + assert(vec.size() == 2); +} +} // namespace p4_reference_const_cast + +int main() { + p4_reference_const_cast::test(); + return 0; +} diff --git a/tests/std/expr/expr.compound/expr.post/out/refcount/expr.const.cast.rs b/tests/std/expr/expr.compound/expr.post/out/refcount/expr.const.cast.rs new file mode 100644 index 0000000000..042683de26 --- /dev/null +++ b/tests/std/expr/expr.compound/expr.post/out/refcount/expr.const.cast.rs @@ -0,0 +1,46 @@ +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 p4_reference_const_cast_Pair { + #[offset(0)] + pub x: i32, +} +pub fn take_0(r: Ptr) { + r.write(8); +} +pub fn test_1() { + let v: Value = Rc::new(RefCell::new(1)); + let cr: Ptr = v.as_pointer(); + cr.write(2); + assert!(({ ((*v.borrow()) == 2) })); + ({ take_0((cr).clone()) }); + assert!(({ ((*v.borrow()) == 8) })); + let pair: Value = + Rc::new(RefCell::new(p4_reference_const_cast_Pair { x: 1 })); + let cpair: Ptr = pair.as_pointer(); + field!(cpair, x).write(2); + assert!(({ ({ (*pair.borrow()).x } == 2) })); + let vec_: Value> = Rc::new(RefCell::new(Vec::from([1]))); + let cvec: Ptr> = vec_.as_pointer(); + { + let __a1 = 2; + cvec.with_mut(|__v: &mut Vec| __v.push(__a1)) + }; + assert!(({ ((*vec_.borrow()).len() == 2_usize) })); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_1() }); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.const.cast.rs b/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.const.cast.rs new file mode 100644 index 0000000000..d08a3a0086 --- /dev/null +++ b/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.const.cast.rs @@ -0,0 +1,46 @@ +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 p4_reference_const_cast_Pair { + pub x: i32, +} +pub unsafe fn take_0(r: *mut i32) { + (*r) = 8; +} +pub unsafe fn test_1() { + let mut v: i32 = 1; + let cr: *const i32 = &v; + (*(&raw const (*cr)).cast_mut()) = 2; + assert!(((v) == (2))); + (unsafe { take_0((cr).cast_mut()) }); + assert!(((v) == (8))); + let mut pair: p4_reference_const_cast_Pair = p4_reference_const_cast_Pair { x: 1 }; + let cpair: *const p4_reference_const_cast_Pair = &pair; + (*(&raw const (*cpair)).cast_mut()).x = 2; + assert!(((pair.x) == (2))); + let mut vec_: Vec = Vec::from([1]); + let cvec: *const Vec = &vec_; + { + let __a1 = 2; + (*(&raw const (*cvec)).cast_mut()).push(__a1) + }; + assert!(((vec_.len()) == (2_usize))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_1() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/expr/expr.compound/out/refcount/expr.cast.rs b/tests/std/expr/expr.compound/out/refcount/expr.cast.rs new file mode 100644 index 0000000000..dd17fc453d --- /dev/null +++ b/tests/std/expr/expr.compound/out/refcount/expr.cast.rs @@ -0,0 +1,38 @@ +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 set_0(r: Ptr) { + r.write(7); +} +pub fn take_1(r: Ptr) { + r.write(8); +} +pub fn test_2() { + let v: Value = Rc::new(RefCell::new(1)); + let cr: Ptr = v.as_pointer(); + ({ set_0((cr).clone()) }); + assert!(({ ((*v.borrow()) == 7) })); + ({ take_1((cr).clone()) }); + assert!(({ ((*v.borrow()) == 8) })); + let vec_: Value> = Rc::new(RefCell::new(Vec::from([1]))); + let cvec: Ptr> = vec_.as_pointer(); + { + let __a1 = 2; + cvec.with_mut(|__v: &mut Vec| __v.push(__a1)) + }; + assert!(({ ((*vec_.borrow()).len() == 2_usize) })); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_2() }); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/expr/expr.compound/out/unsafe/expr.cast.rs b/tests/std/expr/expr.compound/out/unsafe/expr.cast.rs new file mode 100644 index 0000000000..73d497d00d --- /dev/null +++ b/tests/std/expr/expr.compound/out/unsafe/expr.cast.rs @@ -0,0 +1,40 @@ +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 set_0(r: *mut i32) { + (*r) = 7; +} +pub unsafe fn take_1(r: *mut i32) { + (*r) = 8; +} +pub unsafe fn test_2() { + let mut v: i32 = 1; + let cr: *const i32 = &v; + (unsafe { set_0((cr).cast_mut()) }); + assert!(((v) == (7))); + (unsafe { take_1((cr).cast_mut()) }); + assert!(((v) == (8))); + let mut vec_: Vec = Vec::from([1]); + let cvec: *const Vec = &vec_; + { + let __a1 = 2; + (*(&raw const (*cvec)).cast_mut()).push(__a1) + }; + assert!(((vec_.len()) == (2_usize))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_2() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {}