Skip to content

Commit 8815d6f

Browse files
committed
Moved a check related to constants to TypeChecker
And added a proper error message when constant types containing (nested) mapping types are used.
1 parent 51009c0 commit 8815d6f

15 files changed

+30
-21
lines changed

libsolidity/analysis/DeclarationTypeChecker.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -437,14 +437,16 @@ void DeclarationTypeChecker::endVisit(VariableDeclaration const& _variable)
437437
type = TypeProvider::withLocation(ref, typeLoc, isPointer);
438438
}
439439

440-
if (_variable.isConstant() && !type->isValueType())
441-
{
442-
bool allowed = false;
443-
if (auto arrayType = dynamic_cast<ArrayType const*>(type))
444-
allowed = arrayType->isByteArray();
445-
if (!allowed)
446-
m_errorReporter.fatalDeclarationError(9259_error, _variable.location(), "Constants of non-value type not yet implemented.");
447-
}
440+
if (
441+
_variable.isConstant() &&
442+
!dynamic_cast<UserDefinedValueType const*>(type) &&
443+
type->containsNestedMapping()
444+
)
445+
m_errorReporter.fatalDeclarationError(
446+
3530_error,
447+
_variable.location(),
448+
"The type contains a (nested) mapping and therefore cannot be a constant."
449+
);
448450

449451
_variable.annotation().type = type;
450452
}

libsolidity/analysis/TypeChecker.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,15 @@ bool TypeChecker::visit(VariableDeclaration const& _variable)
530530
}
531531
if (_variable.isConstant())
532532
{
533+
if (!varType->isValueType())
534+
{
535+
bool allowed = false;
536+
if (auto arrayType = dynamic_cast<ArrayType const*>(varType))
537+
allowed = arrayType->isByteArray();
538+
if (!allowed)
539+
m_errorReporter.fatalTypeError(9259_error, _variable.location(), "Constants of non-value type not yet implemented.");
540+
}
541+
533542
if (!_variable.value())
534543
m_errorReporter.typeError(4266_error, _variable.location(), "Uninitialized \"constant\" variable.");
535544
else if (!*_variable.value()->annotation().isPure)

libsolidity/ast/Types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,8 @@ class UserDefinedValueType: public Type
11241124
bool containsNestedMapping() const override
11251125
{
11261126
solAssert(nameable(), "Called for a non nameable type.");
1127+
// DeclarationTypeChecker::endVisit(VariableDeclaration const&)
1128+
// assumes that this will never be true.
11271129
solAssert(!underlyingType().containsNestedMapping(), "");
11281130
return false;
11291131
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
int[L] constant L = 6;
22
// ----
33
// TypeError 5462: (4-5): Invalid array length, expected integer literal or constant expression.
4-
// DeclarationError 9259: (0-21): Constants of non-value type not yet implemented.

test/libsolidity/syntaxTests/constantEvaluator/type_reference_in_contract.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ contract C {
33
}
44
// ----
55
// TypeError 5462: (21-22): Invalid array length, expected integer literal or constant expression.
6-
// DeclarationError 9259: (17-38): Constants of non-value type not yet implemented.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
mapping(uint => uint) constant b = b;
22
// ----
3-
// DeclarationError 9259: (0-36): Constants of non-value type not yet implemented.
3+
// DeclarationError 3530: (0-36): The type contains a (nested) mapping and therefore cannot be a constant.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
struct S { uint x; }
22
S constant s;
33
// ----
4-
// DeclarationError 9259: (21-33): Constants of non-value type not yet implemented.
4+
// TypeError 9259: (21-33): Constants of non-value type not yet implemented.

test/libsolidity/syntaxTests/iceRegressionTests/const_struct_with_mapping.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ contract C {
55
S public constant e = 0x1212121212121212121212121212121212121212;
66
}
77
// ----
8-
// DeclarationError 9259: (71-135): Constants of non-value type not yet implemented.
8+
// DeclarationError 3530: (71-135): The type contains a (nested) mapping and therefore cannot be a constant.

test/libsolidity/syntaxTests/nameAndTypeResolution/105_constant_input_parameter.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ contract test {
33
}
44
// ----
55
// DeclarationError 1788: (31-55): The "constant" keyword can only be used for state variables or variables at file level.
6-
// DeclarationError 9259: (31-55): Constants of non-value type not yet implemented.

test/libsolidity/syntaxTests/nameAndTypeResolution/171_assignment_to_const_array_vars.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ contract C {
22
uint[3] constant x = [uint(1), 2, 3];
33
}
44
// ----
5-
// DeclarationError 9259: (17-53): Constants of non-value type not yet implemented.
5+
// TypeError 9259: (17-53): Constants of non-value type not yet implemented.

0 commit comments

Comments
 (0)