Skip to content

Commit 327d02a

Browse files
committed
Fix dead_code warning when returning Result with bridged type in error case
As of Rust 1.79.0, dead code warnings are emitted from enums where the compiler cannot infer that the contents are used. Since presumably the interior of the error _will_ by used by Swift, the enum can be annotated to allow the code to be generated without warnings.
1 parent 717fcef commit 327d02a

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,9 @@ impl BuiltInResult {
378378
custom_rust_ffi_types.push(quote! {
379379
#[repr(C)]
380380
pub enum #ty {
381+
#[allow(unused)]
381382
Ok #ok,
383+
#[allow(unused)]
382384
Err(#err),
383385
}
384386
});

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ 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),
416+
#[allow(unused)]
415417
Err(__swift_bridge__SomeErrEnum),
416418
}
417419

@@ -488,7 +490,9 @@ mod extern_rust_fn_return_result_transparent_enum_type_and_opaque_rust_type {
488490
ExpectedRustTokens::Contains(quote! {
489491
#[repr(C)]
490492
pub enum ResultSomeOkEnumAndSomeErrType{
493+
#[allow(unused)]
491494
Ok(__swift_bridge__SomeOkEnum),
495+
#[allow(unused)]
492496
Err(*mut super::SomeErrType),
493497
}
494498

@@ -562,7 +566,9 @@ mod extern_rust_fn_return_result_unit_type_and_transparent_enum_type {
562566
ExpectedRustTokens::Contains(quote! {
563567
#[repr(C)]
564568
pub enum ResultVoidAndSomeErrEnum{
569+
#[allow(unused)]
565570
Ok,
571+
#[allow(unused)]
566572
Err(__swift_bridge__SomeErrEnum),
567573
}
568574

@@ -633,7 +639,9 @@ mod extern_rust_fn_return_result_tuple_type_and_transparent_enum_type {
633639
quote! {
634640
#[repr(C)]
635641
pub enum ResultTupleI32U32AndSomeErrEnum{
642+
#[allow(unused)]
636643
Ok(__swift_bridge__tuple_I32U32),
644+
#[allow(unused)]
637645
Err(__swift_bridge__SomeErrEnum),
638646
}
639647
},

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)