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

Reexport scale dependencies, introduce #[ink::scale_derive] #1890

Merged
merged 43 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
0e9bcf2
WIP
ascjones Aug 24, 2023
a30251f
Added derives, WIP search and replace of codegen
ascjones Aug 24, 2023
79c68e6
Use ink reexported traits and derives in codegen
ascjones Aug 25, 2023
baa15e7
Use new attribute macros everywhere
ascjones Aug 25, 2023
c145276
Merge branch 'master' into aj/reexport-scale
ascjones Aug 25, 2023
eab2087
Single attribute for deriving
ascjones Aug 25, 2023
eca0cc7
Fix type_info attribute
ascjones Aug 25, 2023
1098025
Rename to scale_derive
ascjones Aug 25, 2023
83452a9
Use new macros
ascjones Aug 25, 2023
d7b4d84
Restore ink::Event derive
ascjones Aug 29, 2023
e87fb60
Remove scale and scale-info dependencies
ascjones Aug 29, 2023
dcb1185
Migrate Error type derives
ascjones Aug 29, 2023
632a05a
Fixing some UI tests
ascjones Aug 29, 2023
cf19fc9
Fixing some UI tests
ascjones Aug 29, 2023
ac437a1
Merge branch 'master' into aj/reexport-scale
ascjones Aug 29, 2023
074a922
Examples dependencies
ascjones Aug 29, 2023
ab37c4f
Remove std feature for scale/scale-info integration-tests
ascjones Aug 29, 2023
84b7f18
spellcheck
ascjones Aug 29, 2023
8833e83
Remove scale/std from last integration-test
ascjones Aug 29, 2023
24ab780
Fix contract_ref type_info
ascjones Aug 29, 2023
6ebcf38
Fix call_builder type_info
ascjones Aug 29, 2023
6c03846
Add UI tests
ascjones Aug 29, 2023
ba0acd7
Fix UI tests
ascjones Aug 29, 2023
f646385
Fix integration-test
ascjones Aug 29, 2023
3a3e080
Fix UI test
ascjones Aug 29, 2023
1dd1ffa
Fmt
ascjones Aug 29, 2023
aa6a104
Fix integration test
ascjones Aug 29, 2023
bdee1be
Fix integration test
ascjones Aug 29, 2023
fe7004d
Docs
ascjones Aug 29, 2023
51480b7
Fix integration-tests
ascjones Aug 29, 2023
e6ddfa5
Fix integration-tests
ascjones Aug 29, 2023
4ebeaf8
Fix integration-test
ascjones Aug 29, 2023
c66333b
CHANGELOG.md
ascjones Aug 29, 2023
d33e628
Fix
ascjones Aug 29, 2023
1f8262d
fmt
ascjones Aug 29, 2023
d355d2b
Fix integration test
ascjones Aug 29, 2023
2a172d3
Fix integration tests
ascjones Aug 29, 2023
6423e62
Fix integration test
ascjones Aug 29, 2023
cf77b37
Fix integration test
ascjones Aug 29, 2023
f1c37ea
Fix integration test
ascjones Aug 29, 2023
652bf78
Merge branch 'master' into aj/reexport-scale
ascjones Aug 31, 2023
bf4cda2
Use CamelCase for `scale_derive`
ascjones Aug 31, 2023
a395df9
UI test
ascjones Aug 31, 2023
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: 2 additions & 0 deletions .config/cargo_spellcheck.dic
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ reentrancy
refcount
scalability
scalable
scale_derive
sr25519
stdin
stdout
subber
tuple
type_info
unordered
untyped
v1
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Persist static buffer size in metadata - [#1880](https://github.com/paritytech/ink/pull/1880)
- Add backend choice to the E2E testcase configuration ‒ [#1864](https://github.com/paritytech/ink/pull/1864)

### Changed
- Reexport `scale` dependencies, introduce `#[ink::scale_derive]` ‒ [#1890](https://github.com/paritytech/ink/pull/1890)

### Added
- Schema generation - [#1765](https://github.com/paritytech/ink/pull/1765)
- Add `set_block_number` to off-chain test api `Engine` - [#1806](https://github.com/paritytech/ink/pull/1806)
Expand Down
4 changes: 2 additions & 2 deletions crates/env/src/call/create_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ where
/// #[ink(storage)]
/// pub struct Contract {}
///
/// #[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
/// #[cfg_attr(feature = "std", derive(::scale_info::TypeInfo))]
/// #[derive(Debug, PartialEq, Eq)]
/// #[ink::scale_derive(Encode, Decode, TypeInfo)]
/// pub enum Error {
/// Foo,
/// }
Expand Down
5 changes: 3 additions & 2 deletions crates/ink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ ink_prelude = { workspace = true }
ink_macro = { workspace = true }

scale = { package = "parity-scale-codec", workspace = true }
scale-info = { workspace = true, default-features = false, features = ["derive"], optional = true }
derive_more = { workspace = true, features = ["from"] }

[dev-dependencies]
ink_ir = { workspace = true, default-features = true }
ink_metadata = { workspace = true }

trybuild = { workspace = true, features = ["diff"] }
# Required for the doctest of `env_access::EnvAccess::instantiate_contract`
scale-info = { workspace = true, features = ["derive"] }


[features]
default = ["std"]
Expand All @@ -44,6 +44,7 @@ std = [
"ink_storage/std",
"ink_macro/std",
"scale/std",
"scale-info/std",
]
# Enable contract debug messages via `debug_print!` and `debug_println!`.
ink-debug = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,16 @@ impl CallBuilder<'_> {
/// messages and trait implementations in a type safe way.
#[repr(transparent)]
#[cfg_attr(feature = "std", derive(
::scale_info::TypeInfo,
::ink::storage::traits::StorageLayout,
))]
#[derive(
::core::fmt::Debug,
::scale::Encode,
::scale::Decode,
::core::hash::Hash,
::core::cmp::PartialEq,
::core::cmp::Eq,
::core::clone::Clone,
)]
#[::ink::scale_derive(Encode, Decode, TypeInfo)]
pub struct #cb_ident {
account_id: AccountId,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,16 @@ impl ContractRef<'_> {
let ref_ident = self.generate_contract_ref_ident();
quote_spanned!(span=>
#[cfg_attr(feature = "std", derive(
::scale_info::TypeInfo,
::ink::storage::traits::StorageLayout,
))]
#[derive(
::core::fmt::Debug,
::scale::Encode,
::scale::Decode,
::core::hash::Hash,
::core::cmp::PartialEq,
::core::cmp::Eq,
::core::clone::Clone,
)]
#[::ink::scale_derive(Encode, Decode, TypeInfo)]
#( #doc_attrs )*
pub struct #ref_ident {
inner: <#storage_ident as ::ink::codegen::ContractCallBuilder>::Type,
Expand All @@ -121,7 +119,7 @@ impl ContractRef<'_> {
impl<E> ::ink::env::call::ConstructorReturnType<#ref_ident>
for ::core::result::Result<#storage_ident, E>
where
E: ::scale::Decode
E: ::ink::scale::Decode
{
const IS_RESULT: bool = true;

Expand Down
4 changes: 1 addition & 3 deletions crates/ink/codegen/src/generator/chain_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ impl GenerateCode for ChainExtension<'_> {
let instance_ident = format_ident!("__ink_{}Instance", ident);
quote_spanned!(span =>
#(#attrs)*
#[cfg_attr(feature = "std", derive(
::scale_info::TypeInfo,
))]
#[::ink::scale_derive(TypeInfo)]
pub enum #ident {}

const _: () = {
Expand Down
28 changes: 14 additions & 14 deletions crates/ink/codegen/src/generator/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ impl Dispatch<'_> {
#( #cfg_attrs )*
#const_ident => {
::core::result::Result::Ok(Self::#constructor_ident(
<#constructor_input as ::scale::Decode>::decode(input)
<#constructor_input as ::ink::scale::Decode>::decode(input)
.map_err(|_| ::ink::reflect::DispatchError::InvalidParameters)?
))
}
Expand All @@ -534,7 +534,7 @@ impl Dispatch<'_> {
);
quote! {
::core::result::Result::Ok(Self::#constructor_ident(
<#constructor_input as ::scale::Decode>::decode(input)
<#constructor_input as ::ink::scale::Decode>::decode(input)
.map_err(|_| ::ink::reflect::DispatchError::InvalidParameters)?
))
}
Expand Down Expand Up @@ -612,12 +612,12 @@ impl Dispatch<'_> {
fn decode_dispatch<I>(input: &mut I)
-> ::core::result::Result<Self, ::ink::reflect::DispatchError>
where
I: ::scale::Input,
I: ::ink::scale::Input,
{
#(
#constructor_selector
)*
match <[::core::primitive::u8; 4usize] as ::scale::Decode>::decode(input)
match <[::core::primitive::u8; 4usize] as ::ink::scale::Decode>::decode(input)
.map_err(|_| ::ink::reflect::DispatchError::InvalidSelector)?
{
#( #constructor_match , )*
Expand All @@ -626,10 +626,10 @@ impl Dispatch<'_> {
}
}

impl ::scale::Decode for __ink_ConstructorDecoder {
fn decode<I>(input: &mut I) -> ::core::result::Result<Self, ::scale::Error>
impl ::ink::scale::Decode for __ink_ConstructorDecoder {
fn decode<I>(input: &mut I) -> ::core::result::Result<Self, ::ink::scale::Error>
where
I: ::scale::Input,
I: ::ink::scale::Input,
{
<Self as ::ink::reflect::DecodeDispatch>::decode_dispatch(input)
.map_err(::core::convert::Into::into)
Expand Down Expand Up @@ -719,7 +719,7 @@ impl Dispatch<'_> {
#( #cfg_attrs )*
#const_ident => {
::core::result::Result::Ok(Self::#message_ident(
<#message_input as ::scale::Decode>::decode(input)
<#message_input as ::ink::scale::Decode>::decode(input)
.map_err(|_| ::ink::reflect::DispatchError::InvalidParameters)?
))
}
Expand All @@ -734,7 +734,7 @@ impl Dispatch<'_> {
expand_message_input(message_span, storage_ident, item.id.clone());
quote! {
::core::result::Result::Ok(Self::#message_ident(
<#message_input as ::scale::Decode>::decode(input)
<#message_input as ::ink::scale::Decode>::decode(input)
.map_err(|_| ::ink::reflect::DispatchError::InvalidParameters)?
))
}
Expand Down Expand Up @@ -809,12 +809,12 @@ impl Dispatch<'_> {
fn decode_dispatch<I>(input: &mut I)
-> ::core::result::Result<Self, ::ink::reflect::DispatchError>
where
I: ::scale::Input,
I: ::ink::scale::Input,
{
#(
#message_selector
)*
match <[::core::primitive::u8; 4usize] as ::scale::Decode>::decode(input)
match <[::core::primitive::u8; 4usize] as ::ink::scale::Decode>::decode(input)
.map_err(|_| ::ink::reflect::DispatchError::InvalidSelector)?
{
#( #message_match , )*
Expand All @@ -823,10 +823,10 @@ impl Dispatch<'_> {
}
}

impl ::scale::Decode for __ink_MessageDecoder {
fn decode<I>(input: &mut I) -> ::core::result::Result<Self, ::scale::Error>
impl ::ink::scale::Decode for __ink_MessageDecoder {
fn decode<I>(input: &mut I) -> ::core::result::Result<Self, ::ink::scale::Error>
where
I: ::scale::Input,
I: ::ink::scale::Input,
{
<Self as ::ink::reflect::DecodeDispatch>::decode_dispatch(input)
.map_err(::core::convert::Into::into)
Expand Down
3 changes: 2 additions & 1 deletion crates/ink/codegen/src/generator/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ impl GenerateCode for Event<'_> {
.then(|| quote::quote! { #[ink(anonymous)] });
quote::quote! (
#[cfg_attr(feature = "std", derive(::ink::EventMetadata))]
#[derive(::ink::Event, ::scale::Encode, ::scale::Decode)]
#[derive(::ink::Event)]
#[::ink::scale_derive(Encode, Decode)]
#anonymous
#item
)
Expand Down
2 changes: 1 addition & 1 deletion crates/ink/codegen/src/generator/storage_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ impl GenerateCode for StorageItem<'_> {
if self.item.config().derive() {
derive = quote! {
#[cfg_attr(feature = "std", derive(
::scale_info::TypeInfo,
::ink::storage::traits::StorageLayout,
))]
#[::ink::scale_derive(TypeInfo)]
#[derive(
::ink::storage::traits::StorableHint,
::ink::storage::traits::StorageKey,
Expand Down
13 changes: 5 additions & 8 deletions crates/ink/codegen/src/generator/trait_def/call_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ impl CallBuilder<'_> {
/// All calls to types (contracts) implementing the trait will be built by this type.
#[doc(hidden)]
#[allow(non_camel_case_types)]
#[derive(
::scale::Encode,
::scale::Decode,
)]
#[::ink::scale_derive(Encode, Decode)]
#[repr(transparent)]
pub struct #call_builder_ident<E>
where
Expand Down Expand Up @@ -198,15 +195,15 @@ impl CallBuilder<'_> {

#[cfg(feature = "std")]
/// We require this manual implementation since the derive produces incorrect trait bounds.
impl<E> ::scale_info::TypeInfo for #call_builder_ident<E>
impl<E> ::ink::scale_info::TypeInfo for #call_builder_ident<E>
where
E: ::ink::env::Environment,
<E as ::ink::env::Environment>::AccountId: ::scale_info::TypeInfo + 'static,
<E as ::ink::env::Environment>::AccountId: ::ink::scale_info::TypeInfo + 'static,
{
type Identity = <E as ::ink::env::Environment>::AccountId;

fn type_info() -> ::scale_info::Type {
<<E as ::ink::env::Environment>::AccountId as ::scale_info::TypeInfo>::type_info()
fn type_info() -> ::ink::scale_info::Type {
<<E as ::ink::env::Environment>::AccountId as ::ink::scale_info::TypeInfo>::type_info()
}
}
)
Expand Down
15 changes: 6 additions & 9 deletions crates/ink/codegen/src/generator/trait_def/call_forwarder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ impl CallForwarder<'_> {
/// will be handled by this type.
#[doc(hidden)]
#[allow(non_camel_case_types)]
#[derive(
::scale::Encode,
::scale::Decode,
)]
#[::ink::scale_derive(Encode, Decode)]
#[repr(transparent)]
pub struct #call_forwarder_ident<E>
where
Expand Down Expand Up @@ -193,18 +190,18 @@ impl CallForwarder<'_> {

