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
12 changes: 12 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2802,6 +2802,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())
Expand Down
6 changes: 6 additions & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,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;
Expand Down
29 changes: 29 additions & 0 deletions tests/std/expr/expr.compound/expr.cast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <cassert>
#include <vector>

// 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<int> vec{1};
const std::vector<int> &cvec = vec;
((std::vector<int> &)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;
}
41 changes: 41 additions & 0 deletions tests/std/expr/expr.compound/expr.post/expr.const.cast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <cassert>
#include <vector>

// 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<T2&>;
// (4.2) a glvalue of type T1 can be explicitly converted to an xvalue of type
// T2 using the cast const_cast<T2&&>;
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<int &>(cr) = 2;
assert(v == 2);
take(const_cast<int &&>(cr));
assert(v == 8);

Pair pair{1};
const Pair &cpair = pair;
const_cast<Pair &>(cpair).x = 2;
assert(pair.x == 2);

std::vector<int> vec{1};
const std::vector<int> &cvec = vec;
const_cast<std::vector<int> &>(cvec).push_back(2);
assert(vec.size() == 2);
}
} // namespace p4_reference_const_cast

int main() {
p4_reference_const_cast::test();
return 0;
}
Original file line number Diff line number Diff line change
@@ -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<i32>) {
r.write(8);
}
pub fn test_1() {
let v: Value<i32> = Rc::new(RefCell::new(1));
let cr: Ptr<i32> = v.as_pointer();
cr.write(2);
assert!(({ ((*v.borrow()) == 2) }));
({ take_0((cr).clone()) });
assert!(({ ((*v.borrow()) == 8) }));
let pair: Value<p4_reference_const_cast_Pair> =
Rc::new(RefCell::new(p4_reference_const_cast_Pair { x: 1 }));
let cpair: Ptr<p4_reference_const_cast_Pair> = pair.as_pointer();
field!(cpair, x).write(2);
assert!(({ ({ (*pair.borrow()).x } == 2) }));
let vec_: Value<Vec<i32>> = Rc::new(RefCell::new(Vec::from([1])));
let cvec: Ptr<Vec<i32>> = vec_.as_pointer();
{
let __a1 = 2;
cvec.with_mut(|__v: &mut Vec<i32>| __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() {}
Original file line number Diff line number Diff line change
@@ -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<i32> = Vec::from([1]);
let cvec: *const Vec<i32> = &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() {}
38 changes: 38 additions & 0 deletions tests/std/expr/expr.compound/out/refcount/expr.cast.rs
Original file line number Diff line number Diff line change
@@ -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<i32>) {
r.write(7);
}
pub fn take_1(r: Ptr<i32>) {
r.write(8);
}
pub fn test_2() {
let v: Value<i32> = Rc::new(RefCell::new(1));
let cr: Ptr<i32> = v.as_pointer();
({ set_0((cr).clone()) });
assert!(({ ((*v.borrow()) == 7) }));
({ take_1((cr).clone()) });
assert!(({ ((*v.borrow()) == 8) }));
let vec_: Value<Vec<i32>> = Rc::new(RefCell::new(Vec::from([1])));
let cvec: Ptr<Vec<i32>> = vec_.as_pointer();
{
let __a1 = 2;
cvec.with_mut(|__v: &mut Vec<i32>| __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() {}
40 changes: 40 additions & 0 deletions tests/std/expr/expr.compound/out/unsafe/expr.cast.rs
Original file line number Diff line number Diff line change
@@ -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<i32> = Vec::from([1]);
let cvec: *const Vec<i32> = &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() {}
Loading