From cc562e4beb91cc97da5f2b13cf166808e8be0baa Mon Sep 17 00:00:00 2001 From: Eric Saxby Date: Wed, 19 Jun 2024 00:53:16 -0700 Subject: [PATCH] Add integration test for unused code in results --- .../src/bridged_type/bridgeable_result.rs | 1 + .../codegen_tests/result_codegen_tests.rs | 4 ++ crates/swift-integration-tests/src/result.rs | 37 +++++++++++++++++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/crates/swift-bridge-ir/src/bridged_type/bridgeable_result.rs b/crates/swift-bridge-ir/src/bridged_type/bridgeable_result.rs index dff7cf42..8737cce8 100644 --- a/crates/swift-bridge-ir/src/bridged_type/bridgeable_result.rs +++ b/crates/swift-bridge-ir/src/bridged_type/bridgeable_result.rs @@ -378,6 +378,7 @@ impl BuiltInResult { custom_rust_ffi_types.push(quote! { #[repr(C)] pub enum #ty { + #[allow(unused)] Ok #ok, #[allow(unused)] Err(#err), 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 11ecd6c5..7482ab6c 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 @@ -411,6 +411,7 @@ mod extern_rust_fn_return_result_opaque_rust_type_and_transparent_enum_type { ExpectedRustTokens::Contains(quote! { #[repr(C)] pub enum ResultSomeOkTypeAndSomeErrEnum{ + #[allow(unused)] Ok(*mut super::SomeOkType), #[allow(unused)] Err(__swift_bridge__SomeErrEnum), @@ -489,6 +490,7 @@ mod extern_rust_fn_return_result_transparent_enum_type_and_opaque_rust_type { ExpectedRustTokens::Contains(quote! { #[repr(C)] pub enum ResultSomeOkEnumAndSomeErrType{ + #[allow(unused)] Ok(__swift_bridge__SomeOkEnum), #[allow(unused)] Err(*mut super::SomeErrType), @@ -564,6 +566,7 @@ mod extern_rust_fn_return_result_unit_type_and_transparent_enum_type { ExpectedRustTokens::Contains(quote! { #[repr(C)] pub enum ResultVoidAndSomeErrEnum{ + #[allow(unused)] Ok, #[allow(unused)] Err(__swift_bridge__SomeErrEnum), @@ -636,6 +639,7 @@ mod extern_rust_fn_return_result_tuple_type_and_transparent_enum_type { quote! { #[repr(C)] pub enum ResultTupleI32U32AndSomeErrEnum{ + #[allow(unused)] Ok(__swift_bridge__tuple_I32U32), #[allow(unused)] Err(__swift_bridge__SomeErrEnum), diff --git a/crates/swift-integration-tests/src/result.rs b/crates/swift-integration-tests/src/result.rs index 9941f058..9a6c16a8 100644 --- a/crates/swift-integration-tests/src/result.rs +++ b/crates/swift-integration-tests/src/result.rs @@ -1,8 +1,4 @@ //! See also: crates/swift-bridge-ir/src/codegen/codegen_tests/result_codegen_tests.rs -// This is a temporary workaround until https://github.com/chinedufn/swift-bridge/issues/270 -// is closed. When tests are compiled they have `-D warnings` (deny warnings) enabled, so -// tests won't even compile unless this warning is ignored. -#![allow(dead_code)] #[swift_bridge::bridge] mod ffi { @@ -41,6 +37,15 @@ mod ffi { fn val(&self) -> u32; } + #[swift_bridge(swift_repr = "struct")] + struct ResultTestTransparentStruct(pub String); + + extern "Rust" { + fn rust_func_returns_result_null_transparent_struct( + succeed: bool, + ) -> Result<(), ResultTestTransparentStruct>; + } + enum ResultTransparentEnum { NamedField { data: i32 }, UnnamedFields(u8, String), @@ -141,6 +146,30 @@ fn rust_func_return_result_unit_struct_opaque_rust( } } +fn rust_func_returns_result_null_transparent_struct( + succeed: bool, +) -> Result<(), ffi::ResultTestTransparentStruct> { + if succeed { + Ok(()) + } else { + Err(ffi::ResultTestTransparentStruct("failed".to_string())) + } +} + +impl std::error::Error for ffi::ResultTestTransparentStruct {} + +impl std::fmt::Debug for ffi::ResultTestTransparentStruct { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self) + } +} + +impl std::fmt::Display for ffi::ResultTestTransparentStruct { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + pub struct ResultTestOpaqueRustType { val: u32, }