Skip to content

Commit d63b7ba

Browse files
committed
Allow unused returning transparent structs or enums in Results
1 parent 717fcef commit d63b7ba

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,14 @@ impl BuiltInResult {
375375
.err_ty
376376
.to_ffi_compatible_rust_type(swift_bridge_path, types);
377377
let mut custom_rust_ffi_types = vec![];
378+
// TODO: remove allowances when rustc no longer issues dead code warnings for `#[repr(C)]`
379+
// structs or enums: https://github.com/rust-lang/rust/issues/126706
378380
custom_rust_ffi_types.push(quote! {
379381
#[repr(C)]
380382
pub enum #ty {
383+
#[allow(unused)]
381384
Ok #ok,
385+
#[allow(unused)]
382386
Err(#err),
383387
}
384388
});

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,15 @@ mod extern_rust_fn_return_result_opaque_rust_type_and_transparent_enum_type {
407407
}
408408
}
409409

410+
// In Rust 1.79.0 dead_code warnings are issued for wrapped data in enums in spite of the enum
411+
// having `#[repr(C)]`.
410412
fn expected_rust_tokens() -> ExpectedRustTokens {
411413
ExpectedRustTokens::Contains(quote! {
412414
#[repr(C)]
413415
pub enum ResultSomeOkTypeAndSomeErrEnum{
416+
#[allow(unused)]
414417
Ok(*mut super::SomeOkType),
418+
#[allow(unused)]
415419
Err(__swift_bridge__SomeErrEnum),
416420
}
417421

@@ -484,11 +488,14 @@ mod extern_rust_fn_return_result_transparent_enum_type_and_opaque_rust_type {
484488
}
485489
}
486490

491+
// Allows unused to avoid dead_code warnings in Rust 1.79.0 or later.
487492
fn expected_rust_tokens() -> ExpectedRustTokens {
488493
ExpectedRustTokens::Contains(quote! {
489494
#[repr(C)]
490495
pub enum ResultSomeOkEnumAndSomeErrType{
496+
#[allow(unused)]
491497
Ok(__swift_bridge__SomeOkEnum),
498+
#[allow(unused)]
492499
Err(*mut super::SomeErrType),
493500
}
494501

@@ -558,11 +565,14 @@ mod extern_rust_fn_return_result_unit_type_and_transparent_enum_type {
558565
}
559566
}
560567

568+
// Allows unused to avoid dead_code warnings in Rust 1.79.0 or later.
561569
fn expected_rust_tokens() -> ExpectedRustTokens {
562570
ExpectedRustTokens::Contains(quote! {
563571
#[repr(C)]
564572
pub enum ResultVoidAndSomeErrEnum{
573+
#[allow(unused)]
565574
Ok,
575+
#[allow(unused)]
566576
Err(__swift_bridge__SomeErrEnum),
567577
}
568578

@@ -628,12 +638,15 @@ mod extern_rust_fn_return_result_tuple_type_and_transparent_enum_type {
628638
}
629639
}
630640

641+
// Allows unused to avoid dead_code warnings in Rust 1.79.0 or later.
631642
fn expected_rust_tokens() -> ExpectedRustTokens {
632643
ExpectedRustTokens::ContainsMany(vec![
633644
quote! {
634645
#[repr(C)]
635646
pub enum ResultTupleI32U32AndSomeErrEnum{
647+
#[allow(unused)]
636648
Ok(__swift_bridge__tuple_I32U32),
649+
#[allow(unused)]
637650
Err(__swift_bridge__SomeErrEnum),
638651
}
639652
},

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)