danielpclark / rutie

“The Tie Between Ruby and Rust.”
MIT License
938 stars 62 forks source link

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

Closed andrewtbiehl closed 7 months ago

andrewtbiehl commented 1 year ago

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:

P.S. Thanks for such a great library! It's been amazing to use overall. :smile: :gem: :gear:

andrewtbiehl commented 7 months ago

This issue has now been resolved via commits 0f6a2854756a2493fcdc09f47ad6f992f1fe1dc0 and c26245d414a9ea5091a4657a0740539973d8562e.