Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3604,7 +3604,8 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
bool Converter::VisitCompoundLiteralExpr(clang::CompoundLiteralExpr *expr) {
auto record = expr->getType()->getAsRecordDecl();
if (!record || !record->hasAttr<clang::TransparentUnionAttr>()) {
return true;
Convert(expr->getInitializer());
return false;
}
auto init = clang::cast<clang::InitListExpr>(expr->getInitializer());
assert(init->getNumInits() == 1);
Expand Down Expand Up @@ -3958,7 +3959,9 @@ bool Converter::VisitCXXDefaultArgExpr(clang::CXXDefaultArgExpr *expr) {
if (expr->getType()->isPointerType()) {
StrCat(token::kDefault);
computed_expr_type_ = ComputedExprType::FreshPointer;
return false;
}
Convert(expr->getExpr());
return false;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/unit/default_function_arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Bar global_bar(9);

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

template <typename T> int shifted(T x, int y = -1) { return x + y; }

template <typename R> int first(R &&r) { return r[0]; }

template <typename T> struct Holder {
T v;
explicit Holder(T v = 0) : v(v) {}
Expand Down Expand Up @@ -55,5 +59,9 @@ int main() {
Holder<int> h(4);
assert(h.v == 4);

assert(shifted(3) == 2);
assert(shifted(3, 4) == 7);
assert(first((int[]){4, 5, 6}) == 4);

return 0;
}
15 changes: 15 additions & 0 deletions tests/unit/out/refcount/default_function_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ pub fn by_global_ref_7(b: Option<Ptr<Bar>>) -> i32 {
let b: Ptr<Bar> = b.unwrap_or_else(|| global_bar_6.with(|v| v.as_pointer()));
return b.with(|__s| __s.v);
}
pub fn shifted_8(mut x: i32, y: Option<i32>) -> i32 {
let mut y: i32 = y.unwrap_or_else(|| -1_i32);
return (x + y);
}
pub fn first_9(r: Ptr<i32>) -> i32 {
return (elem!((r), 0).read());
}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(4)]
pub struct Holder_int_ {
Expand Down Expand Up @@ -111,6 +118,14 @@ fn main_0() -> i32 {
);
let mut h: Holder_int_ = Holder_int_::new({ 4 });
assert!((h.v == 4));
assert!((({ shifted_8(3, None,) }) == 2));
assert!((({ shifted_8(3, Some(4),) }) == 7));
assert!(
(({
let _r: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([4, 5, 6])));
first_9(_r.as_pointer())
}) == 4)
);
return 0;
}
pub fn __cpp2rust_init_globals() {
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/out/unsafe/default_function_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ pub unsafe fn by_global_ref_7(b: Option<*const Bar>) -> i32 {
});
return (*b).v;
}
pub unsafe fn shifted_8(mut x: i32, mut y: Option<i32>) -> i32 {
let mut y: i32 = y.unwrap_or_else(|| unsafe { -1_i32 });
return ((x) + (y));
}
pub unsafe fn first_9(r: *mut [i32; 3]) -> i32 {
return (*r)[(0) as usize];
}
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct Holder_int_ {
Expand Down Expand Up @@ -109,6 +116,14 @@ unsafe fn main_0() -> i32 {
);
let mut h: Holder_int_ = Holder_int_::new({ 4 });
assert!(((h.v) == (4)));
assert!(((unsafe { shifted_8(3, None,) }) == (2)));
assert!(((unsafe { shifted_8(3, Some(4),) }) == (7)));
assert!(
((unsafe {
let mut _r: [i32; 3] = [4, 5, 6];
first_9(&mut _r)
}) == (4))
);
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {
Expand Down
Loading