Skip to content

Commit a661591

Browse files
committed
Add extra as Ptr to avoid AsPointer ambiguity
1 parent ed3c7bf commit a661591

7 files changed

Lines changed: 131 additions & 7 deletions

File tree

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,12 @@ pub fn main() {{
23712371

23722372
void ConverterRefCount::ConvertAddrOf(clang::Expr *expr,
23732373
clang::QualType pointer_type) {
2374+
if (const auto *arr = ctx_.getAsArrayType(expr->getType())) {
2375+
PushConversionKind push(*this, ConversionKind::Unboxed);
2376+
StrCat(std::format("({} as Ptr<{}>)", ConvertPointer(expr),
2377+
ToString(arr->getElementType())));
2378+
return;
2379+
}
23742380
StrCat(ConvertPointer(expr));
23752381
}
23762382

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

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,72 @@ impl ByteRepr for view {
5858
}
5959
}
6060
}
61+
#[derive(Default)]
62+
pub struct entry {
63+
pub id: Value<i32>,
64+
}
65+
impl Clone for entry {
66+
fn clone(&self) -> Self {
67+
Self {
68+
id: Rc::new(RefCell::new((*self.id.borrow()).clone())),
69+
}
70+
}
71+
}
72+
impl ByteRepr for entry {
73+
fn byte_size() -> usize {
74+
4
75+
}
76+
fn to_bytes(&self, buf: &mut [u8]) {
77+
(*self.id.borrow()).to_bytes(&mut buf[0..4]);
78+
}
79+
fn from_bytes(buf: &[u8]) -> Self {
80+
Self {
81+
id: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
82+
}
83+
}
84+
}
85+
thread_local!(
86+
pub static e0_0: Value<entry> = Rc::new(RefCell::new(entry {
87+
id: Rc::new(RefCell::new(1)),
88+
}));
89+
);
90+
thread_local!(
91+
pub static e1_1: Value<entry> = Rc::new(RefCell::new(entry {
92+
id: Rc::new(RefCell::new(2)),
93+
}));
94+
);
95+
thread_local!(
96+
pub static registry_2: Value<Box<[Ptr<entry>]>> = Rc::new(RefCell::new(Box::new([
97+
(e0_0.with(Value::clone).as_pointer()),
98+
(e1_1.with(Value::clone).as_pointer()),
99+
Ptr::<entry>::null(),
100+
])));
101+
);
102+
pub fn get_registry_3(out: Ptr<Ptr<Ptr<entry>>>) {
103+
let out: Value<Ptr<Ptr<Ptr<entry>>>> = Rc::new(RefCell::new(out));
104+
let __rhs = (registry_2.with(Value::clone).as_pointer() as Ptr<Ptr<entry>>)
105+
.reinterpret_cast::<Ptr<entry>>();
106+
(*out.borrow()).write(__rhs);
107+
}
61108
pub fn main() {
62109
std::process::exit(main_0());
63110
}
64111
fn main_0() -> i32 {
65112
let text: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::from(*b"hi\0")));
66113
let cp: Value<Ptr<u8>> = Rc::new(RefCell::new((text.as_pointer() as Ptr<u8>)));
67-
let u: Value<Ptr<u8>> = Rc::new(RefCell::new((*cp.borrow()).reinterpret_cast::<u8>()));
114+
let u: Value<Ptr<u8>> = Rc::new(RefCell::new(
115+
((*cp.borrow()).reinterpret_cast::<u8>()).clone(),
116+
));
68117
assert!(((((((*u.borrow()).offset((0) as isize).read()) as i32) == ('h' as i32)) as i32) != 0));
69118
assert!(((((((*u.borrow()).offset((1) as isize).read()) as i32) == ('i' as i32)) as i32) != 0));
70119
let h: Value<header> = Rc::new(RefCell::new(header {
71120
tag: Rc::new(RefCell::new(7)),
72121
size: Rc::new(RefCell::new(32)),
73122
}));
74123
let hp: Value<Ptr<header>> = Rc::new(RefCell::new((h.as_pointer())));
75-
let v: Value<Ptr<view>> = Rc::new(RefCell::new((*hp.borrow()).reinterpret_cast::<view>()));
124+
let v: Value<Ptr<view>> = Rc::new(RefCell::new(
125+
((*hp.borrow()).reinterpret_cast::<view>()).clone(),
126+
));
76127
assert!(((((*(*(*v.borrow()).upgrade().deref()).tag.borrow()) == 7) as i32) != 0));
77128
let data: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::from(*b"hi\0")));
78129
let vp: Value<AnyPtr> = Rc::new(RefCell::new(
@@ -99,5 +150,27 @@ fn main_0() -> i32 {
99150
}
100151
.reinterpret_cast::<u8>();
101152
assert!(((((*sel.borrow()).is_null()) as i32) != 0));
153+
let avail: Value<Ptr<Ptr<entry>>> = Rc::new(RefCell::new(Ptr::<Ptr<entry>>::null()));
154+
({ get_registry_3((avail.as_pointer())) });
155+
assert!((((!((*avail.borrow()).is_null())) as i32) != 0));
156+
assert!(
157+
((((*(*((*avail.borrow()).offset((0) as isize).read())
158+
.upgrade()
159+
.deref())
160+
.id
161+
.borrow())
162+
== 1) as i32)
163+
!= 0)
164+
);
165+
assert!(
166+
((((*(*((*avail.borrow()).offset((1) as isize).read())
167+
.upgrade()
168+
.deref())
169+
.id
170+
.borrow())
171+
== 2) as i32)
172+
!= 0)
173+
);
174+
assert!((((((*avail.borrow()).offset((2) as isize).read()).is_null()) as i32) != 0));
102175
return 0;
103176
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,15 @@ fn main_0() -> i32 {
207207
.lo
208208
.borrow_mut()) = 8080_u16;
209209
assert!(
210-
((((((((*(*c.borrow()).u.borrow()).raw_().reinterpret_cast::<u8>())
210+
((((((((*(*c.borrow()).u.borrow()).raw_().reinterpret_cast::<u8>() as Ptr<u8>)
211211
.reinterpret_cast::<u8>())
212212
.offset((2) as isize)
213213
.read()) as i32)
214214
== 144) as i32)
215215
!= 0)
216216
);
217217
assert!(
218-
((((((((*(*c.borrow()).u.borrow()).raw_().reinterpret_cast::<u8>())
218+
((((((((*(*c.borrow()).u.borrow()).raw_().reinterpret_cast::<u8>() as Ptr<u8>)
219219
.reinterpret_cast::<u8>())
220220
.offset((3) as isize)
221221
.read()) as i32)

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,17 @@ fn main_0() -> i32 {
230230
{
231231
(((*(*c.borrow()).view.borrow())
232232
.raw_()
233-
.reinterpret_cast::<u8>()) as Ptr<u8>)
233+
.reinterpret_cast::<u8>() as Ptr<u8>)
234+
.clone() as Ptr<u8>)
234235
.to_any()
235236
.memcpy(
236237
&((src.as_pointer() as Ptr<u8>) as Ptr<u8>).to_any(),
237238
(*len.borrow()) as usize,
238239
);
239240
(((*(*c.borrow()).view.borrow())
240241
.raw_()
241-
.reinterpret_cast::<u8>()) as Ptr<u8>)
242+
.reinterpret_cast::<u8>() as Ptr<u8>)
243+
.clone() as Ptr<u8>)
242244
.to_any()
243245
.clone()
244246
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn main_0() -> i32 {
2121
({ decay_cast_0(((a1.as_pointer() as Ptr<u32>).offset(0))) });
2222
({ bit_cast_1(((a1.as_pointer() as Ptr<u32>) as Ptr<u32>).to_any()) });
2323
({ bit_cast_1((((a1.as_pointer() as Ptr<u32>).offset(0)) as Ptr<u32>).to_any()) });
24-
({ bit_cast_1(((a1.as_pointer()) as Ptr<u32>).to_any()) });
24+
({ bit_cast_1(((a1.as_pointer() as Ptr<u32>) as Ptr<u32>).to_any()) });
2525
let ptr: Value<AnyPtr> = Rc::new(RefCell::new(
2626
((a1.as_pointer() as Ptr<u32>) as Ptr<u32>).to_any(),
2727
));

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ pub struct header {
1717
pub struct view {
1818
pub tag: i32,
1919
}
20+
#[repr(C)]
21+
#[derive(Copy, Clone, Default)]
22+
pub struct entry {
23+
pub id: i32,
24+
}
25+
pub static mut e0_0: entry = unsafe { entry { id: 1 } };
26+
pub static mut e1_1: entry = unsafe { entry { id: 2 } };
27+
pub static mut registry_2: [*const entry; 3] = unsafe {
28+
[
29+
(&raw const e0_0 as *const entry),
30+
(&raw const e1_1 as *const entry),
31+
std::ptr::null(),
32+
]
33+
};
34+
pub unsafe fn get_registry_3(mut out: *mut *mut *const entry) {
35+
(*out) = ((&raw mut registry_2 as *mut [*const entry; 3]) as *mut *const entry);
36+
}
2037
pub fn main() {
2138
unsafe {
2239
std::process::exit(main_0() as i32);
@@ -50,5 +67,11 @@ unsafe fn main_0() -> i32 {
5067
(0 as *mut ::libc::c_void)
5168
} as *mut libc::c_char);
5269
assert!(((((sel).is_null()) as i32) != 0));
70+
let mut avail: *mut *const entry = std::ptr::null_mut();
71+
(unsafe { get_registry_3((&mut avail as *mut *mut *const entry)) });
72+
assert!((((!((avail).is_null())) as i32) != 0));
73+
assert!((((((*(*avail.offset((0) as isize))).id) == (1)) as i32) != 0));
74+
assert!((((((*(*avail.offset((1) as isize))).id) == (2)) as i32) != 0));
75+
assert!(((((*avail.offset((2) as isize)).is_null()) as i32) != 0));
5376
return 0;
5477
}

‎tests/unit/ptr_cast_init.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ struct view {
1010
int tag;
1111
};
1212

13+
struct entry {
14+
int id;
15+
};
16+
17+
static const struct entry e0 = {1};
18+
static const struct entry e1 = {2};
19+
static const struct entry *registry[] = {&e0, &e1, NULL};
20+
21+
static void get_registry(const struct entry ***out) {
22+
*out = (const struct entry **)&registry;
23+
}
24+
1325
int main(void) {
1426
const char text[] = "hi";
1527
const char *cp = text;
@@ -31,5 +43,13 @@ int main(void) {
3143
n = 200;
3244
sel = (n < 100) ? vp : NULL;
3345
assert(sel == 0);
46+
47+
const struct entry **avail = NULL;
48+
get_registry(&avail);
49+
assert(avail != NULL);
50+
assert(avail[0]->id == 1);
51+
assert(avail[1]->id == 2);
52+
assert(avail[2] == NULL);
53+
3454
return 0;
3555
}

0 commit comments

Comments
 (0)