Skip to content

Commit 6659e9c

Browse files
authored
Remove compile_as_dependency config option (#1168)
* Remove `compile_as_dependency` from IR * 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 * Remove UI tests related to `compile_as_dependency` * Fix `instantiate_contract` doc tests * Remove `compile_as_dependency` from doc comments * Remove more references in doc comments
1 parent 184ec88 commit 6659e9c

17 files changed

+16
-283
lines changed

crates/lang/codegen/src/generator/as_dependency/mod.rs

-42
Original file line numberDiff line numberDiff line change
@@ -27,48 +27,6 @@ use derive_more::From;
2727
use proc_macro2::TokenStream as TokenStream2;
2828
use quote::quote;
2929

30-
/// Generates `#[cfg(...)]` code to guard against compilation under `ink-as-dependency`.
31-
#[derive(From)]
32-
pub struct NotAsDependencyCfg<'a> {
33-
contract: &'a ir::Contract,
34-
}
35-
36-
impl GenerateCode for NotAsDependencyCfg<'_> {
37-
fn generate_code(&self) -> TokenStream2 {
38-
if self.contract.config().is_compile_as_dependency_enabled() {
39-
// We use `__ink_DO_NOT_COMPILE` in order to craft a `cfg` that
40-
// never evaluates to `true` and therefore is always disabled.
41-
return quote! { #[cfg(feature = "__ink_DO_NOT_COMPILE")] }
42-
}
43-
quote! { #[cfg(not(feature = "ink-as-dependency"))] }
44-
}
45-
}
46-
47-
/// Generates `#[cfg(...)]` code to only allow compilation when `ink-as-dependency` is enabled.
48-
///
49-
/// The `ink-as-dependency` can be enabled mainly by 2 different ways:
50-
///
51-
/// - Enabling it in the associated `Cargo.toml` as crate feature.
52-
/// - Note: This can be enabled by dependencies of an ink! smart contract.
53-
/// - Enabling it in the configuration header with `#[ink::contract(compile_as_dependency = true)]`.
54-
/// - If set here the contract will always be compiled as it is was a dependency.
55-
#[derive(From)]
56-
pub struct OnlyAsDependencyCfg<'a> {
57-
contract: &'a ir::Contract,
58-
}
59-
60-
impl GenerateCode for OnlyAsDependencyCfg<'_> {
61-
fn generate_code(&self) -> TokenStream2 {
62-
if self.contract.config().is_compile_as_dependency_enabled() {
63-
// We return no code since no code is required to disable compilation.
64-
return quote! {}
65-
}
66-
quote! {
67-
#[cfg(feature = "ink-as-dependency")]
68-
}
69-
}
70-
}
71-
7230
/// Generates code for generating a contract reference.
7331
///
7432
/// Contract references are used to dynamically depend on a smart contract.

crates/lang/codegen/src/generator/dispatch.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use core::iter;
1717
use crate::{
1818
generator,
1919
GenerateCode,
20-
GenerateCodeUsing as _,
2120
};
2221
use derive_more::From;
2322
use ir::{
@@ -55,8 +54,6 @@ impl GenerateCode for Dispatch<'_> {
5554
let mut constructor_spans = Vec::new();
5655
let mut message_spans = Vec::new();
5756

58-
let cfg_not_as_dependency =
59-
self.generate_code_using::<generator::NotAsDependencyCfg>();
6057
let amount_dispatchables =
6158
self.generate_contract_amount_dispatchables_trait_impl();
6259
let contract_dispatchable_messages =
@@ -83,7 +80,7 @@ impl GenerateCode for Dispatch<'_> {
8380
#message_decoder_type
8481

8582
#[cfg(not(test))]
86-
#cfg_not_as_dependency
83+
#[cfg(not(feature = "ink-as-dependency"))]
8784
const _: () = {
8885
#entry_points
8986
};

crates/lang/codegen/src/generator/metadata.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::{
16-
generator,
17-
GenerateCode,
18-
GenerateCodeUsing as _,
19-
};
15+
use crate::GenerateCode;
2016
use ::core::iter;
2117
use derive_more::From;
2218
use ir::{
@@ -43,12 +39,10 @@ impl GenerateCode for Metadata<'_> {
4339
fn generate_code(&self) -> TokenStream2 {
4440
let contract = self.generate_contract();
4541
let layout = self.generate_layout();
46-
let cfg_not_as_dependency =
47-
self.generate_code_using::<generator::NotAsDependencyCfg>();
4842

4943
quote! {
5044
#[cfg(feature = "std")]
51-
#cfg_not_as_dependency
45+
#[cfg(not(feature = "ink-as-dependency"))]
5246
const _: () = {
5347
#[no_mangle]
5448
pub fn __ink_generate_metadata() -> ::ink_metadata::MetadataVersioned {

crates/lang/codegen/src/generator/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ pub use self::{
5151
input_types_tuple,
5252
output_ident,
5353
},
54-
as_dependency::{
55-
ContractReference,
56-
NotAsDependencyCfg,
57-
OnlyAsDependencyCfg,
58-
},
54+
as_dependency::ContractReference,
5955
blake2b::Blake2x256,
6056
chain_extension::ChainExtension,
6157
contract::Contract,

crates/lang/ir/src/ast/attr_args.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use syn::{
3030

3131
/// The attribute arguments for the configuration of an ink! smart contract.
3232
///
33-
/// These are the segments `env = ::my::env::Environment` and `compile_as_dependency = true`
34-
/// in `#[ink::contract(env = ::my::env::Environment, compile_as_dependency = true`.
33+
/// For example, the segment `env = ::my::env::Environment`
34+
/// in `#[ink::contract(env = ::my::env::Environment)]`.
3535
#[derive(Debug, PartialEq, Eq)]
3636
pub struct AttributeArgs {
3737
args: Punctuated<MetaNameValue, Token![,]>,

crates/lang/ir/src/ir/config.rs

+1-55
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ use syn::spanned::Spanned;
2424
/// The ink! configuration.
2525
#[derive(Debug, Default, PartialEq, Eq)]
2626
pub struct Config {
27-
/// If `true` compiles this ink! smart contract always as
28-
/// if it was a dependency of another smart contract.
29-
/// This configuration is mainly needed for testing and
30-
/// the default is `false`.
31-
as_dependency: Option<bool>,
3227
/// The environmental types definition.
3328
///
3429
/// This must be a type that implements `ink_env::Environment` and can
@@ -120,24 +115,11 @@ impl TryFrom<ast::AttributeArgs> for Config {
120115
type Error = syn::Error;
121116

122117
fn try_from(args: ast::AttributeArgs) -> Result<Self, Self::Error> {
123-
let mut as_dependency: Option<(bool, ast::MetaNameValue)> = None;
124118
let mut env: Option<(Environment, ast::MetaNameValue)> = None;
125119
let mut whitelisted_attributes = WhitelistedAttributes::default();
126120

127121
for arg in args.into_iter() {
128-
if arg.name.is_ident("compile_as_dependency") {
129-
if let Some((_, ast)) = as_dependency {
130-
return Err(duplicate_config_err(ast, arg, "compile_as_dependency"))
131-
}
132-
if let ast::PathOrLit::Lit(syn::Lit::Bool(lit_bool)) = &arg.value {
133-
as_dependency = Some((lit_bool.value, arg))
134-
} else {
135-
return Err(format_err_spanned!(
136-
arg,
137-
"expected a bool literal for `compile_as_dependency` ink! configuration argument",
138-
))
139-
}
140-
} else if arg.name.is_ident("env") {
122+
if arg.name.is_ident("env") {
141123
if let Some((_, ast)) = env {
142124
return Err(duplicate_config_err(ast, arg, "env"))
143125
}
@@ -161,7 +143,6 @@ impl TryFrom<ast::AttributeArgs> for Config {
161143
}
162144
}
163145
Ok(Config {
164-
as_dependency: as_dependency.map(|(value, _)| value),
165146
env: env.map(|(value, _)| value),
166147
whitelisted_attributes,
167148
})
@@ -180,15 +161,6 @@ impl Config {
180161
.unwrap_or(Environment::default().path)
181162
}
182163

183-
/// Return `true` if this ink! smart contract shall always be compiled as
184-
/// if it was a dependency of another smart contract, returns `false`
185-
/// otherwise.
186-
///
187-
/// If nothing has been specified returns the default which is `false`.
188-
pub fn is_compile_as_dependency_enabled(&self) -> bool {
189-
self.as_dependency.unwrap_or(false)
190-
}
191-
192164
/// Return set of attributes that can be passed to call builder in the codegen.
193165
pub fn whitelisted_attributes(&self) -> &WhitelistedAttributes {
194166
&self.whitelisted_attributes
@@ -232,38 +204,13 @@ mod tests {
232204
assert_try_from(syn::parse_quote! {}, Ok(Config::default()))
233205
}
234206

235-
#[test]
236-
fn as_dependency_works() {
237-
assert_try_from(
238-
syn::parse_quote! {
239-
compile_as_dependency = false
240-
},
241-
Ok(Config {
242-
as_dependency: Some(false),
243-
env: None,
244-
whitelisted_attributes: Default::default(),
245-
}),
246-
)
247-
}
248-
249-
#[test]
250-
fn as_dependency_invalid_value_fails() {
251-
assert_try_from(
252-
syn::parse_quote! { compile_as_dependency = "invalid" },
253-
Err(
254-
"expected a bool literal for `compile_as_dependency` ink! configuration argument"
255-
)
256-
)
257-
}
258-
259207
#[test]
260208
fn env_works() {
261209
assert_try_from(
262210
syn::parse_quote! {
263211
env = ::my::env::Types
264212
},
265213
Ok(Config {
266-
as_dependency: None,
267214
env: Some(Environment {
268215
path: syn::parse_quote! { ::my::env::Types },
269216
}),
@@ -309,7 +256,6 @@ mod tests {
309256
keep_attr = "foo, bar"
310257
},
311258
Ok(Config {
312-
as_dependency: None,
313259
env: None,
314260
whitelisted_attributes: attrs,
315261
}),

crates/lang/ir/src/ir/contract.rs

-4
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ impl Contract {
100100
///
101101
/// - `types`: To specify `Environment` different from the default environment
102102
/// types.
103-
/// - `as-dependency`: If `true` compiles this ink! smart contract always as
104-
/// if it was a dependency of another smart contract.
105-
/// This configuration is mainly needed for testing and
106-
/// the default is `false`.
107103
///
108104
/// Note that we might add more configuration fields in the future if
109105
/// necessary.

crates/lang/macro/src/lib.rs

-34
Original file line numberDiff line numberDiff line change
@@ -119,40 +119,6 @@ pub fn selector_bytes(input: TokenStream) -> TokenStream {
119119
/// The `#[ink::contract]` macro can be provided with some additional comma-separated
120120
/// header arguments:
121121
///
122-
/// - `compile_as_dependency: bool`
123-
///
124-
/// Tells the ink! code generator to **always** or **never**
125-
/// compile the smart contract as if it was used as a dependency of another ink!
126-
/// smart contract.
127-
///
128-
/// Normally this flag is only really useful for ink! developers who
129-
/// want to inspect code generation of ink! smart contracts.
130-
/// The author is not aware of any particular practical use case for users that
131-
/// makes use of this flag but contract writers are encouraged to disprove this.
132-
///
133-
/// Note that it is recommended to make use of the built-in crate feature
134-
/// `ink-as-dependency` to flag smart contract dependencies listed in a contract's
135-
/// `Cargo.toml` as actual dependencies to ink!.
136-
///
137-
/// **Usage Example:**
138-
/// ```
139-
/// # use ink_lang as ink;
140-
/// #[ink::contract(compile_as_dependency = true)]
141-
/// mod my_contract {
142-
/// # #[ink(storage)]
143-
/// # pub struct MyStorage;
144-
/// # impl MyStorage {
145-
/// # #[ink(constructor)]
146-
/// # pub fn construct() -> Self { MyStorage {} }
147-
/// # #[ink(message)]
148-
/// # pub fn message(&self) {}
149-
/// # }
150-
/// // ...
151-
/// }
152-
/// ```
153-
///
154-
/// **Default value:** Depends on the crate feature propagation of `Cargo.toml`.
155-
///
156122
/// - `keep_attr: String`
157123
///
158124
/// Tells the ink! code generator which attributes should be passed to call builders.

crates/lang/src/env_access.rs

+9-19
Original file line numberDiff line numberDiff line change
@@ -431,25 +431,15 @@ where
431431
/// # use ink_lang as ink;
432432
/// # #[ink::contract]
433433
/// # pub mod my_contract {
434-
/// # use ink_lang as ink;
435-
/// # #[ink::contract(compile_as_dependency = true)]
436-
/// # pub mod other_contract {
437-
/// # #[ink(storage)]
438-
/// # pub struct OtherContract { }
439-
/// #
440-
/// # impl OtherContract {
441-
/// # #[ink(constructor)]
442-
/// # pub fn new() -> Self {
443-
/// # Self {}
444-
/// # }
445-
/// #
446-
/// # #[ink(message)]
447-
/// # pub fn some_operation(&self) {
448-
/// # // ...
449-
/// # }
450-
/// # }
451-
/// # }
452-
/// #
434+
/// # // In order for this to actually work with another contract we'd need a way
435+
/// # // to turn the `ink-as-dependency` crate feature on in doctests, which we
436+
/// # // can't do.
437+
/// # //
438+
/// # // Instead we use our own contract's `Ref`, which is fine for this example
439+
/// # // (just need something that implements the `ContractRef` trait).
440+
/// # pub mod other_contract {
441+
/// # pub use super::MyContractRef as OtherContractRef;
442+
/// # }
453443
/// use ink_env::{
454444
/// DefaultEnvironment,
455445
/// call::{build_create, Selector, ExecutionInput}

crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-01.rs

-19
This file was deleted.

crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-01.stderr

-5
This file was deleted.

crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-02.rs

-19
This file was deleted.

crates/lang/tests/ui/contract/fail/config-compile-as-dependency-invalid-type-02.stderr

-5
This file was deleted.

crates/lang/tests/ui/contract/fail/config-compile-as-dependency-missing-arg.rs

-19
This file was deleted.

crates/lang/tests/ui/contract/fail/config-compile-as-dependency-missing-arg.stderr

-5
This file was deleted.

0 commit comments

Comments
 (0)