Skip to content

Commit 8ce4905

Browse files
Refactor contract ref generation and add automatic re-exporting (#2710)
* codegen: Only generate contract refs in test, std or dependency mode * codegen: Automatically re-export contract refs in test, std or dependency mode * codegen: Implement `::ink::codegen::ContractCallBuilder` for contract refs * codegen: Implement `::ink::codegen::ContractCallBuilder` for call forwarder * tests: Fix integration tests that re-export contract refs * docs: Fix doc tests that re-export contract refs * Update changelog * make `contract_ref` attribute type alias `pub`
1 parent 349605e commit 8ce4905

File tree

25 files changed

+45
-77
lines changed

25 files changed

+45
-77
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
[Unreleased]
88

9+
### Changed
10+
11+
- Refactor contract ref generation and add automatic re-exporting - [#2710](https://github.com/use-ink/ink/pull/2710)
12+
913
## Version 6.0.0-beta
1014

1115
### Added

crates/env/src/call/create_builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ where
387387
/// # pub fn message(&self) {}
388388
/// # }
389389
/// # }
390-
/// # use contract::MyContractRef;
390+
/// #
391391
/// let my_contract: MyContractRef = build_create::<MyContractRef>()
392392
/// .code_hash(ink::H256::from([0x42; 32]))
393393
/// .endowment(25.into())
@@ -431,7 +431,7 @@ where
431431
/// # pub fn message(&self) {}
432432
/// # }
433433
/// # }
434-
/// # use contract::{MyContractRef, ConstructorError};
434+
/// # use contract::ConstructorError;
435435
/// let my_contract: MyContractRef = build_create::<MyContractRef>()
436436
/// .code_hash(ink::H256::from([0x42; 32]))
437437
/// .endowment(25.into())

crates/ink/codegen/src/generator/as_dependency/call_builder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl GenerateCode for CallBuilder<'_> {
5252
let call_builder_impls = self.generate_call_forwarder_impls();
5353
let call_builder_inherent_impls = self.generate_call_builder_inherent_impls();
5454
quote! {
55+
#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
5556
const _: () = {
5657
#call_builder_struct
5758
#auxiliary_trait_impls
@@ -124,6 +125,7 @@ impl CallBuilder<'_> {
124125
_marker: core::marker::PhantomData<Abi>,
125126
}
126127

128+
#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
127129
const _: () = {
128130
impl ::ink::codegen::ContractCallBuilder for #storage_ident {
129131
type Type<Abi> = #cb_ident<Abi>;

crates/ink/codegen/src/generator/as_dependency/contract_ref.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,16 @@ impl GenerateCode for ContractRef<'_> {
5656
let call_builder_trait_impl = self.generate_call_builder_trait_impl();
5757
let auxiliary_trait_impls = self.generate_auxiliary_trait_impls();
5858
quote! {
59+
#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
5960
#contract_ref
60-
#contract_ref_trait_impls
61-
#contract_ref_inherent_impls
62-
#call_builder_trait_impl
63-
#auxiliary_trait_impls
61+
62+
#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
63+
const _: () = {
64+
#contract_ref_trait_impls
65+
#contract_ref_inherent_impls
66+
#call_builder_trait_impl
67+
#auxiliary_trait_impls
68+
};
6469
}
6570
}
6671
}
@@ -104,6 +109,7 @@ impl ContractRef<'_> {
104109
let ref_ident_abi_alias = format_ident!("{ref_ident_default_abi}{suffix}");
105110
quote! {
106111
#[allow(dead_code)]
112+
#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
107113
pub type #ref_ident_abi_alias = #ref_ident::<#abi_ty>;
108114
}
109115
});
@@ -152,10 +158,12 @@ impl ContractRef<'_> {
152158

153159
// Default type alias (i.e. `ContractRef` for a contract named `Contract`).
154160
#[allow(dead_code)]
161+
#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
155162
pub type #ref_ident_default_abi = #ref_ident::<#abi>;
156163
// ABI specific type aliases (i.e. `ContractRefInk` and `ContractRefSol`) as appropriate.
157164
#ref_ident_abi_aliases
158165

166+
#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
159167
const _: () = {
160168
impl ::ink::env::ContractReference for #storage_ident {
161169
type Type = #ref_ident;
@@ -309,6 +317,10 @@ impl ContractRef<'_> {
309317
&mut self.inner
310318
}
311319
}
320+
321+
impl<TypeAbi> ::ink::codegen::ContractCallBuilder for #ref_ident<TypeAbi> {
322+
type Type<TraitAbi> = <#storage_ident as ::ink::codegen::ContractCallBuilder>::Type<TypeAbi>;
323+
}
312324
};
313325
)
314326
}

crates/ink/codegen/src/generator/contract.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ impl GenerateCode for Contract<'_> {
6060
#[cfg(any(ink_abi = "sol", ink_abi = "all"))]
6161
let solidity_metadata = self.generate_code_using::<generator::SolidityMetadata>();
6262

63+
let contract_ref_base_ident =
64+
quote::format_ident!("{}Ref", module.storage().ident());
65+
6366
quote! {
67+
// Note: rustdoc can't resolve `self::` prefixes for this re-export.
68+
#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
69+
pub use #ident::#contract_ref_base_ident;
70+
6471
#( #attrs )*
6572
#vis mod #ident {
6673
#env

crates/ink/codegen/src/generator/trait_def/call_forwarder.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,13 @@ impl CallForwarder<'_> {
354354
&mut self.builder
355355
}
356356
}
357+
358+
impl<E, TypeAbi> ::ink::codegen::ContractCallBuilder for #call_forwarder_ident<E, TypeAbi>
359+
where
360+
E: ::ink::env::Environment,
361+
{
362+
type Type<TraitAbi> = #call_builder_ident<E, TypeAbi>;
363+
}
357364
)
358365
}
359366

crates/ink/macro/src/contract_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn analyze_or_err(
6868
#trait_def_impl
6969

7070
// Type alias for contract ref.
71-
type #contract_ref_name =
71+
pub type #contract_ref_name =
7272
<<::ink::reflect::TraitDefinitionRegistry<#env> as #trait_name>
7373
::__ink_TraitInfo as ::ink::codegen::TraitCallForwarder>::Forwarder<#abi_ty>;
7474
))

crates/primitives/src/contract.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ pub trait ContractEnv {
139139
/// }
140140
/// }
141141
///
142-
/// use contract::{
143-
/// Contract,
144-
/// ContractRef,
145-
/// };
142+
/// use contract::Contract;
146143
/// # use ink::codegen::utils::IsSameType;
147144
/// # use ink::env::ContractReference;
148145
///

integration-tests/internal/lang-err/call-builder/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,7 @@ mod call_builder {
171171
ChainBackend,
172172
ContractsBackend,
173173
};
174-
use integration_flipper::{
175-
Flipper,
176-
FlipperRef,
177-
};
174+
use integration_flipper::FlipperRef;
178175

179176
type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
180177

@@ -200,7 +197,7 @@ mod call_builder {
200197
.submit()
201198
.await
202199
.expect("instantiate `flipper` failed");
203-
let flipper_call = flipper.call_builder::<Flipper>();
200+
let flipper_call = flipper.call_builder::<FlipperRef>();
204201

205202
let flipper_get = flipper_call.get();
206203
let get_call_result = client.call(&origin, &flipper_get).dry_run().await?;

integration-tests/internal/lang-err/constructors-return-value/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#![cfg_attr(not(feature = "std"), no_std, no_main)]
22

3-
pub use self::constructors_return_value::{
4-
ConstructorError,
5-
ConstructorsReturnValue,
6-
ConstructorsReturnValueRef,
7-
};
3+
pub use self::constructors_return_value::ConstructorError;
84

95
#[ink::contract]
106
pub mod constructors_return_value {

0 commit comments

Comments
 (0)