#[cfg(feature = "std")]
/// We require this manual implementation since the derive produces incorrect trait bounds.
impl<E> ::scale_info::TypeInfo for #call_forwarder_ident<E>
impl<E> ::ink::scale_info::TypeInfo for #call_forwarder_ident<E>
where
E: ::ink::env::Environment,
<E as ::ink::env::Environment>::AccountId: ::scale_info::TypeInfo + 'static,
<E as ::ink::env::Environment>::AccountId: ::ink::scale_info::TypeInfo + 'static,
{
type Identity = <
<Self as ::ink::codegen::TraitCallBuilder>::Builder as ::scale_info::TypeInfo
<Self as ::ink::codegen::TraitCallBuilder>::Builder as ::ink::scale_info::TypeInfo
>::Identity;

fn type_info() -> ::scale_info::Type {
fn type_info() -> ::ink::scale_info::Type {
<
<Self as ::ink::codegen::TraitCallBuilder>::Builder as ::scale_info::TypeInfo
<Self as ::ink::codegen::TraitCallBuilder>::Builder as ::ink::scale_info::TypeInfo
>::type_info()
}
}
Expand Down
37 changes: 37 additions & 0 deletions crates/ink/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod chain_extension;
mod contract;
mod event;
mod ink_test;
mod scale;
mod selector;
mod storage;
mod storage_item;
Expand Down Expand Up @@ -1549,5 +1550,41 @@ synstructure::decl_derive!(
storage::storage_layout_derive
);

/// Derive the re-exported traits `ink::scale::Encode`, `ink::scale::Decode` and
/// `ink::scale_info::TypeInfo`. It enables using the built in derive macros for these
/// traits without depending directly on the `parity-scale-codec` and `scale-info` crates.
///
/// # Options
/// - `encode`: derives `ink::scale::Encode`
/// - `decode`: derives `ink::scale::Decode`
/// - `type_info`: derives `ink::scale_info::TypeInfo`
///
/// # Examples
///
/// ```
/// #[ink::scale_derive(Encode, Decode, TypeInfo)]
/// pub enum Error {}
/// ```
/// This is a convenience macro that expands to include the additional `crate` attributes
/// required for the path of the re-exported crates.
///
/// ```
/// #[derive(::ink::scale::Encode, ::ink::scale::Decode)]
/// #[codec(crate = ::ink::scale)]
/// #[cfg_attr(
/// feature = "std",
/// derive(::scale_info::TypeInfo),
/// scale_info(crate = ::ink::scale_info)
/// )]
/// pub enum Error {}
/// ```
#[proc_macro_attribute]
pub fn scale_derive(attr: TokenStream, item: TokenStream) -> TokenStream {
match scale::derive(attr.into(), item.into()) {
Ok(output) => output.into(),
Err(err) => err.to_compile_error().into(),
}
}

#[cfg(test)]
pub use contract::generate_or_err;
Loading