Fix check that mappings can only have storage location

This commit is contained in:
Mathias Baumann 2019-02-05 15:51:19 +01:00
parent d7e2838702
commit 77f407d450
6 changed files with 19 additions and 9 deletions

View File

@ -3,6 +3,7 @@
Bugfixes: Bugfixes:
* Code generator: Defensively pad allocation of creationCode and runtimeCode to multiples of 32 bytes. * Code generator: Defensively pad allocation of creationCode and runtimeCode to multiples of 32 bytes.
* Parser: Disallow empty import statements. * 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). * 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) { auto checkArgumentAndReturnParameter = [&](VariableDeclaration const& var) {
if (type(var)->category() == Type::Category::Mapping) if (type(var)->category() == Type::Category::Mapping)
{ {
if (!type(var)->dataStoredIn(DataLocation::Storage)) if (var.referenceLocation() != VariableDeclaration::Location::Storage)
m_errorReporter.typeError(var.location(), "Mapping types can only have a data location of \"storage\"." ); {
else if (!isLibraryFunction && _function.isPublic()) 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."); 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 else
{ {

View File

@ -2,5 +2,5 @@ contract c {
function f1(mapping(uint => uint) calldata) pure external returns (mapping(uint => uint) memory) {} 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: (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 for parameters or return variables can only be used in 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 { 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 {} 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.