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

Remove compile_as_dependency config option #1168

Merged
merged 6 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
42 changes: 0 additions & 42 deletions crates/lang/codegen/src/generator/as_dependency/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 1 addition & 4 deletions crates/lang/codegen/src/generator/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use core::iter;
use crate::{
generator,
GenerateCode,
GenerateCodeUsing as _,
};
use derive_more::From;
use ir::{
Expand Down Expand Up @@ -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::<generator::NotAsDependencyCfg>();
let amount_dispatchables =
self.generate_contract_amount_dispatchables_trait_impl();
let contract_dispatchable_messages =
Expand All @@ -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
};
Expand Down
10 changes: 2 additions & 8 deletions crates/lang/codegen/src/generator/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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::<generator::NotAsDependencyCfg>();

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 {
Expand Down
6 changes: 1 addition & 5 deletions crates/lang/codegen/src/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions crates/lang/ir/src/ast/attr_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MetaNameValue, Token![,]>,
Expand Down
56 changes: 1 addition & 55 deletions crates/lang/ir/src/ir/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>,
/// The environmental types definition.
///
/// This must be a type that implements `ink_env::Environment` and can
Expand Down Expand Up @@ -120,24 +115,11 @@ impl TryFrom<ast::AttributeArgs> for Config {
type Error = syn::Error;

fn try_from(args: ast::AttributeArgs) -> Result<Self, Self::Error> {
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"))
}
Expand All @@ -161,7 +143,6 @@ impl TryFrom<ast::AttributeArgs> for Config {
}
}
Ok(Config {
as_dependency: as_dependency.map(|(value, _)| value),
env: env.map(|(value, _)| value),
whitelisted_attributes,
})
Expand All @@ -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
Expand Down Expand Up @@ -232,38 +204,13 @@ 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(
syn::parse_quote! {
env = ::my::env::Types
},
Ok(Config {
as_dependency: None,
env: Some(Environment {
path: syn::parse_quote! { ::my::env::Types },
}),
Expand Down Expand Up @@ -309,7 +256,6 @@ mod tests {
keep_attr = "foo, bar"
},
Ok(Config {
as_dependency: None,
env: None,
whitelisted_attributes: attrs,
}),
Expand Down
4 changes: 0 additions & 4 deletions crates/lang/ir/src/ir/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 0 additions & 34 deletions crates/lang/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 9 additions & 19 deletions crates/lang/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading