diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index b63c7fcb3..6bd5023eb 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -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()) { - return true; + Convert(expr->getInitializer()); + return false; } auto init = clang::cast(expr->getInitializer()); assert(init->getNumInits() == 1); @@ -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; } diff --git a/tests/unit/default_function_arguments.cpp b/tests/unit/default_function_arguments.cpp index 6f9d20216..58a3293e4 100644 --- a/tests/unit/default_function_arguments.cpp +++ b/tests/unit/default_function_arguments.cpp @@ -21,6 +21,10 @@ Bar global_bar(9); int by_global_ref(const Bar &b = global_bar) { return b.v; } +template int shifted(T x, int y = -1) { return x + y; } + +template int first(R &&r) { return r[0]; } + template struct Holder { T v; explicit Holder(T v = 0) : v(v) {} @@ -55,5 +59,9 @@ int main() { Holder h(4); assert(h.v == 4); + assert(shifted(3) == 2); + assert(shifted(3, 4) == 7); + assert(first((int[]){4, 5, 6}) == 4); + return 0; } diff --git a/tests/unit/out/refcount/default_function_arguments.rs b/tests/unit/out/refcount/default_function_arguments.rs index 513a8fa24..980b6ed80 100644 --- a/tests/unit/out/refcount/default_function_arguments.rs +++ b/tests/unit/out/refcount/default_function_arguments.rs @@ -57,6 +57,13 @@ pub fn by_global_ref_7(b: Option>) -> i32 { let b: Ptr = 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 { + let mut y: i32 = y.unwrap_or_else(|| -1_i32); + return (x + y); +} +pub fn first_9(r: Ptr) -> i32 { + return (elem!((r), 0).read()); +} #[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct Holder_int_ { @@ -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> = Rc::new(RefCell::new(Box::new([4, 5, 6]))); + first_9(_r.as_pointer()) + }) == 4) + ); return 0; } pub fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/unsafe/default_function_arguments.rs b/tests/unit/out/unsafe/default_function_arguments.rs index 68b21a9f1..c757e79ca 100644 --- a/tests/unit/out/unsafe/default_function_arguments.rs +++ b/tests/unit/out/unsafe/default_function_arguments.rs @@ -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 { + 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_ { @@ -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() {