Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compile time error when using associated_to on a method #179

Closed
chinedufn opened this issue Feb 24, 2023 · 3 comments
Closed

Add compile time error when using associated_to on a method #179

chinedufn opened this issue Feb 24, 2023 · 3 comments

Comments

@chinedufn
Copy link
Owner

chinedufn commented Feb 24, 2023

Right now, the following code compiles:

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

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

We do not want this to compile, since associated_to is meant for declaring static methods.


We can make this more clear by:

  • Renaming associated_to to static_method_of (and making sure to update our docs and tests to use the new name)

  • Introducing a compile time error when using static_method_of on a method (any function that takes self, &self or &mut self).

We can keep associated_to around until version 0.2.0, just like how we handle into_return_type

// TODO: Right before we release 0.2.0 we should remove this
// "into_return_type" variant since it is deprecated.
//
"return_into" | "into_return_type" => FunctionAttr::ReturnInto,

chinedufn added a commit to chinedufn/OctoBase that referenced this issue Feb 24, 2023
This commit removes the `#[swift_bridge(associated_to = ...)]` from the methods on the `Block`, `DynamicValue` and `Workspace` types.

The `associated_to` attribute is meant to be used for static methods. It does nothing on instance methods.

In the future, `swift-bridge` will turn this into a compile time error chinedufn/swift-bridge#179 .
@chinedufn chinedufn changed the title Compile time error when using associated to on a method Add compile time error when using associated_to on a method Feb 24, 2023
@NiwakaDev
Copy link
Collaborator

I'd like to work on this issue!!

@chinedufn
Copy link
Owner Author

Go for it!

NiwakaDev added a commit that referenced this issue Mar 29, 2023
This commit adds support for generating a compile error message when using `#[swift_bridge(associated_to = OpaqueRustType)]` on instance methods.

Related to #179.

```rust
#[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
Copy link
Owner Author

Closed via #206

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants