-
Notifications
You must be signed in to change notification settings - Fork 28
Derive TypeInfo for fields with associated types without bounds #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e77fd47
Add bounds for generic type param
ascjones 506da3f
Fmt
ascjones 08e267b
Remove redundant clone
ascjones 62a28e3
Make clippy happy
ascjones 44718bc
Merge branch 'master' into aj-bounds
ascjones 4bd32a3
Fmt
ascjones 97fa4ac
Remove readding of type to bounds
ascjones 41f73bc
Unused imports
ascjones 549a806
Merge branch 'master' into aj-bounds
ascjones ae244d1
Merge branch 'master' into aj-bounds
ascjones af1541c
Merge branch 'master' into aj-bounds
ascjones 91a49a4
Merge branch 'master' into aj-bounds
ascjones 1183560
Doc tweaks (#37)
dvdplm 0eef2d3
Adapt and simplify code to scale-info's needs
dvdplm b5195ff
Resolve todo
dvdplm e47a056
Fmt
dvdplm c877f21
Add ui test for Unions
dvdplm 2b3274c
Only run trybuild-tests on nightly
dvdplm 89a2b8c
Unify and simply collect_types_to_bind()
dvdplm 9060b1a
Move a few trivial tests to trybuild tests instead
dvdplm f9d3a33
Add more trybuild tests
dvdplm 81525b9
remove trivial test
dvdplm df26405
Make type_contains_idents more self-contained
dvdplm cc3bc31
Obey the fmt
dvdplm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // Copyright 2019-2020 Parity Technologies (UK) Ltd. | ||
| // | ||
| // 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. | ||
|
|
||
| use alloc::vec::Vec; | ||
| use proc_macro2::Ident; | ||
| use syn::{ | ||
| parse_quote, | ||
| punctuated::Punctuated, | ||
| spanned::Spanned, | ||
| visit::Visit, | ||
| Generics, | ||
| Result, | ||
| Type, | ||
| }; | ||
|
|
||
| /// Adds a `TypeInfo + 'static` bound to all relevant generic types including | ||
| /// associated types (e.g. `T::A: TypeInfo`), correctly dealing with | ||
| /// self-referential types. | ||
| pub fn add(input_ident: &Ident, generics: &mut Generics, data: &syn::Data) -> Result<()> { | ||
| let ty_params = generics.type_params_mut().fold(Vec::new(), |mut acc, p| { | ||
| p.bounds.push(parse_quote!(::scale_info::TypeInfo)); | ||
| p.bounds.push(parse_quote!('static)); | ||
| acc.push(p.ident.clone()); | ||
| acc | ||
| }); | ||
|
|
||
| if ty_params.is_empty() { | ||
| return Ok(()) | ||
| } | ||
|
|
||
| let types = collect_types_to_bind(input_ident, data, &ty_params)?; | ||
|
|
||
| if !types.is_empty() { | ||
| let where_clause = generics.make_where_clause(); | ||
|
|
||
| types.into_iter().for_each(|ty| { | ||
| where_clause | ||
| .predicates | ||
| .push(parse_quote!(#ty : ::scale_info::TypeInfo + 'static)) | ||
| }); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Visits the ast and checks if the given type contains one of the given | ||
| /// idents. | ||
| fn type_contains_idents(ty: &Type, idents: &[Ident]) -> bool { | ||
| struct ContainIdents<'a> { | ||
| result: bool, | ||
| idents: &'a [Ident], | ||
| } | ||
|
|
||
| impl<'a, 'ast> Visit<'ast> for ContainIdents<'a> { | ||
| fn visit_ident(&mut self, i: &'ast Ident) { | ||
| if self.idents.iter().any(|id| id == i) { | ||
| self.result = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let mut visitor = ContainIdents { | ||
| result: false, | ||
| idents, | ||
| }; | ||
| visitor.visit_type(ty); | ||
| visitor.result | ||
| } | ||
|
|
||
| /// Returns all types that must be added to the where clause with the respective | ||
| /// trait bound. | ||
| fn collect_types_to_bind( | ||
| input_ident: &Ident, | ||
| data: &syn::Data, | ||
| ty_params: &[Ident], | ||
| ) -> Result<Vec<Type>> { | ||
| let types_from_fields = |fields: &Punctuated<syn::Field, _>| -> Vec<syn::Type> { | ||
| fields | ||
| .iter() | ||
| .filter(|field| { | ||
| // Only add a bound if the type uses a generic. | ||
| type_contains_idents(&field.ty, &ty_params) | ||
| && | ||
| // Remove all remaining types that start/contain the input ident | ||
| // to not have them in the where clause. | ||
| !type_contains_idents(&field.ty, &[input_ident.clone()]) | ||
| }) | ||
| .map(|f| f.ty.clone()) | ||
| .collect() | ||
| }; | ||
|
|
||
| let types = match *data { | ||
| syn::Data::Struct(ref data) => { | ||
| match &data.fields { | ||
| syn::Fields::Named(syn::FieldsNamed { named: fields, .. }) | ||
| | syn::Fields::Unnamed(syn::FieldsUnnamed { | ||
| unnamed: fields, .. | ||
| }) => types_from_fields(fields), | ||
| syn::Fields::Unit => Vec::new(), | ||
| } | ||
| } | ||
|
|
||
| syn::Data::Enum(ref data) => { | ||
| data.variants | ||
| .iter() | ||
| .flat_map(|variant| { | ||
| match &variant.fields { | ||
| syn::Fields::Named(syn::FieldsNamed { | ||
| named: fields, .. | ||
| }) | ||
| | syn::Fields::Unnamed(syn::FieldsUnnamed { | ||
| unnamed: fields, | ||
| .. | ||
| }) => types_from_fields(fields), | ||
| syn::Fields::Unit => Vec::new(), | ||
| } | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| syn::Data::Union(ref data) => { | ||
| return Err(syn::Error::new( | ||
| data.union_token.span(), | ||
| "Union types are not supported.", | ||
| )) | ||
| } | ||
| }; | ||
|
|
||
| Ok(types) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| use scale_info::TypeInfo; | ||
|
|
||
| enum PawType<Paw> { | ||
| Big(Paw), | ||
| Small(Paw), | ||
| } | ||
| #[derive(TypeInfo)] | ||
| struct Cat<Tail, Ear, Paw> { | ||
| tail: Tail, | ||
| ears: [Ear; 3], | ||
| paws: PawType<Paw>, | ||
| } | ||
|
|
||
| fn assert_type_info<T: TypeInfo + 'static>() {} | ||
|
|
||
| fn main() { | ||
| assert_type_info::<Cat<bool, u8, u16>>(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| error[E0277]: the trait bound `PawType<u16>: TypeInfo` is not satisfied | ||
| --> $DIR/fail_missing_derive.rs:17:5 | ||
| | | ||
| 14 | fn assert_type_info<T: TypeInfo + 'static>() {} | ||
| | -------- required by this bound in `assert_type_info` | ||
| ... | ||
| 17 | assert_type_info::<Cat<bool, u8, u16>>(); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `TypeInfo` is not implemented for `PawType<u16>` | ||
| | | ||
| = note: required because of the requirements on the impl of `TypeInfo` for `Cat<bool, u8, u16>` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| use scale_info::TypeInfo; | ||
|
|
||
| #[derive(TypeInfo)] | ||
| struct Me<'a> { | ||
| me: &'a Me<'a>, | ||
| } | ||
|
|
||
| fn assert_type_info<T: TypeInfo + 'static>() {} | ||
|
|
||
| fn main() { | ||
| assert_type_info::<Me>(); | ||
dvdplm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| error[E0477]: the type `Me<'a>` does not fulfill the required lifetime | ||
| --> $DIR/fail_non_static_lifetime.rs:3:10 | ||
| | | ||
| 3 | #[derive(TypeInfo)] | ||
| | ^^^^^^^^ in this macro invocation | ||
| | | ||
| ::: $WORKSPACE/derive/src/lib.rs | ||
| | | ||
| | pub fn type_info(input: TokenStream) -> TokenStream { | ||
| | --------------------------------------------------- in this expansion of `#[derive(TypeInfo)]` | ||
| | | ||
| = note: type must satisfy the static lifetime | ||
|
|
||
| error[E0477]: the type `&'a Me<'a>` does not fulfill the required lifetime | ||
| --> $DIR/fail_non_static_lifetime.rs:3:10 | ||
| | | ||
| 3 | #[derive(TypeInfo)] | ||
| | ^^^^^^^^ in this macro invocation | ||
| | | ||
| ::: $WORKSPACE/derive/src/lib.rs | ||
| | | ||
| | pub fn type_info(input: TokenStream) -> TokenStream { | ||
| | --------------------------------------------------- in this expansion of `#[derive(TypeInfo)]` | ||
| | | ||
| = note: type must satisfy the static lifetime |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| use scale_info::TypeInfo; | ||
|
|
||
| #[derive(TypeInfo)] | ||
| #[repr(C)] | ||
| union Commonwealth { | ||
| a: u8, | ||
| b: f32, | ||
| } | ||
|
|
||
| fn assert_type_info<T: TypeInfo + 'static>() {} | ||
|
|
||
| fn main() { | ||
| assert_type_info::<Commonwealth>(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| error: Unions not supported | ||
| --> $DIR/fail_unions.rs:4:1 | ||
| | | ||
| 4 | / #[repr(C)] | ||
| 5 | | union Commonwealth { | ||
| 6 | | a: u8, | ||
| 7 | | b: f32, | ||
| 8 | | } | ||
| | |_^ | ||
|
|
||
| error[E0277]: the trait bound `Commonwealth: TypeInfo` is not satisfied | ||
| --> $DIR/fail_unions.rs:13:24 | ||
| | | ||
| 10 | fn assert_type_info<T: TypeInfo + 'static>() {} | ||
| | -------- required by this bound in `assert_type_info` | ||
| ... | ||
| 13 | assert_type_info::<Commonwealth>(); | ||
| | ^^^^^^^^^^^^ the trait `TypeInfo` is not implemented for `Commonwealth` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| use scale_info::TypeInfo; | ||
|
|
||
| #[allow(dead_code)] | ||
| #[derive(TypeInfo)] | ||
| enum PawType<Paw> { | ||
| Big(Paw), | ||
| Small(Paw), | ||
| } | ||
| #[derive(TypeInfo)] | ||
| struct Cat<Tail, Ear, Paw> { | ||
| _tail: Tail, | ||
| _ears: [Ear; 3], | ||
| _paws: PawType<Paw>, | ||
| } | ||
|
|
||
| fn assert_type_info<T: TypeInfo + 'static>() {} | ||
|
|
||
| fn main() { | ||
| assert_type_info::<Cat<bool, u8, u16>>(); | ||
| } |
41 changes: 41 additions & 0 deletions
41
test_suite/tests/ui/pass_complex_generic_self_referential_type.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| use scale_info::TypeInfo; | ||
|
|
||
|
|
||
| #[derive(TypeInfo)] | ||
| struct Nested<P> { | ||
| _pos: P, | ||
| } | ||
|
|
||
| #[derive(TypeInfo)] | ||
| struct Is<N> { | ||
| _nested: N, | ||
| } | ||
|
|
||
| #[derive(TypeInfo)] | ||
| struct That<I, S> { | ||
| _is: I, | ||
| _selfie: S, | ||
| } | ||
|
|
||
| #[derive(TypeInfo)] | ||
| struct Thing<T> { | ||
| _that: T, | ||
| } | ||
|
|
||
| #[derive(TypeInfo)] | ||
| struct Other<T> { | ||
| _thing: T, | ||
| } | ||
|
|
||
| #[derive(TypeInfo)] | ||
| struct Selfie<Pos> { | ||
| _another: Box<Selfie<Pos>>, | ||
| _pos: Pos, | ||
| _nested: Box<Other<Thing<That<Is<Nested<Pos>>, Selfie<Pos>>>>>, | ||
| } | ||
|
|
||
| fn assert_type_info<T: TypeInfo + 'static>() {} | ||
|
|
||
| fn main() { | ||
| assert_type_info::<Selfie<bool>>(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| use scale_info::TypeInfo; | ||
|
|
||
| #[derive(TypeInfo)] | ||
| struct Me { | ||
| _me: Box<Me>, | ||
| } | ||
|
|
||
| fn assert_type_info<T: TypeInfo + 'static>() {} | ||
|
|
||
| fn main() { | ||
| assert_type_info::<Me>(); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.