From c87f52c503b06de4e1af00e8d5d04d47797c7f65 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 10:22:28 +0000 Subject: [PATCH 1/3] Convert casts to a reference of the operand's own type as the operand --- cpp2rust/converter/converter.cpp | 12 ++++ .../converter/models/converter_refcount.cpp | 6 ++ tests/unit/unconst.cpp | 63 +++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 244826bf55..b66da8a759 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -2795,6 +2795,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 0320f283f5..e101cfdeee 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1540,6 +1540,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/unit/unconst.cpp b/tests/unit/unconst.cpp index d56728ab27..c0da9529fc 100644 --- a/tests/unit/unconst.cpp +++ b/tests/unit/unconst.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include #define UNCONST(p) ((void *)(uintptr_t)(const void *)(p)) @@ -7,6 +9,16 @@ void set(int &r) { r = 7; } template void set_through(const T &v) { set(const_cast(v)); } +struct Pair { + int x; +}; + +void take(int &&r) { r = 8; } + +void grow(std::vector &&v) { v.push_back(3); } + +void reset(int *&r, int *to) { r = to; } + int main() { const int a = 1; const int *p = &a; @@ -20,5 +32,56 @@ int main() { v = 0; set_through(v); assert(v == 7); + v = 0; + set((int &)cr); + assert(v == 7); + const_cast(cr) = 2; + assert(v == 2); + (int &)cr = 3; + assert(v == 3); + take((int &&)cr); + assert(v == 8); + assert(static_cast(v) == 8); + + Pair pair{1}; + const Pair &cpair = pair; + const_cast(cpair).x = 2; + assert(pair.x == 2); + ((Pair &)cpair).x = 3; + assert(static_cast(pair).x == 3); + int *px = &const_cast(cpair).x; + *px = 4; + const_cast(cpair).x += 1; + assert(pair.x == 5); + + int w = 0; + int *ptr = &v; + int *const &cptr = ptr; + reset((int *&)cptr, &w); + assert(ptr == &w); + *(int *&)cptr = 4; + assert(w == 4); + const_cast(cptr) = &v; + assert(ptr == &v); + (int *&)cptr = &w; + assert(ptr == &w); + + std::string str = "ab"; + const std::string &cstr = str; + const_cast(cstr).resize(3); + assert(str.size() == 3); + ((std::string &)cstr).clear(); + assert(static_cast(str).empty()); + + std::vector vec{1}; + const std::vector &cvec = vec; + const_cast &>(cvec).push_back(2); + assert(vec.size() == 2); + ((std::vector &)cvec)[0] = 5; + assert(vec[0] == 5); + set(const_cast &>(cvec)[0]); + assert(vec[0] == 7); + grow((std::vector &&) cvec); + assert(static_cast &>(vec).size() == 3); return 0; } From 0411af235f6d52e6859cdd7545d1c2c1a9e4ec2a Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 11:16:39 +0000 Subject: [PATCH 2/3] Move the reference cast tests to tests/std --- tests/std/expr/expr.compound/expr.cast.cpp | 29 +++++++++ .../expr.post/expr.const.cast.cpp | 41 ++++++++++++ tests/unit/unconst.cpp | 63 ------------------- 3 files changed, 70 insertions(+), 63 deletions(-) create mode 100644 tests/std/expr/expr.compound/expr.cast.cpp create mode 100644 tests/std/expr/expr.compound/expr.post/expr.const.cast.cpp 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/unit/unconst.cpp b/tests/unit/unconst.cpp index c0da9529fc..d56728ab27 100644 --- a/tests/unit/unconst.cpp +++ b/tests/unit/unconst.cpp @@ -1,7 +1,5 @@ #include #include -#include -#include #define UNCONST(p) ((void *)(uintptr_t)(const void *)(p)) @@ -9,16 +7,6 @@ void set(int &r) { r = 7; } template void set_through(const T &v) { set(const_cast(v)); } -struct Pair { - int x; -}; - -void take(int &&r) { r = 8; } - -void grow(std::vector &&v) { v.push_back(3); } - -void reset(int *&r, int *to) { r = to; } - int main() { const int a = 1; const int *p = &a; @@ -32,56 +20,5 @@ int main() { v = 0; set_through(v); assert(v == 7); - v = 0; - set((int &)cr); - assert(v == 7); - const_cast(cr) = 2; - assert(v == 2); - (int &)cr = 3; - assert(v == 3); - take((int &&)cr); - assert(v == 8); - assert(static_cast(v) == 8); - - Pair pair{1}; - const Pair &cpair = pair; - const_cast(cpair).x = 2; - assert(pair.x == 2); - ((Pair &)cpair).x = 3; - assert(static_cast(pair).x == 3); - int *px = &const_cast(cpair).x; - *px = 4; - const_cast(cpair).x += 1; - assert(pair.x == 5); - - int w = 0; - int *ptr = &v; - int *const &cptr = ptr; - reset((int *&)cptr, &w); - assert(ptr == &w); - *(int *&)cptr = 4; - assert(w == 4); - const_cast(cptr) = &v; - assert(ptr == &v); - (int *&)cptr = &w; - assert(ptr == &w); - - std::string str = "ab"; - const std::string &cstr = str; - const_cast(cstr).resize(3); - assert(str.size() == 3); - ((std::string &)cstr).clear(); - assert(static_cast(str).empty()); - - std::vector vec{1}; - const std::vector &cvec = vec; - const_cast &>(cvec).push_back(2); - assert(vec.size() == 2); - ((std::vector &)cvec)[0] = 5; - assert(vec[0] == 5); - set(const_cast &>(cvec)[0]); - assert(vec[0] == 7); - grow((std::vector &&) cvec); - assert(static_cast &>(vec).size() == 3); return 0; } From 1293638afebd24fa5ce7c98cf0037826523d4634 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 12:46:18 +0000 Subject: [PATCH 3/3] Update tests --- .../expr.post/out/refcount/expr.const.cast.rs | 46 +++++++++++++++++++ .../expr.post/out/unsafe/expr.const.cast.rs | 46 +++++++++++++++++++ .../expr.compound/out/refcount/expr.cast.rs | 38 +++++++++++++++ .../expr.compound/out/unsafe/expr.cast.rs | 40 ++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 tests/std/expr/expr.compound/expr.post/out/refcount/expr.const.cast.rs create mode 100644 tests/std/expr/expr.compound/expr.post/out/unsafe/expr.const.cast.rs create mode 100644 tests/std/expr/expr.compound/out/refcount/expr.cast.rs create mode 100644 tests/std/expr/expr.compound/out/unsafe/expr.cast.rs 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() {}