Skip to content

Commit 0b6cc7f

Browse files
authored
Use refcount spelling for reinterpret_cast pointer (#640)
Master used the unsafe spelling through GetUnsafeTypeAsString which introduced raw pointers into refcount.
1 parent c3abec5 commit 0b6cc7f

5 files changed

Lines changed: 48 additions & 8 deletions

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2623,7 +2623,9 @@ void ReplaceAll(std::string &str, std::string_view from, std::string_view to) {
26232623
}
26242624

26252625
const char *GetConstCastSuffix(clang::QualType to, clang::QualType from) {
2626-
if (to.isConstQualified() == from.isConstQualified()) {
2626+
if (to.isConstQualified() == from.isConstQualified() ||
2627+
to.getCanonicalType().getUnqualifiedType() !=
2628+
from.getCanonicalType().getUnqualifiedType()) {
26272629
return nullptr;
26282630
}
26292631
return to.isConstQualified() ? ".cast_const()" : ".cast_mut()";

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,9 +1576,9 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
15761576
case clang::Stmt::CXXReinterpretCastExprClass:
15771577
assert(expr->getType()->isPointerType() &&
15781578
"Only pointer casts are supported in reinterpret_cast");
1579-
EmitFresh(
1580-
std::format("{}.reinterpret_cast::<{}>()", ToString(expr->getSubExpr()),
1581-
GetUnsafeTypeAsString(expr->getType()->getPointeeType())));
1579+
EmitFresh(std::format("{}.reinterpret_cast::<{}>()",
1580+
ToString(expr->getSubExpr()),
1581+
ConvertPointeeType(expr->getType())));
15821582
return false;
15831583
case clang::Stmt::CStyleCastExprClass:
15841584
case clang::Stmt::CXXStaticCastExprClass:

‎tests/std/expr/expr.compound/expr.post/expr.reinterpret.cast.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
22
#include <cassert>
33

4+
// N4950 [expr.reinterpret.cast]/7: an object pointer can be explicitly
5+
// converted to an object pointer of a different type.
6+
namespace p7_object_pointer_conversion {
7+
void test() {
8+
int value = 5;
9+
int *ptr = &value;
10+
int **ptr_ptr = &ptr;
11+
const int *const *converted = reinterpret_cast<const int *const *>(ptr_ptr);
12+
assert(converted == ptr_ptr);
13+
assert(*converted == ptr);
14+
value = 7;
15+
assert(**converted == 7);
16+
}
17+
} // namespace p7_object_pointer_conversion
18+
419
// N4950 [expr.reinterpret.cast]/10: the null member pointer value is converted
520
// to the null member pointer value of the destination type.
621
namespace p10_null_converts_to_null {
@@ -44,6 +59,7 @@ void test() {
4459
} // namespace p10_2_data_member_round_trip
4560

4661
int main() {
62+
p7_object_pointer_conversion::test();
4763
p10_null_converts_to_null::test();
4864
p10_2_data_member_round_trip::test();
4965
return 0;

‎tests/std/expr/expr.compound/expr.post/out/refcount/expr.reinterpret.cast.rs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9+
pub fn test_0() {
10+
let value: Value<i32> = Rc::new(RefCell::new(5));
11+
let ptr: Value<Ptr<i32>> = Rc::new(RefCell::new((value.as_pointer())));
12+
let mut ptr_ptr: Ptr<Ptr<i32>> = (ptr.as_pointer());
13+
let mut converted: Ptr<Ptr<i32>> = ptr_ptr.reinterpret_cast::<Ptr<i32>>();
14+
assert!(({ (converted).clone() } == { (ptr_ptr).clone() }));
15+
assert!(({ ({ (converted.read()) } == { (*ptr.borrow()).clone() }) }));
16+
(*value.borrow_mut()) = 7;
17+
assert!((((converted.read()).read()) == 7));
18+
}
919
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
1020
#[byte_size(8)]
1121
pub struct p10_null_converts_to_null_HasTwoInts {
@@ -20,7 +30,7 @@ pub struct p10_null_converts_to_null_HasChar {
2030
#[offset(0)]
2131
pub character: i8,
2232
}
23-
pub fn test_0() {
33+
pub fn test_1() {
2434
let mut pm: isize = -1_isize;
2535
let mut converted: isize = pm;
2636
assert!(((converted) == -1_isize));
@@ -39,7 +49,7 @@ pub struct p10_2_data_member_round_trip_HasChar {
3949
#[offset(0)]
4050
pub character: i8,
4151
}
42-
pub fn test_1() {
52+
pub fn test_2() {
4353
let mut converted: isize =
4454
(offset_of_field!(p10_2_data_member_round_trip_HasTwoInts, second) as isize);
4555
let mut back: isize = converted;
@@ -58,6 +68,7 @@ pub fn main() {
5868
fn main_0() -> i32 {
5969
({ test_0() });
6070
({ test_1() });
71+
({ test_2() });
6172
return 0;
6273
}
6374
pub fn __cpp2rust_init_globals() {}

‎tests/std/expr/expr.compound/expr.post/out/unsafe/expr.reinterpret.cast.rs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ use std::collections::BTreeMap;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
9+
pub unsafe fn test_0() {
10+
let mut value: i32 = 5;
11+
let mut ptr: *mut i32 = (&mut value as *mut i32);
12+
let mut ptr_ptr: *mut *mut i32 = (&mut ptr as *mut *mut i32);
13+
let mut converted: *const *const i32 = (ptr_ptr as *const *const i32);
14+
assert!(((converted) == (ptr_ptr as *const *const i32)));
15+
assert!(((*converted) == ((ptr).cast_const())));
16+
value = 7;
17+
assert!(((*(*converted)) == (7)));
18+
}
919
#[repr(C)]
1020
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
1121
pub struct p10_null_converts_to_null_HasTwoInts {
@@ -17,7 +27,7 @@ pub struct p10_null_converts_to_null_HasTwoInts {
1727
pub struct p10_null_converts_to_null_HasChar {
1828
pub character: libc::c_char,
1929
}
20-
pub unsafe fn test_0() {
30+
pub unsafe fn test_1() {
2131
let mut pm: isize = -1_isize;
2232
let mut converted: isize = pm;
2333
assert!(((converted) == -1_isize));
@@ -33,7 +43,7 @@ pub struct p10_2_data_member_round_trip_HasTwoInts {
3343
pub struct p10_2_data_member_round_trip_HasChar {
3444
pub character: libc::c_char,
3545
}
36-
pub unsafe fn test_1() {
46+
pub unsafe fn test_2() {
3747
let mut converted: isize =
3848
(::std::mem::offset_of!(p10_2_data_member_round_trip_HasTwoInts, second) as isize);
3949
let mut back: isize = converted;
@@ -62,6 +72,7 @@ pub fn main() {
6272
unsafe fn main_0() -> i32 {
6373
(unsafe { test_0() });
6474
(unsafe { test_1() });
75+
(unsafe { test_2() });
6576
return 0;
6677
}
6778
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)