Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Warn (error - 0.5) if function parameter (return as well) uses defaul…
…t data location.
  • Loading branch information
chase1745 committed Jul 8, 2018
commit 8f8f79ddf976f19c0f8ab7ec70c179fdb9539aba
21 changes: 19 additions & 2 deletions libsolidity/analysis/ReferencesResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ void ReferencesResolver::endVisit(VariableDeclaration const& _variable)
using Location = VariableDeclaration::Location;
Location varLoc = _variable.referenceLocation();
DataLocation typeLoc = DataLocation::Memory;
bool const v050 = _variable.sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::V050);
Copy link
Contributor

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?

Copy link
Collaborator

@erak erak Jul 9, 2018

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.)

// References are forced to calldata for external function parameters (not return)
// and memory for parameters (also return) of publicly visible functions.
// They default to memory for function parameters and storage for local variables.
Expand Down Expand Up @@ -347,7 +348,17 @@ void ReferencesResolver::endVisit(VariableDeclaration const& _variable)
"(remove the \"storage\" or \"calldata\" keyword)."
);
if (varLoc == Location::Default || !contract.isLibrary())
{
if (varLoc == Location::Default && !_variable.isEventParameter())
{
typeError(
_variable.location(),
"Location must be specified as \"memory\" "
"for parameters in publicly visible functions."
);
}
typeLoc = DataLocation::Memory;
}
else
{
if (varLoc == Location::CallData)
Expand All @@ -372,13 +383,19 @@ void ReferencesResolver::endVisit(VariableDeclaration const& _variable)
else if (varLoc == Location::Default)
{
if (_variable.isCallableParameter())
typeLoc = DataLocation::Memory;
{
typeError(
_variable.location(),
"Location must be specified as either "
"\"memory\" or \"storage\" for parameters."
);
}
else
{
typeLoc = DataLocation::Storage;
if (_variable.isLocalVariable())
{
if (_variable.sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::V050))
if (v050)
typeError(
_variable.location(),
"Data location must be specified as either \"memory\" or \"storage\"."
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library C {
function f(uint[]) private pure {}
}
// ----
// Warning: (27-33): Parameter is declared as memory. Use an explicit "memory" keyword to silence this warning.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library C {
function f() private pure returns(uint[]) {}
}
// ----
// Warning: (50-56): Parameter is declared as memory. Use an explicit "memory" keyword to silence this warning.
Copy link
Contributor

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.

Copy link
Contributor Author

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.