Skip to content

Commit 4ee0f84

Browse files
authored
Merge pull request #443 from dtolnay/comma
Fix placement of commas in C++ member functions that call Rust methods
2 parents 0cb4514 + 9d84036 commit 4ee0f84

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

gen/src/write.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,11 +698,13 @@ fn write_rust_function_shim_impl(
698698
write!(out, "::rust::repr::PtrLen error$ = ");
699699
}
700700
write!(out, "{}(", invoke);
701+
let mut needs_comma = false;
701702
if sig.receiver.is_some() {
702703
write!(out, "*this");
704+
needs_comma = true;
703705
}
704-
for (i, arg) in sig.args.iter().enumerate() {
705-
if i > 0 || sig.receiver.is_some() {
706+
for arg in &sig.args {
707+
if needs_comma {
706708
write!(out, ", ");
707709
}
708710
match &arg.ty {
@@ -725,15 +727,17 @@ fn write_rust_function_shim_impl(
725727
ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
726728
_ => {}
727729
}
730+
needs_comma = true;
728731
}
729732
if indirect_return {
730-
if !sig.args.is_empty() {
733+
if needs_comma {
731734
write!(out, ", ");
732735
}
733736
write!(out, "&return$.value");
737+
needs_comma = true;
734738
}
735739
if indirect_call {
736-
if !sig.args.is_empty() || indirect_return {
740+
if needs_comma {
737741
write!(out, ", ");
738742
}
739743
write!(out, "extern$");

tests/ffi/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ pub mod ffi {
275275
fn r_return_r2(n: usize) -> Box<R2>;
276276
fn get(self: &R2) -> usize;
277277
fn set(self: &mut R2, n: usize) -> usize;
278-
fn r_method_on_shared(self: &Shared) -> usize;
278+
fn r_method_on_shared(self: &Shared) -> String;
279279

280280
#[cxx_name = "rAliasedFunction"]
281281
fn r_aliased_function(x: i32) -> String;
@@ -319,8 +319,8 @@ impl R2 {
319319
}
320320

321321
impl ffi::Shared {
322-
fn r_method_on_shared(&self) -> usize {
323-
2020
322+
fn r_method_on_shared(&self) -> String {
323+
"2020".to_owned()
324324
}
325325
}
326326

tests/ffi/tests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ extern "C" const char *cxx_run_test() noexcept {
626626
ASSERT(r2->get() == 2021);
627627
ASSERT(r2->set(2020) == 2020);
628628
ASSERT(r2->get() == 2020);
629-
ASSERT(Shared{0}.r_method_on_shared() == 2020);
629+
ASSERT(std::string(Shared{0}.r_method_on_shared()) == "2020");
630630

631631
ASSERT(std::string(rAliasedFunction(2020)) == "2020");
632632

0 commit comments

Comments
 (0)