Skip to content

Commit 0004ad8

Browse files
committed
Fix ICE when a constant variable declaration forward references a struct
1 parent 7b58afa commit 0004ad8

16 files changed

+25
-31
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Compiler Features:
1212
Bugfixes:
1313
* Control Flow Graph: Perform proper virtual lookup for modifiers for uninitialized variable and unreachable code analysis.
1414
* Immutables: Fix wrong error when the constructor of a base contract uses ``return`` and the parent contract contains immutable variables.
15+
* TypeChecker: Fix ICE when a constant variable declaration forward references a struct.
1516

1617

1718
Solc-Js:

libsolidity/analysis/DeclarationTypeChecker.cpp

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

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-
);
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.fatalTypeError(9259_error, _variable.location(), "Only constants of value type and byte array type are implemented.");
447+
}
450448

451449
_variable.annotation().type = type;
452450
}

libsolidity/analysis/TypeChecker.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -530,15 +530,6 @@ 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-
542533
if (!_variable.value())
543534
m_errorReporter.typeError(4266_error, _variable.location(), "Uninitialized \"constant\" variable.");
544535
else if (!*_variable.value()->annotation().isPure)

libsolidity/ast/Types.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,11 +1110,7 @@ class UserDefinedValueType: public Type
11101110
u256 storageSize() const override { return underlyingType().storageSize(); }
11111111
unsigned storageBytes() const override { return underlyingType().storageBytes(); }
11121112

1113-
bool isValueType() const override
1114-
{
1115-
solAssert(underlyingType().isValueType(), "");
1116-
return true;
1117-
}
1113+
bool isValueType() const override { return true; }
11181114
bool nameable() const override
11191115
{
11201116
solAssert(underlyingType().nameable(), "");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
int[L] constant L = 6;
22
// ----
33
// TypeError 5462: (4-5): Invalid array length, expected integer literal or constant expression.
4+
// TypeError 9259: (0-21): Only constants of value type and byte arrays are implemented.

test/libsolidity/syntaxTests/constantEvaluator/type_reference_in_contract.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ contract C {
33
}
44
// ----
55
// TypeError 5462: (21-22): Invalid array length, expected integer literal or constant expression.
6+
// TypeError 9259: (17-38): Only constants of value type and byte arrays are 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 3530: (0-36): The type contains a (nested) mapping and therefore cannot be a constant.
3+
// TypeError 9259: (0-36): Only constants of value type and byte arrays are implemented.
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-
// TypeError 9259: (21-33): Constants of non-value type not yet implemented.
4+
// TypeError 9259: (21-33): Only constants of value type and byte arrays are 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 3530: (71-135): The type contains a (nested) mapping and therefore cannot be a constant.
8+
// TypeError 9259: (71-135): Only constants of value type and byte arrays are implemented.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ 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+
// TypeError 9259: (31-55): Only constants of value type and byte arrays are implemented.

0 commit comments

Comments
 (0)