-
Notifications
You must be signed in to change notification settings - Fork 426
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
Don't copy attributes to call builders #1130
Don't copy attributes to call builders #1130
Conversation
🦑 📈 ink! Example Contracts ‒ Changes Report 📉 🦑These are the results when building the
Link to the run | Last update: Thu Feb 10 00:06:15 CET 2022 |
So just from your PR description the change seems okay. However, I tried to build the pausable contract you linked and it built fine. Can you explain how to reproduce the compilation error you're talking about? |
The pausable contract in the We started the migration to The error is next: error[E0277]: the trait bound `my_pausable::_::CallBuilder: brush::contracts::pausable::PausableStorage` is not satisfied
--> lib.rs:23:28
|
23 | #[brush::modifiers(when_not_paused)]
| ^^^^^^^^^^^^^^^ the trait `brush::contracts::pausable::PausableStorage` is not implemented for `my_pausable::_::CallBuilder`
|
note: required by a bound in `brush::contracts::pausable::when_not_paused`
--> /Users/green/External/4ire.labs/openbrush-contracts/contracts/security/pausable/mod.rs:40:8
|
40 | T: PausableStorage,
| ^^^^^^^^^^^^^^^ required by this bound in `brush::contracts::pausable::when_not_paused`
error[E0271]: type mismatch resolving `for<'r> <[[email protected]:23:9: 23:45] as FnOnce<(&'r mut my_pausable::_::CallBuilder,)>>::Output == Result<_, _>`
--> lib.rs:23:28
|
23 | #[brush::modifiers(when_not_paused)]
| ^^^^^^^^^^^^^^^ expected enum `Result`, found struct `ink_env::call::CallBuilder`
|
= note: expected enum `Result<_, _>`
found struct `ink_env::call::CallBuilder<DefaultEnvironment, Set<ink_env::AccountId>, Unset<u64>, Unset<u128>, Set<ExecutionInput<ArgumentList<ArgumentListEnd, ArgumentListEnd>>>, Set<ReturnType<Result<(), brush::contracts::pausable::PausableError>>>>`
note: required by a bound in `brush::contracts::pausable::when_not_paused`
--> /Users/green/External/4ire.labs/openbrush-contracts/contracts/security/pausable/mod.rs:41:26
|
41 | F: FnOnce(&mut T) -> Result<R, E>,
| ^^^^^^^^^^^^ required by this bound in `brush::contracts::pausable::when_not_paused`
error[E0308]: mismatched types
--> lib.rs:23:9
|
23 | #[brush::modifiers(when_not_paused)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `ink_env::call::CallBuilder`, found enum `Result`
24 | pub fn flip(&mut self) -> Result<(), PausableError> {
| ------------------------- expected `ink_env::call::CallBuilder<DefaultEnvironment, Set<ink_env::AccountId>, Unset<u64>, Unset<u128>, Set<ExecutionInput<ArgumentList<ArgumentListEnd, ArgumentListEnd>>>, Set<ReturnType<Result<(), brush::contracts::pausable::PausableError>>>>` because of return type
|
= note: expected struct `ink_env::call::CallBuilder<DefaultEnvironment, Set<ink_env::AccountId>, Unset<u64>, Unset<u128>, Set<ExecutionInput<ArgumentList<ArgumentListEnd, ArgumentListEnd>>>, Set<ReturnType<Result<(), brush::contracts::pausable::PausableError>>>>`
found enum `Result<_, _>`
= note: this error originates in the attribute macro `brush::modifiers` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyFlipperRef: brush::contracts::pausable::PausableStorage` is not satisfied
--> lib.rs:23:28
|
23 | #[brush::modifiers(when_not_paused)]
| ^^^^^^^^^^^^^^^ the trait `brush::contracts::pausable::PausableStorage` is not implemented for `MyFlipperRef`
|
note: required by a bound in `brush::contracts::pausable::when_not_paused`
--> /Users/green/External/4ire.labs/openbrush-contracts/contracts/security/pausable/mod.rs:40:8
|
40 | T: PausableStorage,
| ^^^^^^^^^^^^^^^ required by this bound in `brush::contracts::pausable::when_not_paused`
|
Codecov Report
@@ Coverage Diff @@
## master #1130 +/- ##
==========================================
+ Coverage 78.80% 78.83% +0.02%
==========================================
Files 252 252
Lines 9397 9391 -6
==========================================
- Hits 7405 7403 -2
+ Misses 1992 1988 -4
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@xgreenx I'm not so sure about this PR and would like to ask for some more clarification.
If I run Generally I feel like the current behavior is what I as a developer would expect and it's also closer to Rust than to have those attributes disappear. I can imagine some clippy use cases (e.g. complex types) where it's justified to have those attributes be retained. Can you explain the use case you have some more? Maybe we find another solution. Maybe you can elaborate how you mean this:
|
Yes, it was an idea of the change to remove all attributes=D As I mentioned it is not important for generated structures like builders. But I agree, maybe
We implemented modifiers in OpenBrush like we describe here. The idea is that modifier's code is integrated into the method. For example usage of modifiers |
This is the solution I would prefer. So to have attributes like |
Then we need to decide how it can be marked for filtering=) We can remove all attributes by default, only keeping attributes that contains substring from the whitelist: ["ink", "doc", "clippy", "allow", "deny"](Maybe you know other useful attributes?) If the user wants to keep the attribute that is not from the whitelist, he can use the |
Let's do the other way around, so have attributes propagated by default and if one needs them filtered out to annotate this case explicitly. Would this still work for your use case then? I would prefer this way since it comes with way less surprises, you should be able to hide this from an OpenBrush developer and people who use |
@xgreenx How about changing this PR so that the syntax |
That means that users of OpenBrush should explicitly use modifiers
We can't create a workaround for the problem with attributes without explicit specification of I think in most cases developers will add attributes that are related to the contract itself, not to builders. So it is why I think in most cases better remove not expected attributes for builders. |
It means that we need to introduce the same syntaxis for It is hard to imagine for me when we need to pass attributes to builders except |
@xgreenx Here is the list of built-in attributes: https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index, so those would need to be whitelisted as well, right? Couldn't your |
Not all=) We are talking about the contract's public methods so only attributes: Diagnostics, Documentation, Conditional compilation.
|
Ok, so having discussed this, from my pov it's fine if you implement whitelisting + users having to opt-in if they want to keep the propagation. I think this then is still a better solution than how the PR is currently approved (removing the attributes altogether). |
I will work on it=) Are you okay with syntaxis |
Cool! I would put it on the top level of the contract as |
Okay, the same for |
Yup, fine with me! |
Created a new pull request #1145 |
The current codegen copy all attributes from the original method. But that attributes were initially added for the original function and may require some restrictions on the struct that uses these attributes.
For example, OpenBrush provides
#[brush::modifiers]
attribute that inserts some code before the function.In the case of
when_paused
modifier, the struct should implement thePausableStorage
trait. That contract derives that trait but auto-generated call builders don't derive it(and they can't because it is complicated for them). And it will cause a compilation error because call builders will try to use that modifier.The idea of that PR is to not insert attributes from the original method for auto-generated call builders.
Call builders are used only for cross-contract communication so they don't participate in the code logic of the contract and they don't participate in the metadata generation(ABI). So attributes like
#[doc]
is not important.BUT, maybe someone wants to apply some functional attributes like
clippy
orallow
and that change will break it. So maybe we also need to think about how ink! can provide customization of attributes(which attributes should be passed to auto-generated structures and which not).