Skip to content

Commit f471a3f

Browse files
authored
Fix default arguments (#340)
If no default argument is provided, the Rust argument should be `None`, not `Some(Default)`
1 parent d16fb00 commit f471a3f

6 files changed

Lines changed: 98 additions & 2 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,11 @@ void Converter::EmitArgList(const CallInfo &info) {
19181918
for (unsigned i = 0; i < info.args.size(); i++) {
19191919
const auto &ca = info.args[i];
19201920

1921+
if (ca.has_default && clang::isa<clang::CXXDefaultArgExpr>(ca.expr)) {
1922+
StrCat("None", token::kComma);
1923+
continue;
1924+
}
1925+
19211926
if (ca.has_default) {
19221927
StrCat("Some");
19231928
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <cassert>
2+
3+
int foo(int a, int b = 10) { return a + b; }
4+
5+
bool baz(int *a, int *b = nullptr) { return a == b; }
6+
7+
int main() {
8+
assert(foo(1) == 11);
9+
assert(foo(1, 2) == 3);
10+
11+
int a = 0;
12+
assert(baz(&a) == false);
13+
assert(baz(&a, &a) == true);
14+
15+
return 0;
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 foo_0(a: i32, b: Option<i32>) -> i32 {
10+
let a: Value<i32> = Rc::new(RefCell::new(a));
11+
let b: Value<i32> = Rc::new(RefCell::new(b.unwrap_or(10)));
12+
return ((*a.borrow()) + (*b.borrow()));
13+
}
14+
pub fn baz_1(a: Ptr<i32>, b: Option<Ptr<i32>>) -> bool {
15+
let a: Value<Ptr<i32>> = Rc::new(RefCell::new(a));
16+
let b: Value<Ptr<i32>> = Rc::new(RefCell::new(b.unwrap_or(Ptr::<i32>::null())));
17+
return {
18+
let _lhs = (*a.borrow()).clone();
19+
_lhs == (*b.borrow()).clone()
20+
};
21+
}
22+
pub fn main() {
23+
std::process::exit(main_0());
24+
}
25+
fn main_0() -> i32 {
26+
assert!((({ foo_0(1, None,) }) == 11));
27+
assert!((({ foo_0(1, Some(2),) }) == 3));
28+
let a: Value<i32> = Rc::new(RefCell::new(0));
29+
assert!(((({ baz_1((a.as_pointer()), None,) }) as i32) == (false as i32)));
30+
assert!(
31+
((({
32+
let _a: Ptr<i32> = (a.as_pointer());
33+
let _b: Ptr<i32> = (a.as_pointer());
34+
baz_1(_a, Some(_b))
35+
}) as i32)
36+
== (true as i32))
37+
);
38+
return 0;
39+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn main() {
2323
std::process::exit(main_0());
2424
}
2525
fn main_0() -> i32 {
26-
assert!((({ apply_1(5, Some(Default::default()),) }) == 5));
26+
assert!((({ apply_1(5, None,) }) == 5));
2727
assert!((({ apply_1(5, Some(FnPtr::<fn(i32) -> i32>::null()),) }) == 5));
2828
assert!((({ apply_1(5, Some(FnPtr::<fn(i32) -> i32>::new(identity_0)),) }) == 5));
2929
let negate: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::new(
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn foo_0(mut a: i32, mut b: Option<i32>) -> i32 {
10+
let mut b: i32 = b.unwrap_or(10);
11+
return ((a) + (b));
12+
}
13+
pub unsafe fn baz_1(mut a: *mut i32, mut b: Option<*mut i32>) -> bool {
14+
let mut b: *mut i32 = b.unwrap_or(std::ptr::null_mut());
15+
return ((a) == (b));
16+
}
17+
pub fn main() {
18+
unsafe {
19+
std::process::exit(main_0() as i32);
20+
}
21+
}
22+
unsafe fn main_0() -> i32 {
23+
assert!(((unsafe { foo_0(1, None,) }) == (11)));
24+
assert!(((unsafe { foo_0(1, Some(2),) }) == (3)));
25+
let mut a: i32 = 0;
26+
assert!((((unsafe { baz_1((&mut a as *mut i32), None,) }) as i32) == (false as i32)));
27+
assert!(
28+
(((unsafe {
29+
let _a: *mut i32 = (&mut a as *mut i32);
30+
let _b: *mut i32 = (&mut a as *mut i32);
31+
baz_1(_a, Some(_b))
32+
}) as i32)
33+
== (true as i32))
34+
);
35+
return 0;
36+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn main() {
2222
}
2323
}
2424
unsafe fn main_0() -> i32 {
25-
assert!(((unsafe { apply_1(5, Some(Default::default()),) }) == (5)));
25+
assert!(((unsafe { apply_1(5, None,) }) == (5)));
2626
assert!(((unsafe { apply_1(5, Some(None),) }) == (5)));
2727
assert!(((unsafe { apply_1(5, Some(Some(identity_0)),) }) == (5)));
2828
let mut negate: Option<unsafe fn(i32) -> i32> = Some(|x: i32| {

0 commit comments

Comments
 (0)