Skip to content
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

Add skip_type_params attribute #96

Merged
merged 51 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
39ee6b4
Add new top-level attribute `scale_info(bounds(T: SomeTrait + OtherTr…
dvdplm May 1, 2021
b0eda68
Fmt
ascjones May 4, 2021
bd6dd5d
cleanup
dvdplm May 6, 2021
171cd4c
Merge branch 'dp-custom-bound' of github.com:paritytech/scale-info in…
dvdplm May 6, 2021
030d407
Merge branch 'master' into dp-custom-bound
ascjones May 27, 2021
b937c9a
Skip type params prototype
ascjones May 28, 2021
725ceca
Merge branch 'master' into aj-skip-type-params
ascjones Jun 7, 2021
56edb9c
Fmt
ascjones Jun 7, 2021
f503781
Clippy
ascjones Jun 7, 2021
1743b48
Satisfy clippy
ascjones Jun 7, 2021
bf9cf6d
Fix skip type params
ascjones Jun 7, 2021
2d4f1e8
Fix UI test
ascjones Jun 7, 2021
9c4f6d6
Add some more tests
ascjones Jun 11, 2021
100dfcf
Add failing test for type params with default values
ascjones Jun 11, 2021
c92041b
Fix for type params with defaults, compare on ident
ascjones Jun 11, 2021
a23cd47
Merge branch 'master' into aj-skip-type-params
ascjones Jun 14, 2021
3678114
Merge branch 'master' into aj-skip-type-params
ascjones Jun 14, 2021
4cdf0c5
WIP use bounds attribute for skipping type params
ascjones Jun 14, 2021
99cb9d3
Add required 'static bounds to all type params
ascjones Jun 15, 2021
c39167d
Fmt
ascjones Jun 15, 2021
835efe3
Satisfy clippy
ascjones Jun 15, 2021
75d25ad
Add UI test for skipping bounds
ascjones Jun 15, 2021
3012932
WIP docs
ascjones Jun 15, 2021
20ff837
Revert "Use bounds attribute for skipping type params"
ascjones Jun 15, 2021
7cdd440
WIP dual attribute parsing
ascjones Jun 15, 2021
6d97c49
Use new attribute parsing
ascjones Jun 16, 2021
7cab3e3
Fix up attribute parsing
ascjones Jun 16, 2021
1f84f41
Fmt
ascjones Jun 16, 2021
0716333
Reorder impls
ascjones Jun 16, 2021
a2d239b
Refactor attribute handling
ascjones Jun 16, 2021
ecf21f3
Fmt
ascjones Jun 16, 2021
f876d22
Add docs to attributes
ascjones Jun 16, 2021
b58904f
Add `'static` bounds for all type params, add some ui tests
ascjones Jun 16, 2021
9f64f74
Merge remote-tracking branch 'origin/master' into aj-skip-type-params
ascjones Jun 16, 2021
d61f75d
Check for duplicate attributes
ascjones Jun 16, 2021
a1741d2
Add helpful error message for type params in not in cuatom bounds and…
ascjones Jun 16, 2021
2da8e3f
Improve error message where a type param is missing from the bounds, …
ascjones Jun 16, 2021
2dc9484
Fix test and fmt
ascjones Jun 16, 2021
39e40b1
Refactor and validate missing skip type params
ascjones Jun 17, 2021
44c2af2
Update validation UI test
ascjones Jun 17, 2021
91799c7
Error message formatting
ascjones Jun 17, 2021
e4d570d
Merge remote-tracking branch 'origin/master' into aj-skip-type-params
ascjones Jun 22, 2021
271c2b5
Fix compilation after merge
ascjones Jun 22, 2021
a7e1e6b
Merge remote-tracking branch 'origin/master' into aj-skip-type-params
ascjones Jun 23, 2021
411e50c
Add TypeParameter struct and optional type
ascjones Jun 24, 2021
c5f2bd1
Add ui test for skipping type params
ascjones Jun 25, 2021
d453e69
Add example to named_type_params macro
ascjones Jun 25, 2021
b228318
Type hint for named_type_params with MetaForm
ascjones Jun 25, 2021
f060e59
Add bounds attribute docs
ascjones Jun 25, 2021
7bfa773
Add some docs attribute usage
ascjones Jun 25, 2021
eb41e0b
Fmt
ascjones Jun 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 202 additions & 0 deletions derive/src/attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// Copyright 2019-2021 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 syn::{
parse::{
Parse,
ParseBuffer,
},
punctuated::Punctuated,
spanned::Spanned,
Token,
};

