Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Rust 1.30: importing derive macro with use works or fails depending on whether unrelated dependency is present #55386

Closed
ghost opened this issue Oct 26, 2018 · 2 comments

Comments

@ghost
Copy link

ghost commented Oct 26, 2018

Using Rust 1.30 it is possible to bring macros into scope using use instead of #[macro_use]. I am therefore (perhaps a bit over-zealously) trying to get rid of all instances of #[macro_use] in my crates, when I hit upon what seems to be a strange edge case regarding the following bit of code:

extern crate serde;

use serde::Serialize;

#[derive(Serialize)]
pub enum Foo {
    Bar,
}

Using the following Cargo.toml, which includes a dependency on ron which is not used in the code:

[package]
name = "foo"
version = "0.1.0"

[dependencies]
serde = "1.0.78"
ron = "0.4.0"

This compiles fine:

 % cargo build    
    Finished dev [unoptimized + debuginfo] target(s) in 1.65s

However when I remove the dependency on ron (which itself depends on serde) from my Cargo.toml:

[package]
name = "foo"
version = "0.1.0"

[dependencies]
serde = "1.0.78"

Then I get a compile error:

 % cargo build
   Compiling foo v0.1.0 (/sysroot/home/bram.senders/source/lws-connector-tmp)                  
error: cannot find derive macro `Serialize` in this scope                                      
 --> src/lib.rs:5:10                                                                           
  |                                                                                            
5 | #[derive(Serialize)]                                                                       
  |          ^^^^^^^^^                                                                         
                                                                                               
error: aborting due to previous error                                                          
                                                                                               
error: Could not compile `foo`.                                                                

Meta

 % rustc --version --verbose
rustc 1.30.0 (da5f414c2 2018-10-24)
binary: rustc
commit-hash: da5f414c2c0bfe5198934493f04c676e2b23ff2e
commit-date: 2018-10-24
host: x86_64-unknown-linux-gnu
release: 1.30.0
LLVM version: 8.0
@sfackler
Copy link
Member

ron enables serde's serde_derive feature which reexports the derive macros. If you stop depending on ron, that feature stops being activated. You can either directly depend on the serde_derive crate or enable the feature yourself: https://docs.rs/crate/ron/0.4.0/source/Cargo.toml.orig.

@ghost
Copy link
Author

ghost commented Nov 2, 2018

Aaah, I see! I had never realized before that a dependant crate could modify which features I can use from another crate. So this is not a bug. Sorry for the noise and thank you very much for the explanation!

@ghost ghost closed this as completed Nov 2, 2018
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant