Unverified Commit d3c7e7e3 authored by Bastian Köcher's avatar Bastian Köcher Committed by GitHub
Browse files

Fixes unnamed field access in `parity-codec-derive` (#68)

We can not just use `usize`, because `quote!` converts `let i: usize =
0;` to `0usize` and this is not accepted as field accessor.
parent bdcbe67f
......@@ -19,8 +19,8 @@ version = "3.3.0"
dependencies = [
"arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 3.3.0",
"serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -29,8 +29,8 @@ version = "3.3.0"
dependencies = [
"proc-macro-crate 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.31 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -51,7 +51,7 @@ dependencies = [
[[package]]
name = "quote"
version = "0.6.11"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -59,26 +59,26 @@ dependencies = [
[[package]]
name = "serde"
version = "1.0.88"
version = "1.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde_derive"
version = "1.0.88"
version = "1.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.31 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "0.15.26"
version = "0.15.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
......@@ -87,7 +87,7 @@ name = "toml"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -100,9 +100,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945"
"checksum proc-macro-crate 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4c6cf4e5b00300d151dfffae39f529dfa5188f42eeb14201229aa420d6aad10c"
"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915"
"checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1"
"checksum serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)" = "9f301d728f2b94c9a7691c90f07b0b4e8a4517181d9461be94c04bddeb4bd850"
"checksum serde_derive 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)" = "beed18e6f5175aef3ba670e57c60ef3b1b74d250d962a26604bff4c80e970dd4"
"checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9"
"checksum quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faf4799c5d274f3868a4aae320a0a182cbd2baee377b378f080e16a23e9d80db"
"checksum serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)" = "aa5f7c20820475babd2c077c3ab5f8c77a31c15e16ea38687b4c02d3e48680f4"
"checksum serde_derive 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)" = "58fc82bec244f168b23d1963b45c8bf5726e9a15a9d146a067f9081aeed2de79"
"checksum syn 0.15.31 (registry+https://github.com/rust-lang/crates.io-index)" = "d2b4cfac95805274c6afdb12d8f770fa2d27c045953e7b630a81801953699a9a"
"checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
......@@ -10,7 +10,7 @@ edition = "2018"
proc-macro = true
[dependencies]
syn = { version = "0.15.26", features = [ "full", "visit" ] }
syn = { version = "0.15.31", features = [ "full", "visit" ] }
quote = "0.6"
proc-macro2 = "0.4"
proc-macro-crate = "0.1.3"
......
......@@ -131,7 +131,7 @@ fn encode_fields<F>(
}
fn try_impl_encode_single_field_optimisation(data: &Data) -> Option<TokenStream> {
let ref closure = quote!(f);
let closure = &quote!(f);
fn filter_skip_named<'a>(fields: &'a syn::FieldsNamed) -> impl Iterator<Item=&Field> + 'a {
fields.named.iter()
......@@ -149,7 +149,7 @@ fn try_impl_encode_single_field_optimisation(data: &Data) -> Option<TokenStream>
match data.fields {
Fields::Named(ref fields) if filter_skip_named(fields).count() == 1 => {
let field = filter_skip_named(fields).next().unwrap();
let ref name = field.ident;
let name = &field.ident;
Some(encode_single_field(
closure,
field,
......@@ -157,11 +157,13 @@ fn try_impl_encode_single_field_optimisation(data: &Data) -> Option<TokenStream>
))
},
Fields::Unnamed(ref fields) if filter_skip_unnamed(fields).count() == 1 => {
let (ids, field) = filter_skip_unnamed(fields).next().unwrap();
let (id, field) = filter_skip_unnamed(fields).next().unwrap();
let id = syn::Index::from(id);
Some(encode_single_field(
closure,
field,
quote!(&self.#ids)
quote!(&self.#id)
))
},
_ => None,
......@@ -181,7 +183,7 @@ fn try_impl_encode_single_field_optimisation(data: &Data) -> Option<TokenStream>
fn impl_encode(data: &Data, type_name: &Ident) -> TokenStream {
let self_ = quote!(self);
let ref dest = quote!(dest);
let dest = &quote!(dest);
let encoding = match *data {
Data::Struct(ref data) => {
match data.fields {
......@@ -193,7 +195,10 @@ fn impl_encode(data: &Data, type_name: &Ident) -> TokenStream {
Fields::Unnamed(ref fields) => encode_fields(
dest,
&fields.unnamed,
|i, _| quote!(&#self_.#i),
|i, _| {
let i = syn::Index::from(i);
quote!(&#self_.#i)
},
),
Fields::Unit => quote!(drop(#dest);),
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment