Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()";
Expand Down
6 changes: 3 additions & 3 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions tests/std/expr/expr.compound/expr.post/expr.reinterpret.cast.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
#include <cassert>

// 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<const int *const *>(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 {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32> = Rc::new(RefCell::new(5));
let ptr: Value<Ptr<i32>> = Rc::new(RefCell::new((value.as_pointer())));
let mut ptr_ptr: Ptr<Ptr<i32>> = (ptr.as_pointer());
let mut converted: Ptr<Ptr<i32>> = ptr_ptr.reinterpret_cast::<Ptr<i32>>();
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 {
Expand All @@ -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));
Expand All @@ -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;
Expand All @@ -58,6 +68,7 @@ pub fn main() {
fn main_0() -> i32 {
({ test_0() });
({ test_1() });
({ test_2() });
return 0;
}
pub fn __cpp2rust_init_globals() {}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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));
Expand All @@ -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;
Expand Down Expand Up @@ -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() {}
Loading