aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-08-16 21:56:14 +0800
committerGitHub <noreply@github.com>2018-08-16 21:56:14 +0800
commit410d288dfc2e08c42df58c7e01ad5c332ce92727 (patch)
tree8a905ac23df94b0df2f2dd1a0b6b6dfbf5b58008
parent146e89c2ec795ae799a91b54a959e672a998b8f9 (diff)
parent583e7156ba214c98096217d18f6270e4a46491d9 (diff)
downloaddexon-solidity-410d288dfc2e08c42df58c7e01ad5c332ce92727.tar
dexon-solidity-410d288dfc2e08c42df58c7e01ad5c332ce92727.tar.gz
dexon-solidity-410d288dfc2e08c42df58c7e01ad5c332ce92727.tar.bz2
dexon-solidity-410d288dfc2e08c42df58c7e01ad5c332ce92727.tar.lz
dexon-solidity-410d288dfc2e08c42df58c7e01ad5c332ce92727.tar.xz
dexon-solidity-410d288dfc2e08c42df58c7e01ad5c332ce92727.tar.zst
dexon-solidity-410d288dfc2e08c42df58c7e01ad5c332ce92727.zip
Merge pull request #4629 from ethereum/mapping_dynamic_key
Dynamic type as mapping key returns error instead of assertion fail
-rw-r--r--Changelog.md1
-rw-r--r--libsolidity/analysis/TypeChecker.cpp16
-rw-r--r--test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_1.sol5
-rw-r--r--test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_2.sol8
-rw-r--r--test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_3.sol8
-rw-r--r--test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_4.sol5
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/mapping_dynamic_key.sol3
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/mapping_dynamic_key_public.sol5
8 files changed, 50 insertions, 1 deletions
diff --git a/Changelog.md b/Changelog.md
index 5a421fa5..1e16df59 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -110,6 +110,7 @@ Bugfixes:
* Type Checker: Fix freeze for negative fixed-point literals very close to ``0``, such as ``-1e-100``.
* Type Checker: Report error when using structs in events without experimental ABIEncoderV2. This used to crash or log the wrong values.
* Type Checker: Report error when using indexed structs in events with experimental ABIEncoderV2. This used to log wrong values.
+ * Type Checker: Dynamic types as key for public mappings return error instead of assertion fail.
* Type System: Allow arbitrary exponents for literals with a mantissa of zero.
### 0.4.24 (2018-05-16)
diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp
index 5033fd63..fca64f6a 100644
--- a/libsolidity/analysis/TypeChecker.cpp
+++ b/libsolidity/analysis/TypeChecker.cpp
@@ -819,7 +819,9 @@ bool TypeChecker::visit(VariableDeclaration const& _variable)
)
m_errorReporter.typeError(_variable.location(), "Internal or recursive type is not allowed for public state variables.");
- if (varType->category() == Type::Category::Array)
+ switch (varType->category())
+ {
+ case Type::Category::Array:
if (auto arrayType = dynamic_cast<ArrayType const*>(varType.get()))
if (
((arrayType->location() == DataLocation::Memory) ||
@@ -827,6 +829,18 @@ bool TypeChecker::visit(VariableDeclaration const& _variable)
!arrayType->validForCalldata()
)
m_errorReporter.typeError(_variable.location(), "Array is too large to be encoded.");
+ break;
+ case Type::Category::Mapping:
+ if (auto mappingType = dynamic_cast<MappingType const*>(varType.get()))
+ if (
+ mappingType->keyType()->isDynamicallySized() &&
+ _variable.visibility() == Declaration::Visibility::Public
+ )
+ m_errorReporter.typeError(_variable.location(), "Dynamically-sized keys for public mappings are not supported.");
+ break;
+ default:
+ break;
+ }
return false;
}
diff --git a/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_1.sol b/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_1.sol
new file mode 100644
index 00000000..ea2d282c
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_1.sol
@@ -0,0 +1,5 @@
+contract c {
+ mapping(uint[] => uint) data;
+}
+// ----
+// ParserError: (26-27): Expected '=>' but got '['
diff --git a/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_2.sol b/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_2.sol
new file mode 100644
index 00000000..713cddeb
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_2.sol
@@ -0,0 +1,8 @@
+contract c {
+ struct S {
+ uint x;
+ }
+ mapping(S => uint) data;
+}
+// ----
+// ParserError: (47-48): Expected elementary type name for mapping key type
diff --git a/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_3.sol b/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_3.sol
new file mode 100644
index 00000000..655af9de
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_3.sol
@@ -0,0 +1,8 @@
+contract c {
+ struct S {
+ string s;
+ }
+ mapping(S => uint) data;
+}
+// ----
+// ParserError: (49-50): Expected elementary type name for mapping key type
diff --git a/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_4.sol b/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_4.sol
new file mode 100644
index 00000000..f4dcb00a
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_4.sol
@@ -0,0 +1,5 @@
+contract c {
+ mapping(string[] => uint) data;
+}
+// ----
+// ParserError: (28-29): Expected '=>' but got '['
diff --git a/test/libsolidity/syntaxTests/types/mapping/mapping_dynamic_key.sol b/test/libsolidity/syntaxTests/types/mapping/mapping_dynamic_key.sol
new file mode 100644
index 00000000..825ee09a
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/mapping_dynamic_key.sol
@@ -0,0 +1,3 @@
+contract c {
+ mapping(string => uint) data;
+}
diff --git a/test/libsolidity/syntaxTests/types/mapping/mapping_dynamic_key_public.sol b/test/libsolidity/syntaxTests/types/mapping/mapping_dynamic_key_public.sol
new file mode 100644
index 00000000..9fb575af
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/mapping_dynamic_key_public.sol
@@ -0,0 +1,5 @@
+contract c {
+ mapping(string => uint) public data;
+}
+// ----
+// TypeError: (14-49): Dynamically-sized keys for public mappings are not supported.