-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Introduce pure specifier on functions #2745
Conversation
@@ -57,6 +57,8 @@ bool StaticAnalyzer::visit(FunctionDefinition const& _function) | |||
solAssert(m_localVarUseCount.empty(), ""); | |||
m_nonPayablePublic = _function.isPublic() && !_function.isPayable(); | |||
m_constructor = _function.isConstructor(); | |||
if (_function.stateMutability() == StateMutability::Pure) | |||
m_errorReporter.warning(_function.location(), "Function is marked pure. Be careful, pureness is not enforced yet."); |
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.
I think this is the safe way to introduce it unless the call graph analyser is finished.
c986134
to
5ae7d0e
Compare
Need to add tests for pure functions and function types. |
@@ -1,7 +1,8 @@ | |||
### 0.4.16 (unreleased) | |||
|
|||
Features: | |||
* ABI JSON: Include new field ``statemutability`` with values ``view``, ``nonpayable`` and ``payable``. | |||
* Introduce ``pure`` functions. The pureness is not enforced yet, use with care. | |||
* ABI JSON: Include new field ``statemutability`` with values ``pure``, ``view``, ``nonpayable`` and ``payable``. |
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.
any chance of making statemutability: state_mutability
? Pure aesthetics I know. But might make it easier to read. Atleast a mixedCase or -
of some kind.
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.
There's no prior example in the ABI so not sure which way to go. (However this was merged in a different PR so please open an issue/PR to solve it.)
5ae7d0e
to
6959fe5
Compare
libsolidity/ast/ASTJsonConverter.cpp
Outdated
make_pair( | ||
m_legacy ? "constant" : "isDeclaredConst", | ||
_node.stateMutability() == StateMutability::View || | ||
_node.stateMutability() == StateMutability::Pure |
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.
Actually I don' think this should contain pure as it is only for the AST. By using this AST one couldn't reliable rebuild the source.
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.
Not sure I understand. constant
/ isDeclaredConst
is used by static analyzers and I would like to break them rather later than sooner. We have an additional field that provides the state mutability, so all the information is there.
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.
While I still don't agree it belongs to isDeclaredConstant
, because that refers to the constant
specifier, I can understand the reasoning for static analyzers not yet aware of the statemutability
field. Ultimately, removing the obsolete field will solve this.
libsolidity/ast/ASTPrinter.cpp
Outdated
(_node.isPublic() ? " - public" : "") + | ||
( | ||
_node.stateMutability() == StateMutability::View || | ||
_node.stateMutability() == StateMutability::Pure ? |
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.
Same applies here as for the AST JSON.
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.
But the AST printer should at least contain the information about state mutability.
4ba6558
to
5cf2997
Compare
Pure Functions | ||
************** | ||
|
||
Functions can be declared ``pure`` in which case they promise not to read from or modify the state. |
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.
Perhaps we should explain in more detail what reading from or writing to the state means.
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.
We can also postpone this explanation so that we do not delay the release.
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.
Can we move it to #2792? Do you want to list all the disallowed things?
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.
Yes, we can move it to #2792. We should list as much as people need to understand.
5cf2997
to
6c0f21e
Compare
I think there are some places where Also ASTPrinter sholud be updated for completeness. |
I've thought I've changed the ABI (seems like it was gone through some wrong conflict resolution in rebasing), but explained above why I don't think it applies to ASTJsonConverter and ASTPrinter in #2745 (comment). |
6c0f21e
to
75d35ab
Compare
75d35ab
to
f646247
Compare
@chriseth should be ready |
Part of #992. Depends on #2754 and #2762.