Skip to content

Commit a21d202

Browse files
committed
Ignore explicit casts to reference types
1 parent b5dd144 commit a21d202

4 files changed

Lines changed: 104 additions & 0 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,6 +2520,11 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
25202520
Convert(expr->getSubExpr());
25212521
return false;
25222522
}
2523+
// A cast to a reference type only rebinds the operand, it converts nothing
2524+
if (type->isReferenceType()) {
2525+
Convert(sub_expr);
2526+
return false;
2527+
}
25232528
switch (expr->getStmtClass()) {
25242529
case clang::Stmt::CXXReinterpretCastExprClass:
25252530
case clang::Stmt::CXXStaticCastExprClass:

‎tests/unit/out/refcount/refs_as_args.rs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,37 @@ pub fn more_refs_0(x1: i32, x2: i32, r1: Ptr<i32>, r2: Ptr<i32>) {
4242
let __rhs = (rx2.read());
4343
r1.write(__rhs);
4444
}
45+
#[derive(Default)]
46+
pub struct Val {
47+
pub x: Value<i32>,
48+
}
49+
impl Clone for Val {
50+
fn clone(&self) -> Self {
51+
let __this: Value<Val> = Rc::new(RefCell::new(Self {
52+
x: Rc::new(RefCell::new((*self.x.borrow()))),
53+
}));
54+
let this: Ptr<Val> = __this.as_pointer();
55+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
56+
}
57+
}
58+
impl ByteRepr for Val {
59+
fn byte_size() -> usize {
60+
4
61+
}
62+
fn to_bytes(&self, buf: &mut [u8]) {
63+
(*self.x.borrow()).to_bytes(&mut buf[0..4]);
64+
}
65+
fn from_bytes(buf: &[u8]) -> Self {
66+
Self {
67+
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
68+
}
69+
}
70+
}
71+
pub fn sum_1(a: Val, b: Val) -> i32 {
72+
let a: Value<Val> = Rc::new(RefCell::new(a));
73+
let b: Value<Val> = Rc::new(RefCell::new(b));
74+
return ((*(*a.borrow()).x.borrow()) + (*(*b.borrow()).x.borrow()));
75+
}
4576
pub fn main() {
4677
__cpp2rust_init_globals();
4778
std::process::exit(main_0());
@@ -51,6 +82,32 @@ fn main_0() -> i32 {
5182
let x2: Value<i32> = Rc::new(RefCell::new(2));
5283
({ more_refs_0(3, 4, x1.as_pointer(), x2.as_pointer()) });
5384
assert!((((*x1.borrow()) + (*x2.borrow())) == 21));
85+
let v: Value<Val> = Rc::new(RefCell::new(Val {
86+
x: Rc::new(RefCell::new(5)),
87+
}));
88+
let acc: Value<i32> = Rc::new(RefCell::new(
89+
({
90+
let _a: Val = (*v.borrow()).clone();
91+
let _b: Val = (*v.borrow()).clone();
92+
sum_1(_a, _b)
93+
}),
94+
));
95+
(*acc.borrow_mut()) += ({
96+
let _a: Val = (*v.borrow()).clone();
97+
let _b: Val = (*v.borrow()).clone();
98+
sum_1(_a, _b)
99+
});
100+
(*acc.borrow_mut()) += ({
101+
let _a: Val = (*v.borrow()).clone();
102+
let _b: Val = (*v.borrow()).clone();
103+
sum_1(_a, _b)
104+
});
105+
(*acc.borrow_mut()) += ({
106+
let _a: Val = (*v.borrow()).clone();
107+
let _b: Val = (*v.borrow()).clone();
108+
sum_1(_a, _b)
109+
});
110+
assert!(((*acc.borrow()) == 40));
54111
return 0;
55112
}
56113
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/refs_as_args.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ pub unsafe fn more_refs_0(mut x1: i32, mut x2: i32, r1: *mut i32, r2: *const i32
1717
(*rx2) += ((((((((1) + (*rx1)) + (*rx2)) + (*pr1)) + (*pr2)) + (*rpr1)) + (*rpr2)) + (*r));
1818
(*r1) = (*rx2);
1919
}
20+
#[repr(C)]
21+
#[derive(Copy, Clone, Default)]
22+
pub struct Val {
23+
pub x: i32,
24+
}
25+
pub unsafe fn sum_1(mut a: Val, mut b: Val) -> i32 {
26+
return ((a.x) + (b.x));
27+
}
2028
pub fn main() {
2129
unsafe {
2230
__cpp2rust_init_globals();
@@ -28,6 +36,28 @@ unsafe fn main_0() -> i32 {
2836
let x2: i32 = 2;
2937
(unsafe { more_refs_0(3, 4, &mut x1, &x2) });
3038
assert!((((x1) + (x2)) == (21)));
39+
let mut v: Val = Val { x: 5 };
40+
let mut acc: i32 = (unsafe {
41+
let _a: Val = v;
42+
let _b: Val = v;
43+
sum_1(_a, _b)
44+
});
45+
acc += (unsafe {
46+
let _a: Val = v;
47+
let _b: Val = v;
48+
sum_1(_a, _b)
49+
});
50+
acc += (unsafe {
51+
let _a: Val = v;
52+
let _b: Val = v;
53+
sum_1(_a, _b)
54+
});
55+
acc += (unsafe {
56+
let _a: Val = v;
57+
let _b: Val = v;
58+
sum_1(_a, _b)
59+
});
60+
assert!(((acc) == (40)));
3161
return 0;
3262
}
3363
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/refs_as_args.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,22 @@ void more_refs(int x1, int x2, int &r1, const int &r2) {
1212
r1 = rx2;
1313
}
1414

15+
struct Val {
16+
int x;
17+
};
18+
19+
static int sum(Val a, Val b) { return a.x + b.x; }
20+
1521
int main() {
1622
int x1 = 1;
1723
const int x2 = 2;
1824
more_refs(3, 4, x1, x2);
1925
assert(x1 + x2 == 21);
26+
Val v{5};
27+
int acc = sum(static_cast<Val &>(v), v);
28+
acc += sum(static_cast<const Val &>(v), v);
29+
acc += sum(static_cast<Val &&>(v), v);
30+
acc += sum((const Val &)v, v);
31+
assert(acc == 40);
2032
return 0;
2133
}

0 commit comments

Comments
 (0)