chinedufn / swift-bridge

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

Fix declaring a type's method before declaring the type #68

Open chinedufn opened 2 years ago

chinedufn commented 2 years ago

Right now this panics:

#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        fn method(self: &SomeType);
    }

    extern "Rust" {
        type SomeType;
    }
}
chinedufn commented 1 year ago

Guide

We can fix this by adding a parser tests with the bridge module in this pull request's body, and then getting that parser test to pass.

Here's a similar parser test to use as inspiration https://github.com/chinedufn/swift-bridge/blob/07a15d78782dc371a7d24301ac9625124e42f8d8/crates/swift-bridge-ir/src/parse/parse_extern_mod.rs#L629-L658 .

Goodbyefrog commented 1 year ago

Good evening, I am planning on working on this issue to get better practice on swift bridge project and how it operates, but I am a running into a misunderstanding. We cannot directly declare a method before declaring the type itself. That being said, I was wondering if it were possible to just change the order of the declarations within the FFI module.

Along the lines of :

[swift_bridge::bridge]

mod ffi { extern "Rust" { type SomeType; } extern "Rust" { fn method(self: &SomeType); } }

/////////////////////////////////////////////////

I feel as though that solution is too simple a fix, and that there is more to this issue than what I'm seeing. Based on my observation, this is a foreign function interface bridge module that contains a function called fn method(self: &SomeType) and the type sometype. Both to be exported from rust to a desired swift project for use.

I noticed the comment about adding a parser test to the bridge module.

1) Would we be testing to see if the sometype was declared before the types method? 2) How would you explain the process of creating an efficient parser test to someone who is new? 3) In what cases would we need to use parser test?

chinedufn commented 1 year ago

Good evening, I am planning on working on this issue to get better practice on swift bridge project and how it operates

Hey. It might be good to also check out this recommendation and see if it's an easier starting point:

https://github.com/chinedufn/swift-bridge/issues/170#issuecomment-1614059284

That being said, I was wondering if it were possible to just change the order of the declarations within the FFI module.

We want swift-bridge to be able to work regardless of the order of the declarations.

The problem that we are trying to solve is that we want swift-bridge to be smart enough to detect these out of order declarations.

Would we be testing to see if the sometype was declared before the types method?

We would write a test case where SomeType is declared after the method, like it is in this PR's body.

How would you explain the process of creating an efficient parser test to someone who is new?

I might start by reading and running some of the existing tests (such as the one linked above) to get a sense of how they work. Would also do some reading of the official Rust book if you haven't already to get more comfortable with Rust.

After that I might take one of the existing tests and copy/paste/modify it to test the case laid out in this PR's body.

In what cases would we need to use parser test?

Whenever we want to test the parser. In this case we are testing that the parser can successfully parse a module that has a method defined before the type is defined. This test would currently fail. We want to make it pass.