Skip to content

Commit 11bd7e2

Browse files
authored
Fix cast to same-type-reference (#649)
1 parent 152187c commit 11bd7e2

8 files changed

Lines changed: 258 additions & 0 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,6 +2811,18 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
28112811
}
28122812
// A cast to a reference type only rebinds the operand, it converts nothing
28132813
if (type->isReferenceType()) {
2814+
if (auto to = type.getNonReferenceType();
2815+
!isAddrOf() && sub_expr->isLValue() && !to.isConstQualified() &&
2816+
sub_expr->getType().isConstQualified() &&
2817+
ctx_.hasSameUnqualifiedType(to, sub_expr->getType())) {
2818+
std::string object;
2819+
{
2820+
ScopedValue no_autoref(autoref_mut_, std::nullopt);
2821+
object = ToString(sub_expr);
2822+
}
2823+
EmitDeref(std::format("(&raw const {}).cast_mut()", object), to);
2824+
return false;
2825+
}
28142826
const char *suffix = isAddrOf()
28152827
? GetConstCastSuffix(type.getNonReferenceType(),
28162828
sub_expr->getType())

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,12 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
15581558
EmitFresh(GetDefaultAsString(expr->getType()));
15591559
return false;
15601560
}
1561+
if (auto type = expr->getTypeAsWritten();
1562+
type->isReferenceType() &&
1563+
ctx_.hasSameUnqualifiedType(type.getNonReferenceType(),
1564+
expr->getSubExpr()->getType())) {
1565+
return Convert(expr->getSubExpr());
1566+
}
15611567
if (expr->getCastKind() == clang::CK_PointerToIntegral ||
15621568
expr->getCastKind() == clang::CK_IntegralToPointer) {
15631569
std::string dst_type;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <cassert>
2+
#include <vector>
3+
4+
// N4950 [expr.cast]/4: The conversions performed by
5+
// (4.1) a const_cast (7.6.1.11),
6+
namespace p4_cast_notation_as_const_cast {
7+
void set(int &r) { r = 7; }
8+
9+
void take(int &&r) { r = 8; }
10+
11+
void test() {
12+
int v = 1;
13+
const int &cr = v;
14+
set((int &)cr);
15+
assert(v == 7);
16+
take((int &&)cr);
17+
assert(v == 8);
18+
19+
std::vector<int> vec{1};
20+
const std::vector<int> &cvec = vec;
21+
((std::vector<int> &)cvec).push_back(2);
22+
assert(vec.size() == 2);
23+
}
24+
} // namespace p4_cast_notation_as_const_cast
25+
26+
int main() {
27+
p4_cast_notation_as_const_cast::test();
28+
return 0;
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <cassert>
2+
#include <vector>
3+
4+
// N4950 [expr.const.cast]/4: For two object types T1 and T2, if a pointer to T1
5+
// can be explicitly converted to the type "pointer to T2" using a const_cast,
6+
// then the following conversions can also be made:
7+
// (4.1) an lvalue of type T1 can be explicitly converted to an lvalue of type
8+
// T2 using the cast const_cast<T2&>;
9+
// (4.2) a glvalue of type T1 can be explicitly converted to an xvalue of type
10+
// T2 using the cast const_cast<T2&&>;
11+
namespace p4_reference_const_cast {
12+
struct Pair {
13+
int x;
14+
};
15+
16+
void take(int &&r) { r = 8; }
17+
18+
void test() {
19+
int v = 1;
20+
const int &cr = v;
21+
const_cast<int &>(cr) = 2;
22+
assert(v == 2);
23+
take(const_cast<int &&>(cr));
24+
assert(v == 8);
25+
26+
Pair pair{1};
27+
const Pair &cpair = pair;
28+
const_cast<Pair &>(cpair).x = 2;
29+
assert(pair.x == 2);
30+
31+
std::vector<int> vec{1};
32+
const std::vector<int> &cvec = vec;
33+
const_cast<std::vector<int> &>(cvec).push_back(2);
34+
assert(vec.size() == 2);
35+
}
36+
} // namespace p4_reference_const_cast
37+
38+
int main() {
39+
p4_reference_const_cast::test();
40+
return 0;
41+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
10+
#[byte_size(4)]
11+
pub struct p4_reference_const_cast_Pair {
12+
#[offset(0)]
13+
pub x: i32,
14+
}
15+
pub fn take_0(r: Ptr<i32>) {
16+
r.write(8);
17+
}
18+
pub fn test_1() {
19+
let v: Value<i32> = Rc::new(RefCell::new(1));
20+
let cr: Ptr<i32> = v.as_pointer();
21+
cr.write(2);
22+
assert!(({ ((*v.borrow()) == 2) }));
23+
({ take_0((cr).clone()) });
24+
assert!(({ ((*v.borrow()) == 8) }));
25+
let pair: Value<p4_reference_const_cast_Pair> =
26+
Rc::new(RefCell::new(p4_reference_const_cast_Pair { x: 1 }));
27+
let cpair: Ptr<p4_reference_const_cast_Pair> = pair.as_pointer();
28+
field!(cpair, x).write(2);
29+
assert!(({ ({ (*pair.borrow()).x } == 2) }));
30+
let vec_: Value<Vec<i32>> = Rc::new(RefCell::new(Vec::from([1])));
31+
let cvec: Ptr<Vec<i32>> = vec_.as_pointer();
32+
{
33+
let __a1 = 2;
34+
cvec.with_mut(|__v: &mut Vec<i32>| __v.push(__a1))
35+
};
36+
assert!(({ ((*vec_.borrow()).len() == 2_usize) }));
37+
}
38+
pub fn main() {
39+
__cpp2rust_init_globals();
40+
std::process::exit(main_0());
41+
}
42+
fn main_0() -> i32 {
43+
({ test_1() });
44+
return 0;
45+
}
46+
pub fn __cpp2rust_init_globals() {}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
#[repr(C)]
10+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
11+
pub struct p4_reference_const_cast_Pair {
12+
pub x: i32,
13+
}
14+
pub unsafe fn take_0(r: *mut i32) {
15+
(*r) = 8;
16+
}
17+
pub unsafe fn test_1() {
18+
let mut v: i32 = 1;
19+
let cr: *const i32 = &v;
20+
(*(&raw const (*cr)).cast_mut()) = 2;
21+
assert!(((v) == (2)));
22+
(unsafe { take_0((cr).cast_mut()) });
23+
assert!(((v) == (8)));
24+
let mut pair: p4_reference_const_cast_Pair = p4_reference_const_cast_Pair { x: 1 };
25+
let cpair: *const p4_reference_const_cast_Pair = &pair;
26+
(*(&raw const (*cpair)).cast_mut()).x = 2;
27+
assert!(((pair.x) == (2)));
28+
let mut vec_: Vec<i32> = Vec::from([1]);
29+
let cvec: *const Vec<i32> = &vec_;
30+
{
31+
let __a1 = 2;
32+
(*(&raw const (*cvec)).cast_mut()).push(__a1)
33+
};
34+
assert!(((vec_.len()) == (2_usize)));
35+
}
36+
pub fn main() {
37+
unsafe {
38+
__cpp2rust_init_globals();
39+
std::process::exit(main_0() as i32);
40+
}
41+
}
42+
unsafe fn main_0() -> i32 {
43+
(unsafe { test_1() });
44+
return 0;
45+
}
46+
pub unsafe fn __cpp2rust_init_globals() {}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn set_0(r: Ptr<i32>) {
10+
r.write(7);
11+
}
12+
pub fn take_1(r: Ptr<i32>) {
13+
r.write(8);
14+
}
15+
pub fn test_2() {
16+
let v: Value<i32> = Rc::new(RefCell::new(1));
17+
let cr: Ptr<i32> = v.as_pointer();
18+
({ set_0((cr).clone()) });
19+
assert!(({ ((*v.borrow()) == 7) }));
20+
({ take_1((cr).clone()) });
21+
assert!(({ ((*v.borrow()) == 8) }));
22+
let vec_: Value<Vec<i32>> = Rc::new(RefCell::new(Vec::from([1])));
23+
let cvec: Ptr<Vec<i32>> = vec_.as_pointer();
24+
{
25+
let __a1 = 2;
26+
cvec.with_mut(|__v: &mut Vec<i32>| __v.push(__a1))
27+
};
28+
assert!(({ ((*vec_.borrow()).len() == 2_usize) }));
29+
}
30+
pub fn main() {
31+
__cpp2rust_init_globals();
32+
std::process::exit(main_0());
33+
}
34+
fn main_0() -> i32 {
35+
({ test_2() });
36+
return 0;
37+
}
38+
pub fn __cpp2rust_init_globals() {}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn set_0(r: *mut i32) {
10+
(*r) = 7;
11+
}
12+
pub unsafe fn take_1(r: *mut i32) {
13+
(*r) = 8;
14+
}
15+
pub unsafe fn test_2() {
16+
let mut v: i32 = 1;
17+
let cr: *const i32 = &v;
18+
(unsafe { set_0((cr).cast_mut()) });
19+
assert!(((v) == (7)));
20+
(unsafe { take_1((cr).cast_mut()) });
21+
assert!(((v) == (8)));
22+
let mut vec_: Vec<i32> = Vec::from([1]);
23+
let cvec: *const Vec<i32> = &vec_;
24+
{
25+
let __a1 = 2;
26+
(*(&raw const (*cvec)).cast_mut()).push(__a1)
27+
};
28+
assert!(((vec_.len()) == (2_usize)));
29+
}
30+
pub fn main() {
31+
unsafe {
32+
__cpp2rust_init_globals();
33+
std::process::exit(main_0() as i32);
34+
}
35+
}
36+
unsafe fn main_0() -> i32 {
37+
(unsafe { test_2() });
38+
return 0;
39+
}
40+
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)