mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Moved a check related to constants to TypeChecker
And added a proper error message when constant types containing (nested) mapping types are used.
This commit is contained in:
parent
51009c005d
commit
8815d6f5f0
@ -437,14 +437,16 @@ void DeclarationTypeChecker::endVisit(VariableDeclaration const& _variable)
|
|||||||
type = TypeProvider::withLocation(ref, typeLoc, isPointer);
|
type = TypeProvider::withLocation(ref, typeLoc, isPointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_variable.isConstant() && !type->isValueType())
|
if (
|
||||||
{
|
_variable.isConstant() &&
|
||||||
bool allowed = false;
|
!dynamic_cast<UserDefinedValueType const*>(type) &&
|
||||||
if (auto arrayType = dynamic_cast<ArrayType const*>(type))
|
type->containsNestedMapping()
|
||||||
allowed = arrayType->isByteArray();
|
)
|
||||||
if (!allowed)
|
m_errorReporter.fatalDeclarationError(
|
||||||
m_errorReporter.fatalDeclarationError(9259_error, _variable.location(), "Constants of non-value type not yet implemented.");
|
3530_error,
|
||||||
}
|
_variable.location(),
|
||||||
|
"The type contains a (nested) mapping and therefore cannot be a constant."
|
||||||
|
);
|
||||||
|
|
||||||
_variable.annotation().type = type;
|
_variable.annotation().type = type;
|
||||||
}
|
}
|
||||||
|
@ -530,6 +530,15 @@ bool TypeChecker::visit(VariableDeclaration const& _variable)
|
|||||||
}
|
}
|
||||||
if (_variable.isConstant())
|
if (_variable.isConstant())
|
||||||
{
|
{
|
||||||
|
if (!varType->isValueType())
|
||||||
|
{
|
||||||
|
bool allowed = false;
|
||||||
|
if (auto arrayType = dynamic_cast<ArrayType const*>(varType))
|
||||||
|
allowed = arrayType->isByteArray();
|
||||||
|
if (!allowed)
|
||||||
|
m_errorReporter.fatalTypeError(9259_error, _variable.location(), "Constants of non-value type not yet implemented.");
|
||||||
|
}
|
||||||
|
|
||||||
if (!_variable.value())
|
if (!_variable.value())
|
||||||
m_errorReporter.typeError(4266_error, _variable.location(), "Uninitialized \"constant\" variable.");
|
m_errorReporter.typeError(4266_error, _variable.location(), "Uninitialized \"constant\" variable.");
|
||||||
else if (!*_variable.value()->annotation().isPure)
|
else if (!*_variable.value()->annotation().isPure)
|
||||||
|
@ -1124,6 +1124,8 @@ public:
|
|||||||
bool containsNestedMapping() const override
|
bool containsNestedMapping() const override
|
||||||
{
|
{
|
||||||
solAssert(nameable(), "Called for a non nameable type.");
|
solAssert(nameable(), "Called for a non nameable type.");
|
||||||
|
// DeclarationTypeChecker::endVisit(VariableDeclaration const&)
|
||||||
|
// assumes that this will never be true.
|
||||||
solAssert(!underlyingType().containsNestedMapping(), "");
|
solAssert(!underlyingType().containsNestedMapping(), "");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
int[L] constant L = 6;
|
int[L] constant L = 6;
|
||||||
// ----
|
// ----
|
||||||
// TypeError 5462: (4-5): Invalid array length, expected integer literal or constant expression.
|
// TypeError 5462: (4-5): Invalid array length, expected integer literal or constant expression.
|
||||||
// DeclarationError 9259: (0-21): Constants of non-value type not yet implemented.
|
|
||||||
|
@ -3,4 +3,3 @@ contract C {
|
|||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// TypeError 5462: (21-22): Invalid array length, expected integer literal or constant expression.
|
// TypeError 5462: (21-22): Invalid array length, expected integer literal or constant expression.
|
||||||
// DeclarationError 9259: (17-38): Constants of non-value type not yet implemented.
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
mapping(uint => uint) constant b = b;
|
mapping(uint => uint) constant b = b;
|
||||||
// ----
|
// ----
|
||||||
// DeclarationError 9259: (0-36): Constants of non-value type not yet implemented.
|
// DeclarationError 3530: (0-36): The type contains a (nested) mapping and therefore cannot be a constant.
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
struct S { uint x; }
|
struct S { uint x; }
|
||||||
S constant s;
|
S constant s;
|
||||||
// ----
|
// ----
|
||||||
// DeclarationError 9259: (21-33): Constants of non-value type not yet implemented.
|
// TypeError 9259: (21-33): Constants of non-value type not yet implemented.
|
||||||
|
@ -5,4 +5,4 @@ contract C {
|
|||||||
S public constant e = 0x1212121212121212121212121212121212121212;
|
S public constant e = 0x1212121212121212121212121212121212121212;
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// DeclarationError 9259: (71-135): Constants of non-value type not yet implemented.
|
// DeclarationError 3530: (71-135): The type contains a (nested) mapping and therefore cannot be a constant.
|
||||||
|
@ -3,4 +3,3 @@ contract test {
|
|||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// DeclarationError 1788: (31-55): The "constant" keyword can only be used for state variables or variables at file level.
|
// DeclarationError 1788: (31-55): The "constant" keyword can only be used for state variables or variables at file level.
|
||||||
// DeclarationError 9259: (31-55): Constants of non-value type not yet implemented.
|
|
||||||
|
@ -2,4 +2,4 @@ contract C {
|
|||||||
uint[3] constant x = [uint(1), 2, 3];
|
uint[3] constant x = [uint(1), 2, 3];
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// DeclarationError 9259: (17-53): Constants of non-value type not yet implemented.
|
// TypeError 9259: (17-53): Constants of non-value type not yet implemented.
|
||||||
|
@ -3,4 +3,4 @@ contract C {
|
|||||||
S constant x = S(5, new uint[](4));
|
S constant x = S(5, new uint[](4));
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// DeclarationError 9259: (52-86): Constants of non-value type not yet implemented.
|
// TypeError 9259: (52-86): Constants of non-value type not yet implemented.
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
contract C {
|
contract C {
|
||||||
// This should probably have a better error message at some point.
|
|
||||||
// Constant mappings should not be possible in general.
|
|
||||||
mapping(uint => uint) constant x;
|
mapping(uint => uint) constant x;
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// DeclarationError 9259: (148-180): Constants of non-value type not yet implemented.
|
// DeclarationError 3530: (17-49): The type contains a (nested) mapping and therefore cannot be a constant.
|
||||||
|
@ -5,4 +5,4 @@ contract C {
|
|||||||
S public constant c;
|
S public constant c;
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// DeclarationError 9259: (71-90): Constants of non-value type not yet implemented.
|
// DeclarationError 3530: (71-90): The type contains a (nested) mapping and therefore cannot be a constant.
|
||||||
|
@ -3,4 +3,3 @@ contract Foo {
|
|||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// DeclarationError 1788: (30-55): The "constant" keyword can only be used for state variables or variables at file level.
|
// DeclarationError 1788: (30-55): The "constant" keyword can only be used for state variables or variables at file level.
|
||||||
// DeclarationError 9259: (30-55): Constants of non-value type not yet implemented.
|
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
MyInt constant x = MyInt.wrap(20);
|
||||||
|
type MyInt is int;
|
Loading…
Reference in New Issue
Block a user