chinedufn / swift-bridge

swift-bridge facilitates Rust and Swift interop.
https://chinedufn.github.io/swift-bridge
Apache License 2.0
842 stars 62 forks source link

Add compile error for associated_to attribute #206

Closed NiwakaDev closed 1 year ago

NiwakaDev commented 1 year ago

This commit adds support for generating a compile error message when using #[swift_bridge(associated_to = OpaqueRustType)] on instance methods.

Related to #179.

#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        type SomeType;

        #[swift_bridge(associated_to = SomeType)]
        fn immutable_method(&self);

        #[swift_bridge(associated_to = SomeType)]
        fn mutable_method(&mut self);

        #[swift_bridge(associated_to = SomeType)]
        fn owned_method(self);
    }
}

Will generate the following error:

error: The associated_to attribute is used for only an associated method.
  --> tests/ui/invalid-associated-to-attribute.rs:10:29
   |
10 |         fn immutable_method(&self);
   |                             ^^^^^

error: The associated_to attribute is used for only an associated method.
  --> tests/ui/invalid-associated-to-attribute.rs:13:27
   |
13 |         fn mutable_method(&mut self);
   |                           ^^^^^^^^^

error: The associated_to attribute is used for only an associated method.
  --> tests/ui/invalid-associated-to-attribute.rs:16:25
   |
16 |         fn owned_method(self);
   |                         ^^^^
chinedufn commented 1 year ago

Great commit message by the way, thanks. Just one note:

This commit adds support for generating a compile error message when using #[swift_bridge(associated_to = OpaqueRustType)] on non-associated methods.

non-associated methods should be replaced with instance methods.