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

Conditional Codegen for Messages, Constructors and Events #1458

Merged
merged 7 commits into from
Nov 3, 2022

Conversation

SkymanOne
Copy link
Contributor

@SkymanOne SkymanOne commented Oct 30, 2022

This PR closes #1231

It allows constructors, messages and events to be conditionally compiled (i.e. have #[cfg(...)] attribute)

Example

/* --Snippet -- */
#[ink(message)]
#[cfg(feature = "foo")]
pub fn another_get(&self) -> String {
    self.value.clone()
}
/* --Snippet -- */

/* --Snippet -- */
#[ink(constructor)]
#[cfg(feature = "another_foo")]
pub fn another_new(val: String) -> Self {
    Self { value: val }
}
/* --Snippet -- */

/* --Snippet -- */
#[ink(event)]
#[cfg(feature = "foo")]
pub struct FeaturedAuctionEchoed {
    auction: Auction,
}
/* --Snippet -- */

@SkymanOne SkymanOne changed the title PoC Conditional Codegen Conditional Codegen for Messages, Constructors and Events Oct 31, 2022
@SkymanOne SkymanOne marked this pull request as ready for review October 31, 2022 10:05
crates/ink/codegen/src/generator/mod.rs Outdated Show resolved Hide resolved
crates/ink/codegen/src/generator/mod.rs Outdated Show resolved Hide resolved
crates/ink/codegen/src/generator/mod.rs Outdated Show resolved Hide resolved
crates/ink/codegen/src/generator/mod.rs Outdated Show resolved Hide resolved
crates/ink/codegen/src/generator/mod.rs Outdated Show resolved Hide resolved
crates/ink/codegen/src/generator/mod.rs Outdated Show resolved Hide resolved
crates/ink/codegen/src/generator/metadata.rs Outdated Show resolved Hide resolved
@SkymanOne SkymanOne requested a review from ascjones November 2, 2022 09:07
@athei
Copy link
Contributor

athei commented Nov 3, 2022

Just make cure it builds with the feature enabled cargo check --features foo.

@codecov-commenter
Copy link

Codecov Report

Merging #1458 (b83f0e6) into master (408a299) will increase coverage by 5.72%.
The diff coverage is 69.56%.

@@            Coverage Diff             @@
##           master    #1458      +/-   ##
==========================================
+ Coverage   64.39%   70.12%   +5.72%     
==========================================
  Files         200      200              
  Lines        6115     6133      +18     
==========================================
+ Hits         3938     4301     +363     
+ Misses       2177     1832     -345     
Impacted Files Coverage Δ
crates/ink/codegen/src/generator/dispatch.rs 94.54% <ø> (ø)
crates/ink/ir/src/ir/item_impl/mod.rs 86.82% <45.45%> (+19.02%) ⬆️
crates/ink/ir/src/ir/item_mod.rs 89.71% <75.00%> (+27.64%) ⬆️
crates/ink/codegen/src/generator/events.rs 97.29% <100.00%> (+0.03%) ⬆️
crates/ink/ir/src/ir/item_impl/iter.rs 100.00% <100.00%> (ø)
crates/metadata/src/layout/mod.rs 75.63% <0.00%> (-1.69%) ⬇️
crates/allocator/src/bump.rs 86.77% <0.00%> (+0.22%) ⬆️
crates/ink/ir/src/ir/attrs.rs 81.99% <0.00%> (+0.55%) ⬆️
crates/ink/ir/src/ir/item_impl/callable.rs 91.83% <0.00%> (+4.08%) ⬆️
crates/ink/ir/src/ir/trait_def/item/mod.rs 90.08% <0.00%> (+4.95%) ⬆️
... and 21 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@SkymanOne
Copy link
Contributor Author

Just make cure it builds with the feature enabled cargo check --features foo.

Checked. It works

@SkymanOne SkymanOne merged commit 06e9cf2 into master Nov 3, 2022
@SkymanOne SkymanOne deleted the gn/cond-compl branch November 3, 2022 11:43
Copy link
Collaborator

@ascjones ascjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this does not work

/// ```
/// The function would would iterate over the `cfg` attributes
/// and return `true` if any of the feature flags are set
pub fn is_code_span_enabled(attrs: &[Attribute]) -> bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more natural place for this would be ir/utils.rs

for attr_tokens in cfg_attrs {
let _s = attr_tokens.to_token_stream();
if cfg!(_s) {
return true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this logic is wrong in the case of multiple cfg attributes e.g.

fn main() {
    foo_bar();
}

#[cfg(feature = "foo")]
#[cfg(feature = "bar")]
fn foo_bar() {}

cargo build --features foo -> 🔴
cargo build --features bar -> 🔴
cargo build --features foo,bar -> 🟢

So having separate cfg attrs seems to be the equivalent of #[cfg(all(feature = "foo", feature = "bar"))]

The implementation here is instead #[cfg(any(feature = "foo", feature = "bar"))].

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic would be easier to read when expressed e.g. in the form:

pub fn is_code_span_enabled(attrs: &[Attribute]) -> bool {
    attrs
        .iter()
        .filter(|a| a.path.is_ident("cfg"))
        .all(|a| {
            let predicate = &a.tokens.to_token_stream();
            cfg!(predicate)
        })
}

}
for attr_tokens in cfg_attrs {
let _s = attr_tokens.to_token_stream();
if cfg!(_s) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this even work with the token stream? cfg! is a compile time macro.

I've tried it and it doesn't appear to work. Even with the literal cfg!(feature = "foo") it doesn't work for some reason. cfg!(unix) literal does work.

I think we need to rethink the approach here, maybe see whether we can carry those attributes through to the generated code (I know it is everywhere)

@SkymanOne SkymanOne restored the gn/cond-compl branch November 7, 2022 16:27
@ascjones ascjones mentioned this pull request Nov 7, 2022
8 tasks
SkymanOne pushed a commit that referenced this pull request Nov 7, 2022
ascjones pushed a commit that referenced this pull request Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Messages and contstructor cannot be conditionally compiled
4 participants