diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 95082a5ed8..74f4520448 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -2623,7 +2623,9 @@ void ReplaceAll(std::string &str, std::string_view from, std::string_view to) { } const char *GetConstCastSuffix(clang::QualType to, clang::QualType from) { - if (to.isConstQualified() == from.isConstQualified()) { + if (to.isConstQualified() == from.isConstQualified() || + to.getCanonicalType().getUnqualifiedType() != + from.getCanonicalType().getUnqualifiedType()) { return nullptr; } return to.isConstQualified() ? ".cast_const()" : ".cast_mut()"; diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 9916a2ee43..ad8673a452 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1547,9 +1547,9 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) { case clang::Stmt::CXXReinterpretCastExprClass: assert(expr->getType()->isPointerType() && "Only pointer casts are supported in reinterpret_cast"); - EmitFresh( - std::format("{}.reinterpret_cast::<{}>()", ToString(expr->getSubExpr()), - GetUnsafeTypeAsString(expr->getType()->getPointeeType()))); + EmitFresh(std::format("{}.reinterpret_cast::<{}>()", + ToString(expr->getSubExpr()), + ConvertPointeeType(expr->getType()))); return false; case clang::Stmt::CStyleCastExprClass: case clang::Stmt::CXXStaticCastExprClass: diff --git a/tests/std/expr/expr.compound/expr.post/expr.reinterpret.cast.cpp b/tests/std/expr/expr.compound/expr.post/expr.reinterpret.cast.cpp index 2e1e9922e7..942212906a 100644 --- a/tests/std/expr/expr.compound/expr.post/expr.reinterpret.cast.cpp +++ b/tests/std/expr/expr.compound/expr.post/expr.reinterpret.cast.cpp @@ -1,6 +1,21 @@ // ADDITIONAL_COMPILE_FLAGS: -std=c++23 #include +// N4950 [expr.reinterpret.cast]/7: an object pointer can be explicitly +// converted to an object pointer of a different type. +namespace p7_object_pointer_conversion { +void test() { + int value = 5; + int *ptr = &value; + int **ptr_ptr = &ptr; + const int *const *converted = reinterpret_cast(ptr_ptr); + assert(converted == ptr_ptr); + assert(*converted == ptr); + value = 7; + assert(**converted == 7); +} +} // namespace p7_object_pointer_conversion + // N4950 [expr.reinterpret.cast]/10: the null member pointer value is converted // to the null member pointer value of the destination type. namespace p10_null_converts_to_null { @@ -44,6 +59,7 @@ void test() { } // namespace p10_2_data_member_round_trip int main() { + p7_object_pointer_conversion::test(); p10_null_converts_to_null::test(); p10_2_data_member_round_trip::test(); return 0; diff --git a/tests/std/expr/expr.compound/expr.post/out/refcount/expr.reinterpret.cast.rs b/tests/std/expr/expr.compound/expr.post/out/refcount/expr.reinterpret.cast.rs index 1785ef8cd7..d2498f6af4 100644 --- a/tests/std/expr/expr.compound/expr.post/out/refcount/expr.reinterpret.cast.rs +++ b/tests/std/expr/expr.compound/expr.post/out/refcount/expr.reinterpret.cast.rs @@ -6,6 +6,16 @@ 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(5)); + let ptr: Value> = Rc::new(RefCell::new((value.as_pointer()))); + let mut ptr_ptr: Ptr> = (ptr.as_pointer()); + let mut converted: Ptr> = ptr_ptr.reinterpret_cast::>(); + assert!(({ (converted).clone() } == { (ptr_ptr).clone() })); + assert!(({ ({ (converted.read()) } == { (*ptr.borrow()).clone() }) })); + (*value.borrow_mut()) = 7; + assert!((((converted.read()).read()) == 7)); +} #[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] #[byte_size(8)] pub struct p10_null_converts_to_null_HasTwoInts { @@ -20,7 +30,7 @@ pub struct p10_null_converts_to_null_HasChar { #[offset(0)] pub character: i8, } -pub fn test_0() { +pub fn test_1() { let mut pm: isize = -1_isize; let mut converted: isize = pm; assert!(((converted) == -1_isize)); @@ -39,7 +49,7 @@ pub struct p10_2_data_member_round_trip_HasChar { #[offset(0)] pub character: i8, } -pub fn test_1() { +pub fn test_2() { let mut converted: isize = (offset_of_field!(p10_2_data_member_round_trip_HasTwoInts, second) as isize); let mut back: isize = converted; @@ -58,6 +68,7 @@ pub fn main() { fn main_0() -> i32 { ({ test_0() }); ({ test_1() }); + ({ test_2() }); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.reinterpret.cast.rs b/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.reinterpret.cast.rs index 02a478c52e..122a08ee3f 100644 --- a/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.reinterpret.cast.rs +++ b/tests/std/expr/expr.compound/expr.post/out/unsafe/expr.reinterpret.cast.rs @@ -6,6 +6,16 @@ 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 = 5; + let mut ptr: *mut i32 = (&mut value as *mut i32); + let mut ptr_ptr: *mut *mut i32 = (&mut ptr as *mut *mut i32); + let mut converted: *const *const i32 = (ptr_ptr as *const *const i32); + assert!(((converted) == (ptr_ptr as *const *const i32))); + assert!(((*converted) == ((ptr).cast_const()))); + value = 7; + assert!(((*(*converted)) == (7))); +} #[repr(C)] #[derive(Copy, Clone, VaArg, FnPtrArg, Default)] pub struct p10_null_converts_to_null_HasTwoInts { @@ -17,7 +27,7 @@ pub struct p10_null_converts_to_null_HasTwoInts { pub struct p10_null_converts_to_null_HasChar { pub character: libc::c_char, } -pub unsafe fn test_0() { +pub unsafe fn test_1() { let mut pm: isize = -1_isize; let mut converted: isize = pm; assert!(((converted) == -1_isize)); @@ -33,7 +43,7 @@ pub struct p10_2_data_member_round_trip_HasTwoInts { pub struct p10_2_data_member_round_trip_HasChar { pub character: libc::c_char, } -pub unsafe fn test_1() { +pub unsafe fn test_2() { let mut converted: isize = (::std::mem::offset_of!(p10_2_data_member_round_trip_HasTwoInts, second) as isize); let mut back: isize = converted; @@ -62,6 +72,7 @@ pub fn main() { unsafe fn main_0() -> i32 { (unsafe { test_0() }); (unsafe { test_1() }); + (unsafe { test_2() }); return 0; } pub unsafe fn __cpp2rust_init_globals() {}