diff --git a/derive/src/encode.rs b/derive/src/encode.rs index 834d389b..d55b87c6 100644 --- a/derive/src/encode.rs +++ b/derive/src/encode.rs @@ -70,16 +70,19 @@ fn encode_single_field( } }; + // This may have different hygiene than the field span + let i_self = quote! { self }; + quote_spanned! { field.span() => - fn encode_to(&self, dest: &mut EncOut) { + fn encode_to(&#i_self, dest: &mut EncOut) { _parity_scale_codec::Encode::encode_to(&#final_field_variable, dest) } - fn encode(&self) -> _parity_scale_codec::alloc::vec::Vec { + fn encode(&#i_self) -> _parity_scale_codec::alloc::vec::Vec { _parity_scale_codec::Encode::encode(&#final_field_variable) } - fn using_encoded R>(&self, f: F) -> R { + fn using_encoded R>(&#i_self, f: F) -> R { _parity_scale_codec::Encode::using_encoded(&#final_field_variable, f) } } diff --git a/tests/mod.rs b/tests/mod.rs index c885e234..04c87d74 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -526,3 +526,18 @@ fn crafted_input_for_vec_t() { ); } +#[test] +fn weird_derive() { + // Tests that compilation succeeds when the macro invocation + // hygiene context is different from the field hygiene context. + macro_rules! make_struct { + (#[$attr:meta]) => ( + #[$attr] + pub struct MyStruct { + field: u8 + } + ) + } + + make_struct!(#[derive(Encode, Decode)]); +}