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 error for associated_to attribute #206

Merged

Conversation

NiwakaDev
Copy link
Collaborator

@NiwakaDev NiwakaDev commented Mar 28, 2023

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);
   |                         ^^^^

@NiwakaDev NiwakaDev force-pushed the add_compile_error_for_associated_to branch from 916323c to 137179d Compare March 28, 2023 11:23
Copy link
Owner

@chinedufn chinedufn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Owner

@chinedufn chinedufn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small piece of feedback around the error message.

Want to change it and update the tests to pass with the new message?

Then you can squash and merge.

Thanks!


EDIT - oh and when you squash and merge please remember to paste in the commit message from your PR body. Thanks!

@@ -206,6 +208,12 @@ struct {struct_name};
let message = format!(r#"Only `extern` blocks, structs and enums are supported."#);
Error::new_spanned(item, message)
}
ParseError::InvalidAssociatedTo { self_ } => {
let message = format!(
r#"The associated_to attribute is used for only an associated method."#
Copy link
Owner

@chinedufn chinedufn Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The associated_to attribute can only be used on static methods.

@chinedufn
Copy link
Owner

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.

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

Successfully merging this pull request may close these issues.

2 participants