Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion swarm-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,16 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke

quote! {
match #trait_to_impl::poll(&mut self.#field, cx) {
std::task::Poll::Ready(e) => 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 => {},
}
}
Expand Down
17 changes: 17 additions & 0 deletions swarm/tests/swarm_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,23 @@ fn custom_out_event_no_type_parameters() {
require_net_behaviour::<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::<Foo>();
}

#[test]
fn ui() {
let t = trybuild::TestCases::new();
Expand Down
Loading