From 4d3b9f6c6c06ed3031e082e75361d7bed8b4b3b3 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 17 May 2021 11:34:12 +0100 Subject: [PATCH 1/9] Add 'static bound to `TypeInfo` trait --- README.md | 4 +-- derive/src/trait_bounds.rs | 8 ++--- src/build.rs | 8 ++--- src/impls.rs | 30 +++++++++---------- src/lib.rs | 4 +-- src/tests.rs | 4 +-- src/ty/fields.rs | 2 +- src/ty/mod.rs | 2 +- test_suite/tests/ui/fail_missing_derive.rs | 2 +- .../tests/ui/fail_missing_derive.stderr | 2 +- .../tests/ui/fail_non_static_lifetime.rs | 2 +- test_suite/tests/ui/fail_unions.rs | 2 +- test_suite/tests/ui/fail_unions.stderr | 2 +- ...use_codec_attrs_without_deriving_encode.rs | 2 +- .../tests/ui/pass_basic_generic_type.rs | 2 +- ...s_complex_generic_self_referential_type.rs | 2 +- .../tests/ui/pass_non_static_lifetime.rs | 2 +- test_suite/tests/ui/pass_self_referential.rs | 2 +- .../tests/ui/pass_with_valid_codec_attrs.rs | 2 +- 19 files changed, 42 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 86a24411..005f083a 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ struct Foo { impl TypeInfo for Foo where - T: TypeInfo + 'static, + T: TypeInfo, { type Identity = Self; @@ -146,7 +146,7 @@ enum Foo{ impl TypeInfo for Foo where - T: TypeInfo + 'static, + T: TypeInfo, { type Identity = Self; diff --git a/derive/src/trait_bounds.rs b/derive/src/trait_bounds.rs index 65da4257..0a870205 100644 --- a/derive/src/trait_bounds.rs +++ b/derive/src/trait_bounds.rs @@ -31,7 +31,7 @@ use syn::{ use crate::utils; -/// Generates a where clause for a `TypeInfo` impl, adding `TypeInfo + 'static` bounds to all +/// Generates a where clause for a `TypeInfo` impl, adding `TypeInfo` bounds to all /// relevant generic types including associated types (e.g. `T::A: TypeInfo`), correctly dealing /// with self-referential types. pub fn make_where_clause<'a>( @@ -66,18 +66,18 @@ pub fn make_where_clause<'a>( types.into_iter().for_each(|(ty, is_compact)| { // Compact types need extra bounds, T: HasCompact and ::Type: TypeInfo + 'static + // HasCompact>::Type: TypeInfo if is_compact { where_clause .predicates .push(parse_quote!(#ty : :: #parity_scale_codec ::HasCompact)); where_clause .predicates - .push(parse_quote!(<#ty as :: #parity_scale_codec ::HasCompact>::Type : :: #scale_info ::TypeInfo + 'static)); + .push(parse_quote!(<#ty as :: #parity_scale_codec ::HasCompact>::Type : :: #scale_info ::TypeInfo)); } else { where_clause .predicates - .push(parse_quote!(#ty : :: #scale_info ::TypeInfo + 'static)); + .push(parse_quote!(#ty : :: #scale_info ::TypeInfo)); } }); diff --git a/src/build.rs b/src/build.rs index 3bad9718..869aa1cc 100644 --- a/src/build.rs +++ b/src/build.rs @@ -30,7 +30,7 @@ //! //! impl TypeInfo for Foo //! where -//! T: TypeInfo + 'static, +//! T: TypeInfo, //! { //! type Identity = Self; //! @@ -74,7 +74,7 @@ //! //! impl TypeInfo for Foo //! where -//! T: TypeInfo + 'static, +//! T: TypeInfo, //! { //! type Identity = Self; //! @@ -264,7 +264,7 @@ impl FieldsBuilder { pub fn compact_of(mut self, name: &'static str, type_name: &'static str) -> Self where T: scale::HasCompact, - ::Type: TypeInfo + 'static, + ::Type: TypeInfo, { self.fields .push(Field::compact_of::(Some(name), type_name)); @@ -286,7 +286,7 @@ impl FieldsBuilder { pub fn compact_of(mut self, type_name: &'static str) -> Self where T: scale::HasCompact, - ::Type: TypeInfo + 'static, + ::Type: TypeInfo, { self.fields.push(Field::compact_of::(None, type_name)); self diff --git a/src/impls.rs b/src/impls.rs index 16da5b06..440a8912 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -68,7 +68,7 @@ impl_metadata_for_primitives!( i128 => TypeDefPrimitive::I128, ); -impl TypeInfo for [T; N] { +impl TypeInfo for [T; N] { type Identity = Self; fn type_info() -> Type { @@ -113,7 +113,7 @@ impl_metadata_for_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P); impl TypeInfo for Vec where - T: TypeInfo + 'static, + T: TypeInfo, { type Identity = [T]; @@ -124,7 +124,7 @@ where impl TypeInfo for Option where - T: TypeInfo + 'static, + T: TypeInfo, { type Identity = Self; @@ -142,8 +142,8 @@ where impl TypeInfo for Result where - T: TypeInfo + 'static, - E: TypeInfo + 'static, + T: TypeInfo, + E: TypeInfo, { type Identity = Self; @@ -175,8 +175,8 @@ where impl TypeInfo for BTreeMap where - K: TypeInfo + 'static, - V: TypeInfo + 'static, + K: TypeInfo, + V: TypeInfo, { type Identity = Self; @@ -190,7 +190,7 @@ where impl TypeInfo for BTreeSet where - T: TypeInfo + 'static, + T: TypeInfo, { type Identity = Self; @@ -213,9 +213,9 @@ where } } -impl TypeInfo for &T +impl TypeInfo for &'static T where - T: TypeInfo + ?Sized + 'static, + T: TypeInfo + ?Sized, { type Identity = T; @@ -224,9 +224,9 @@ where } } -impl TypeInfo for &mut T +impl TypeInfo for &'static mut T where - T: TypeInfo + ?Sized + 'static, + T: TypeInfo + ?Sized, { type Identity = T; @@ -237,7 +237,7 @@ where impl TypeInfo for [T] where - T: TypeInfo + 'static, + T: TypeInfo, { type Identity = Self; @@ -264,7 +264,7 @@ impl TypeInfo for String { impl TypeInfo for PhantomData where - T: TypeInfo + ?Sized + 'static, + T: TypeInfo + ?Sized, { type Identity = Self; @@ -275,7 +275,7 @@ where impl TypeInfo for scale::Compact where - T: TypeInfo + 'static, + T: TypeInfo, { type Identity = Self; fn type_info() -> Type { diff --git a/src/lib.rs b/src/lib.rs index c482b34c..e6f6596e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -129,7 +129,7 @@ pub use self::{ pub use scale_info_derive::TypeInfo; /// Implementors return their meta type information. -pub trait TypeInfo { +pub trait TypeInfo: 'static { /// The type identifying for which type info is provided. /// /// # Note @@ -146,7 +146,7 @@ pub trait TypeInfo { /// Returns the runtime bridge to the types compile-time type information. pub fn meta_type() -> MetaType where - T: ?Sized + TypeInfo + 'static, + T: ?Sized + TypeInfo, { MetaType::new::() } diff --git a/src/tests.rs b/src/tests.rs index fea9a82e..2674e9b1 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -154,7 +154,7 @@ fn struct_with_generics() { impl TypeInfo for MyStruct where - T: TypeInfo + 'static, + T: TypeInfo, { type Identity = Self; @@ -193,7 +193,7 @@ fn basic_struct_with_phantoms() { impl TypeInfo for SomeStruct where - T: TypeInfo + 'static, + T: TypeInfo, { type Identity = Self; diff --git a/src/ty/fields.rs b/src/ty/fields.rs index 1ef06ce9..9543b472 100644 --- a/src/ty/fields.rs +++ b/src/ty/fields.rs @@ -135,7 +135,7 @@ impl Field { pub fn compact_of(name: Option<&'static str>, type_name: &'static str) -> Field where T: HasCompact, - ::Type: TypeInfo + 'static, + ::Type: TypeInfo, { Self::new(name, MetaType::new::<::Type>(), type_name) } diff --git a/src/ty/mod.rs b/src/ty/mod.rs index e3dd5cf4..5708d534 100644 --- a/src/ty/mod.rs +++ b/src/ty/mod.rs @@ -370,7 +370,7 @@ impl TypeDefSequence { /// compile-time type. pub fn of() -> Self where - T: TypeInfo + 'static, + T: TypeInfo, { Self::new(MetaType::new::()) } diff --git a/test_suite/tests/ui/fail_missing_derive.rs b/test_suite/tests/ui/fail_missing_derive.rs index 1a485c92..94a42c6f 100644 --- a/test_suite/tests/ui/fail_missing_derive.rs +++ b/test_suite/tests/ui/fail_missing_derive.rs @@ -11,7 +11,7 @@ struct Cat { paws: PawType, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::>(); diff --git a/test_suite/tests/ui/fail_missing_derive.stderr b/test_suite/tests/ui/fail_missing_derive.stderr index 54501aa1..9d14b20e 100644 --- a/test_suite/tests/ui/fail_missing_derive.stderr +++ b/test_suite/tests/ui/fail_missing_derive.stderr @@ -1,7 +1,7 @@ error[E0277]: the trait bound `PawType: TypeInfo` is not satisfied --> $DIR/fail_missing_derive.rs:17:5 | -14 | fn assert_type_info() {} +14 | fn assert_type_info() {} | -------- required by this bound in `assert_type_info` ... 17 | assert_type_info::>(); diff --git a/test_suite/tests/ui/fail_non_static_lifetime.rs b/test_suite/tests/ui/fail_non_static_lifetime.rs index 26e96e75..484ecbc1 100644 --- a/test_suite/tests/ui/fail_non_static_lifetime.rs +++ b/test_suite/tests/ui/fail_non_static_lifetime.rs @@ -5,7 +5,7 @@ struct Me<'a> { me: &'a Me<'a>, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::(); diff --git a/test_suite/tests/ui/fail_unions.rs b/test_suite/tests/ui/fail_unions.rs index 46bd4666..972d7b94 100644 --- a/test_suite/tests/ui/fail_unions.rs +++ b/test_suite/tests/ui/fail_unions.rs @@ -7,7 +7,7 @@ union Commonwealth { b: f32, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::(); diff --git a/test_suite/tests/ui/fail_unions.stderr b/test_suite/tests/ui/fail_unions.stderr index 68c189ed..39678df1 100644 --- a/test_suite/tests/ui/fail_unions.stderr +++ b/test_suite/tests/ui/fail_unions.stderr @@ -11,7 +11,7 @@ error: Unions not supported error[E0277]: the trait bound `Commonwealth: TypeInfo` is not satisfied --> $DIR/fail_unions.rs:13:24 | -10 | fn assert_type_info() {} +10 | fn assert_type_info() {} | -------- required by this bound in `assert_type_info` ... 13 | assert_type_info::(); diff --git a/test_suite/tests/ui/fail_use_codec_attrs_without_deriving_encode.rs b/test_suite/tests/ui/fail_use_codec_attrs_without_deriving_encode.rs index cddaed8f..625b29e3 100644 --- a/test_suite/tests/ui/fail_use_codec_attrs_without_deriving_encode.rs +++ b/test_suite/tests/ui/fail_use_codec_attrs_without_deriving_encode.rs @@ -7,7 +7,7 @@ struct AttrValidation { b: u16, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::(); diff --git a/test_suite/tests/ui/pass_basic_generic_type.rs b/test_suite/tests/ui/pass_basic_generic_type.rs index 90a2daa4..14c517cc 100644 --- a/test_suite/tests/ui/pass_basic_generic_type.rs +++ b/test_suite/tests/ui/pass_basic_generic_type.rs @@ -13,7 +13,7 @@ struct Cat { _paws: PawType, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::>(); diff --git a/test_suite/tests/ui/pass_complex_generic_self_referential_type.rs b/test_suite/tests/ui/pass_complex_generic_self_referential_type.rs index 3c3d80dc..af6c9dcb 100644 --- a/test_suite/tests/ui/pass_complex_generic_self_referential_type.rs +++ b/test_suite/tests/ui/pass_complex_generic_self_referential_type.rs @@ -34,7 +34,7 @@ struct Selfie { _nested: Box>, Selfie>>>>, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::>(); diff --git a/test_suite/tests/ui/pass_non_static_lifetime.rs b/test_suite/tests/ui/pass_non_static_lifetime.rs index 7b84f281..96356fbf 100644 --- a/test_suite/tests/ui/pass_non_static_lifetime.rs +++ b/test_suite/tests/ui/pass_non_static_lifetime.rs @@ -5,7 +5,7 @@ struct Me<'a> { _me: &'a Me<'a>, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::(); diff --git a/test_suite/tests/ui/pass_self_referential.rs b/test_suite/tests/ui/pass_self_referential.rs index 34a6e90b..b4f23267 100644 --- a/test_suite/tests/ui/pass_self_referential.rs +++ b/test_suite/tests/ui/pass_self_referential.rs @@ -5,7 +5,7 @@ struct Me { _me: Box, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::(); diff --git a/test_suite/tests/ui/pass_with_valid_codec_attrs.rs b/test_suite/tests/ui/pass_with_valid_codec_attrs.rs index 736ea943..a4469cbf 100644 --- a/test_suite/tests/ui/pass_with_valid_codec_attrs.rs +++ b/test_suite/tests/ui/pass_with_valid_codec_attrs.rs @@ -23,7 +23,7 @@ enum ValidEnum { Theng(ValidStruct), } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::(); From cef6e743b5a30d8f2959e4b66ea2868e8b0d0ce9 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 May 2021 16:24:57 +0100 Subject: [PATCH 2/9] Remove some more static bounds --- derive/src/trait_bounds.rs | 1 - src/build.rs | 4 ++-- src/impls.rs | 6 +++--- src/meta_type.rs | 2 +- src/ty/fields.rs | 4 ++-- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/derive/src/trait_bounds.rs b/derive/src/trait_bounds.rs index 0a870205..7d4e69d7 100644 --- a/derive/src/trait_bounds.rs +++ b/derive/src/trait_bounds.rs @@ -85,7 +85,6 @@ pub fn make_where_clause<'a>( let ident = type_param.ident.clone(); let mut bounds = type_param.bounds.clone(); bounds.push(parse_quote!(:: #scale_info ::TypeInfo)); - bounds.push(parse_quote!('static)); where_clause .predicates .push(parse_quote!( #ident : #bounds)); diff --git a/src/build.rs b/src/build.rs index 869aa1cc..a2220bc5 100644 --- a/src/build.rs +++ b/src/build.rs @@ -254,7 +254,7 @@ impl FieldsBuilder { /// Add a named field with the type of the type parameter `T` pub fn field_of(mut self, name: &'static str, type_name: &'static str) -> Self where - T: TypeInfo + ?Sized + 'static, + T: TypeInfo + ?Sized, { self.fields.push(Field::named_of::(name, type_name)); self @@ -276,7 +276,7 @@ impl FieldsBuilder { /// Add an unnamed field with the type of the type parameter `T` pub fn field_of(mut self, type_name: &'static str) -> Self where - T: TypeInfo + ?Sized + 'static, + T: TypeInfo + ?Sized, { self.fields.push(Field::unnamed_of::(type_name)); self diff --git a/src/impls.rs b/src/impls.rs index 440a8912..d2ec3168 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -81,7 +81,7 @@ macro_rules! impl_metadata_for_tuple { impl<$($ty),*> TypeInfo for ($($ty,)*) where $( - $ty: TypeInfo+ 'static, + $ty: TypeInfo, )* { type Identity = Self; @@ -161,7 +161,7 @@ where impl TypeInfo for Cow<'static, T> where - T: ToOwned + TypeInfo + ?Sized + 'static, + T: ToOwned + TypeInfo + ?Sized, { type Identity = Self; @@ -204,7 +204,7 @@ where impl TypeInfo for Box where - T: TypeInfo + ?Sized + 'static, + T: TypeInfo + ?Sized, { type Identity = T; diff --git a/src/meta_type.rs b/src/meta_type.rs index db5fbc39..4e94475f 100644 --- a/src/meta_type.rs +++ b/src/meta_type.rs @@ -88,7 +88,7 @@ impl MetaType { /// Creates a new meta type from the given compile-time known type. pub fn new() -> Self where - T: TypeInfo + ?Sized + 'static, + T: TypeInfo + ?Sized, { Self { fn_type_info: ::type_info, diff --git a/src/ty/fields.rs b/src/ty/fields.rs index 9543b472..2fb501e6 100644 --- a/src/ty/fields.rs +++ b/src/ty/fields.rs @@ -115,7 +115,7 @@ impl Field { /// compile-time type. pub fn named_of(name: &'static str, type_name: &'static str) -> Field where - T: TypeInfo + ?Sized + 'static, + T: TypeInfo + ?Sized, { Self::new(Some(name), MetaType::new::(), type_name) } @@ -126,7 +126,7 @@ impl Field { /// given compile-time type. pub fn unnamed_of(type_name: &'static str) -> Field where - T: TypeInfo + ?Sized + 'static, + T: TypeInfo + ?Sized, { Self::new(None, MetaType::new::(), type_name) } From d8ec489959ce805ac826dc075d954ca97265f996 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 18 May 2021 16:25:48 +0100 Subject: [PATCH 3/9] Add basic test for struct with lifetime --- test_suite/tests/derive.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test_suite/tests/derive.rs b/test_suite/tests/derive.rs index febae6f3..e0d0a003 100644 --- a/test_suite/tests/derive.rs +++ b/test_suite/tests/derive.rs @@ -231,6 +231,22 @@ fn fields_with_type_alias() { assert_type!(S, ty); } +#[test] +fn basic_reference_with_lifetime() { + #[allow(unused)] + #[derive(TypeInfo)] + struct S<'a, T> { + f: &'a T, + } + + let struct_type = Type::builder() + .path(Path::new("S", "derive")) + .type_params(tuple_meta_type!(u32)) + .composite(Fields::named().field_of::<&'static u32>("f", "&'static T")); + + assert_type!(S, struct_type); +} + #[test] fn associated_types_derive_without_bounds() { trait Types { From 39901fe16de4f6024978152398d62b2e70b50217 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 19 May 2021 10:34:42 +0100 Subject: [PATCH 4/9] Add StaticTypeInfo trait --- src/lib.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e6f6596e..3c58e5ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -129,7 +129,7 @@ pub use self::{ pub use scale_info_derive::TypeInfo; /// Implementors return their meta type information. -pub trait TypeInfo: 'static { +pub trait TypeInfo { /// The type identifying for which type info is provided. /// /// # Note @@ -143,10 +143,24 @@ pub trait TypeInfo: 'static { fn type_info() -> Type; } +/// Convenience trait for implementors, combining `TypeInfo` and `'static` bounds. +/// +/// # Note +/// +/// Currently because of the `'static` constraint on [`std::any::TypeId::of`] (see [`MetaType`]), +/// `TypeInfo` constraints must also be accompanied by a `'static` bound. This trait is useful to +/// for implementors so only a single constraint is required. +pub trait StaticTypeInfo: TypeInfo + 'static {} + +impl StaticTypeInfo for T +where + T: TypeInfo + 'static, +{} + /// Returns the runtime bridge to the types compile-time type information. pub fn meta_type() -> MetaType where - T: ?Sized + TypeInfo, + T: ?Sized + StaticTypeInfo, { MetaType::new::() } From 755606480987ed74f4e57203ef5635800dc46174 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 19 May 2021 10:48:19 +0100 Subject: [PATCH 5/9] Revert adding 'static to TypeInfo --- README.md | 4 +-- derive/src/trait_bounds.rs | 9 ++--- src/build.rs | 12 +++---- src/impls.rs | 36 +++++++++---------- src/lib.rs | 2 +- src/meta_type.rs | 2 +- src/tests.rs | 4 +-- src/ty/fields.rs | 6 ++-- src/ty/mod.rs | 2 +- test_suite/tests/derive.rs | 16 --------- test_suite/tests/ui/fail_missing_derive.rs | 2 +- .../tests/ui/fail_missing_derive.stderr | 2 +- .../tests/ui/fail_non_static_lifetime.rs | 2 +- test_suite/tests/ui/fail_unions.rs | 2 +- test_suite/tests/ui/fail_unions.stderr | 2 +- ...use_codec_attrs_without_deriving_encode.rs | 2 +- .../tests/ui/pass_basic_generic_type.rs | 2 +- ...s_complex_generic_self_referential_type.rs | 2 +- .../tests/ui/pass_non_static_lifetime.rs | 2 +- test_suite/tests/ui/pass_self_referential.rs | 2 +- .../tests/ui/pass_with_valid_codec_attrs.rs | 2 +- 21 files changed, 50 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index 005f083a..86a24411 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ struct Foo { impl TypeInfo for Foo where - T: TypeInfo, + T: TypeInfo + 'static, { type Identity = Self; @@ -146,7 +146,7 @@ enum Foo{ impl TypeInfo for Foo where - T: TypeInfo, + T: TypeInfo + 'static, { type Identity = Self; diff --git a/derive/src/trait_bounds.rs b/derive/src/trait_bounds.rs index 7d4e69d7..65da4257 100644 --- a/derive/src/trait_bounds.rs +++ b/derive/src/trait_bounds.rs @@ -31,7 +31,7 @@ use syn::{ use crate::utils; -/// Generates a where clause for a `TypeInfo` impl, adding `TypeInfo` bounds to all +/// Generates a where clause for a `TypeInfo` impl, adding `TypeInfo + 'static` bounds to all /// relevant generic types including associated types (e.g. `T::A: TypeInfo`), correctly dealing /// with self-referential types. pub fn make_where_clause<'a>( @@ -66,18 +66,18 @@ pub fn make_where_clause<'a>( types.into_iter().for_each(|(ty, is_compact)| { // Compact types need extra bounds, T: HasCompact and ::Type: TypeInfo + // HasCompact>::Type: TypeInfo + 'static if is_compact { where_clause .predicates .push(parse_quote!(#ty : :: #parity_scale_codec ::HasCompact)); where_clause .predicates - .push(parse_quote!(<#ty as :: #parity_scale_codec ::HasCompact>::Type : :: #scale_info ::TypeInfo)); + .push(parse_quote!(<#ty as :: #parity_scale_codec ::HasCompact>::Type : :: #scale_info ::TypeInfo + 'static)); } else { where_clause .predicates - .push(parse_quote!(#ty : :: #scale_info ::TypeInfo)); + .push(parse_quote!(#ty : :: #scale_info ::TypeInfo + 'static)); } }); @@ -85,6 +85,7 @@ pub fn make_where_clause<'a>( let ident = type_param.ident.clone(); let mut bounds = type_param.bounds.clone(); bounds.push(parse_quote!(:: #scale_info ::TypeInfo)); + bounds.push(parse_quote!('static)); where_clause .predicates .push(parse_quote!( #ident : #bounds)); diff --git a/src/build.rs b/src/build.rs index a2220bc5..3bad9718 100644 --- a/src/build.rs +++ b/src/build.rs @@ -30,7 +30,7 @@ //! //! impl TypeInfo for Foo //! where -//! T: TypeInfo, +//! T: TypeInfo + 'static, //! { //! type Identity = Self; //! @@ -74,7 +74,7 @@ //! //! impl TypeInfo for Foo //! where -//! T: TypeInfo, +//! T: TypeInfo + 'static, //! { //! type Identity = Self; //! @@ -254,7 +254,7 @@ impl FieldsBuilder { /// Add a named field with the type of the type parameter `T` pub fn field_of(mut self, name: &'static str, type_name: &'static str) -> Self where - T: TypeInfo + ?Sized, + T: TypeInfo + ?Sized + 'static, { self.fields.push(Field::named_of::(name, type_name)); self @@ -264,7 +264,7 @@ impl FieldsBuilder { pub fn compact_of(mut self, name: &'static str, type_name: &'static str) -> Self where T: scale::HasCompact, - ::Type: TypeInfo, + ::Type: TypeInfo + 'static, { self.fields .push(Field::compact_of::(Some(name), type_name)); @@ -276,7 +276,7 @@ impl FieldsBuilder { /// Add an unnamed field with the type of the type parameter `T` pub fn field_of(mut self, type_name: &'static str) -> Self where - T: TypeInfo + ?Sized, + T: TypeInfo + ?Sized + 'static, { self.fields.push(Field::unnamed_of::(type_name)); self @@ -286,7 +286,7 @@ impl FieldsBuilder { pub fn compact_of(mut self, type_name: &'static str) -> Self where T: scale::HasCompact, - ::Type: TypeInfo, + ::Type: TypeInfo + 'static, { self.fields.push(Field::compact_of::(None, type_name)); self diff --git a/src/impls.rs b/src/impls.rs index d2ec3168..16da5b06 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -68,7 +68,7 @@ impl_metadata_for_primitives!( i128 => TypeDefPrimitive::I128, ); -impl TypeInfo for [T; N] { +impl TypeInfo for [T; N] { type Identity = Self; fn type_info() -> Type { @@ -81,7 +81,7 @@ macro_rules! impl_metadata_for_tuple { impl<$($ty),*> TypeInfo for ($($ty,)*) where $( - $ty: TypeInfo, + $ty: TypeInfo+ 'static, )* { type Identity = Self; @@ -113,7 +113,7 @@ impl_metadata_for_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P); impl TypeInfo for Vec where - T: TypeInfo, + T: TypeInfo + 'static, { type Identity = [T]; @@ -124,7 +124,7 @@ where impl TypeInfo for Option where - T: TypeInfo, + T: TypeInfo + 'static, { type Identity = Self; @@ -142,8 +142,8 @@ where impl TypeInfo for Result where - T: TypeInfo, - E: TypeInfo, + T: TypeInfo + 'static, + E: TypeInfo + 'static, { type Identity = Self; @@ -161,7 +161,7 @@ where impl TypeInfo for Cow<'static, T> where - T: ToOwned + TypeInfo + ?Sized, + T: ToOwned + TypeInfo + ?Sized + 'static, { type Identity = Self; @@ -175,8 +175,8 @@ where impl TypeInfo for BTreeMap where - K: TypeInfo, - V: TypeInfo, + K: TypeInfo + 'static, + V: TypeInfo + 'static, { type Identity = Self; @@ -190,7 +190,7 @@ where impl TypeInfo for BTreeSet where - T: TypeInfo, + T: TypeInfo + 'static, { type Identity = Self; @@ -204,7 +204,7 @@ where impl TypeInfo for Box where - T: TypeInfo + ?Sized, + T: TypeInfo + ?Sized + 'static, { type Identity = T; @@ -213,9 +213,9 @@ where } } -impl TypeInfo for &'static T +impl TypeInfo for &T where - T: TypeInfo + ?Sized, + T: TypeInfo + ?Sized + 'static, { type Identity = T; @@ -224,9 +224,9 @@ where } } -impl TypeInfo for &'static mut T +impl TypeInfo for &mut T where - T: TypeInfo + ?Sized, + T: TypeInfo + ?Sized + 'static, { type Identity = T; @@ -237,7 +237,7 @@ where impl TypeInfo for [T] where - T: TypeInfo, + T: TypeInfo + 'static, { type Identity = Self; @@ -264,7 +264,7 @@ impl TypeInfo for String { impl TypeInfo for PhantomData where - T: TypeInfo + ?Sized, + T: TypeInfo + ?Sized + 'static, { type Identity = Self; @@ -275,7 +275,7 @@ where impl TypeInfo for scale::Compact where - T: TypeInfo, + T: TypeInfo + 'static, { type Identity = Self; fn type_info() -> Type { diff --git a/src/lib.rs b/src/lib.rs index 3c58e5ab..48ff5d1c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -160,7 +160,7 @@ where /// Returns the runtime bridge to the types compile-time type information. pub fn meta_type() -> MetaType where - T: ?Sized + StaticTypeInfo, + T: ?Sized + TypeInfo + 'static, { MetaType::new::() } diff --git a/src/meta_type.rs b/src/meta_type.rs index 4e94475f..db5fbc39 100644 --- a/src/meta_type.rs +++ b/src/meta_type.rs @@ -88,7 +88,7 @@ impl MetaType { /// Creates a new meta type from the given compile-time known type. pub fn new() -> Self where - T: TypeInfo + ?Sized, + T: TypeInfo + ?Sized + 'static, { Self { fn_type_info: ::type_info, diff --git a/src/tests.rs b/src/tests.rs index 2674e9b1..fea9a82e 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -154,7 +154,7 @@ fn struct_with_generics() { impl TypeInfo for MyStruct where - T: TypeInfo, + T: TypeInfo + 'static, { type Identity = Self; @@ -193,7 +193,7 @@ fn basic_struct_with_phantoms() { impl TypeInfo for SomeStruct where - T: TypeInfo, + T: TypeInfo + 'static, { type Identity = Self; diff --git a/src/ty/fields.rs b/src/ty/fields.rs index 2fb501e6..1ef06ce9 100644 --- a/src/ty/fields.rs +++ b/src/ty/fields.rs @@ -115,7 +115,7 @@ impl Field { /// compile-time type. pub fn named_of(name: &'static str, type_name: &'static str) -> Field where - T: TypeInfo + ?Sized, + T: TypeInfo + ?Sized + 'static, { Self::new(Some(name), MetaType::new::(), type_name) } @@ -126,7 +126,7 @@ impl Field { /// given compile-time type. pub fn unnamed_of(type_name: &'static str) -> Field where - T: TypeInfo + ?Sized, + T: TypeInfo + ?Sized + 'static, { Self::new(None, MetaType::new::(), type_name) } @@ -135,7 +135,7 @@ impl Field { pub fn compact_of(name: Option<&'static str>, type_name: &'static str) -> Field where T: HasCompact, - ::Type: TypeInfo, + ::Type: TypeInfo + 'static, { Self::new(name, MetaType::new::<::Type>(), type_name) } diff --git a/src/ty/mod.rs b/src/ty/mod.rs index 5708d534..e3dd5cf4 100644 --- a/src/ty/mod.rs +++ b/src/ty/mod.rs @@ -370,7 +370,7 @@ impl TypeDefSequence { /// compile-time type. pub fn of() -> Self where - T: TypeInfo, + T: TypeInfo + 'static, { Self::new(MetaType::new::()) } diff --git a/test_suite/tests/derive.rs b/test_suite/tests/derive.rs index e0d0a003..febae6f3 100644 --- a/test_suite/tests/derive.rs +++ b/test_suite/tests/derive.rs @@ -231,22 +231,6 @@ fn fields_with_type_alias() { assert_type!(S, ty); } -#[test] -fn basic_reference_with_lifetime() { - #[allow(unused)] - #[derive(TypeInfo)] - struct S<'a, T> { - f: &'a T, - } - - let struct_type = Type::builder() - .path(Path::new("S", "derive")) - .type_params(tuple_meta_type!(u32)) - .composite(Fields::named().field_of::<&'static u32>("f", "&'static T")); - - assert_type!(S, struct_type); -} - #[test] fn associated_types_derive_without_bounds() { trait Types { diff --git a/test_suite/tests/ui/fail_missing_derive.rs b/test_suite/tests/ui/fail_missing_derive.rs index 94a42c6f..1a485c92 100644 --- a/test_suite/tests/ui/fail_missing_derive.rs +++ b/test_suite/tests/ui/fail_missing_derive.rs @@ -11,7 +11,7 @@ struct Cat { paws: PawType, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::>(); diff --git a/test_suite/tests/ui/fail_missing_derive.stderr b/test_suite/tests/ui/fail_missing_derive.stderr index 9d14b20e..54501aa1 100644 --- a/test_suite/tests/ui/fail_missing_derive.stderr +++ b/test_suite/tests/ui/fail_missing_derive.stderr @@ -1,7 +1,7 @@ error[E0277]: the trait bound `PawType: TypeInfo` is not satisfied --> $DIR/fail_missing_derive.rs:17:5 | -14 | fn assert_type_info() {} +14 | fn assert_type_info() {} | -------- required by this bound in `assert_type_info` ... 17 | assert_type_info::>(); diff --git a/test_suite/tests/ui/fail_non_static_lifetime.rs b/test_suite/tests/ui/fail_non_static_lifetime.rs index 484ecbc1..26e96e75 100644 --- a/test_suite/tests/ui/fail_non_static_lifetime.rs +++ b/test_suite/tests/ui/fail_non_static_lifetime.rs @@ -5,7 +5,7 @@ struct Me<'a> { me: &'a Me<'a>, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::(); diff --git a/test_suite/tests/ui/fail_unions.rs b/test_suite/tests/ui/fail_unions.rs index 972d7b94..46bd4666 100644 --- a/test_suite/tests/ui/fail_unions.rs +++ b/test_suite/tests/ui/fail_unions.rs @@ -7,7 +7,7 @@ union Commonwealth { b: f32, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::(); diff --git a/test_suite/tests/ui/fail_unions.stderr b/test_suite/tests/ui/fail_unions.stderr index 39678df1..68c189ed 100644 --- a/test_suite/tests/ui/fail_unions.stderr +++ b/test_suite/tests/ui/fail_unions.stderr @@ -11,7 +11,7 @@ error: Unions not supported error[E0277]: the trait bound `Commonwealth: TypeInfo` is not satisfied --> $DIR/fail_unions.rs:13:24 | -10 | fn assert_type_info() {} +10 | fn assert_type_info() {} | -------- required by this bound in `assert_type_info` ... 13 | assert_type_info::(); diff --git a/test_suite/tests/ui/fail_use_codec_attrs_without_deriving_encode.rs b/test_suite/tests/ui/fail_use_codec_attrs_without_deriving_encode.rs index 625b29e3..cddaed8f 100644 --- a/test_suite/tests/ui/fail_use_codec_attrs_without_deriving_encode.rs +++ b/test_suite/tests/ui/fail_use_codec_attrs_without_deriving_encode.rs @@ -7,7 +7,7 @@ struct AttrValidation { b: u16, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::(); diff --git a/test_suite/tests/ui/pass_basic_generic_type.rs b/test_suite/tests/ui/pass_basic_generic_type.rs index 14c517cc..90a2daa4 100644 --- a/test_suite/tests/ui/pass_basic_generic_type.rs +++ b/test_suite/tests/ui/pass_basic_generic_type.rs @@ -13,7 +13,7 @@ struct Cat { _paws: PawType, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::>(); diff --git a/test_suite/tests/ui/pass_complex_generic_self_referential_type.rs b/test_suite/tests/ui/pass_complex_generic_self_referential_type.rs index af6c9dcb..3c3d80dc 100644 --- a/test_suite/tests/ui/pass_complex_generic_self_referential_type.rs +++ b/test_suite/tests/ui/pass_complex_generic_self_referential_type.rs @@ -34,7 +34,7 @@ struct Selfie { _nested: Box>, Selfie>>>>, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::>(); diff --git a/test_suite/tests/ui/pass_non_static_lifetime.rs b/test_suite/tests/ui/pass_non_static_lifetime.rs index 96356fbf..7b84f281 100644 --- a/test_suite/tests/ui/pass_non_static_lifetime.rs +++ b/test_suite/tests/ui/pass_non_static_lifetime.rs @@ -5,7 +5,7 @@ struct Me<'a> { _me: &'a Me<'a>, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::(); diff --git a/test_suite/tests/ui/pass_self_referential.rs b/test_suite/tests/ui/pass_self_referential.rs index b4f23267..34a6e90b 100644 --- a/test_suite/tests/ui/pass_self_referential.rs +++ b/test_suite/tests/ui/pass_self_referential.rs @@ -5,7 +5,7 @@ struct Me { _me: Box, } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::(); diff --git a/test_suite/tests/ui/pass_with_valid_codec_attrs.rs b/test_suite/tests/ui/pass_with_valid_codec_attrs.rs index a4469cbf..736ea943 100644 --- a/test_suite/tests/ui/pass_with_valid_codec_attrs.rs +++ b/test_suite/tests/ui/pass_with_valid_codec_attrs.rs @@ -23,7 +23,7 @@ enum ValidEnum { Theng(ValidStruct), } -fn assert_type_info() {} +fn assert_type_info() {} fn main() { assert_type_info::(); From 73ea38f5fd8a8429a2c711ec93993401861bc54d Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 19 May 2021 10:52:34 +0100 Subject: [PATCH 6/9] Use StaticTypeInfo in derive macro --- derive/src/trait_bounds.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/derive/src/trait_bounds.rs b/derive/src/trait_bounds.rs index 65da4257..5edff2b9 100644 --- a/derive/src/trait_bounds.rs +++ b/derive/src/trait_bounds.rs @@ -66,26 +66,25 @@ pub fn make_where_clause<'a>( types.into_iter().for_each(|(ty, is_compact)| { // Compact types need extra bounds, T: HasCompact and ::Type: TypeInfo + 'static + // HasCompact>::Type: StaticTypeInfo if is_compact { where_clause .predicates .push(parse_quote!(#ty : :: #parity_scale_codec ::HasCompact)); where_clause .predicates - .push(parse_quote!(<#ty as :: #parity_scale_codec ::HasCompact>::Type : :: #scale_info ::TypeInfo + 'static)); + .push(parse_quote!(<#ty as :: #parity_scale_codec ::HasCompact>::Type : :: #scale_info ::StaticTypeInfo)); } else { where_clause .predicates - .push(parse_quote!(#ty : :: #scale_info ::TypeInfo + 'static)); + .push(parse_quote!(#ty : :: #scale_info ::StaticTypeInfo)); } }); generics.type_params().into_iter().for_each(|type_param| { let ident = type_param.ident.clone(); let mut bounds = type_param.bounds.clone(); - bounds.push(parse_quote!(:: #scale_info ::TypeInfo)); - bounds.push(parse_quote!('static)); + bounds.push(parse_quote!(:: #scale_info ::StaticTypeInfo)); where_clause .predicates .push(parse_quote!( #ident : #bounds)); From fb21eb98d009b12958d0a434598777e12f3db40f Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 19 May 2021 10:56:23 +0100 Subject: [PATCH 7/9] Fmt --- src/lib.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 48ff5d1c..97bd6ccc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -152,10 +152,7 @@ pub trait TypeInfo { /// for implementors so only a single constraint is required. pub trait StaticTypeInfo: TypeInfo + 'static {} -impl StaticTypeInfo for T -where - T: TypeInfo + 'static, -{} +impl StaticTypeInfo for T where T: TypeInfo + 'static {} /// Returns the runtime bridge to the types compile-time type information. pub fn meta_type() -> MetaType From c2c783fa90fda170278d3d56a203206067043e19 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 19 May 2021 11:00:52 +0100 Subject: [PATCH 8/9] Revert "Use StaticTypeInfo in derive macro" This reverts commit 73ea38f5 --- derive/src/trait_bounds.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/derive/src/trait_bounds.rs b/derive/src/trait_bounds.rs index 5edff2b9..65da4257 100644 --- a/derive/src/trait_bounds.rs +++ b/derive/src/trait_bounds.rs @@ -66,25 +66,26 @@ pub fn make_where_clause<'a>( types.into_iter().for_each(|(ty, is_compact)| { // Compact types need extra bounds, T: HasCompact and ::Type: StaticTypeInfo + // HasCompact>::Type: TypeInfo + 'static if is_compact { where_clause .predicates .push(parse_quote!(#ty : :: #parity_scale_codec ::HasCompact)); where_clause .predicates - .push(parse_quote!(<#ty as :: #parity_scale_codec ::HasCompact>::Type : :: #scale_info ::StaticTypeInfo)); + .push(parse_quote!(<#ty as :: #parity_scale_codec ::HasCompact>::Type : :: #scale_info ::TypeInfo + 'static)); } else { where_clause .predicates - .push(parse_quote!(#ty : :: #scale_info ::StaticTypeInfo)); + .push(parse_quote!(#ty : :: #scale_info ::TypeInfo + 'static)); } }); generics.type_params().into_iter().for_each(|type_param| { let ident = type_param.ident.clone(); let mut bounds = type_param.bounds.clone(); - bounds.push(parse_quote!(:: #scale_info ::StaticTypeInfo)); + bounds.push(parse_quote!(:: #scale_info ::TypeInfo)); + bounds.push(parse_quote!('static)); where_clause .predicates .push(parse_quote!( #ident : #bounds)); From 6e6d7ad1444b1ec146f258727510cd882f7e1331 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 19 May 2021 13:10:37 +0100 Subject: [PATCH 9/9] Update src/lib.rs Co-authored-by: David --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 97bd6ccc..fb753784 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,7 +149,7 @@ pub trait TypeInfo { /// /// Currently because of the `'static` constraint on [`std::any::TypeId::of`] (see [`MetaType`]), /// `TypeInfo` constraints must also be accompanied by a `'static` bound. This trait is useful to -/// for implementors so only a single constraint is required. +/// implementors so only a single constraint is required. pub trait StaticTypeInfo: TypeInfo + 'static {} impl StaticTypeInfo for T where T: TypeInfo + 'static {}