paritytech/parity-scale-codec

MaxEncodedLen derive not adding bounds when used with compact

Opened this issue · 2 comments

With this code:

// Cargo.toml
// parity-scale-codec = { version = "3.1.3", features = ["max-encoded-len"] }

use parity_scale_codec::{Decode, Encode, HasCompact, MaxEncodedLen};

#[derive(Encode, Decode, MaxEncodedLen)]
pub struct Broken<T: HasCompact> {
    #[codec(compact)]
    compact: T,
}

This is the error:

no function or associated item named `max_encoded_len` found for type parameter `T` in the current scope
items from traits can only be used if the type parameter is bounded by the trait

Expanded:

impl<T: HasCompact> ::parity_scale_codec::MaxEncodedLen for Broken<T>
where
    T: ::parity_scale_codec::HasCompact, // missing bound!
{
    fn max_encoded_len() -> ::core::primitive::usize {
        0_usize.saturating_add(<T>::max_encoded_len())
    }
}

Works with parity-scale-codec-derive pinned to 3.1.2 (expanded output shown):

// Cargo.toml
// parity-scale-codec = { version = "3", features = ["max-encoded-len"] }
// parity-scale-codec-derive = { version = "=3.1.2", features = ["max-encoded-len"] }

impl<T: HasCompact + ::parity_scale_codec::MaxEncodedLen> ::parity_scale_codec::MaxEncodedLen
    for Broken<T>
{
    fn max_encoded_len() -> ::core::primitive::usize {
        0_usize.saturating_add(<T>::max_encoded_len())
    }
}

This is the commit that broke it: 24c4856 since 361a0bf works.

bkchr commented

So is it now working or not?

Still broken, 361a0bf was the last working commit (sorry for any confusion there!)