diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index f18779d78aa..6ec2bd4e362 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -398,7 +398,16 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result return std::task::Poll::Ready(e.map_out(#map_out_event).map_in(#map_in_event)), + std::task::Poll::Ready(e) => { + // For a field whose `ToSwarm` is uninhabited (e.g. `Infallible`, as with + // `connection_limits::Behaviour`), no `e` can ever reach this arm, so the + // call below is unreachable in practice. Recent nightly lints that as + // `unreachable_code` under `-D warnings`; the arm itself must still be + // generated so this compiles for every other field whose `ToSwarm` is + // inhabited. + #[allow(unreachable_code)] + return std::task::Poll::Ready(e.map_out(#map_out_event).map_in(#map_in_event)); + } std::task::Poll::Pending => {}, } } diff --git a/swarm/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index 817a2990380..f69e5a20ed3 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -613,6 +613,23 @@ fn custom_out_event_no_type_parameters() { require_net_behaviour::>(); } +// Regression test for https://github.com/libp2p/rust-libp2p/issues/6600: a field whose +// `NetworkBehaviour::ToSwarm` is uninhabited (`dummy::Behaviour`'s is `Infallible`) must not +// make the generated `poll()` fail to compile under `-D warnings` on toolchains that lint the +// resulting unreachable `Poll::Ready(e) => ...` arm as `unreachable_code`. +#[test] +fn uninhabited_to_swarm_field_compiles() { + #[allow(dead_code)] + #[derive(NetworkBehaviour)] + #[behaviour(prelude = "libp2p_swarm::derive_prelude")] + struct Foo { + ping: ping::Behaviour, + limits: dummy::Behaviour, + } + + require_net_behaviour::(); +} + #[test] fn ui() { let t = trybuild::TestCases::new();