Skip to content

Commit 879d9ca

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 879d9ca

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,16 @@ 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 in code generated by procedural macros began to be surfaced to
411+
// callers of the macros. While the wrapped data in results returned to Swift will presumably be read,
412+
// rustc does not see its usage and issues a warning.
410413
fn expected_rust_tokens() -> ExpectedRustTokens {
411414
ExpectedRustTokens::Contains(quote! {
412415
#[repr(C)]
413416
pub enum ResultSomeOkTypeAndSomeErrEnum{
417+
#[allow(unused)]
414418
Ok(*mut super::SomeOkType),
419+
#[allow(unused)]
415420
Err(__swift_bridge__SomeErrEnum),
416421
}
417422

@@ -484,11 +489,14 @@ mod extern_rust_fn_return_result_transparent_enum_type_and_opaque_rust_type {
484489
}
485490
}
486491

492+
// Allows unused to avoid dead_code warnings in Rust 1.79.0 or later.
487493
fn expected_rust_tokens() -> ExpectedRustTokens {
488494
ExpectedRustTokens::Contains(quote! {
489495
#[repr(C)]
490496
pub enum ResultSomeOkEnumAndSomeErrType{
497+
#[allow(unused)]
491498
Ok(__swift_bridge__SomeOkEnum),
499+
#[allow(unused)]
492500
Err(*mut super::SomeErrType),
493501
}
494502

@@ -558,11 +566,14 @@ mod extern_rust_fn_return_result_unit_type_and_transparent_enum_type {
558566
}
559567
}
560568

569+
// Allows unused to avoid dead_code warnings in Rust 1.79.0 or later.
561570
fn expected_rust_tokens() -> ExpectedRustTokens {
562571
ExpectedRustTokens::Contains(quote! {
563572
#[repr(C)]
564573
pub enum ResultVoidAndSomeErrEnum{
574+
#[allow(unused)]
565575
Ok,
576+
#[allow(unused)]
566577
Err(__swift_bridge__SomeErrEnum),
567578
}
568579

@@ -628,12 +639,15 @@ mod extern_rust_fn_return_result_tuple_type_and_transparent_enum_type {
628639
}
629640
}
630641

642+
// Allows unused to avoid dead_code warnings in Rust 1.79.0 or later.
631643
fn expected_rust_tokens() -> ExpectedRustTokens {
632644
ExpectedRustTokens::ContainsMany(vec![
633645
quote! {
634646
#[repr(C)]
635647
pub enum ResultTupleI32U32AndSomeErrEnum{
648+
#[allow(unused)]
636649
Ok(__swift_bridge__tuple_I32U32),
650+
#[allow(unused)]
637651
Err(__swift_bridge__SomeErrEnum),
638652
}
639653
},

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)