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

Switch to stable toolchain in CI, fix clippy/tests/fmt #193

Merged
merged 6 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
toolchain: stable
override: true
components: rustfmt, clippy
target: wasm32-unknown-unknown
Expand Down
64 changes: 0 additions & 64 deletions .rustfmt.toml

This file was deleted.

27 changes: 11 additions & 16 deletions derive/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
// limitations under the License.

use syn::{
parse::{
Parse,
ParseBuffer,
},
parse::{Parse, ParseBuffer},
punctuated::Punctuated,
spanned::Spanned,
Token,
Expand Down Expand Up @@ -55,7 +52,7 @@ impl Attributes {

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

Expand All @@ -67,7 +64,7 @@ impl Attributes {
return Err(syn::Error::new(
attr.span(),
"Duplicate `bounds` attributes",
))
));
}
bounds = Some(parsed_bounds);
}
Expand All @@ -76,7 +73,7 @@ impl Attributes {
return Err(syn::Error::new(
attr.span(),
"Duplicate `skip_type_params` attributes",
))
));
}
skip_type_params = Some(parsed_skip_type_params);
}
Expand All @@ -85,7 +82,7 @@ impl Attributes {
return Err(syn::Error::new(
attr.span(),
"Duplicate `capture_docs` attributes",
))
));
}
capture_docs = Some(parsed_capture_docs);
}
Expand All @@ -95,7 +92,7 @@ impl Attributes {
return Err(syn::Error::new(
attr.span(),
"Duplicate `crate` attributes",
))
));
}

crate_path = Some(parsed_crate_path);
Expand All @@ -119,7 +116,7 @@ impl Attributes {
- skip it with `#[scale_info(skip_type_params({}))]`",
type_param.ident, type_param.ident
);
return Err(syn::Error::new(type_param.span(), msg))
return Err(syn::Error::new(type_param.span(), msg));
}
}
}
Expand Down Expand Up @@ -239,12 +236,10 @@ impl Parse for CaptureDocsAttr {
"default" => Ok(Self::Default),
"always" => Ok(Self::Always),
"never" => Ok(Self::Never),
_ => {
Err(syn::Error::new_spanned(
capture_docs_lit,
r#"Invalid capture_docs value. Expected one of: "default", "always", "never" "#,
))
}
_ => Err(syn::Error::new_spanned(
capture_docs_lit,
r#"Invalid capture_docs value. Expected one of: "default", "always", "never" "#,
)),
}
}
}
Expand Down
44 changes: 12 additions & 32 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,17 @@ mod attr;
mod trait_bounds;
mod utils;

use self::attr::{
Attributes,
CaptureDocsAttr,
CratePathAttr,
};
use self::attr::{Attributes, CaptureDocsAttr, CratePathAttr};
use proc_macro::TokenStream;
use proc_macro2::{
Span,
TokenStream as TokenStream2,
};
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::quote;
use syn::{
parse::{
Error,
Result,
},
parse::{Error, Result},
parse_quote,
punctuated::Punctuated,
token::Comma,
visit_mut::VisitMut,
Data,
DataEnum,
DataStruct,
DeriveInput,
Field,
Fields,
Ident,
Lifetime,
Data, DataEnum, DataStruct, DeriveInput, Field, Fields, Ident, Lifetime,
};

#[proc_macro_derive(TypeInfo, attributes(scale_info, codec))]
Expand Down Expand Up @@ -97,7 +80,11 @@ impl TypeInfoImpl {

let type_params = self.ast.generics.type_params().map(|tp| {
let ty_ident = &tp.ident;
let ty = if self.attrs.skip_type_params().map_or(true, |skip| !skip.skip(tp)) {
let ty = if self
.attrs
.skip_type_params()
.map_or(true, |skip| !skip.skip(tp))
{
quote! { ::core::option::Option::Some(#scale_info::meta_type::<#ty_ident>()) }
} else {
quote! { ::core::option::Option::None }
Expand All @@ -110,9 +97,7 @@ impl TypeInfoImpl {
let build_type = match &self.ast.data {
Data::Struct(ref s) => self.generate_composite_type(s, &scale_info),
Data::Enum(ref e) => self.generate_variant_type(e, &scale_info),
Data::Union(_) => {
return Err(Error::new_spanned(&self.ast, "Unions not supported"))
}
Data::Union(_) => return Err(Error::new_spanned(&self.ast, "Unions not supported")),
};
let docs = self.generate_docs(&self.ast.attrs);

Expand Down Expand Up @@ -204,11 +189,7 @@ impl TypeInfoImpl {
.collect()
}

fn generate_variant_type(
&self,
data_enum: &DataEnum,
scale_info: &syn::Path,
) -> TokenStream2 {
fn generate_variant_type(&self, data_enum: &DataEnum, scale_info: &syn::Path) -> TokenStream2 {
let variants = &data_enum.variants;

let variants = variants
Expand Down Expand Up @@ -272,8 +253,7 @@ impl TypeInfoImpl {
if meta.path.get_ident().map_or(false, |ident| ident == "doc") {
if let syn::Lit::Str(lit) = &meta.lit {
let lit_value = lit.value();
let stripped =
lit_value.strip_prefix(' ').unwrap_or(&lit_value);
let stripped = lit_value.strip_prefix(' ').unwrap_or(&lit_value);
let lit: syn::Lit = parse_quote!(#stripped);
Some(lit)
} else {
Expand Down
67 changes: 25 additions & 42 deletions derive/src/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,11 @@ use syn::{
parse_quote,
punctuated::Punctuated,
spanned::Spanned,
visit::{
self,
Visit,
},
Generics,
Result,
Type,
TypePath,
WhereClause,
visit::{self, Visit},
Generics, Result, Type, TypePath, WhereClause,
};

use crate::{
attr::Attributes,
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
Expand All @@ -54,12 +44,13 @@ pub fn make_where_clause<'a>(
data: &'a syn::Data,
scale_info: &syn::Path,
) -> Result<WhereClause> {
let mut where_clause = generics.where_clause.clone().unwrap_or_else(|| {
WhereClause {
let mut where_clause = generics
.where_clause
.clone()
.unwrap_or_else(|| WhereClause {
where_token: <syn::Token![where]>::default(),
predicates: Punctuated::new(),
}
});
});

// Use custom bounds as where clause.
if let Some(custom_bounds) = attrs.bounds() {
Expand All @@ -72,7 +63,7 @@ pub fn make_where_clause<'a>(
where_clause.predicates.push(parse_quote!(#ident: 'static))
}

return Ok(where_clause)
return Ok(where_clause);
}

for lifetime in generics.lifetimes() {
Expand All @@ -87,7 +78,7 @@ pub fn make_where_clause<'a>(
.collect::<Vec<Ident>>();

if ty_params_ids.is_empty() {
return Ok(where_clause)
return Ok(where_clause);
}

let types = collect_types_to_bind(input_ident, data, &ty_params_ids)?;
Expand Down Expand Up @@ -160,7 +151,7 @@ fn type_or_sub_type_path_starts_with_ident(ty: &Type, ident: &Ident) -> bool {
if let Some(segment) = i.path.segments.first() {
if &segment.ident == self.ident {
self.result = true;
return
return;
}
}
}
Expand Down Expand Up @@ -199,33 +190,25 @@ fn collect_types_to_bind(
};

let types = match *data {
syn::Data::Struct(ref data) => {
match &data.fields {
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(),
}
}

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()
}
})
.collect(),

syn::Data::Union(ref data) => {
return Err(syn::Error::new(
Expand Down
Loading