Unverified Commit 1beac25d authored by thiolliere's avatar thiolliere Committed by GitHub
Browse files

Fix type inference in derive decode (#254)


Co-authored-by: default avatarBastian Köcher <bkchr@users.noreply.github.com>
parent d7ed5038
Pipeline #125968 passed with stages
in 20 minutes and 48 seconds
......@@ -4,14 +4,19 @@ All notable changes to this crate are documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this crate adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [2.0.1] - 2021-02-26
### Fix
- Fix type inference issue in `Decode` derive macro. Pr #254
## [2.0.0] - 2021-01-26
### Added
- `Decode::skip` allows to skip some encoded types. Pr #243
- `Decode::encoded_fixed_size` allows to get the fixed encoded size of a type. PR #243
- `Error` now contains a chain of causes. This full error description can also be activated on
no std using the feature `chain-error`. PR #242
- `Encode::encoded_size` allows to get the encoded size of a type more efficiently. PR #245
no std using the feature `chain-error`. PR #242
- `Encode::encoded_size` allows to get the encoded size of a type more efficiently. PR #245
### Changed
- `CompactAs::decode_from` now returns result. This allow for decoding to fail from their compact
......
This diff is collapsed.
[package]
name = "parity-scale-codec"
description = "SCALE - Simple Concatenating Aggregated Little Endians"
version = "2.0.0"
version = "2.0.1"
authors = ["Parity Technologies <admin@parity.io>"]
license = "Apache-2.0"
repository = "https://github.com/paritytech/parity-scale-codec"
......@@ -11,7 +11,7 @@ edition = "2018"
[dependencies]
arrayvec = { version = "0.5.1", default-features = false, features = ["array-sizes-33-128", "array-sizes-129-255"] }
serde = { version = "1.0.102", optional = true }
parity-scale-codec-derive = { path = "derive", version = "2.0.0", default-features = false, optional = true }
parity-scale-codec-derive = { path = "derive", version = "2.0.1", default-features = false, optional = true }
bitvec = { version = "0.20.1", default-features = false, features = ["alloc"], optional = true }
byte-slice-cast = { version = "1.0.0", default-features = false }
generic-array = { version = "0.14.4", optional = true }
......
[package]
name = "parity-scale-codec-derive"
description = "Serialization and deserialization derive macro for Parity SCALE Codec"
version = "2.0.0"
version = "2.0.1"
authors = ["Parity Technologies <admin@parity.io>"]
license = "Apache-2.0"
edition = "2018"
......@@ -16,4 +16,4 @@ proc-macro2 = "1.0.6"
proc-macro-crate = "0.1.4"
[dev-dependencies]
parity-scale-codec = { path = "..", version = "2.0.0" }
parity-scale-codec = { path = "..", version = "2.0.1" }
......@@ -20,11 +20,22 @@ use syn::{
use crate::utils;
pub fn quote(data: &Data, type_name: &Ident, input: &TokenStream) -> TokenStream {
/// Generate function block for function `Decode::decode`.
///
/// * data: data info of the type,
/// * type_name: name of the type,
/// * type_generics: the generics of the type in turbofish format, without bounds, e.g. `::<T, I>`
/// * input: the variable name for the argument of function `decode`.
pub fn quote(
data: &Data,
type_name: &Ident,
type_generics: &TokenStream,
input: &TokenStream,
) -> TokenStream {
match *data {
Data::Struct(ref data) => match data.fields {
Fields::Named(_) | Fields::Unnamed(_) => create_instance(
quote! { #type_name },
quote! { #type_name #type_generics },
&type_name.to_string(),
input,
&data.fields,
......@@ -50,7 +61,7 @@ pub fn quote(data: &Data, type_name: &Ident, input: &TokenStream) -> TokenStream
let index = utils::variant_index(v, i);
let create = create_instance(
quote! { #type_name :: #name },
quote! { #type_name #type_generics :: #name },
&format!("{}::{}", type_name, name),
input,
&v.fields,
......@@ -127,9 +138,10 @@ fn create_decode_expr(field: &Field, name: &str, input: &TokenStream) -> TokenSt
} else if skip {
quote_spanned! { field.span() => Default::default() }
} else {
let field_type = &field.ty;
quote_spanned! { field.span() =>
{
let #res = _parity_scale_codec::Decode::decode(#input);
let #res = <#field_type as _parity_scale_codec::Decode>::decode(#input);
match #res {
Err(e) => return Err(e.chain(#err_msg)),
Ok(#res) => #res,
......
......@@ -194,9 +194,10 @@ pub fn decode_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let ty_gen_turbofish = ty_generics.as_turbofish();
let input_ = quote!(__codec_input_edqy);
let decoding = decode::quote(&input.data, name, &input_);
let decoding = decode::quote(&input.data, name, &quote!(#ty_gen_turbofish), &input_);
let impl_block = quote! {
impl #impl_generics _parity_scale_codec::Decode for #name #ty_generics #where_clause {
......
// Copyright 2021 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Test for type inference issue in decode.
#[cfg(not(feature = "derive"))]
use parity_scale_codec_derive::Decode;
use parity_scale_codec::Decode;
pub trait Trait {
type Value;
type AccountId: Decode;
}
#[derive(Decode)]
pub enum A<T: Trait> {
_C(
(T::AccountId, T::AccountId),
Vec<(T::Value, T::Value)>,
),
}
#[derive(Decode)]
pub struct B<T: Trait>((T::AccountId, T::AccountId), Vec<(T::Value, T::Value)>);
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