[FRAME] Remove V1 Module Syntax (#14685)

* Remove V1 pallet syntax

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Remove more

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* More...

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Move no_bound derives to own folder

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* fmt

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Keep re-exports

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This commit is contained in:
Oliver Tale-Yazdi
2023-07-31 18:49:39 +02:00
committed by GitHub
parent 49816ff4d9
commit 0853bbba72
22 changed files with 48 additions and 6418 deletions
@@ -0,0 +1,109 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// 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::spanned::Spanned;
/// Derive Clone but do not bound any generic.
pub fn derive_clone_no_bound(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input: syn::DeriveInput = match syn::parse(input) {
Ok(input) => input,
Err(e) => return e.to_compile_error().into(),
};
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let impl_ = match input.data {
syn::Data::Struct(struct_) => match struct_.fields {
syn::Fields::Named(named) => {
let fields = named.named.iter().map(|i| &i.ident).map(|i| {
quote::quote_spanned!(i.span() =>
#i: core::clone::Clone::clone(&self.#i)
)
});
quote::quote!( Self { #( #fields, )* } )
},
syn::Fields::Unnamed(unnamed) => {
let fields =
unnamed.unnamed.iter().enumerate().map(|(i, _)| syn::Index::from(i)).map(|i| {
quote::quote_spanned!(i.span() =>
core::clone::Clone::clone(&self.#i)
)
});
quote::quote!( Self ( #( #fields, )* ) )
},
syn::Fields::Unit => {
quote::quote!(Self)
},
},
syn::Data::Enum(enum_) => {
let variants = enum_.variants.iter().map(|variant| {
let ident = &variant.ident;
match &variant.fields {
syn::Fields::Named(named) => {
let captured = named.named.iter().map(|i| &i.ident);
let cloned = captured.clone().map(|i| {
quote::quote_spanned!(i.span() =>
#i: core::clone::Clone::clone(#i)
)
});
quote::quote!(
Self::#ident { #( ref #captured, )* } => Self::#ident { #( #cloned, )*}
)
},
syn::Fields::Unnamed(unnamed) => {
let captured = unnamed
.unnamed
.iter()
.enumerate()
.map(|(i, f)| syn::Ident::new(&format!("_{}", i), f.span()));
let cloned = captured.clone().map(|i| {
quote::quote_spanned!(i.span() =>
core::clone::Clone::clone(#i)
)
});
quote::quote!(
Self::#ident ( #( ref #captured, )* ) => Self::#ident ( #( #cloned, )*)
)
},
syn::Fields::Unit => quote::quote!( Self::#ident => Self::#ident ),
}
});
quote::quote!(match self {
#( #variants, )*
})
},
syn::Data::Union(_) => {
let msg = "Union type not supported by `derive(CloneNoBound)`";
return syn::Error::new(input.span(), msg).to_compile_error().into()
},
};
quote::quote!(
const _: () = {
impl #impl_generics core::clone::Clone for #name #ty_generics #where_clause {
fn clone(&self) -> Self {
#impl_
}
}
};
)
.into()
}
@@ -0,0 +1,123 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// 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::spanned::Spanned;
/// Derive Debug but do not bound any generics.
pub fn derive_debug_no_bound(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input: syn::DeriveInput = match syn::parse(input) {
Ok(input) => input,
Err(e) => return e.to_compile_error().into(),
};
let input_ident = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let impl_ = match input.data {
syn::Data::Struct(struct_) => match struct_.fields {
syn::Fields::Named(named) => {
let fields =
named.named.iter().map(|i| &i.ident).map(
|i| quote::quote_spanned!(i.span() => .field(stringify!(#i), &self.#i) ),
);
quote::quote!(
fmt.debug_struct(stringify!(#input_ident))
#( #fields )*
.finish()
)
},
syn::Fields::Unnamed(unnamed) => {
let fields = unnamed
.unnamed
.iter()
.enumerate()
.map(|(i, _)| syn::Index::from(i))
.map(|i| quote::quote_spanned!(i.span() => .field(&self.#i) ));
quote::quote!(
fmt.debug_tuple(stringify!(#input_ident))
#( #fields )*
.finish()
)
},
syn::Fields::Unit => quote::quote!(fmt.write_str(stringify!(#input_ident))),
},
syn::Data::Enum(enum_) => {
let variants = enum_.variants.iter().map(|variant| {
let ident = &variant.ident;
let full_variant_str = format!("{}::{}", input_ident, ident);
match &variant.fields {
syn::Fields::Named(named) => {
let captured = named.named.iter().map(|i| &i.ident);
let debugged = captured.clone().map(|i| {
quote::quote_spanned!(i.span() =>
.field(stringify!(#i), &#i)
)
});
quote::quote!(
Self::#ident { #( ref #captured, )* } => {
fmt.debug_struct(#full_variant_str)
#( #debugged )*
.finish()
}
)
},
syn::Fields::Unnamed(unnamed) => {
let captured = unnamed
.unnamed
.iter()
.enumerate()
.map(|(i, f)| syn::Ident::new(&format!("_{}", i), f.span()));
let debugged = captured
.clone()
.map(|i| quote::quote_spanned!(i.span() => .field(&#i)));
quote::quote!(
Self::#ident ( #( ref #captured, )* ) => {
fmt.debug_tuple(#full_variant_str)
#( #debugged )*
.finish()
}
)
},
syn::Fields::Unit => quote::quote!(
Self::#ident => fmt.write_str(#full_variant_str)
),
}
});
quote::quote!(match *self {
#( #variants, )*
})
},
syn::Data::Union(_) => {
let msg = "Union type not supported by `derive(DebugNoBound)`";
return syn::Error::new(input.span(), msg).to_compile_error().into()
},
};
quote::quote!(
const _: () = {
impl #impl_generics core::fmt::Debug for #input_ident #ty_generics #where_clause {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
#impl_
}
}
};
)
.into()
}
@@ -0,0 +1,163 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// 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 proc_macro2::Span;
use quote::{quote, quote_spanned};
use syn::{spanned::Spanned, Data, DeriveInput, Fields};
/// Derive Default but do not bound any generic.
pub fn derive_default_no_bound(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = syn::parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let impl_ = match input.data {
Data::Struct(struct_) => match struct_.fields {
Fields::Named(named) => {
let fields = named.named.iter().map(|field| &field.ident).map(|ident| {
quote_spanned! {ident.span() =>
#ident: core::default::Default::default()
}
});
quote!(Self { #( #fields, )* })
},
Fields::Unnamed(unnamed) => {
let fields = unnamed.unnamed.iter().map(|field| {
quote_spanned! {field.span()=>
core::default::Default::default()
}
});
quote!(Self( #( #fields, )* ))
},
Fields::Unit => {
quote!(Self)
},
},
Data::Enum(enum_) => {
if enum_.variants.is_empty() {
return syn::Error::new_spanned(name, "cannot derive Default for an empty enum")
.to_compile_error()
.into()
}
// all #[default] attrs with the variant they're on; i.e. a var
let default_variants = enum_
.variants
.into_iter()
.filter(|variant| variant.attrs.iter().any(|attr| attr.path().is_ident("default")))
.collect::<Vec<_>>();
match &*default_variants {
[] => {
return syn::Error::new(
name.clone().span(),
// writing this as a regular string breaks rustfmt for some reason
r#"no default declared, make a variant default by placing `#[default]` above it"#,
)
.into_compile_error()
.into()
},
// only one variant with the #[default] attribute set
[default_variant] => {
let variant_attrs = default_variant
.attrs
.iter()
.filter(|a| a.path().is_ident("default"))
.collect::<Vec<_>>();
// check that there is only one #[default] attribute on the variant
if let [first_attr, second_attr, additional_attrs @ ..] = &*variant_attrs {
let mut err =
syn::Error::new(Span::call_site(), "multiple `#[default]` attributes");
err.combine(syn::Error::new_spanned(first_attr, "`#[default]` used here"));
err.extend([second_attr].into_iter().chain(additional_attrs).map(
|variant| {
syn::Error::new_spanned(variant, "`#[default]` used again here")
},
));
return err.into_compile_error().into()
}
let variant_ident = &default_variant.ident;
let fully_qualified_variant_path = quote!(Self::#variant_ident);
match &default_variant.fields {
Fields::Named(named) => {
let fields =
named.named.iter().map(|field| &field.ident).map(|ident| {
quote_spanned! {ident.span()=>
#ident: core::default::Default::default()
}
});
quote!(#fully_qualified_variant_path { #( #fields, )* })
},
Fields::Unnamed(unnamed) => {
let fields = unnamed.unnamed.iter().map(|field| {
quote_spanned! {field.span()=>
core::default::Default::default()
}
});
quote!(#fully_qualified_variant_path( #( #fields, )* ))
},
Fields::Unit => fully_qualified_variant_path,
}
},
[first, additional @ ..] => {
let mut err = syn::Error::new(Span::call_site(), "multiple declared defaults");
err.combine(syn::Error::new_spanned(first, "first default"));
err.extend(
additional
.into_iter()
.map(|variant| syn::Error::new_spanned(variant, "additional default")),
);
return err.into_compile_error().into()
},
}
},
Data::Union(union_) =>
return syn::Error::new_spanned(
union_.union_token,
"Union type not supported by `derive(DefaultNoBound)`",
)
.to_compile_error()
.into(),
};
quote!(
const _: () = {
impl #impl_generics core::default::Default for #name #ty_generics #where_clause {
fn default() -> Self {
#impl_
}
}
};
)
.into()
}
@@ -0,0 +1,23 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// 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.
//! Derive macros to derive traits without bounding generic parameters.
pub mod clone;
pub mod debug;
pub mod default;
pub mod partial_eq;
@@ -0,0 +1,139 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// 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::spanned::Spanned;
/// Derive PartialEq but do not bound any generic.
pub fn derive_partial_eq_no_bound(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input: syn::DeriveInput = match syn::parse(input) {
Ok(input) => input,
Err(e) => return e.to_compile_error().into(),
};
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let impl_ = match input.data {
syn::Data::Struct(struct_) => match struct_.fields {
syn::Fields::Named(named) => {
let fields = named
.named
.iter()
.map(|i| &i.ident)
.map(|i| quote::quote_spanned!(i.span() => self.#i == other.#i ));
quote::quote!( true #( && #fields )* )
},
syn::Fields::Unnamed(unnamed) => {
let fields = unnamed
.unnamed
.iter()
.enumerate()
.map(|(i, _)| syn::Index::from(i))
.map(|i| quote::quote_spanned!(i.span() => self.#i == other.#i ));
quote::quote!( true #( && #fields )* )
},
syn::Fields::Unit => {
quote::quote!(true)
},
},
syn::Data::Enum(enum_) => {
let variants =
enum_.variants.iter().map(|variant| {
let ident = &variant.ident;
match &variant.fields {
syn::Fields::Named(named) => {
let names = named.named.iter().map(|i| &i.ident);
let other_names = names.clone().enumerate().map(|(n, ident)| {
syn::Ident::new(&format!("_{}", n), ident.span())
});
let capture = names.clone();
let other_capture = names
.clone()
.zip(other_names.clone())
.map(|(i, other_i)| quote::quote!(#i: #other_i));
let eq = names.zip(other_names).map(
|(i, other_i)| quote::quote_spanned!(i.span() => #i == #other_i),
);
quote::quote!(
(
Self::#ident { #( #capture, )* },
Self::#ident { #( #other_capture, )* },
) => true #( && #eq )*
)
},
syn::Fields::Unnamed(unnamed) => {
let names = unnamed
.unnamed
.iter()
.enumerate()
.map(|(i, f)| syn::Ident::new(&format!("_{}", i), f.span()));
let other_names =
unnamed.unnamed.iter().enumerate().map(|(i, f)| {
syn::Ident::new(&format!("_{}_other", i), f.span())
});
let eq = names.clone().zip(other_names.clone()).map(
|(i, other_i)| quote::quote_spanned!(i.span() => #i == #other_i),
);
quote::quote!(
(
Self::#ident ( #( #names, )* ),
Self::#ident ( #( #other_names, )* ),
) => true #( && #eq )*
)
},
syn::Fields::Unit => quote::quote!( (Self::#ident, Self::#ident) => true ),
}
});
let mut different_variants = vec![];
for (i, i_variant) in enum_.variants.iter().enumerate() {
for (j, j_variant) in enum_.variants.iter().enumerate() {
if i != j {
let i_ident = &i_variant.ident;
let j_ident = &j_variant.ident;
different_variants.push(quote::quote!(
(Self::#i_ident { .. }, Self::#j_ident { .. }) => false
))
}
}
}
quote::quote!( match (self, other) {
#( #variants, )*
#( #different_variants, )*
})
},
syn::Data::Union(_) => {
let msg = "Union type not supported by `derive(PartialEqNoBound)`";
return syn::Error::new(input.span(), msg).to_compile_error().into()
},
};
quote::quote!(
const _: () = {
impl #impl_generics core::cmp::PartialEq for #name #ty_generics #where_clause {
fn eq(&self, other: &Self) -> bool {
#impl_
}
}
};
)
.into()
}