-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Introduce pure specifier on functions #2745
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
Changes from all commits
5668377
e9a9a07
93e6e83
504e628
23c791e
f646247
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -475,7 +475,7 @@ Functions can be declared ``view`` in which case they promise not to modify the | |
|
|
||
| contract C { | ||
| function f(uint a, uint b) view returns (uint) { | ||
| return a * (b + 42); | ||
| return a * (b + 42) + now; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -488,6 +488,27 @@ Functions can be declared ``view`` in which case they promise not to modify the | |
| .. warning:: | ||
| The compiler does not enforce yet that a ``view`` method is not modifying state. | ||
|
|
||
| .. _pure-functions: | ||
|
|
||
| ************** | ||
| Pure Functions | ||
| ************** | ||
|
|
||
| Functions can be declared ``pure`` in which case they promise not to read from or modify the state. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| :: | ||
|
|
||
| pragma solidity ^0.4.0; | ||
|
|
||
| contract C { | ||
| function f(uint a, uint b) pure returns (uint) { | ||
| return a * (b + 42); | ||
| } | ||
| } | ||
|
|
||
| .. warning:: | ||
| The compiler does not enforce yet that a ``pure`` method is not reading from the state. | ||
|
|
||
| .. index:: ! fallback function, function;fallback | ||
|
|
||
| .. _fallback-function: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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."); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| return true; | ||
| } | ||
|
|
||
|
|
||
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.Uh oh!
There was an error while loading. Please reload this page.
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.)