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
23 changes: 22 additions & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2767,7 +2767,28 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
}
// A cast to a reference type only rebinds the operand, it converts nothing
if (type->isReferenceType()) {
Convert(sub_expr);
const char *suffix = nullptr;
if (isAddrOf()) {
switch (
GetConstCastType(type.getNonReferenceType(), sub_expr->getType())) {
case ConstCastType::MutableToConst:
suffix = ".cast_const()";
break;
case ConstCastType::ConstToMutable:
suffix = ".cast_mut()";
break;
default:
break;
}
}
{
PushParen paren(*this, suffix);
Convert(sub_expr);
}
if (suffix) {
StrCat(suffix);
computed_expr_type_ = ComputedExprType::FreshPointer;
}
return false;
}
switch (expr->getStmtClass()) {
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/out/refcount/unconst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ 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 set_through_1(v: Ptr<i32>) {
({ set_0((v).clone()) });
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand All @@ -15,6 +21,13 @@ fn main_0() -> i32 {
let mut p: Ptr<i32> = (a.as_pointer());
let mut q: Ptr<i32> = (<AnyPtr>::from_int((p).to_any().to_int())).reinterpret_cast::<i32>();
assert!(({ (p).clone() } == { (q).clone() }));
let v: Value<i32> = Rc::new(RefCell::new(1));
let cr: Ptr<i32> = v.as_pointer();
({ set_0((cr).clone()) });
assert!(((*v.borrow()) == 7));
(*v.borrow_mut()) = 0;
({ set_through_1(v.as_pointer()) });
assert!(((*v.borrow()) == 7));
return 0;
}
pub fn __cpp2rust_init_globals() {}
13 changes: 13 additions & 0 deletions tests/unit/out/unsafe/unconst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ 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 set_through_1(v: *const i32) {
(unsafe { set_0((v).cast_mut()) });
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand All @@ -18,6 +24,13 @@ unsafe fn main_0() -> i32 {
let mut q: *mut i32 =
(((((p) as *const ::libc::c_void) as u64) as *mut ::libc::c_void) as *mut i32);
assert!(((p) == ((q).cast_const())));
let mut v: i32 = 1;
let cr: *const i32 = &v;
(unsafe { set_0((cr).cast_mut()) });
assert!(((v) == (7)));
v = 0;
(unsafe { set_through_1(&v) });
assert!(((v) == (7)));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
12 changes: 12 additions & 0 deletions tests/unit/unconst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@

#define UNCONST(p) ((void *)(uintptr_t)(const void *)(p))

void set(int &r) { r = 7; }

template <typename T> void set_through(const T &v) { set(const_cast<T &>(v)); }

int main() {
const int a = 1;
const int *p = &a;
auto q = static_cast<int *>(UNCONST(p));
assert(p == q);

int v = 1;
const int &cr = v;
set(const_cast<int &>(cr));
assert(v == 7);
v = 0;
set_through(v);
assert(v == 7);
return 0;
}
Loading