-
Notifications
You must be signed in to change notification settings - Fork 65
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
Support Argument-Label for Swift #156
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NNNIIIICCCEEEE!!!!
Left some minor feedback but overall this is great. Awesome work!!!
@@ -92,6 +92,7 @@ pub(crate) struct ParsedExternFn { | |||
pub args_into: Option<Vec<Ident>>, | |||
/// Get one of the associated type's fields | |||
pub get_field: Option<GetField>, | |||
pub argument_labels: Option<HashMap<Ident, LitStr>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just HashMap<Ident, LitStr>
instead of Option
?
let value: LitStr = input.parse()?; | ||
ArgumentAttr::ArgumentLabel(value) | ||
} | ||
_ => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a UI test file for the unrecognized argument attribute error.
Similar to
swift-bridge/crates/swift-bridge-macro/tests/ui/unrecognized-function-attribute.rs
Lines 1 to 12 in e0d3d62
//! # To Run | |
//! cargo test -p swift-bridge-macro -- ui trybuild=unrecognized-function-attribute.rs | |
#[swift_bridge::bridge] | |
mod ffi { | |
extern "Rust" { | |
#[swift_bridge(InvalidAttribute)] | |
fn some_function(); | |
} | |
} | |
fn main() {} |
swift-bridge/crates/swift-bridge-macro/tests/ui/unrecognized-function-attribute.stderr
Lines 1 to 5 in c75d6e7
error: Unrecognized attribute "InvalidAttribute". | |
--> tests/ui/unrecognized-function-attribute.rs:7:24 | |
| | |
7 | #[swift_bridge(InvalidAttribute)] | |
| ^^^^^^^^^^^^^^^^ |
use crate::test_utils::parse_ok; | ||
use quote::{format_ident, quote}; | ||
|
||
/// Verify that we can parse a function that has a argument label. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great test.
@@ -177,13 +177,17 @@ mod tests { | |||
|
|||
#[swift_bridge(return_into)] | |||
fn some_function () -> Foo; | |||
|
|||
fn print_hello_world(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's this? Seems like this should be removed?
} | ||
} | ||
}; | ||
|
||
let module = parse_ok(tokens); | ||
|
||
assert!(module.functions[0].return_into); | ||
assert_eq!(module.functions.len(), 2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's this? Seems like this should be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry...I usually add some test-codes to understand how swift-bridge
works. I forgot to remove this. From now on, I'll remove them before sending a PR.
} | ||
}; | ||
|
||
params.push(format!("_ {}", param)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can keep the push
on the outside and just change it to params.push(param)
.
Then in the match block we can put the _
or the argument_label
.
import XCTest | ||
@testable import SwiftRustIntegrationTestRunner | ||
|
||
/// Tests the #[swift_bridge(label = "someArg")] attribute. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Tests argument attributes such as `#[swift_bridge(label = "someArg")]`.
XCTAssertEqual(test_argument_label(someArg: 10, 100), 110) | ||
} | ||
|
||
func testPerformanceExample() throws { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can delete all of this setup and tear down and performance stuff. We never use them.
One more thing. We need to add docs. Can put them under here swift-bridge/book/src/bridge-module/functions/README.md Lines 39 to 83 in 36c90e7
|
let func_definition = match function.host_lang { | ||
HostLang::Rust => { | ||
gen_func_swift_calls_rust(function, &self.types, &self.swift_bridge_path) | ||
let result = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling this result
makes it seem like we return a Result
. Can we change this back?
Addressed your review! Please let me know if there are any omissions. |
Thanks! |
This PR addresses #153 and introduces
#[swift_bridge(label = "...")]
to support Swift'sArgument-Label
.Here's an example of using this: