Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Deriving NetworkBehaviour without specifying OutEvent gives compilation error: use of undeclared type NetworkBehaviour #2921

Closed
folex opened this issue Sep 20, 2022 · 3 comments

Comments

@folex
Copy link
Contributor

folex commented Sep 20, 2022

Summary

The following snippet gives a compilation error

use libp2p::identify::Identify;
use libp2p::NetworkBehaviour;
use libp2p::ping::Ping;

#[derive(NetworkBehaviour)]
pub struct Behaviour {
    identify: Identify,
    ping: Ping,
}

Here's a repo with that snippet, it reproduces the error on compilation https://github.com/folex/libp2p-network-behaviour-derive-error/tree/master

Expected behaviour

I expect macro to derive OutEvent enum automatically, as specified in doc

// If we find a #[behaviour(out_event = "Foo")] attribute on the
// struct, we set Foo as the out event. If not, the OutEvent is
// generated.

Actual behaviour

Compilation error

error[E0433]: failed to resolve: use of undeclared type `NetworkBehaviour`
 --> src/main.rs:5:10
  |
5 | #[derive(NetworkBehaviour)]
  |          ^^^^^^^^^^^^^^^^ use of undeclared type `NetworkBehaviour`
  |
  = note: this error originates in the derive macro `NetworkBehaviour` (in Nightly builds, run with -Z macro-backtrace for more info)

Debug Output

<output>

Possible Solution

Adding an out_event with a enum name yields no compilation errors.

Version

libp2p = "0.48.0"
libp2p-core = { version = "0.36.0", default-features = false, features = [ "secp256k1" ] }
libp2p-metrics = { version = "0.9.0", features = ["kad"] }
libp2p-noise = "0.39.0"
libp2p-swarm = "0.39.0"
$ rustup show
active toolchain
----------------
nightly-x86_64-apple-darwin (default)

$ cargo --version
cargo 1.65.0-nightly (4ed54cecc 2022-08-27)

Would you like to work on fixing this bug?

Maybe

@elenaf9
Copy link
Contributor

elenaf9 commented Sep 20, 2022

Thank you for the detailed bug report @folex!
I believe the issue is that when generating the out event the macro references the NetworkBehavour trait, which you don't have imported:

quote! {#variant(<#ty as NetworkBehaviour>::OutEvent)}

So quick fix for you would be to just import the trait from libp2p::swarm::NetworkBehaviour (right now you only have the macro itself imported).

That being said, ideally this shouldn't be necessary if we reference the full path in the swarm-derive code like it is done here:

let trait_to_impl = quote! {::libp2p::swarm::NetworkBehaviour};

Would you be interested in doing a PR with this fix?

@folex
Copy link
Contributor Author

folex commented Sep 21, 2022

Thanks for the explanation, sorry I didn't see it myself. Name clashing confused me a bunch.

Yep, I will submit a PR today!

@elenaf9
Copy link
Contributor

elenaf9 commented Sep 27, 2022

Fixed with #2932. Thanks @folex!

@elenaf9 elenaf9 closed this as completed Sep 27, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants