Merge pull request #8977 from ethereum/mapping-type-error

Error when Mapping type in (non-local) storage is assigned to
This commit is contained in:
chriseth 2020-05-19 16:52:54 +02:00 committed by GitHub
commit f70ff0dfaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 1 deletions

View File

@ -11,6 +11,7 @@ Compiler Features:
Bugfixes:
* Optimizer: Fixed a bug in BlockDeDuplicator.
* Type Checker: Disallow assignments to storage variables of type ``mapping``.
### 0.6.8 (2020-05-14)

View File

@ -453,7 +453,15 @@ bool TypeChecker::visit(VariableDeclaration const& _variable)
if (contractType->contractDefinition().isLibrary())
m_errorReporter.typeError(1273_error, _variable.location(), "The type of a variable cannot be a library.");
if (_variable.value())
expectType(*_variable.value(), *varType);
{
if (_variable.isStateVariable() && dynamic_cast<MappingType const*>(varType))
{
m_errorReporter.typeError(6280_error, _variable.location(), "Mappings cannot be assigned to.");
_variable.value()->accept(*this);
}
else
expectType(*_variable.value(), *varType);
}
if (_variable.isConstant())
{
if (!_variable.type()->isValueType())

View File

@ -0,0 +1,10 @@
contract D {
mapping (uint => uint) a;
mapping (uint => uint) b;
function foo() public view {
mapping (uint => uint) storage c = b;
b = c;
}
}
// ----
// TypeError: (160-161): Mappings cannot be assigned to.

View File

@ -0,0 +1,27 @@
contract C {
mapping (uint => address payable [ ]) public a = a ;
}
contract D {
mapping (uint => uint) a;
mapping (uint => uint) b = a;
}
contract F {
mapping (uint => uint) a;
mapping (uint => uint) b;
function foo() public {
a = b;
}
}
contract G {
uint x = 1;
mapping (uint => uint) b = x;
}
// ----
// TypeError: (17-67): Mappings cannot be assigned to.
// TypeError: (120-148): Mappings cannot be assigned to.
// TypeError: (263-264): Mappings cannot be assigned to.
// TypeError: (312-340): Mappings cannot be assigned to.

View File

@ -0,0 +1,8 @@
contract D {
mapping (uint => uint) a;
function foo() public view {
mapping (uint => int) storage c = a;
}
}
// ----
// TypeError: (84-119): Type mapping(uint256 => uint256) is not implicitly convertible to expected type mapping(uint256 => int256).