Skip to content

Commit d3d4e55

Browse files
authored
Use fully-qualified syntax for decay calls (#426)
Addresses the second issue mentioned in #425
1 parent 87fc83d commit d3d4e55

11 files changed

Lines changed: 164 additions & 37 deletions

File tree

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ std::string ConverterRefCount::ConvertObject(clang::Expr *expr,
300300
auto pointee = expr->getType()->getPointeeType();
301301
if (IsBoxedType(pointee) || pointee->isArrayType()) {
302302
computed_expr_type_ = ComputedExprType::FreshPointer;
303-
return std::format("{}.decay()", std::move(str));
303+
return std::format("Ptr::<{}>::decay(&({}))", ToString(pointee),
304+
std::move(str));
304305
}
305306
}
306307
return str;
@@ -872,8 +873,10 @@ bool ConverterRefCount::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
872873
return false;
873874
}
874875

875-
if (isObject() && WantsElementPtr() && IsBoxedType(ref->getPointeeType())) {
876-
StrCat(str, ".decay()");
876+
if (auto pointee = ref->getPointeeType();
877+
isObject() && WantsElementPtr() && IsBoxedType(pointee)) {
878+
StrCat(std::format("Ptr::<{}>::decay(&({}))", ToString(pointee),
879+
std::move(str)));
877880
computed_expr_type_ = ComputedExprType::FreshPointer;
878881
return false;
879882
}
@@ -1162,7 +1165,8 @@ bool ConverterRefCount::VisitCallExpr(clang::CallExpr *expr) {
11621165

11631166
if (isObject() && WantsElementPtr() && ref &&
11641167
IsBoxedType(ref->getPointeeType())) {
1165-
StrCat(std::format("{}.decay()", std::move(str)));
1168+
StrCat(std::format("Ptr::<{}>::decay(&({}))",
1169+
ToString(ref->getPointeeType()), std::move(str)));
11661170
computed_expr_type_ = ComputedExprType::FreshPointer;
11671171
return false;
11681172
}
@@ -2623,24 +2627,32 @@ void ConverterRefCount::ConvertDeref(clang::Expr *expr) {
26232627
return;
26242628
}
26252629

2630+
std::string str;
26262631
{
2632+
Buffer buf(*this);
26272633
bool deref = !isAddrOf();
2628-
PushParen paren(*this, deref);
2629-
if (deref) {
2630-
StrCat(GetPointerDerefPrefix(pointee_type));
2631-
}
2632-
Convert(expr);
2633-
if (deref) {
2634-
StrCat(GetPointerDerefSuffix(pointee_type));
2635-
SetValueFreshness(pointee_type);
2634+
{
2635+
PushParen paren(*this, deref);
2636+
if (deref) {
2637+
StrCat(GetPointerDerefPrefix(pointee_type));
2638+
}
2639+
Convert(expr);
2640+
if (deref) {
2641+
StrCat(GetPointerDerefSuffix(pointee_type));
2642+
SetValueFreshness(pointee_type);
2643+
}
26362644
}
2645+
str = std::move(buf).str();
26372646
}
26382647

26392648
if (isObject() && WantsElementPtr() &&
26402649
(IsBoxedType(pointee_type) || pointee_type->isArrayType())) {
2641-
StrCat(".decay()");
2650+
StrCat(std::format("Ptr::<{}>::decay(&({}))", ToString(pointee_type),
2651+
std::move(str)));
26422652
computed_expr_type_ = ComputedExprType::FreshPointer;
2653+
return;
26432654
}
2655+
StrCat(std::move(str));
26442656
}
26452657

26462658
void ConverterRefCount::ConvertArrow(clang::Expr *expr) {

‎tests/unit/nested_vector.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <cassert>
2+
#include <vector>
3+
4+
int main() {
5+
std::vector<std::vector<int>> v1;
6+
std::vector<int> v2 = {1};
7+
v1.push_back(v2);
8+
9+
assert(v1.back().at(0) == 1);
10+
return 0;
11+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn main_0() -> i32 {
7171
});
7272
'loop_: for mut row in m.as_pointer() as Ptr<Value<Vec<i32>>> {
7373
let row: Ptr<Vec<i32>> = row.upgrade().deref().as_pointer();
74-
'loop_: for mut col in row.decay() as Ptr<i32> {
74+
'loop_: for mut col in Ptr::<Vec<i32>>::decay(&(row)) as Ptr<i32> {
7575
let __rhs = (col.read());
7676
(*square.borrow_mut()) += __rhs;
7777
}

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ fn main_0() -> i32 {
4646
(*v.borrow_mut()).push(20);
4747
let p: Value<Ptr<Vec<i32>>> = Rc::new(RefCell::new((v.as_pointer())));
4848
let a: Value<i32> = Rc::new(RefCell::new(
49-
((((*p.borrow()).decay()) as Ptr<i32>).offset(0_usize).read()),
49+
(((Ptr::<Vec<i32>>::decay(&(*p.borrow()))) as Ptr<i32>)
50+
.offset(0_usize)
51+
.read()),
5052
));
51-
(((*p.borrow()).decay()) as Ptr<i32>)
53+
((Ptr::<Vec<i32>>::decay(&(*p.borrow()))) as Ptr<i32>)
5254
.offset(1_usize)
5355
.write(30);
5456
let h: Value<Holder> = Rc::new(RefCell::new(<Holder>::default()));
@@ -64,16 +66,30 @@ fn main_0() -> i32 {
6466
.offset(1_usize)
6567
.write(60);
6668
assert!(((*a.borrow()) == 10));
67-
assert!((((((*p.borrow()).decay()) as Ptr<i32>).offset(1_usize).read()) == 30));
69+
assert!(
70+
((((Ptr::<Vec<i32>>::decay(&(*p.borrow()))) as Ptr<i32>)
71+
.offset(1_usize)
72+
.read())
73+
== 30)
74+
);
6875
assert!(((*b.borrow()) == 40));
6976
assert!(
7077
((((*(*hp.borrow()).upgrade().deref()).v.as_pointer() as Ptr<i32>)
7178
.offset(1_usize)
7279
.read())
7380
== 60)
7481
);
75-
({ write_through_0((((*p.borrow()).decay() as Ptr<i32>).offset(0_usize as isize))) });
76-
assert!((((((*p.borrow()).decay()) as Ptr<i32>).offset(0_usize).read()) == 42));
82+
({
83+
write_through_0(
84+
((Ptr::<Vec<i32>>::decay(&(*p.borrow())) as Ptr<i32>).offset(0_usize as isize)),
85+
)
86+
});
87+
assert!(
88+
((((Ptr::<Vec<i32>>::decay(&(*p.borrow()))) as Ptr<i32>)
89+
.offset(0_usize)
90+
.read())
91+
== 42)
92+
);
7793
return 0;
7894
}
7995
pub fn __cpp2rust_init_globals() {}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 main() {
10+
__cpp2rust_init_globals();
11+
std::process::exit(main_0());
12+
}
13+
fn main_0() -> i32 {
14+
let v1: Value<Vec<Value<Vec<i32>>>> = Rc::new(RefCell::new(Vec::new()));
15+
let v2: Value<Vec<i32>> = Rc::new(RefCell::new(vec![1]));
16+
(v1.as_pointer() as Ptr<Vec<Value<Vec<i32>>>>).with_mut(|__v: &mut Vec<Value<Vec<i32>>>| {
17+
__v.push(Rc::new(RefCell::new((*v2.borrow()).clone())))
18+
});
19+
assert!(
20+
(((Ptr::<Vec<i32>>::decay(&((*v1.borrow())[(*v1.borrow()).len() - 1].as_pointer()))
21+
as Ptr<i32>)
22+
.offset(0_usize as isize)
23+
.read())
24+
== 1)
25+
);
26+
return 0;
27+
}
28+
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ fn main_0() -> i32 {
5050
))
5151
.as_pointer() as Ptr<u8>)
5252
);
53-
println!("{}", (({ fn2_1(s.as_pointer(),) }).decay() as Ptr<u8>));
53+
println!(
54+
"{}",
55+
(Ptr::<Vec<u8>>::decay(&({ fn2_1(s.as_pointer(),) })) as Ptr<u8>)
56+
);
5457
return 0;
5558
}
5659
pub fn __cpp2rust_init_globals() {}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ pub fn nested_emplace_move_5(bw: Ptr<Writer>) {
180180
pub fn self_ref_push_6(comps: Ptr<Vec<Chunk>>) {
181181
let comps: Value<Ptr<Vec<Chunk>>> = Rc::new(RefCell::new(comps));
182182
{
183-
let a0_clone = (*((*comps.borrow()).decay() as Ptr<Chunk>).upgrade().deref()).clone();
183+
let a0_clone = (*(Ptr::<Vec<Chunk>>::decay(&(*comps.borrow())) as Ptr<Chunk>)
184+
.upgrade()
185+
.deref())
186+
.clone();
184187
(*comps.borrow()).with_mut(|__v: &mut Vec<Chunk>| __v.push(a0_clone))
185188
};
186189
}

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,36 @@ pub fn fn_0(v: Ptr<Vec<i32>>, v3: Vec<i32>) {
1515
(*v2.borrow_mut()).push(0);
1616
(*v2.borrow_mut()).push(1);
1717
(*v2.borrow_mut()).push(3);
18-
(*x.borrow_mut()) = ((v.decay() as Ptr<i32>).offset(2_usize).read());
18+
(*x.borrow_mut()) = ((Ptr::<Vec<i32>>::decay(&(v)) as Ptr<i32>)
19+
.offset(2_usize)
20+
.read());
1921
(v2.as_pointer() as Ptr<i32>).offset(0_usize).write(1);
20-
((if true { v3.as_pointer() } else { v.decay() }) as Ptr<i32>)
22+
((if true {
23+
v3.as_pointer()
24+
} else {
25+
Ptr::<Vec<i32>>::decay(&(v))
26+
}) as Ptr<i32>)
2127
.offset(0_usize)
2228
.write(7);
2329
(v2.as_pointer() as Ptr<Vec<i32>>).write((*v.upgrade().deref()).clone());
24-
(((*v4.borrow()).decay()) as Ptr<i32>)
30+
((Ptr::<Vec<i32>>::decay(&(*v4.borrow()))) as Ptr<i32>)
2531
.offset(1_usize)
2632
.write(13);
2733
assert!(((*x.borrow()) == 6));
28-
assert!((((v.decay() as Ptr<i32>).read()) == 4));
29-
assert!((((v.decay() as Ptr<i32>).offset(1_usize).read()) == 5));
30-
assert!((((v.decay() as Ptr<i32>).offset(2_usize).read()) == 6));
31-
assert!((((v.decay() as Ptr<i32>).to_last().read()) == 20));
34+
assert!((((Ptr::<Vec<i32>>::decay(&(v)) as Ptr<i32>).read()) == 4));
35+
assert!(
36+
(((Ptr::<Vec<i32>>::decay(&(v)) as Ptr<i32>)
37+
.offset(1_usize)
38+
.read())
39+
== 5)
40+
);
41+
assert!(
42+
(((Ptr::<Vec<i32>>::decay(&(v)) as Ptr<i32>)
43+
.offset(2_usize)
44+
.read())
45+
== 6)
46+
);
47+
assert!((((Ptr::<Vec<i32>>::decay(&(v)) as Ptr<i32>).to_last().read()) == 20));
3248
assert!((((v2.as_pointer() as Ptr<i32>).offset(0_usize).read()) == 4));
3349
assert!((((v2.as_pointer() as Ptr<i32>).offset(1_usize).read()) == 5));
3450
assert!((((v2.as_pointer() as Ptr<i32>).offset(2_usize).read()) == 6));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ fn main_0() -> i32 {
5757
.write(6);
5858
'loop_: for mut v2 in v.as_pointer() as Ptr<Value<Vec<i32>>> {
5959
let v2: Ptr<Vec<i32>> = v2.upgrade().deref().as_pointer();
60-
'loop_: for mut i in v2.decay() as Ptr<i32> {
60+
'loop_: for mut i in Ptr::<Vec<i32>>::decay(&(v2)) as Ptr<i32> {
6161
i.with_mut(|__v| __v.prefix_inc());
6262
}
6363
}
6464
'loop_: for mut v2 in v.as_pointer() as Ptr<Value<Vec<i32>>> {
6565
let v2: Ptr<Vec<i32>> = v2.upgrade().deref().as_pointer();
66-
'loop_: for mut i in v2.decay() as Ptr<i32> {
66+
'loop_: for mut i in Ptr::<Vec<i32>>::decay(&(v2)) as Ptr<i32> {
6767
let i: Value<i32> = Rc::new(RefCell::new(i.read()));
6868
println!("{}", ((*i.borrow()) + 3));
6969
}

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,35 @@ pub fn fn_1(v: Ptr<Vec<i32>>, v3: Vec<i32>) {
2222
(*v2.borrow_mut()).push(0);
2323
(*v2.borrow_mut()).push(1);
2424
(*v2.borrow_mut()).push(3);
25-
(*x.borrow_mut()) = ((v.decay() as Ptr<i32>).offset(2_usize).read());
25+
(*x.borrow_mut()) = ((Ptr::<Vec<i32>>::decay(&(v)) as Ptr<i32>)
26+
.offset(2_usize)
27+
.read());
2628
(v2.as_pointer() as Ptr<i32>).offset(0_usize).write(1);
27-
((if true { v3.as_pointer() } else { v.decay() }) as Ptr<i32>)
29+
((if true {
30+
v3.as_pointer()
31+
} else {
32+
Ptr::<Vec<i32>>::decay(&(v))
33+
}) as Ptr<i32>)
2834
.offset(0_usize)
2935
.write(7);
30-
(((*v4.borrow()).decay()) as Ptr<i32>)
36+
((Ptr::<Vec<i32>>::decay(&(*v4.borrow()))) as Ptr<i32>)
3137
.offset(1_usize)
3238
.write(13);
3339
assert!(((*x.borrow()) == 6));
34-
assert!((((v.decay() as Ptr<i32>).read()) == 4));
35-
assert!((((v.decay() as Ptr<i32>).offset(1_usize).read()) == 5));
36-
assert!((((v.decay() as Ptr<i32>).offset(2_usize).read()) == 6));
37-
assert!((((v.decay() as Ptr<i32>).to_last().read()) == 20));
40+
assert!((((Ptr::<Vec<i32>>::decay(&(v)) as Ptr<i32>).read()) == 4));
41+
assert!(
42+
(((Ptr::<Vec<i32>>::decay(&(v)) as Ptr<i32>)
43+
.offset(1_usize)
44+
.read())
45+
== 5)
46+
);
47+
assert!(
48+
(((Ptr::<Vec<i32>>::decay(&(v)) as Ptr<i32>)
49+
.offset(2_usize)
50+
.read())
51+
== 6)
52+
);
53+
assert!((((Ptr::<Vec<i32>>::decay(&(v)) as Ptr<i32>).to_last().read()) == 20));
3854
assert!((((v3.as_pointer() as Ptr<i32>).offset(0_usize).read()) == 7));
3955
assert!((((v3.as_pointer() as Ptr<i32>).offset(1_usize).read()) == 13));
4056
v.with_mut(|__v: &mut Vec<i32>| __v.push(20));

0 commit comments

Comments
 (0)