From 772035a3b115730f0b38d3cf3bdf39e7f2dac153 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 21:48:37 +0000 Subject: [PATCH 1/2] Render the refcount pointee type in reinterpret_cast --- .../converter/models/converter_refcount.cpp | 6 +++--- .../expr.post/expr.reinterpret.cast.cpp | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index c98074028d..30b0d45b0a 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..e3ad772717 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,22 @@ // ADDITIONAL_COMPILE_FLAGS: -std=c++23 #include +// N4950 [expr.reinterpret.cast]/7: converting a pointer to T1 to a pointer to +// T2 and back to its original type yields the original pointer value. +namespace p7_object_pointer_round_trip { +void test() { + int value = 5; + int *ptr = &value; + int **ptr_ptr = &ptr; + const int **converted = reinterpret_cast(ptr_ptr); + assert(**converted == 5); + int **back = reinterpret_cast(converted); + assert(back == ptr_ptr); + **back = 7; + assert(value == 7); +} +} // namespace p7_object_pointer_round_trip + // 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 +60,7 @@ void test() { } // namespace p10_2_data_member_round_trip int main() { + p7_object_pointer_round_trip::test(); p10_null_converts_to_null::test(); p10_2_data_member_round_trip::test(); return 0; From 1bfe97ff39bea3f123be6ecc96852f9f3750c781 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 23:19:25 +0000 Subject: [PATCH 2/2] Update tests --- cpp2rust/converter/converter_lib.cpp | 4 +++- .../expr/expr.compound/expr.post/expr.reinterpret.cast.cpp | 3 +-- .../expr.post/out/unsafe/expr.reinterpret.cast.rs | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) 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/tests/std/expr/expr.compound/expr.post/expr.reinterpret.cast.cpp b/tests/std/expr/expr.compound/expr.post/expr.reinterpret.cast.cpp index c7255411cd..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 @@ -8,8 +8,7 @@ void test() { int value = 5; int *ptr = &value; int **ptr_ptr = &ptr; - const int *const *converted = - reinterpret_cast(ptr_ptr); + const int *const *converted = reinterpret_cast(ptr_ptr); assert(converted == ptr_ptr); assert(*converted == ptr); value = 7; 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 e154434965..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 @@ -11,7 +11,7 @@ pub unsafe fn test_0() { 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).cast_const()))); + assert!(((converted) == (ptr_ptr as *const *const i32))); assert!(((*converted) == ((ptr).cast_const()))); value = 7; assert!(((*(*converted)) == (7)));