Skip to content

Commit 648d1b9

Browse files
authored
Set computed expression type for default args and init lists (#516)
1 parent 87c5fca commit 648d1b9

4 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3643,7 +3643,8 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
36433643
bool Converter::VisitCompoundLiteralExpr(clang::CompoundLiteralExpr *expr) {
36443644
auto record = expr->getType()->getAsRecordDecl();
36453645
if (!record || !record->hasAttr<clang::TransparentUnionAttr>()) {
3646-
return true;
3646+
Convert(expr->getInitializer());
3647+
return false;
36473648
}
36483649
auto init = clang::cast<clang::InitListExpr>(expr->getInitializer());
36493650
assert(init->getNumInits() == 1);
@@ -3998,7 +3999,9 @@ bool Converter::VisitCXXDefaultArgExpr(clang::CXXDefaultArgExpr *expr) {
39983999
if (expr->getType()->isPointerType()) {
39994000
StrCat(token::kDefault);
40004001
computed_expr_type_ = ComputedExprType::FreshPointer;
4002+
return false;
40014003
}
4004+
Convert(expr->getExpr());
40024005
return false;
40034006
}
40044007

‎tests/unit/default_function_arguments.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Bar global_bar(9);
2121

2222
int by_global_ref(const Bar &b = global_bar) { return b.v; }
2323

24+
template <typename T> int shifted(T x, int y = -1) { return x + y; }
25+
26+
template <typename R> int first(R &&r) { return r[0]; }
27+
2428
template <typename T> struct Holder {
2529
T v;
2630
explicit Holder(T v = 0) : v(v) {}
@@ -55,5 +59,9 @@ int main() {
5559
Holder<int> h(4);
5660
assert(h.v == 4);
5761

62+
assert(shifted(3) == 2);
63+
assert(shifted(3, 4) == 7);
64+
assert(first((int[]){4, 5, 6}) == 4);
65+
5866
return 0;
5967
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ pub fn by_global_ref_7(b: Option<Ptr<Bar>>) -> i32 {
5757
let b: Ptr<Bar> = b.unwrap_or_else(|| global_bar_6.with(|v| v.as_pointer()));
5858
return b.with(|__s| __s.v);
5959
}
60+
pub fn shifted_8(mut x: i32, y: Option<i32>) -> i32 {
61+
let mut y: i32 = y.unwrap_or_else(|| -1_i32);
62+
return (x + y);
63+
}
64+
pub fn first_9(r: Ptr<i32>) -> i32 {
65+
return (elem!((r), 0).read());
66+
}
6067
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
6168
#[byte_size(4)]
6269
pub struct Holder_int_ {
@@ -111,6 +118,14 @@ fn main_0() -> i32 {
111118
);
112119
let mut h: Holder_int_ = Holder_int_::new({ 4 });
113120
assert!((h.v == 4));
121+
assert!((({ shifted_8(3, None,) }) == 2));
122+
assert!((({ shifted_8(3, Some(4),) }) == 7));
123+
assert!(
124+
(({
125+
let _r: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([4, 5, 6])));
126+
first_9(_r.as_pointer())
127+
}) == 4)
128+
);
114129
return 0;
115130
}
116131
pub fn __cpp2rust_init_globals() {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ pub unsafe fn by_global_ref_7(b: Option<*const Bar>) -> i32 {
5353
});
5454
return (*b).v;
5555
}
56+
pub unsafe fn shifted_8(mut x: i32, mut y: Option<i32>) -> i32 {
57+
let mut y: i32 = y.unwrap_or_else(|| unsafe { -1_i32 });
58+
return ((x) + (y));
59+
}
60+
pub unsafe fn first_9(r: *mut [i32; 3]) -> i32 {
61+
return (*r)[(0) as usize];
62+
}
5663
#[repr(C)]
5764
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
5865
pub struct Holder_int_ {
@@ -109,6 +116,14 @@ unsafe fn main_0() -> i32 {
109116
);
110117
let mut h: Holder_int_ = Holder_int_::new({ 4 });
111118
assert!(((h.v) == (4)));
119+
assert!(((unsafe { shifted_8(3, None,) }) == (2)));
120+
assert!(((unsafe { shifted_8(3, Some(4),) }) == (7)));
121+
assert!(
122+
((unsafe {
123+
let mut _r: [i32; 3] = [4, 5, 6];
124+
first_9(&mut _r)
125+
}) == (4))
126+
);
112127
return 0;
113128
}
114129
pub unsafe fn __cpp2rust_init_globals() {

0 commit comments

Comments
 (0)