const SCALE_INFO: &str = "scale_info";

mod keywords {
syn::custom_keyword!(scale_info);
syn::custom_keyword!(bounds);
syn::custom_keyword!(skip_type_params);
}

/// Parsed and validated set of `#[scale_info(...)]` attributes for an item.
pub struct Attributes {
bounds: Option<BoundsAttr>,
skip_type_params: Option<SkipTypeParamsAttr>,
}

impl Attributes {
/// Extract out `#[scale_info(...)]` attributes from an item.
pub fn from_ast(item: &syn::DeriveInput) -> syn::Result<Self> {
let mut bounds = None;
let mut skip_type_params = None;

let attributes_parser = |input: &ParseBuffer| {
let attrs: Punctuated<ScaleInfoAttr, Token![,]> =
input.parse_terminated(ScaleInfoAttr::parse)?;
Ok(attrs)
};

for attr in &item.attrs {
if !attr.path.is_ident(SCALE_INFO) {
continue
}
let scale_info_attrs = attr.parse_args_with(attributes_parser)?;

for scale_info_attr in scale_info_attrs {
// check for duplicates
match scale_info_attr {
ScaleInfoAttr::Bounds(parsed_bounds) => {
if bounds.is_some() {
return Err(syn::Error::new(
attr.span(),
"Duplicate `bounds` attributes",
))
}
bounds = Some(parsed_bounds);
}
ScaleInfoAttr::SkipTypeParams(parsed_skip_type_params) => {
if skip_type_params.is_some() {
return Err(syn::Error::new(
attr.span(),
"Duplicate `skip_type_params` attributes",
))
}
skip_type_params = Some(parsed_skip_type_params);
}
}
}
}

// validate type params which do not appear in custom bounds but are not skipped.
if let Some(ref bounds) = bounds {
for type_param in item.generics.type_params() {
if !bounds.contains_type_param(type_param) {
let type_param_skipped = skip_type_params
.as_ref()
.map(|skip| skip.skip(type_param))
.unwrap_or(false);
if !type_param_skipped {
let msg = format!(
"Type parameter requires a `TypeInfo` bound, so either: \n \
- add it to `#[scale_info(bounds({}: TypeInfo))]` \n \
- skip it with `#[scale_info(skip_type_params({}))]`",
type_param.ident, type_param.ident
);
return Err(syn::Error::new(type_param.span(), msg))
}
}
}
}

Ok(Self {
bounds,
skip_type_params,
})
}

/// Get the `#[scale_info(bounds(...))]` attribute, if present.
pub fn bounds(&self) -> Option<&BoundsAttr> {
self.bounds.as_ref()
}

/// Get the `#[scale_info(skip_type_params(...))]` attribute, if present.
pub fn skip_type_params(&self) -> Option<&SkipTypeParamsAttr> {
self.skip_type_params.as_ref()
}
}

/// Parsed representation of the `#[scale_info(bounds(...))]` attribute.
#[derive(Clone)]
pub struct BoundsAttr {
predicates: Punctuated<syn::WherePredicate, Token![,]>,
}

impl Parse for BoundsAttr {
fn parse(input: &ParseBuffer) -> syn::Result<Self> {
input.parse::<keywords::bounds>()?;
let content;
syn::parenthesized!(content in input);
let predicates = content.parse_terminated(syn::WherePredicate::parse)?;
Ok(Self { predicates })
}
}

