Skip to content

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

Closed
@ghost

Description

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions