From e13f07de513d2bcfde1ce177f1135e5d8ba7f6f1 Mon Sep 17 00:00:00 2001 From: MikuroXina Date: Mon, 13 Feb 2023 23:57:10 +0900 Subject: [PATCH 1/2] Enforce test --- .../async_function_codegen_tests.rs | 10 +++++-- .../boxed_fnonce_codegen_tests.rs | 5 +++- ...n_opaque_rust_type_return_codegen_tests.rs | 5 +++- .../generic_opaque_rust_type_codegen_tests.rs | 5 +++- .../opaque_rust_type_codegen_tests.rs | 7 +++-- .../codegen_tests/result_codegen_tests.rs | 10 +++++-- .../src/codegen/generate_rust_tokens.rs | 15 ++++++++--- .../tests/ui/incorrect-return-type.rs | 26 +++++++++++++++++++ .../tests/ui/incorrect-return-type.stderr | 23 ++++++++++++++++ 9 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 crates/swift-bridge-macro/tests/ui/incorrect-return-type.rs create mode 100644 crates/swift-bridge-macro/tests/ui/incorrect-return-type.stderr diff --git a/crates/swift-bridge-ir/src/codegen/codegen_tests/async_function_codegen_tests.rs b/crates/swift-bridge-ir/src/codegen/codegen_tests/async_function_codegen_tests.rs index 114a5e36..6fd80dd4 100644 --- a/crates/swift-bridge-ir/src/codegen/codegen_tests/async_function_codegen_tests.rs +++ b/crates/swift-bridge-ir/src/codegen/codegen_tests/async_function_codegen_tests.rs @@ -564,13 +564,19 @@ mod extern_rust_async_function_returns_result_opaque { Ok(ok) => { swift_bridge::result::ResultPtrAndPtr { is_ok: true, - ok_or_err: Box::into_raw(Box::new(ok)) as *mut super::OkType as *mut std::ffi::c_void + ok_or_err: Box::into_raw(Box::new({ + let val: super::OkType = ok; + val + })) as *mut super::OkType as *mut std::ffi::c_void } } Err(err) => { swift_bridge::result::ResultPtrAndPtr { is_ok: false, - ok_or_err: Box::into_raw(Box::new(err)) as *mut super::ErrorType as *mut std::ffi::c_void + ok_or_err: Box::into_raw(Box::new({ + let val: super::ErrorType = err; + val + })) as *mut super::ErrorType as *mut std::ffi::c_void } } }; diff --git a/crates/swift-bridge-ir/src/codegen/codegen_tests/boxed_fnonce_codegen_tests.rs b/crates/swift-bridge-ir/src/codegen/codegen_tests/boxed_fnonce_codegen_tests.rs index e3a299ba..123d8387 100644 --- a/crates/swift-bridge-ir/src/codegen/codegen_tests/boxed_fnonce_codegen_tests.rs +++ b/crates/swift-bridge-ir/src/codegen/codegen_tests/boxed_fnonce_codegen_tests.rs @@ -411,7 +411,10 @@ mod test_swift_takes_callback_return_opaque_rust_type { quote! { #[export_name = "__swift_bridge__$some_function$param0"] pub extern "C" fn some_function_param0(some_function_callback: *mut Box super::ARustType>) -> *mut super::ARustType { - Box::into_raw(Box::new(unsafe { Box::from_raw(some_function_callback)() })) as *mut super::ARustType + Box::into_raw(Box::new({ + let val: super::ARustType = unsafe { Box::from_raw(some_function_callback)() }; + val + })) as *mut super::ARustType } #[export_name = "__swift_bridge__$some_function$_free$param0"] diff --git a/crates/swift-bridge-ir/src/codegen/codegen_tests/extern_rust_function_opaque_rust_type_return_codegen_tests.rs b/crates/swift-bridge-ir/src/codegen/codegen_tests/extern_rust_function_opaque_rust_type_return_codegen_tests.rs index d563ab01..518dc89c 100644 --- a/crates/swift-bridge-ir/src/codegen/codegen_tests/extern_rust_function_opaque_rust_type_return_codegen_tests.rs +++ b/crates/swift-bridge-ir/src/codegen/codegen_tests/extern_rust_function_opaque_rust_type_return_codegen_tests.rs @@ -23,7 +23,10 @@ mod test_extern_rust_function_owned_opaque_rust_type_return { ExpectedRustTokens::Contains(quote! { #[export_name = "__swift_bridge__$some_function"] pub extern "C" fn __swift_bridge__some_function () -> *mut super::SomeType { - Box::into_raw(Box::new(super::some_function())) as *mut super::SomeType + Box::into_raw(Box::new({ + let val: super::SomeType = super::some_function(); + val + })) as *mut super::SomeType } }) } diff --git a/crates/swift-bridge-ir/src/codegen/codegen_tests/generic_opaque_rust_type_codegen_tests.rs b/crates/swift-bridge-ir/src/codegen/codegen_tests/generic_opaque_rust_type_codegen_tests.rs index d72f0bf1..fc38dbe5 100644 --- a/crates/swift-bridge-ir/src/codegen/codegen_tests/generic_opaque_rust_type_codegen_tests.rs +++ b/crates/swift-bridge-ir/src/codegen/codegen_tests/generic_opaque_rust_type_codegen_tests.rs @@ -218,7 +218,10 @@ mod generic_opaque_rust_type_return { ExpectedRustTokens::Contains(quote! { #[export_name = "__swift_bridge__$some_function"] pub extern "C" fn __swift_bridge__some_function () -> *mut super::SomeType { - Box::into_raw(Box::new(super::some_function())) as *mut super::SomeType + Box::into_raw(Box::new({ + let val: super::SomeType = super::some_function(); + val + })) as *mut super::SomeType } }) } diff --git a/crates/swift-bridge-ir/src/codegen/codegen_tests/opaque_rust_type_codegen_tests.rs b/crates/swift-bridge-ir/src/codegen/codegen_tests/opaque_rust_type_codegen_tests.rs index 4ac501a4..63aacb4d 100644 --- a/crates/swift-bridge-ir/src/codegen/codegen_tests/opaque_rust_type_codegen_tests.rs +++ b/crates/swift-bridge-ir/src/codegen/codegen_tests/opaque_rust_type_codegen_tests.rs @@ -126,7 +126,7 @@ extension HashableTypeRef: Hashable{ fn expected_c_header() -> ExpectedCHeader { ExpectedCHeader::ContainsManyAfterTrim(vec![ r#" -uint64_t __swift_bridge__$HashableType$_hash(void* self); +uint64_t __swift_bridge__$HashableType$_hash(void* self); "#, r#" "#, @@ -416,7 +416,10 @@ mod extern_swift_freestanding_fn_with_owned_opaque_rust_type_arg { fn expected_rust_tokens() -> ExpectedRustTokens { ExpectedRustTokens::Contains(quote! { pub fn some_function (arg: super::MyType) { - unsafe { __swift_bridge__some_function( Box::into_raw(Box::new(arg)) as *mut super::MyType ) } + unsafe { __swift_bridge__some_function( Box::into_raw(Box::new({ + let val: super::MyType = arg; + val + })) as *mut super::MyType ) } } extern "C" { diff --git a/crates/swift-bridge-ir/src/codegen/codegen_tests/result_codegen_tests.rs b/crates/swift-bridge-ir/src/codegen/codegen_tests/result_codegen_tests.rs index 598fb285..e7c1622e 100644 --- a/crates/swift-bridge-ir/src/codegen/codegen_tests/result_codegen_tests.rs +++ b/crates/swift-bridge-ir/src/codegen/codegen_tests/result_codegen_tests.rs @@ -151,13 +151,19 @@ mod extern_rust_fn_return_result_opaque_rust { Ok(ok) => { swift_bridge::result::ResultPtrAndPtr { is_ok: true, - ok_or_err: Box::into_raw(Box::new(ok)) as *mut super::SomeType as *mut std::ffi::c_void + ok_or_err: Box::into_raw(Box::new({ + let val: super::SomeType = ok; + val + })) as *mut super::SomeType as *mut std::ffi::c_void } } Err(err) => { swift_bridge::result::ResultPtrAndPtr { is_ok: false, - ok_or_err: Box::into_raw(Box::new(err)) as *mut super::SomeType as *mut std::ffi::c_void + ok_or_err: Box::into_raw(Box::new({ + let val: super::SomeType = err; + val + })) as *mut super::SomeType as *mut std::ffi::c_void } } } diff --git a/crates/swift-bridge-ir/src/codegen/generate_rust_tokens.rs b/crates/swift-bridge-ir/src/codegen/generate_rust_tokens.rs index dd349661..98d85c8e 100644 --- a/crates/swift-bridge-ir/src/codegen/generate_rust_tokens.rs +++ b/crates/swift-bridge-ir/src/codegen/generate_rust_tokens.rs @@ -507,7 +507,10 @@ mod tests { let expected_func = quote! { #[export_name = "__swift_bridge__$some_function"] pub extern "C" fn __swift_bridge__some_function () -> *mut super::Foo { - Box::into_raw(Box::new(super::another_function())) as *mut super::Foo + Box::into_raw(Box::new({ + let val: super::Foo = super::another_function(); + val + })) as *mut super::Foo } }; @@ -531,7 +534,10 @@ mod tests { let expected_func = quote! { #[export_name = "__swift_bridge__$some_function"] pub extern "C" fn __swift_bridge__some_function () -> *mut super::Foo { - Box::into_raw(Box::new(super::some_function().into())) as *mut super::Foo + Box::into_raw(Box::new({ + let val: super::Foo = super::some_function().into(); + val + })) as *mut super::Foo } }; @@ -630,7 +636,10 @@ mod tests { let expected = quote! { #[export_name = "__swift_bridge__$SomeType$new"] pub extern "C" fn __swift_bridge__SomeType_new () -> *mut super::SomeType { - Box::into_raw(Box::new(super::SomeType::new())) as *mut super::SomeType + Box::into_raw(Box::new({ + let val: super::SomeType = super::SomeType::new(); + val + })) as *mut super::SomeType } }; diff --git a/crates/swift-bridge-macro/tests/ui/incorrect-return-type.rs b/crates/swift-bridge-macro/tests/ui/incorrect-return-type.rs new file mode 100644 index 00000000..5d7376e9 --- /dev/null +++ b/crates/swift-bridge-macro/tests/ui/incorrect-return-type.rs @@ -0,0 +1,26 @@ +//! # To Run +//! cargo test -p swift-bridge-macro -- ui trybuild=incorrect-return-type.rs + +pub struct SomeType; + +#[swift_bridge::bridge] +mod ffi { + extern "Rust" { + type SomeType; + + #[swift_bridge(rust_name = "some_function")] + fn fn1() -> SomeType; + #[swift_bridge(rust_name = "another_function")] + fn fn2() -> SomeType; + } +} + +fn some_function() -> &'static SomeType { + &SomeType +} + +fn another_function() -> Option { + None +} + +fn main() {} diff --git a/crates/swift-bridge-macro/tests/ui/incorrect-return-type.stderr b/crates/swift-bridge-macro/tests/ui/incorrect-return-type.stderr new file mode 100644 index 00000000..151a30fc --- /dev/null +++ b/crates/swift-bridge-macro/tests/ui/incorrect-return-type.stderr @@ -0,0 +1,23 @@ +error[E0308]: mismatched types + --> tests/ui/incorrect-return-type.rs:6:1 + | +6 | #[swift_bridge::bridge] + | ^^^^^^^^^^^^^^^^^^^^^^^ + | | + | expected struct `SomeType`, found `&SomeType` + | expected due to this + | + = note: this error originates in the attribute macro `swift_bridge::bridge` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0308]: mismatched types + --> tests/ui/incorrect-return-type.rs:6:1 + | +6 | #[swift_bridge::bridge] + | ^^^^^^^^^^^^^^^^^^^^^^^ + | | + | expected struct `SomeType`, found enum `Option` + | expected due to this + | + = note: expected struct `SomeType` + found enum `Option` + = note: this error originates in the attribute macro `swift_bridge::bridge` (in Nightly builds, run with -Z macro-backtrace for more info) From 70ba0efc1be79eb6b93afa42a0bfd59e63c1dc4e Mon Sep 17 00:00:00 2001 From: MikuroXina Date: Mon, 13 Feb 2023 23:58:29 +0900 Subject: [PATCH 2/2] Make type explicit --- .../swift-bridge-ir/src/bridged_type/bridged_opaque_type.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/swift-bridge-ir/src/bridged_type/bridged_opaque_type.rs b/crates/swift-bridge-ir/src/bridged_type/bridged_opaque_type.rs index 1350de28..ad1c0c6e 100644 --- a/crates/swift-bridge-ir/src/bridged_type/bridged_opaque_type.rs +++ b/crates/swift-bridge-ir/src/bridged_type/bridged_opaque_type.rs @@ -211,7 +211,10 @@ impl BridgeableType for OpaqueForeignType { .generics .angle_bracketed_concrete_generics_tokens(types); quote! { - Box::into_raw(Box::new(#expression)) as *mut super::#ty_name #generics + Box::into_raw(Box::new({ + let val: super::#ty_name #generics = #expression; + val + })) as *mut super::#ty_name #generics } } } else {