impl BoundsAttr {
/// Add the predicates defined in this attribute to the given `where` clause.
pub fn extend_where_clause(&self, where_clause: &mut syn::WhereClause) {
where_clause.predicates.extend(self.predicates.clone());
}

/// Returns true if the given type parameter appears in the custom bounds attribute.
pub fn contains_type_param(&self, type_param: &syn::TypeParam) -> bool {
self.predicates.iter().any(|p| {
if let syn::WherePredicate::Type(ty) = p {
if let syn::Type::Path(ref path) = ty.bounded_ty {
path.path.get_ident() == Some(&type_param.ident)
} else {
false
}
} else {
false
}
})
}
}

/// Parsed representation of the `#[scale_info(skip_type_params(...))]` attribute.
#[derive(Clone)]
pub struct SkipTypeParamsAttr {
type_params: Punctuated<syn::TypeParam, Token![,]>,
}

impl Parse for SkipTypeParamsAttr {
fn parse(input: &ParseBuffer) -> syn::Result<Self> {
input.parse::<keywords::skip_type_params>()?;
let content;
syn::parenthesized!(content in input);
let type_params = content.parse_terminated(syn::TypeParam::parse)?;
Ok(Self { type_params })
}
}

impl SkipTypeParamsAttr {
/// Returns `true` if the given type parameter should be skipped.
pub fn skip(&self, type_param: &syn::TypeParam) -> bool {
self.type_params
.iter()
.any(|tp| tp.ident == type_param.ident)
}
}

/// Parsed representation of one of the `#[scale_info(..)]` attributes.
pub enum ScaleInfoAttr {
Bounds(BoundsAttr),
SkipTypeParams(SkipTypeParamsAttr),
}

impl Parse for ScaleInfoAttr {
fn parse(input: &ParseBuffer) -> syn::Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(keywords::bounds) {
let bounds = input.parse()?;
Ok(Self::Bounds(bounds))
} else if lookahead.peek(keywords::skip_type_params) {
let skip_type_params = input.parse()?;
Ok(Self::SkipTypeParams(skip_type_params))
} else {
Err(input.error("Expected either `bounds` or `skip_type_params`"))
}
}
}
47 changes: 20 additions & 27 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@
extern crate alloc;
extern crate proc_macro;

mod attr;
mod trait_bounds;
mod utils;

