swift-bridge icon indicating copy to clipboard operation
swift-bridge copied to clipboard

Add UI test for incorrect return type

Open chinedufn opened this issue 2 years ago • 1 comments

Given the following bridge module

#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        fn some_function() -> u32;
    }
}
fn some_function() -> String {
    unimplemented!()
}

You'll something along the lines of this not-so-useful error message

  --> path/to/file.rs:44:1
   |
44 | #[swift_bridge::bridge]
   | ^^^^^^^^^^^^^^^^^^^^^^^
   | |
   | expected `u32`, found `String`
   | expected `String` because of return type

We need to fix the span to point to the u32 so that the u32 gets underlined.

chinedufn avatar Apr 26 '22 08:04 chinedufn

Implementation

Add UI test

Add a UI test incorrect_return_type.rs and incorrect_return_type.stderr to https://github.com/chinedufn/swift-bridge/tree/master/crates/swift-bridge-macro/tests/ui

Pass UI test

Use quote_spanned! to set the span on the return type. https://github.com/chinedufn/swift-bridge/blob/54b9c09337701dd7580010d1d5ef6527b855ee98/crates/swift-bridge-ir/src/parsed_extern_fn.rs#L157-L174

i.e.

let ret_span = self.sig.output.span();
quote_spanned! {ret_span=>
  -> #ty
}

chinedufn avatar Jan 28 '23 00:01 chinedufn