Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions derive/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EncOut: _parity_scale_codec::Output>(&self, dest: &mut EncOut) {
fn encode_to<EncOut: _parity_scale_codec::Output>(&#i_self, dest: &mut EncOut) {
_parity_scale_codec::Encode::encode_to(&#final_field_variable, dest)
}

fn encode(&self) -> _parity_scale_codec::alloc::vec::Vec<u8> {
fn encode(&#i_self) -> _parity_scale_codec::alloc::vec::Vec<u8> {
_parity_scale_codec::Encode::encode(&#final_field_variable)
}

fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&#i_self, f: F) -> R {
_parity_scale_codec::Encode::using_encoded(&#final_field_variable, f)
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);
}