bincode icon indicating copy to clipboard operation
bincode copied to clipboard

`Encode` derive macro breaks when inside macro

Open AlexApps99 opened this issue 3 years ago • 2 comments

error[E0425]: cannot find value `field_0` in this scope
  --> bug/src/lib.rs:4:29
   |
1  | / macro_rules! dv {
2  | |     ($($t:item),+) => {
3  | |         $(
4  | |             #[derive(Clone, bincode::Encode, bincode::Decode)]
   | |                             ^^^^^^^^^^^^^^^
   | |                             |
   | |                             not found in this scope
   | |                             in this derive macro expansion (#2)
...  |
7  | |     }
8  | | }
   | |_- in this expansion of `dv!` (#1)
...
11 | / dv! {
12 | |     pub struct Foo {},
13 | |     pub enum Bar {
14 | |         A(Foo),
15 | |         B,
16 | |     }
17 | | }
   | |_- in this macro invocation (#1)
   |
  ::: /home/lxb/.cargo/registry/src/github.com-1ecc6299db9ec823/bincode_derive-2.0.0-rc.1/src/lib.rs:9:1
   |
9  |   pub fn derive_encode(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
   |   ------------------------------------------------------------------------------- in this expansion of `#[derive(bincode::Encode)]` (#2)
macro_rules! dv {
    ($($t:item),+) => {
        $(
            #[derive(Clone, bincode::Encode, bincode::Decode)]
            $t
        )+
    }
}

// Doesn't compile
dv! {
    pub struct Foo {},
    pub enum Bar {
        A(Foo),
        B,
    }
}

// Compiles
#[derive(Clone, bincode::Encode, bincode::Decode)]
pub struct Foo2 {}
#[derive(Clone, bincode::Encode, bincode::Decode)]
pub enum Bar2 {
    A(Foo2),
    B,
}
[package]
name = "bug"
version = "0.1.0"
edition = "2021"

[dependencies]
bincode = { version = "2.0.0-rc.1", default-features = false, features = ["derive"] }

This doesn't seem to happen with Decode

AlexApps99 avatar Jun 21 '22 00:06 AlexApps99

This looks like a macro hygiene thing, and I don't see an easy fix.

trevyn avatar Oct 09 '22 03:10 trevyn