Skip to content

Commit cc562e4

Browse files
committed
Add integration test for unused code in results
1 parent 58a06ab commit cc562e4

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

crates/swift-bridge-ir/src/bridged_type/bridgeable_result.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ impl BuiltInResult {
378378
custom_rust_ffi_types.push(quote! {
379379
#[repr(C)]
380380
pub enum #ty {
381+
#[allow(unused)]
381382
Ok #ok,
382383
#[allow(unused)]
383384
Err(#err),

crates/swift-bridge-ir/src/codegen/codegen_tests/result_codegen_tests.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ mod extern_rust_fn_return_result_opaque_rust_type_and_transparent_enum_type {
411411
ExpectedRustTokens::Contains(quote! {
412412
#[repr(C)]
413413
pub enum ResultSomeOkTypeAndSomeErrEnum{
414+
#[allow(unused)]
414415
Ok(*mut super::SomeOkType),
415416
#[allow(unused)]
416417
Err(__swift_bridge__SomeErrEnum),
@@ -489,6 +490,7 @@ mod extern_rust_fn_return_result_transparent_enum_type_and_opaque_rust_type {
489490
ExpectedRustTokens::Contains(quote! {
490491
#[repr(C)]
491492
pub enum ResultSomeOkEnumAndSomeErrType{
493+
#[allow(unused)]
492494
Ok(__swift_bridge__SomeOkEnum),
493495
#[allow(unused)]
494496
Err(*mut super::SomeErrType),
@@ -564,6 +566,7 @@ mod extern_rust_fn_return_result_unit_type_and_transparent_enum_type {
564566
ExpectedRustTokens::Contains(quote! {
565567
#[repr(C)]
566568
pub enum ResultVoidAndSomeErrEnum{
569+
#[allow(unused)]
567570
Ok,
568571
#[allow(unused)]
569572
Err(__swift_bridge__SomeErrEnum),
@@ -636,6 +639,7 @@ mod extern_rust_fn_return_result_tuple_type_and_transparent_enum_type {
636639
quote! {
637640
#[repr(C)]
638641
pub enum ResultTupleI32U32AndSomeErrEnum{
642+
#[allow(unused)]
639643
Ok(__swift_bridge__tuple_I32U32),
640644
#[allow(unused)]
641645
Err(__swift_bridge__SomeErrEnum),

crates/swift-integration-tests/src/result.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
//! See also: crates/swift-bridge-ir/src/codegen/codegen_tests/result_codegen_tests.rs
2-
// This is a temporary workaround until https://github.com/chinedufn/swift-bridge/issues/270
3-
// is closed. When tests are compiled they have `-D warnings` (deny warnings) enabled, so
4-
// tests won't even compile unless this warning is ignored.
5-
#![allow(dead_code)]
62
73
#[swift_bridge::bridge]
84
mod ffi {
@@ -41,6 +37,15 @@ mod ffi {
4137
fn val(&self) -> u32;
4238
}
4339

40+
#[swift_bridge(swift_repr = "struct")]
41+
struct ResultTestTransparentStruct(pub String);
42+
43+
extern "Rust" {
44+
fn rust_func_returns_result_null_transparent_struct(
45+
succeed: bool,
46+
) -> Result<(), ResultTestTransparentStruct>;
47+
}
48+
4449
enum ResultTransparentEnum {
4550
NamedField { data: i32 },
4651
UnnamedFields(u8, String),
@@ -141,6 +146,30 @@ fn rust_func_return_result_unit_struct_opaque_rust(
141146
}
142147
}
143148

149+
fn rust_func_returns_result_null_transparent_struct(
150+
succeed: bool,
151+
) -> Result<(), ffi::ResultTestTransparentStruct> {
152+
if succeed {
153+
Ok(())
154+
} else {
155+
Err(ffi::ResultTestTransparentStruct("failed".to_string()))
156+
}
157+
}
158+
159+
impl std::error::Error for ffi::ResultTestTransparentStruct {}
160+
161+
impl std::fmt::Debug for ffi::ResultTestTransparentStruct {
162+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163+
write!(f, "{}", self)
164+
}
165+
}
166+
167+
impl std::fmt::Display for ffi::ResultTestTransparentStruct {
168+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
169+
write!(f, "{}", self.0)
170+
}
171+
}
172+
144173
pub struct ResultTestOpaqueRustType {
145174
val: u32,
146175
}

0 commit comments

Comments
 (0)