use alloc::{
string::{
String,
ToString,
},
vec::Vec,
};
use proc_macro::TokenStream;
use proc_macro2::{
Span,
Expand Down Expand Up @@ -66,36 +60,35 @@ fn generate(input: TokenStream2) -> Result<TokenStream2> {
}

fn generate_type(input: TokenStream2) -> Result<TokenStream2> {
let mut ast: DeriveInput = syn::parse2(input.clone())?;
let ast: DeriveInput = syn::parse2(input.clone())?;

utils::check_attributes(&ast)?;
let attrs = attr::Attributes::from_ast(&ast)?;

let scale_info = crate_name_ident("scale-info")?;
let parity_scale_codec = crate_name_ident("parity-scale-codec")?;

let ident = &ast.ident;

let where_clause = if let Some(custom_bounds) = utils::custom_trait_bounds(&ast.attrs)
{
let where_clause = ast.generics.make_where_clause();
where_clause.predicates.extend(custom_bounds);
where_clause.clone()
} else {
trait_bounds::make_where_clause(
ident,
&ast.generics,
&ast.data,
&scale_info,
&parity_scale_codec,
)?
};
let where_clause = trait_bounds::make_where_clause(
&attrs,
ident,
&ast.generics,
&ast.data,
&scale_info,
&parity_scale_codec,
)?;

let (impl_generics, ty_generics, _) = ast.generics.split_for_impl();

let generic_type_ids = ast.generics.type_params().map(|ty| {
let ty_ident = &ty.ident;
let type_params = ast.generics.type_params().map(|tp| {
let ty_ident = &tp.ident;
let ty = if attrs.skip_type_params().map_or(true, |skip| !skip.skip(tp)) {
quote! { Some(:: #scale_info ::meta_type::<#ty_ident>()) }
} else {
quote! { None }
};
quote! {
:: #scale_info ::meta_type::<#ty_ident>()
:: #scale_info ::TypeParameter::new(::core::stringify!(#ty_ident), #ty)
}
});

Expand All @@ -112,7 +105,7 @@ fn generate_type(input: TokenStream2) -> Result<TokenStream2> {
fn type_info() -> :: #scale_info ::Type {
:: #scale_info ::Type::builder()
.path(:: #scale_info ::Path::new(::core::stringify!(#ident), ::core::module_path!()))
.type_params(:: #scale_info ::prelude::vec![ #( #generic_type_ids ),* ])
.type_params(:: #scale_info ::prelude::vec![ #( #type_params ),* ])
#docs
.#build_type
}
Expand Down
42 changes: 38 additions & 4 deletions derive/src/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,26 @@ use syn::{
WhereClause,
};

use crate::utils;
use crate::{
attr::Attributes,
utils,
};

/// 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.
///
/// # Effect of attributes
///
/// `#[scale_info(skip_type_params(..))]`
///
/// Will not add `TypeInfo` bounds for any type parameters skipped via this attribute.
///
/// `#[scale_info(bounds(..))]`
///
/// Replaces *all* auto-generated trait bounds with the user-defined ones.
Copy link
Contributor

@dvdplm dvdplm Jun 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[scale_info(skip_type_params(U))]
#[scale_info(bounds("U: Other + Traits + NotTypeInfoStuff))]
struct A<T, U> {}

In the above, I will not get TypeInfo bounds auto-generated for T (because bounds() override all auto generating machinery) but I will get the bounds I manually specified for U (because bounds(…) trumps skip_type_params). Is that correct?

A few examples like above would be good! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the above, I will not get TypeInfo bounds auto-generated for T (because bounds() override all auto generating machinery) but I will get the bounds I manually specified for U (because bounds(…) trumps skip_type_params). Is that correct?

Correct. It trumps it in respect to the generated where clause. However skip_type_params still has its effect on the generated list of type_params for the type.

A few examples like above would be good! :)

I am in the process of adding more rustdocs on the topic, in the meantime I have added some more ui tests and have updated the PR description.

pub fn make_where_clause<'a>(
attrs: &'a Attributes,
input_ident: &'a Ident,
generics: &'a Generics,
data: &'a syn::Data,
Expand All @@ -47,14 +61,29 @@ pub fn make_where_clause<'a>(
predicates: Punctuated::new(),
}
});

// Use custom bounds as where clause.
if let Some(custom_bounds) = attrs.bounds() {
custom_bounds.extend_where_clause(&mut where_clause);

// `'static` lifetime bounds are always required for type parameters, because of the
// requirement on `std::any::TypeId::of` for any field type constructor.
for type_param in generics.type_params() {
let ident = &type_param.ident;
where_clause.predicates.push(parse_quote!(#ident: 'static))
}

return Ok(where_clause)
}

for lifetime in generics.lifetimes() {
where_clause
.predicates
.push(parse_quote!(#lifetime: 'static))
}

let type_params = generics.type_params();
let ty_params_ids = type_params
let ty_params_ids = generics
.type_params()
.map(|type_param| type_param.ident.clone())
.collect::<Vec<Ident>>();

Expand All @@ -79,7 +108,12 @@ pub fn make_where_clause<'a>(
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));
if attrs
.skip_type_params()
.map_or(true, |skip| !skip.skip(type_param))
{
bounds.push(parse_quote!(:: #scale_info ::TypeInfo));
}
bounds.push(parse_quote!('static));
where_clause
.predicates
Expand Down
Loading