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

Derive fails for macro-generated struct #613

Closed
rsaarelm opened this issue Nov 7, 2016 · 4 comments
Closed

Derive fails for macro-generated struct #613

rsaarelm opened this issue Nov 7, 2016 · 4 comments
Assignees

Comments

@rsaarelm
Copy link

rsaarelm commented Nov 7, 2016

Script to reproduce:

#!/bin/bash

cargo new struct-macro-bug
cd struct-macro-bug

cat > Cargo.toml <<'EOF'
[package]
name = "serde-bug"
version = "0.1.0"

[dependencies]
serde = "0.8"
serde_derive = "0.8"
EOF

cat > src/lib.rs <<'EOF'
#![feature(proc_macro)]

extern crate serde;
#[macro_use]
extern crate serde_derive;

#[derive(Serialize, Deserialize)]
pub struct ComponentData<C> {
    _data: C,
}

macro_rules! Struct {
    {
        $($compname:ident: $comptype:ty,)+
    } => {
        #[derive(Serialize, Deserialize)]
        pub struct _ComponentStore {
            $(pub $compname: $crate::ComponentData<$comptype>),+
        }
    }
}

#[cfg(test)]
mod tests {
    #[derive(Serialize, Deserialize)]
    pub struct X;

    Struct!{a: X,}
    #[test]
    fn it_works() {
    }
}
EOF

cargo test
rustc --version

Output:

     Created library `struct-macro-bug` project
    Updating registry `https://github.com/rust-lang/crates.io-index`
   Compiling unicode-xid v0.0.3
   Compiling quote v0.3.5
   Compiling serde v0.8.17
   Compiling syn v0.9.2
   Compiling serde_codegen_internals v0.10.0
   Compiling post-expansion v0.1.0
   Compiling serde_codegen v0.8.17
   Compiling serde_derive v0.8.17
   Compiling serde-bug v0.1.0 (file:///home/rsaarelm/scratch/serde-bug/struct-macro-bug)
error: custom derive attribute panicked
  --> src/lib.rs:16:18
   |
16 |         #[derive(Serialize, Deserialize)]
   |                  ^^^^^^^^^
...
28 |     Struct!{a: X,}
   |     -------------- in this macro invocation
   |
   = help: message: called `Result::unwrap()` on an `Err` value: "failed to parse macro input: \"#[derive(Serialize)]\\n#[derive(Deserialize)]\\npub struct _ComponentStore {\\n    pub a: $crate::ComponentData<X>,\\n}\\n\""

Build failed, waiting for other jobs to finish...
error: Could not compile `serde-bug`.

To learn more, run the command again with --verbose.
rustc 1.14.0-nightly (cae6ab1c4 2016-11-05)
@rsaarelm rsaarelm added the bug label Nov 7, 2016
@dtolnay
Copy link
Member

dtolnay commented Nov 7, 2016

Thanks for the script! This is a Rust bug. I minimized it some more and opened rust-lang/rust#37637.

@rsaarelm rsaarelm added the derive label Nov 7, 2016
@rsaarelm
Copy link
Author

rsaarelm commented Nov 8, 2016

Thanks! I'll try to get back here when the fix has landed in nightly and I've tried it.

@dtolnay
Copy link
Member

dtolnay commented Nov 10, 2016

The fix merged into Rust. I am leaving this issue open until you or I confirm the fix.

@dtolnay
Copy link
Member

dtolnay commented Nov 19, 2016

This is fixed in serde 0.8.18 and rustc 1.15.0-nightly (ac635aa95 2016-11-18).

     Created library `struct-macro-bug` project
    Updating registry `https://github.com/rust-lang/crates.io-index`
 Downloading serde_derive v0.8.18
   Compiling unicode-xid v0.0.3
   Compiling serde v0.8.18
   Compiling quote v0.3.5
   Compiling syn v0.10.3
   Compiling serde_codegen_internals v0.11.1
   Compiling serde_codegen v0.8.18
   Compiling serde_derive v0.8.18
   Compiling serde-bug v0.1.0
    Finished debug [unoptimized + debuginfo] target(s) in 14.30 secs
     Running target/debug/deps/serde_bug-cc3a865be4a1ef0c

running 1 test
test tests::it_works ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

   Doc-tests serde-bug

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured

rustc 1.15.0-nightly (ac635aa95 2016-11-18)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants