From 1ef37cf0ca5112d5e265c1bdc9c7cff4fbc72428 Mon Sep 17 00:00:00 2001 From: Hernando Castano Date: Mon, 7 Mar 2022 15:19:34 -0800 Subject: [PATCH 1/6] Remove `compile_as_dependency` from IR --- crates/lang/ir/src/ir/config.rs | 56 +-------------------------------- 1 file changed, 1 insertion(+), 55 deletions(-) diff --git a/crates/lang/ir/src/ir/config.rs b/crates/lang/ir/src/ir/config.rs index 1b9be33ad5c..640bc9a13d8 100644 --- a/crates/lang/ir/src/ir/config.rs +++ b/crates/lang/ir/src/ir/config.rs @@ -24,11 +24,6 @@ use syn::spanned::Spanned; /// The ink! configuration. #[derive(Debug, Default, PartialEq, Eq)] pub struct Config { - /// If `true` compiles this ink! smart contract always as - /// if it was a dependency of another smart contract. - /// This configuration is mainly needed for testing and - /// the default is `false`. - as_dependency: Option, /// The environmental types definition. /// /// This must be a type that implements `ink_env::Environment` and can @@ -120,24 +115,11 @@ impl TryFrom for Config { type Error = syn::Error; fn try_from(args: ast::AttributeArgs) -> Result { - let mut as_dependency: Option<(bool, ast::MetaNameValue)> = None; let mut env: Option<(Environment, ast::MetaNameValue)> = None; let mut whitelisted_attributes = WhitelistedAttributes::default(); for arg in args.into_iter() { - if arg.name.is_ident("compile_as_dependency") { - if let Some((_, ast)) = as_dependency { - return Err(duplicate_config_err(ast, arg, "compile_as_dependency")) - } - if let ast::PathOrLit::Lit(syn::Lit::Bool(lit_bool)) = &arg.value { - as_dependency = Some((lit_bool.value, arg)) - } else { - return Err(format_err_spanned!( - arg, - "expected a bool literal for `compile_as_dependency` ink! configuration argument", - )) - } - } else if arg.name.is_ident("env") { + if arg.name.is_ident("env") { if let Some((_, ast)) = env { return Err(duplicate_config_err(ast, arg, "env")) } @@ -161,7 +143,6 @@ impl TryFrom for Config { } } Ok(Config { - as_dependency: as_dependency.map(|(value, _)| value), env: env.map(|(value, _)| value), whitelisted_attributes, }) @@ -180,15 +161,6 @@ impl Config { .unwrap_or(Environment::default().path) } - /// Return `true` if this ink! smart contract shall always be compiled as - /// if it was a dependency of another smart contract, returns `false` - /// otherwise. - /// - /// If nothing has been specified returns the default which is `false`. - pub fn is_compile_as_dependency_enabled(&self) -> bool { - self.as_dependency.unwrap_or(false) - } - /// Return set of attributes that can be passed to call builder in the codegen. pub fn whitelisted_attributes(&self) -> &WhitelistedAttributes { &self.whitelisted_attributes @@ -232,30 +204,6 @@ mod tests { assert_try_from(syn::parse_quote! {}, Ok(Config::default())) } - #[test] - fn as_dependency_works() { - assert_try_from( - syn::parse_quote! { - compile_as_dependency = false - }, - Ok(Config { - as_dependency: Some(false), - env: None, - whitelisted_attributes: Default::default(), - }), - ) - } - - #[test] - fn as_dependency_invalid_value_fails() { - assert_try_from( - syn::parse_quote! { compile_as_dependency = "invalid" }, - Err( - "expected a bool literal for `compile_as_dependency` ink! configuration argument" - ) - ) - } - #[test] fn env_works() { assert_try_from( @@ -263,7 +211,6 @@ mod tests { env = ::my::env::Types }, Ok(Config { - as_dependency: None, env: Some(Environment { path: syn::parse_quote! { ::my::env::Types }, }), @@ -309,7 +256,6 @@ mod tests { keep_attr = "foo, bar" }, Ok(Config { - as_dependency: None, env: None, whitelisted_attributes: attrs, }), From f146decedc89be084eeb7ef156bb2c6abbe1fea2 Mon Sep 17 00:00:00 2001 From: Hernando Castano Date: Mon, 7 Mar 2022 15:23:23 -0800 Subject: [PATCH 2/6] Remove the `DependencyCfg` code generators Since we don't have any ink! attributes to affect the codegen anymore we can just inline the `cfg` blocks into the codegen --- .../src/generator/as_dependency/mod.rs | 42 ------------------- crates/lang/codegen/src/generator/dispatch.rs | 5 +-- crates/lang/codegen/src/generator/metadata.rs | 10 +---- crates/lang/codegen/src/generator/mod.rs | 6 +-- 4 files changed, 4 insertions(+), 59 deletions(-) diff --git a/crates/lang/codegen/src/generator/as_dependency/mod.rs b/crates/lang/codegen/src/generator/as_dependency/mod.rs index 18863a0a682..529ee878de2 100644 --- a/crates/lang/codegen/src/generator/as_dependency/mod.rs +++ b/crates/lang/codegen/src/generator/as_dependency/mod.rs @@ -27,48 +27,6 @@ use derive_more::From; use proc_macro2::TokenStream as TokenStream2; use quote::quote; -/// Generates `#[cfg(...)]` code to guard against compilation under `ink-as-dependency`. -#[derive(From)] -pub struct NotAsDependencyCfg<'a> { - contract: &'a ir::Contract, -} - -impl GenerateCode for NotAsDependencyCfg<'_> { - fn generate_code(&self) -> TokenStream2 { - if self.contract.config().is_compile_as_dependency_enabled() { - // We use `__ink_DO_NOT_COMPILE` in order to craft a `cfg` that - // never evaluates to `true` and therefore is always disabled. - return quote! { #[cfg(feature = "__ink_DO_NOT_COMPILE")] } - } - quote! { #[cfg(not(feature = "ink-as-dependency"))] } - } -} - -/// Generates `#[cfg(...)]` code to only allow compilation when `ink-as-dependency` is enabled. -/// -/// The `ink-as-dependency` can be enabled mainly by 2 different ways: -/// -/// - Enabling it in the associated `Cargo.toml` as crate feature. -/// - Note: This can be enabled by dependencies of an ink! smart contract. -/// - Enabling it in the configuration header with `#[ink::contract(compile_as_dependency = true)]`. -/// - If set here the contract will always be compiled as it is was a dependency. -#[derive(From)] -pub struct OnlyAsDependencyCfg<'a> { - contract: &'a ir::Contract, -} - -impl GenerateCode for OnlyAsDependencyCfg<'_> { - fn generate_code(&self) -> TokenStream2 { - if self.contract.config().is_compile_as_dependency_enabled() { - // We return no code since no code is required to disable compilation. - return quote! {} - } - quote! { - #[cfg(feature = "ink-as-dependency")] - } - } -} - /// Generates code for generating a contract reference. /// /// Contract references are used to dynamically depend on a smart contract. diff --git a/crates/lang/codegen/src/generator/dispatch.rs b/crates/lang/codegen/src/generator/dispatch.rs index f2b21733e68..14116e84def 100644 --- a/crates/lang/codegen/src/generator/dispatch.rs +++ b/crates/lang/codegen/src/generator/dispatch.rs @@ -17,7 +17,6 @@ use core::iter; use crate::{ generator, GenerateCode, - GenerateCodeUsing as _, }; use derive_more::From; use ir::{ @@ -55,8 +54,6 @@ impl GenerateCode for Dispatch<'_> { let mut constructor_spans = Vec::new(); let mut message_spans = Vec::new(); - let cfg_not_as_dependency = - self.generate_code_using::(); let amount_dispatchables = self.generate_contract_amount_dispatchables_trait_impl(); let contract_dispatchable_messages = @@ -83,7 +80,7 @@ impl GenerateCode for Dispatch<'_> { #message_decoder_type #[cfg(not(test))] - #cfg_not_as_dependency + #[cfg(not(feature = "ink-as-dependency"))] const _: () = { #entry_points }; diff --git a/crates/lang/codegen/src/generator/metadata.rs b/crates/lang/codegen/src/generator/metadata.rs index 622221cc814..0142643a755 100644 --- a/crates/lang/codegen/src/generator/metadata.rs +++ b/crates/lang/codegen/src/generator/metadata.rs @@ -12,11 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::{ - generator, - GenerateCode, - GenerateCodeUsing as _, -}; +use crate::GenerateCode; use ::core::iter; use derive_more::From; use ir::{ @@ -43,12 +39,10 @@ impl GenerateCode for Metadata<'_> { fn generate_code(&self) -> TokenStream2 { let contract = self.generate_contract(); let layout = self.generate_layout(); - let cfg_not_as_dependency = - self.generate_code_using::(); quote! { #[cfg(feature = "std")] - #cfg_not_as_dependency + #[cfg(not(feature = "ink-as-dependency"))] const _: () = { #[no_mangle] pub fn __ink_generate_metadata() -> ::ink_metadata::MetadataVersioned { diff --git a/crates/lang/codegen/src/generator/mod.rs b/crates/lang/codegen/src/generator/mod.rs index def2f2d3c83..a6d68df5e69 100644 --- a/crates/lang/codegen/src/generator/mod.rs +++ b/crates/lang/codegen/src/generator/mod.rs @@ -51,11 +51,7 @@ pub use self::{ input_types_tuple, output_ident, }, - as_dependency::{ - ContractReference, - NotAsDependencyCfg, - OnlyAsDependencyCfg, - }, + as_dependency::ContractReference, blake2b::Blake2x256, chain_extension::ChainExtension, contract::Contract, From 45864ce7bf475d3fb5ba0035c3eb8063aa5fd02b Mon Sep 17 00:00:00 2001 From: Hernando Castano Date: Mon, 7 Mar 2022 15:36:11 -0800 Subject: [PATCH 3/6] Remove UI tests related to `compile_as_dependency` --- ...g-compile-as-dependency-invalid-type-01.rs | 19 ------------------- ...mpile-as-dependency-invalid-type-01.stderr | 5 ----- ...g-compile-as-dependency-invalid-type-02.rs | 19 ------------------- ...mpile-as-dependency-invalid-type-02.stderr | 5 ----- ...onfig-compile-as-dependency-missing-arg.rs | 19 ------------------- ...g-compile-as-dependency-missing-arg.stderr | 5 ----- .../config-compile-as-dependency-false.rs | 19 ------------------- .../pass/config-compile-as-dependency-true.rs | 19 ------------------- 8 files changed, 110 deletions(-) delete mode 100644 crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-01.rs delete mode 100644 crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-01.stderr delete mode 100644 crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-02.rs delete mode 100644 crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-02.stderr delete mode 100644 crates/lang/tests/ui/contract/fail/config-compile-as-dependency-missing-arg.rs delete mode 100644 crates/lang/tests/ui/contract/fail/config-compile-as-dependency-missing-arg.stderr delete mode 100644 crates/lang/tests/ui/contract/pass/config-compile-as-dependency-false.rs delete mode 100644 crates/lang/tests/ui/contract/pass/config-compile-as-dependency-true.rs diff --git a/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-01.rs b/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-01.rs deleted file mode 100644 index 479bab49dc2..00000000000 --- a/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-01.rs +++ /dev/null @@ -1,19 +0,0 @@ -use ink_lang as ink; - -#[ink::contract(compile_as_dependency = "yes")] -mod contract { - #[ink(storage)] - pub struct Contract {} - - impl Contract { - #[ink(constructor)] - pub fn constructor() -> Self { - Self {} - } - - #[ink(message)] - pub fn message(&self) {} - } -} - -fn main() {} diff --git a/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-01.stderr b/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-01.stderr deleted file mode 100644 index 6aa45e18c32..00000000000 --- a/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-01.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: expected a bool literal for `compile_as_dependency` ink! configuration argument - --> tests/ui/contract/fail/config-compile-as-dependency-invalid-type-01.rs:3:17 - | -3 | #[ink::contract(compile_as_dependency = "yes")] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-02.rs b/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-02.rs deleted file mode 100644 index e057aab4750..00000000000 --- a/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-02.rs +++ /dev/null @@ -1,19 +0,0 @@ -use ink_lang as ink; - -#[ink::contract(compile_as_dependency = 42)] -mod contract { - #[ink(storage)] - pub struct Contract {} - - impl Contract { - #[ink(constructor)] - pub fn constructor() -> Self { - Self {} - } - - #[ink(message)] - pub fn message(&self) {} - } -} - -fn main() {} diff --git a/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-02.stderr b/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-02.stderr deleted file mode 100644 index 270aee26990..00000000000 --- a/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-02.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: expected a bool literal for `compile_as_dependency` ink! configuration argument - --> tests/ui/contract/fail/config-compile-as-dependency-invalid-type-02.rs:3:17 - | -3 | #[ink::contract(compile_as_dependency = 42)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-missing-arg.rs b/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-missing-arg.rs deleted file mode 100644 index 179a18340a0..00000000000 --- a/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-missing-arg.rs +++ /dev/null @@ -1,19 +0,0 @@ -use ink_lang as ink; - -#[ink::contract(compile_as_dependency)] -mod contract { - #[ink(storage)] - pub struct Contract {} - - impl Contract { - #[ink(constructor)] - pub fn constructor() -> Self { - Self {} - } - - #[ink(message)] - pub fn message(&self) {} - } -} - -fn main() {} diff --git a/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-missing-arg.stderr b/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-missing-arg.stderr deleted file mode 100644 index 8a0176d6d90..00000000000 --- a/crates/lang/tests/ui/contract/fail/config-compile-as-dependency-missing-arg.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: ink! config options require an argument separated by '=' - --> tests/ui/contract/fail/config-compile-as-dependency-missing-arg.rs:3:17 - | -3 | #[ink::contract(compile_as_dependency)] - | ^^^^^^^^^^^^^^^^^^^^^ diff --git a/crates/lang/tests/ui/contract/pass/config-compile-as-dependency-false.rs b/crates/lang/tests/ui/contract/pass/config-compile-as-dependency-false.rs deleted file mode 100644 index 3f6997962ce..00000000000 --- a/crates/lang/tests/ui/contract/pass/config-compile-as-dependency-false.rs +++ /dev/null @@ -1,19 +0,0 @@ -use ink_lang as ink; - -#[ink::contract(compile_as_dependency = false)] -mod contract { - #[ink(storage)] - pub struct Contract {} - - impl Contract { - #[ink(constructor)] - pub fn constructor() -> Self { - Self {} - } - - #[ink(message)] - pub fn message(&self) {} - } -} - -fn main() {} diff --git a/crates/lang/tests/ui/contract/pass/config-compile-as-dependency-true.rs b/crates/lang/tests/ui/contract/pass/config-compile-as-dependency-true.rs deleted file mode 100644 index 003163166a0..00000000000 --- a/crates/lang/tests/ui/contract/pass/config-compile-as-dependency-true.rs +++ /dev/null @@ -1,19 +0,0 @@ -use ink_lang as ink; - -#[ink::contract(compile_as_dependency = true)] -mod contract { - #[ink(storage)] - pub struct Contract {} - - impl Contract { - #[ink(constructor)] - pub fn constructor() -> Self { - Self {} - } - - #[ink(message)] - pub fn message(&self) {} - } -} - -fn main() {} From a2adb8bc80904aa3e42de76839aa53445e3d0965 Mon Sep 17 00:00:00 2001 From: Hernando Castano Date: Mon, 7 Mar 2022 15:37:01 -0800 Subject: [PATCH 4/6] Fix `instantiate_contract` doc tests --- crates/lang/src/env_access.rs | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index 572a529795a..f2f2e4e8e10 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -429,25 +429,15 @@ where /// # use ink_lang as ink; /// # #[ink::contract] /// # pub mod my_contract { - /// # use ink_lang as ink; - /// # #[ink::contract(compile_as_dependency = true)] - /// # pub mod other_contract { - /// # #[ink(storage)] - /// # pub struct OtherContract { } - /// # - /// # impl OtherContract { - /// # #[ink(constructor)] - /// # pub fn new() -> Self { - /// # Self {} - /// # } - /// # - /// # #[ink(message)] - /// # pub fn some_operation(&self) { - /// # // ... - /// # } - /// # } - /// # } - /// # + /// # // In order for this to actually work with another contract we'd need a way + /// # // to turn the `ink-as-dependency` crate feature on in doctests, which we + /// # // can't do. + /// # // + /// # // Instead we use our own contract's `Ref`, which is fine for this example + /// # // (just need something that implements the `ContractRef` trait). + /// # pub mod other_contract { + /// # pub use super::MyContractRef as OtherContractRef; + /// # } /// use ink_env::{ /// DefaultEnvironment, /// call::{build_create, Selector, ExecutionInput} From ec232e50bc2c1d78af7f621cd0528062822975ad Mon Sep 17 00:00:00 2001 From: Hernando Castano Date: Mon, 7 Mar 2022 15:37:26 -0800 Subject: [PATCH 5/6] Remove `compile_as_dependency` from doc comments --- crates/lang/ir/src/ast/attr_args.rs | 4 ++-- crates/lang/macro/src/lib.rs | 34 ----------------------------- 2 files changed, 2 insertions(+), 36 deletions(-) diff --git a/crates/lang/ir/src/ast/attr_args.rs b/crates/lang/ir/src/ast/attr_args.rs index 557f62f61d0..cfc4adb38a7 100644 --- a/crates/lang/ir/src/ast/attr_args.rs +++ b/crates/lang/ir/src/ast/attr_args.rs @@ -30,8 +30,8 @@ use syn::{ /// The attribute arguments for the configuration of an ink! smart contract. /// -/// These are the segments `env = ::my::env::Environment` and `compile_as_dependency = true` -/// in `#[ink::contract(env = ::my::env::Environment, compile_as_dependency = true`. +/// For example, the segment `env = ::my::env::Environment` +/// in `#[ink::contract(env = ::my::env::Environment)]`. #[derive(Debug, PartialEq, Eq)] pub struct AttributeArgs { args: Punctuated, diff --git a/crates/lang/macro/src/lib.rs b/crates/lang/macro/src/lib.rs index 4a50de36442..fa7cf4f7749 100644 --- a/crates/lang/macro/src/lib.rs +++ b/crates/lang/macro/src/lib.rs @@ -119,40 +119,6 @@ pub fn selector_bytes(input: TokenStream) -> TokenStream { /// The `#[ink::contract]` macro can be provided with some additional comma-separated /// header arguments: /// -/// - `compile_as_dependency: bool` -/// -/// Tells the ink! code generator to **always** or **never** -/// compile the smart contract as if it was used as a dependency of another ink! -/// smart contract. -/// -/// Normally this flag is only really useful for ink! developers who -/// want to inspect code generation of ink! smart contracts. -/// The author is not aware of any particular practical use case for users that -/// makes use of this flag but contract writers are encouraged to disprove this. -/// -/// Note that it is recommended to make use of the built-in crate feature -/// `ink-as-dependency` to flag smart contract dependencies listed in a contract's -/// `Cargo.toml` as actual dependencies to ink!. -/// -/// **Usage Example:** -/// ``` -/// # use ink_lang as ink; -/// #[ink::contract(compile_as_dependency = true)] -/// mod my_contract { -/// # #[ink(storage)] -/// # pub struct MyStorage; -/// # impl MyStorage { -/// # #[ink(constructor)] -/// # pub fn construct() -> Self { MyStorage {} } -/// # #[ink(message)] -/// # pub fn message(&self) {} -/// # } -/// // ... -/// } -/// ``` -/// -/// **Default value:** Depends on the crate feature propagation of `Cargo.toml`. -/// /// - `keep_attr: String` /// /// Tells the ink! code generator which attributes should be passed to call builders. From 62cb20e18ad41ae0969236a5e8bb2242a4718612 Mon Sep 17 00:00:00 2001 From: Hernando Castano Date: Mon, 7 Mar 2022 15:46:50 -0800 Subject: [PATCH 6/6] Remove more references in doc comments --- crates/lang/ir/src/ir/contract.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/crates/lang/ir/src/ir/contract.rs b/crates/lang/ir/src/ir/contract.rs index 3ae0e9e8ec2..7d7567e2c0e 100644 --- a/crates/lang/ir/src/ir/contract.rs +++ b/crates/lang/ir/src/ir/contract.rs @@ -100,10 +100,6 @@ impl Contract { /// /// - `types`: To specify `Environment` different from the default environment /// types. - /// - `as-dependency`: If `true` compiles this ink! smart contract always as - /// if it was a dependency of another smart contract. - /// This configuration is mainly needed for testing and - /// the default is `false`. /// /// Note that we might add more configuration fields in the future if /// necessary.