Skip to content

Commit

Permalink
Add compile error for associated_to attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
NiwakaDev committed Mar 28, 2023
1 parent d03f772 commit 137179d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions crates/swift-bridge-ir/src/errors/parse_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub(crate) enum ParseError {
ArgCopyAndRefMut { arg: FnArg },
/// There was an unsupported item in the module, such as a `use` statement.
InvalidModuleItem { item: Item },
/// The associated_to attribute is used for only an associated method.
InvalidAssociatedTo { self_: FnArg },
}

/// An error while parsing a function attribute.
Expand Down Expand Up @@ -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."#
);
Error::new_spanned(self_, message)
}
}
}
}
5 changes: 5 additions & 0 deletions crates/swift-bridge-ir/src/parse/parse_extern_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ impl<'a> ForeignModParser<'a> {
) -> syn::Result<Option<TypeDeclaration>> {
let associated_type = match first {
Some(FnArg::Receiver(recv)) => {
if let Some(_) = attributes.associated_to {
self.errors.push(ParseError::InvalidAssociatedTo {
self_: first.unwrap().clone(),
})
}
if local_type_declarations.len() == 1 {
let ty = local_type_declarations.iter_mut().next().unwrap().1;
let associated_type = Some(TypeDeclaration::Opaque(ty.clone()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl Parse for FunctionAttr {
let key: Ident = input.parse()?;

let attrib = match key.to_string().as_str() {
// TODO: Replace this with "static_method_of" before we release 0.2.0.
"associated_to" => {
input.parse::<Token![=]>()?;
let value: Ident = input.parse()?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! # To Run
//! cargo test -p swift-bridge-macro -- ui trybuild=invalid-associated-to-attribute.rs
#[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);
}
}

pub struct SomeType;

impl SomeType {
fn immutable_method(&self) {

}
fn mutable_method(&mut self) {

}
fn owned_method() {

}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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);
| ^^^^

0 comments on commit 137179d

Please sign in to comment.