diff --git a/CHANGELOG.md b/CHANGELOG.md index ca27953caa4..631057fbe38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [Unreleased] +### Changed + +- Refactor contract ref generation and add automatic re-exporting - [#2710](https://github.com/use-ink/ink/pull/2710) + ## Version 6.0.0-beta ### Added diff --git a/crates/env/src/call/create_builder.rs b/crates/env/src/call/create_builder.rs index a503775eca4..ec398e70e49 100644 --- a/crates/env/src/call/create_builder.rs +++ b/crates/env/src/call/create_builder.rs @@ -387,7 +387,7 @@ where /// # pub fn message(&self) {} /// # } /// # } -/// # use contract::MyContractRef; +/// # /// let my_contract: MyContractRef = build_create::() /// .code_hash(ink::H256::from([0x42; 32])) /// .endowment(25.into()) @@ -431,7 +431,7 @@ where /// # pub fn message(&self) {} /// # } /// # } -/// # use contract::{MyContractRef, ConstructorError}; +/// # use contract::ConstructorError; /// let my_contract: MyContractRef = build_create::() /// .code_hash(ink::H256::from([0x42; 32])) /// .endowment(25.into()) diff --git a/crates/ink/codegen/src/generator/as_dependency/call_builder.rs b/crates/ink/codegen/src/generator/as_dependency/call_builder.rs index d8974cf5ac7..e01e92da848 100644 --- a/crates/ink/codegen/src/generator/as_dependency/call_builder.rs +++ b/crates/ink/codegen/src/generator/as_dependency/call_builder.rs @@ -52,6 +52,7 @@ impl GenerateCode for CallBuilder<'_> { let call_builder_impls = self.generate_call_forwarder_impls(); let call_builder_inherent_impls = self.generate_call_builder_inherent_impls(); quote! { + #[cfg(any(test, feature = "std", feature = "ink-as-dependency"))] const _: () = { #call_builder_struct #auxiliary_trait_impls @@ -124,6 +125,7 @@ impl CallBuilder<'_> { _marker: core::marker::PhantomData, } + #[cfg(any(test, feature = "std", feature = "ink-as-dependency"))] const _: () = { impl ::ink::codegen::ContractCallBuilder for #storage_ident { type Type = #cb_ident; diff --git a/crates/ink/codegen/src/generator/as_dependency/contract_ref.rs b/crates/ink/codegen/src/generator/as_dependency/contract_ref.rs index fcf29ec5f8e..7c1abab1386 100644 --- a/crates/ink/codegen/src/generator/as_dependency/contract_ref.rs +++ b/crates/ink/codegen/src/generator/as_dependency/contract_ref.rs @@ -56,11 +56,16 @@ impl GenerateCode for ContractRef<'_> { let call_builder_trait_impl = self.generate_call_builder_trait_impl(); let auxiliary_trait_impls = self.generate_auxiliary_trait_impls(); quote! { + #[cfg(any(test, feature = "std", feature = "ink-as-dependency"))] #contract_ref - #contract_ref_trait_impls - #contract_ref_inherent_impls - #call_builder_trait_impl - #auxiliary_trait_impls + + #[cfg(any(test, feature = "std", feature = "ink-as-dependency"))] + const _: () = { + #contract_ref_trait_impls + #contract_ref_inherent_impls + #call_builder_trait_impl + #auxiliary_trait_impls + }; } } } @@ -104,6 +109,7 @@ impl ContractRef<'_> { let ref_ident_abi_alias = format_ident!("{ref_ident_default_abi}{suffix}"); quote! { #[allow(dead_code)] + #[cfg(any(test, feature = "std", feature = "ink-as-dependency"))] pub type #ref_ident_abi_alias = #ref_ident::<#abi_ty>; } }); @@ -152,10 +158,12 @@ impl ContractRef<'_> { // Default type alias (i.e. `ContractRef` for a contract named `Contract`). #[allow(dead_code)] + #[cfg(any(test, feature = "std", feature = "ink-as-dependency"))] pub type #ref_ident_default_abi = #ref_ident::<#abi>; // ABI specific type aliases (i.e. `ContractRefInk` and `ContractRefSol`) as appropriate. #ref_ident_abi_aliases + #[cfg(any(test, feature = "std", feature = "ink-as-dependency"))] const _: () = { impl ::ink::env::ContractReference for #storage_ident { type Type = #ref_ident; @@ -309,6 +317,10 @@ impl ContractRef<'_> { &mut self.inner } } + + impl ::ink::codegen::ContractCallBuilder for #ref_ident { + type Type = <#storage_ident as ::ink::codegen::ContractCallBuilder>::Type; + } }; ) } diff --git a/crates/ink/codegen/src/generator/contract.rs b/crates/ink/codegen/src/generator/contract.rs index c0849e6821d..020821f4c78 100644 --- a/crates/ink/codegen/src/generator/contract.rs +++ b/crates/ink/codegen/src/generator/contract.rs @@ -60,7 +60,14 @@ impl GenerateCode for Contract<'_> { #[cfg(any(ink_abi = "sol", ink_abi = "all"))] let solidity_metadata = self.generate_code_using::(); + let contract_ref_base_ident = + quote::format_ident!("{}Ref", module.storage().ident()); + quote! { + // Note: rustdoc can't resolve `self::` prefixes for this re-export. + #[cfg(any(test, feature = "std", feature = "ink-as-dependency"))] + pub use #ident::#contract_ref_base_ident; + #( #attrs )* #vis mod #ident { #env diff --git a/crates/ink/codegen/src/generator/trait_def/call_forwarder.rs b/crates/ink/codegen/src/generator/trait_def/call_forwarder.rs index 479cc5f49df..97717d70934 100644 --- a/crates/ink/codegen/src/generator/trait_def/call_forwarder.rs +++ b/crates/ink/codegen/src/generator/trait_def/call_forwarder.rs @@ -354,6 +354,13 @@ impl CallForwarder<'_> { &mut self.builder } } + + impl ::ink::codegen::ContractCallBuilder for #call_forwarder_ident + where + E: ::ink::env::Environment, + { + type Type = #call_builder_ident; + } ) } diff --git a/crates/ink/macro/src/contract_ref.rs b/crates/ink/macro/src/contract_ref.rs index 56265a40aaa..3e3bd6f04c6 100644 --- a/crates/ink/macro/src/contract_ref.rs +++ b/crates/ink/macro/src/contract_ref.rs @@ -68,7 +68,7 @@ pub fn analyze_or_err( #trait_def_impl // Type alias for contract ref. - type #contract_ref_name = + pub type #contract_ref_name = <<::ink::reflect::TraitDefinitionRegistry<#env> as #trait_name> ::__ink_TraitInfo as ::ink::codegen::TraitCallForwarder>::Forwarder<#abi_ty>; )) diff --git a/crates/primitives/src/contract.rs b/crates/primitives/src/contract.rs index 31282a64523..5e38e5dc40c 100644 --- a/crates/primitives/src/contract.rs +++ b/crates/primitives/src/contract.rs @@ -139,10 +139,7 @@ pub trait ContractEnv { /// } /// } /// -/// use contract::{ -/// Contract, -/// ContractRef, -/// }; +/// use contract::Contract; /// # use ink::codegen::utils::IsSameType; /// # use ink::env::ContractReference; /// diff --git a/integration-tests/internal/lang-err/call-builder/lib.rs b/integration-tests/internal/lang-err/call-builder/lib.rs index caf1380279b..2a75f5a9536 100755 --- a/integration-tests/internal/lang-err/call-builder/lib.rs +++ b/integration-tests/internal/lang-err/call-builder/lib.rs @@ -171,10 +171,7 @@ mod call_builder { ChainBackend, ContractsBackend, }; - use integration_flipper::{ - Flipper, - FlipperRef, - }; + use integration_flipper::FlipperRef; type E2EResult = std::result::Result>; @@ -200,7 +197,7 @@ mod call_builder { .submit() .await .expect("instantiate `flipper` failed"); - let flipper_call = flipper.call_builder::(); + let flipper_call = flipper.call_builder::(); let flipper_get = flipper_call.get(); let get_call_result = client.call(&origin, &flipper_get).dry_run().await?; diff --git a/integration-tests/internal/lang-err/constructors-return-value/lib.rs b/integration-tests/internal/lang-err/constructors-return-value/lib.rs index ab59f0caa2a..a00005d6a9f 100644 --- a/integration-tests/internal/lang-err/constructors-return-value/lib.rs +++ b/integration-tests/internal/lang-err/constructors-return-value/lib.rs @@ -1,10 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use self::constructors_return_value::{ - ConstructorError, - ConstructorsReturnValue, - ConstructorsReturnValueRef, -}; +pub use self::constructors_return_value::ConstructorError; #[ink::contract] pub mod constructors_return_value { diff --git a/integration-tests/internal/lang-err/integration-flipper/lib.rs b/integration-tests/internal/lang-err/integration-flipper/lib.rs index 2d3dc766380..9b200cd912e 100644 --- a/integration-tests/internal/lang-err/integration-flipper/lib.rs +++ b/integration-tests/internal/lang-err/integration-flipper/lib.rs @@ -1,10 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use self::integration_flipper::{ - Flipper, - FlipperRef, -}; - #[ink::contract] pub mod integration_flipper { #[ink(storage)] diff --git a/integration-tests/public/contract-invocation/contract1/lib.rs b/integration-tests/public/contract-invocation/contract1/lib.rs index 69b59715b2d..c86060baef4 100644 --- a/integration-tests/public/contract-invocation/contract1/lib.rs +++ b/integration-tests/public/contract-invocation/contract1/lib.rs @@ -1,7 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use self::contract1::Contract1Ref; - #[ink::contract] mod contract1 { diff --git a/integration-tests/public/contract-invocation/contract2/lib.rs b/integration-tests/public/contract-invocation/contract2/lib.rs index 68e2487fed9..c57999367b5 100644 --- a/integration-tests/public/contract-invocation/contract2/lib.rs +++ b/integration-tests/public/contract-invocation/contract2/lib.rs @@ -1,7 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use self::contract2::Contract2Ref; - #[ink::contract] mod contract2 { diff --git a/integration-tests/public/contract-invocation/virtual_contract/lib.rs b/integration-tests/public/contract-invocation/virtual_contract/lib.rs index 09329751299..d161f6dabcb 100644 --- a/integration-tests/public/contract-invocation/virtual_contract/lib.rs +++ b/integration-tests/public/contract-invocation/virtual_contract/lib.rs @@ -1,7 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use self::virtual_contract::VirtualContractRef; - #[ink::contract] pub mod virtual_contract { use ink::env::call::{ diff --git a/integration-tests/public/contract-invocation/virtual_contract_ver1/lib.rs b/integration-tests/public/contract-invocation/virtual_contract_ver1/lib.rs index 858f290af1e..2e1999075ee 100644 --- a/integration-tests/public/contract-invocation/virtual_contract_ver1/lib.rs +++ b/integration-tests/public/contract-invocation/virtual_contract_ver1/lib.rs @@ -1,7 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use self::virtual_contract_ver1::VirtualContractVer1Ref; - #[ink::contract] mod virtual_contract_ver1 { diff --git a/integration-tests/public/contract-invocation/virtual_contract_ver2/lib.rs b/integration-tests/public/contract-invocation/virtual_contract_ver2/lib.rs index 42b94c9bc76..a86be2cf042 100644 --- a/integration-tests/public/contract-invocation/virtual_contract_ver2/lib.rs +++ b/integration-tests/public/contract-invocation/virtual_contract_ver2/lib.rs @@ -1,7 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use self::virtual_contract_ver2::VirtualContractVer2Ref; - #[ink::contract] mod virtual_contract_ver2 { diff --git a/integration-tests/public/cross-contract-calls/other-contract/lib.rs b/integration-tests/public/cross-contract-calls/other-contract/lib.rs index ed280b63812..132ecbec264 100755 --- a/integration-tests/public/cross-contract-calls/other-contract/lib.rs +++ b/integration-tests/public/cross-contract-calls/other-contract/lib.rs @@ -1,10 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use self::other_contract::{ - OtherContract, - OtherContractRef, -}; - #[ink::contract] mod other_contract { diff --git a/integration-tests/public/incrementer/lib.rs b/integration-tests/public/incrementer/lib.rs index 5163ecd4a3b..ccb4ea093d6 100644 --- a/integration-tests/public/incrementer/lib.rs +++ b/integration-tests/public/incrementer/lib.rs @@ -1,10 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use self::incrementer::{ - Incrementer, - IncrementerRef, -}; - #[ink::contract] mod incrementer { #[ink(storage)] diff --git a/integration-tests/public/multi-contract-caller/accumulator/lib.rs b/integration-tests/public/multi-contract-caller/accumulator/lib.rs index 8bc171a0af5..4ba85a3652e 100644 --- a/integration-tests/public/multi-contract-caller/accumulator/lib.rs +++ b/integration-tests/public/multi-contract-caller/accumulator/lib.rs @@ -1,10 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use self::accumulator::{ - Accumulator, - AccumulatorRef, -}; - #[ink::contract] pub mod accumulator { /// Holds a simple `i32` value that can be incremented and decremented. diff --git a/integration-tests/public/multi-contract-caller/adder/lib.rs b/integration-tests/public/multi-contract-caller/adder/lib.rs index 366693122d2..b10578bc8e8 100644 --- a/integration-tests/public/multi-contract-caller/adder/lib.rs +++ b/integration-tests/public/multi-contract-caller/adder/lib.rs @@ -1,10 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use self::adder::{ - Adder, - AdderRef, -}; - #[ink::contract] mod adder { use accumulator::AccumulatorRef; diff --git a/integration-tests/public/multi-contract-caller/subber/lib.rs b/integration-tests/public/multi-contract-caller/subber/lib.rs index 7b6e522ae0c..34b430b3e80 100644 --- a/integration-tests/public/multi-contract-caller/subber/lib.rs +++ b/integration-tests/public/multi-contract-caller/subber/lib.rs @@ -1,10 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use self::subber::{ - Subber, - SubberRef, -}; - #[ink::contract] mod subber { use accumulator::AccumulatorRef; diff --git a/integration-tests/public/multisig/lib.rs b/integration-tests/public/multisig/lib.rs index b50cc7e9d0c..e9e26e2ace3 100755 --- a/integration-tests/public/multisig/lib.rs +++ b/integration-tests/public/multisig/lib.rs @@ -57,7 +57,6 @@ pub use self::multisig::{ ConfirmationStatus, - Multisig, Transaction, }; diff --git a/integration-tests/public/runtime-call-contract/e2e_tests.rs b/integration-tests/public/runtime-call-contract/e2e_tests.rs index e1f2080d87e..bd16521214f 100644 --- a/integration-tests/public/runtime-call-contract/e2e_tests.rs +++ b/integration-tests/public/runtime-call-contract/e2e_tests.rs @@ -1,7 +1,4 @@ -use super::{ - Flipper, - FlipperRef, -}; +use super::FlipperRef; use ink_e2e::{ ChainBackend, ContractsBackend, @@ -26,7 +23,7 @@ async fn instantiate_and_get(mut client: Client) -> E2EResul .await .expect("instantiate failed"); - let mut call_builder = contract.call_builder::(); + let mut call_builder = contract.call_builder::(); let flip_dry_run = client .call(&ink_e2e::bob(), &call_builder.flip()) .dry_run() diff --git a/integration-tests/public/runtime-call-contract/lib.rs b/integration-tests/public/runtime-call-contract/lib.rs index 58435745766..9cb828b8b3c 100644 --- a/integration-tests/public/runtime-call-contract/lib.rs +++ b/integration-tests/public/runtime-call-contract/lib.rs @@ -1,10 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use flipper::{ - Flipper, - FlipperRef, -}; - #[ink::contract] mod flipper { #[ink(storage)] diff --git a/integration-tests/solidity-abi/sol-cross-contract/other-contract-sol/lib.rs b/integration-tests/solidity-abi/sol-cross-contract/other-contract-sol/lib.rs index 1160e8d1849..a2196a30821 100755 --- a/integration-tests/solidity-abi/sol-cross-contract/other-contract-sol/lib.rs +++ b/integration-tests/solidity-abi/sol-cross-contract/other-contract-sol/lib.rs @@ -1,10 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use self::other_contract::{ - OtherContract, - OtherContractRef, -}; - #[ink::contract] mod other_contract {