Merge pull request #5931 from ethereum/fix-mapping-storage-check

Fix check that mappings can only have storage location
This commit is contained in:
chriseth 2019-02-05 21:03:20 +01:00 committed by GitHub
commit 438e19aa0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 9 deletions

View File

@ -3,6 +3,7 @@
Bugfixes:
* Code generator: Defensively pad allocation of creationCode and runtimeCode to multiples of 32 bytes.
* Parser: Disallow empty import statements.
* Type Checker: Dissallow mappings with data locations other than 'storage'
* Type system: Properly report packed encoded size for arrays and structs (mostly unused until now).

View File

@ -334,10 +334,17 @@ bool TypeChecker::visit(FunctionDefinition const& _function)
auto checkArgumentAndReturnParameter = [&](VariableDeclaration const& var) {
if (type(var)->category() == Type::Category::Mapping)
{
if (!type(var)->dataStoredIn(DataLocation::Storage))
m_errorReporter.typeError(var.location(), "Mapping types can only have a data location of \"storage\"." );
else if (!isLibraryFunction && _function.isPublic())
m_errorReporter.typeError(var.location(), "Mapping types for parameters or return variables can only be used in internal or library functions.");
if (var.referenceLocation() != VariableDeclaration::Location::Storage)
{
if (!isLibraryFunction && _function.isPublic())
m_errorReporter.typeError(var.location(), "Mapping types can only have a data location of \"storage\" and thus only be parameters or return variables for internal or library functions.");
else
m_errorReporter.typeError(var.location(), "Mapping types can only have a data location of \"storage\"." );
}
else
{
solAssert(isLibraryFunction || !_function.isPublic(), "Mapping types for parameters or return variables can only be used in internal or library functions.");
}
}
else
{

View File

@ -2,5 +2,5 @@ contract c {
function f1(mapping(uint => uint) calldata) pure external returns (mapping(uint => uint) memory) {}
}
// ----
// TypeError: (29-59): Mapping types for parameters or return variables can only be used in internal or library functions.
// TypeError: (84-112): Mapping types for parameters or return variables can only be used in internal or library functions.
// TypeError: (29-59): Mapping types can only have a data location of "storage" and thus only be parameters or return variables for internal or library functions.
// TypeError: (84-112): Mapping types can only have a data location of "storage" and thus only be parameters or return variables for internal or library functions.

View File

@ -1,4 +1,6 @@
contract c {
function f4(mapping(uint => uint) memory) pure internal {}
function f4(mapping(uint => uint) storage) pure internal {}
function f5(mapping(uint => uint) memory) pure internal {}
}
// ----
// TypeError: (93-121): Mapping types can only have a data location of "storage".

View File

@ -2,4 +2,4 @@ contract c {
function f3(mapping(uint => uint) memory) view public {}
}
// ----
// TypeError: (29-57): Mapping types for parameters or return variables can only be used in internal or library functions.
// TypeError: (29-57): Mapping types can only have a data location of "storage" and thus only be parameters or return variables for internal or library functions.

View File

@ -3,4 +3,4 @@ contract C {
}
}
// ----
// TypeError: (51-79): Mapping types for parameters or return variables can only be used in internal or library functions.
// TypeError: (51-79): Mapping types can only have a data location of "storage" and thus only be parameters or return variables for internal or library functions.