-
Notifications
You must be signed in to change notification settings - Fork 6.3k
[BREAKING] Throw error if function parameters (return) use default data location. #4014
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
Conversation
| if (_variable.isCallableParameter()) | ||
| typeLoc = DataLocation::Memory; | ||
| if (_variable.isCallableParameter()) { | ||
| typeLoc = DataLocation::Memory; |
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.
Please indent using tabs only.
Please put { on a line of its own.
Coding style: https://github.com/ethereum/solidity/blob/develop/CODING_STYLE.md
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.
My apologies on this! Should be fixed now.
| function f() private pure returns(uint[]) {} | ||
| } | ||
| // ---- | ||
| // Warning: (50-56): Parameter is declared as memory. Use an explicit "memory" keyword to silence this warning. No newline at end of file |
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.
Please add a test that shows that this warning can actually be removed by adding memory.
Please also add testst for external and internal functions and functions using storage parameters, both in contracts and in libraries.
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 have added these tests for all types of function visibility as well as for contracts and libraries. I also added tests showing the warning is removed by adding a storage location.
| auto const& contract = dynamic_cast<ContractDefinition const&>( | ||
| *dynamic_cast<Declaration const&>(*_variable.scope()).scope() | ||
| ); | ||
| if (contract.isLibrary()) { |
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.
Why should this be restricted to libraries only?
Note that for external functions, we have a default data location of calldata which is not possible to express in source code, so we cannot warn about it.
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.
In the original issue you mentioned "We should do the same at least for the case where the contract is a library.", so I only warned for libraries, but I have changed it to warn for contracts as well.
|
I believe the only issue left is with events. It is warning for no default data location for event parameters, which isn't correct since they are not allowed. |
|
Please add a function |
| if (varLoc == Location::Default) | ||
| { | ||
| // Warn users (error in 0.5.0) for using default data location in (return) parameters. | ||
| if (_variable.sourceUnit().annotation().experimentalFeatures.count( |
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.
Please create a bool const v050 variable that holds this condition at the beginning of the function.
| ); | ||
| if (varLoc == Location::Default || !contract.isLibrary()) | ||
| { | ||
| if (varLoc == Location::Default) |
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.
Does this have to be inside the other condition in line 352?
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, because of the or condition, a parameter using memory in a public function would be passed to this if statement, so we need to check if it's using the default location. This is a little confusing, but I didn't want to refactor too much of the existing code.
| using Location = VariableDeclaration::Location; | ||
| Location varLoc = _variable.referenceLocation(); | ||
| DataLocation typeLoc = DataLocation::Memory; | ||
| const bool v050 = bool( |
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.
No need for an explicit typecast here. Also usually we write this as bool const v050.
| { | ||
| if (varLoc == Location::Default && !_variable.isEventParameter()) | ||
| { | ||
| // Warn users (error in 0.5.0) for using default data location in (return) parameters. |
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.
No need for this comment.
| else | ||
| m_errorReporter.warning( | ||
| _variable.location(), | ||
| "Parameter is declared as memory. " |
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.
Please enclose memory in quotes. Perhaps reword it as Parameter location is assumed as "memory".
| else | ||
| m_errorReporter.warning( | ||
| _variable.location(), | ||
| "Parameter is declared as memory. " |
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.
The same comments apply to this block as to the above block.
libsolidity/ast/AST.cpp
Outdated
|
|
||
| bool VariableDeclaration::isEventParameter() const | ||
| { | ||
| return (bool)dynamic_cast<EventDefinition const*>(scope()); |
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.
Please use != nullptr instead of the typecasting.
|
Please add a change log entry, rebase and squash some of the commits (which belong together, i.e. the test changes). |
|
Just pushed a squashed version of commits. |
801b941 to
36f9b1e
Compare
|
@chase1745 yes, please put this inside the 0.5.0 section. |
fc94f0a to
9d9a9a3
Compare
|
Added entry to changelog. |
|
@chase1745 sorry for not responding earlier. This is failing test, mostly in |
| "for parameters in publicly visible functions." | ||
| ); | ||
| else | ||
| m_errorReporter.warning( |
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.
Since this is listed as a breaking change, there is no need to have the non-0.5.0 mode here.
| "\"memory\" or \"storage\" for parameters." | ||
| ); | ||
| else | ||
| m_errorReporter.warning( |
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 here.
91c9c1e to
b91c11b
Compare
|
I removed checking for non 0.5.0 mode since this is now a breaking change. I also fixed the new tests to represent this and fixed other non-related tests to not use default data location so they could run as intended without receiving this new error. All isoltests are passing for me now. |
|
Quite a few non-syntax tests are failing now since its a pretty big change. I'll be going through and fixing what I can find over the weekend. |
|
Still working on them, there's a good number of failures because of this. There also seems to be quite a few external tests throwing errors because of this, not sure what to do about those? |
|
@chase1745 regarding the external tests:
|
|
@bit-shift thanks, yeah must've just missed that one in my OpenZeppelin PR, I'll fix that. |
84eee29 to
b2300ac
Compare
…ts to use explicit storage parameters to silence warnings.
b2300ac to
0c25155
Compare
0c25155 to
3cb1bc1
Compare
|
I rebased from |
| using Location = VariableDeclaration::Location; | ||
| Location varLoc = _variable.referenceLocation(); | ||
| DataLocation typeLoc = DataLocation::Memory; | ||
| bool const v050 = _variable.sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::V050); |
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.
The next release will be version 0.5.0, so there's no need [anymore] to optionally check for this, is it?
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, since this is a breaking change, the warning should be turned into an error (and therefor this check can be removed.)
erak
left a comment
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.
Please remove the check for pragma experimental "v0.5.0" and turn the warning into an error in ReferencesResolver::endVisit()
|
@bit-shift That is the the error for local variables, not related to this PR, so I didn’t change it, thought it may be better in a separate PR. Happy to change it though if you all think it’s fine for this PR. |
|
@chase1745 Ah yeah, right :) Let's do this in another PR. This one is already pretty large. |
|
@bit-shift Sounds good, I’ll try to submit one this afternoon. Can you re-trigger the tests now that @axic merged in the changes to OpenZeppelin? |
|
@chase1745 The external test is still failing (
Doing this will help to find breaking contracts :) |
|
🙄I'll find them all this time! Thanks for the tip! |
|
@bit-shift I found 2 parameters in that file to fix, and am almost positive that is all of them in OpenZeppelin, but running the tests the way you said, the current |
|
Sorry, this needs to be rebased now. Perhaps it would make sense to split this PR into multiple PRs:
|
|
@chriseth Yeah that makes sense I think. I'll work on that and submit some PR's. |
|
@chase1745 Great work here. We're happy to tip for all the extra effort you put in - excited to pay out soon 👍 |
|
@chase1745 can this PR be closed? I think #4518 is the last in the row superseding this one. |
Fixes #3402
Only warns if the contract is a library, but can be changed to all contracts if needed.
Throws Type Error for 0.5.0.