danielpclark/rutie

'methods' macro doesn't support function parameter trailing commas

andrewtbiehl opened this issue · 1 comments

It appears that the methods (and also unsafe_methods) macro doesn't currently support having a trailing comma at the end of its declared methods' parameters. For me this causes issues when using Rutie alongside Rustfmt. This is a very minor issue for me, but I thought I might as well raise it.

For example, running cargo check on the following Rutie example (adapted from the docs) is successful:

#[macro_use]
extern crate rutie;

use rutie::{Class, Object, RString};

class!(RutieExample);

methods!(
    RutieExample,
    _rtself,
    fn pub_greet(raw_first_name: RString, raw_last_name: RString, raw_hometown: RString) -> RString {
        let first_name = raw_first_name.unwrap().to_string();
        let last_name = raw_last_name.unwrap().to_string();
        let hometown = raw_hometown.unwrap().to_string();

        let greeting = format!("Hello {first_name} {last_name} from {hometown}!");

        RString::new_utf8(&greeting)
    }
);

#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn Init_rutie_ruby_example() {
    Class::new("RutieExample", None).define(|klass| {
        klass.def_self("greet", pub_greet);
    });
}

However, running cargo fmt on this code (using the default Rustfmt configuration) results in the following formatting change:

@@ -8,7 +8,11 @@ class!(RutieExample);
 methods!(
     RutieExample,
     _rtself,
-    fn pub_greet(raw_first_name: RString, raw_last_name: RString, raw_hometown: RString) -> RString {
+    fn pub_greet(
+        raw_first_name: RString,
+        raw_last_name: RString,
+        raw_hometown: RString,
+    ) -> RString {
         let first_name = raw_first_name.unwrap().to_string();
         let last_name = raw_last_name.unwrap().to_string();
         let hometown = raw_hometown.unwrap().to_string();

With this change, now running cargo check returns the following errors:

    Checking rutie_ruby_example v0.1.0 (~/Code/rutie_ruby_example)
error: no rules expected the token `)`
  --> src/lib.rs:15:5
   |
15 |     ) -> RString {
   |     ^ no rules expected this token in macro call

error[E0425]: cannot find value `pub_greet` in this scope
  --> src/lib.rs:30:33
   |
30 |         klass.def_self("greet", pub_greet);
   |                                 ^^^^^^^^^ not found in this scope

warning: unused import: `RString`
 --> src/lib.rs:4:28
  |
4 | use rutie::{Class, Object, RString};
  |                            ^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

For more information about this error, try `rustc --explain E0425`.
warning: `rutie_ruby_example` (lib) generated 1 warning
error: could not compile `rutie_ruby_example` due to 2 previous errors; 1 warning emitted

Finally, removing the newly added trailing comma from the parameter list in the pub_greet function signature results in a successful build, so it looks as though this trailing comma is the root issue.

The unsafe_methods macro also appears to have this issue.

I originally encountered this while working in the following environment:

  • Rutie: 0.8.4
  • Rust: rustc 1.65.0 (897e37553 2022-11-02)
  • Ruby: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-darwin21]
  • OS: MacOS Ventura 13.0.1 on an Intel MacBook

P.S. Thanks for such a great library! It's been amazing to use overall. 😄 💎 ⚙️

This issue has now been resolved via commits 0f6a285 and c26245d.