From 1fa6c71bd0f1de88d9b157f2d2a19bf7985272c8 Mon Sep 17 00:00:00 2001 From: hrkrshnn Date: Mon, 13 Sep 2021 10:02:27 +0200 Subject: [PATCH] Allow Mapping keys to have type UserDefinedValueType. Also added syntax and semantic test. --- .../analysis/DeclarationTypeChecker.cpp | 3 ++- .../userDefinedValueType/mapping_key.sol | 18 ++++++++++++++++++ .../parsing/mapping_nonelementary_key_2.sol | 2 +- .../parsing/mapping_nonelementary_key_3.sol | 2 +- .../types/struct_mapping_recursion.sol | 2 +- .../userDefinedValueType/mapping_key.sol | 4 ++++ 6 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 test/libsolidity/semanticTests/userDefinedValueType/mapping_key.sol create mode 100644 test/libsolidity/syntaxTests/userDefinedValueType/mapping_key.sol diff --git a/libsolidity/analysis/DeclarationTypeChecker.cpp b/libsolidity/analysis/DeclarationTypeChecker.cpp index f67d7d501..b015939e2 100644 --- a/libsolidity/analysis/DeclarationTypeChecker.cpp +++ b/libsolidity/analysis/DeclarationTypeChecker.cpp @@ -253,12 +253,13 @@ void DeclarationTypeChecker::endVisit(Mapping const& _mapping) { case Type::Category::Enum: case Type::Category::Contract: + case Type::Category::UserDefinedValueType: break; default: m_errorReporter.fatalTypeError( 7804_error, typeName->location(), - "Only elementary types, contract types or enums are allowed as mapping keys." + "Only elementary types, user defined value types, contract types or enums are allowed as mapping keys." ); break; } diff --git a/test/libsolidity/semanticTests/userDefinedValueType/mapping_key.sol b/test/libsolidity/semanticTests/userDefinedValueType/mapping_key.sol new file mode 100644 index 000000000..e698308b4 --- /dev/null +++ b/test/libsolidity/semanticTests/userDefinedValueType/mapping_key.sol @@ -0,0 +1,18 @@ +type MyInt is int; +contract C { + mapping(MyInt => int) public m; + function set(MyInt key, int value) external { + m[key] = value; + } + function set_unwrapped(int key, int value) external { + m[MyInt.wrap(key)] = value; + } +} +// ==== +// compileViaYul: also +// ---- +// set(int256,int256): 1, 1 -> +// m(int256): 1 -> 1 +// set_unwrapped(int256,int256): 1, 2 -> +// m(int256): 1 -> 2 +// m(int256): 2 -> 0 diff --git a/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_2.sol b/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_2.sol index 71063e34e..68aa61a41 100644 --- a/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_2.sol +++ b/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_2.sol @@ -5,4 +5,4 @@ contract c { mapping(S => uint) data; } // ---- -// TypeError 7804: (47-48): Only elementary types, contract types or enums are allowed as mapping keys. +// TypeError 7804: (47-48): Only elementary types, user defined value types, contract types or enums are allowed as mapping keys. diff --git a/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_3.sol b/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_3.sol index 34f0e82b3..991f2b622 100644 --- a/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_3.sol +++ b/test/libsolidity/syntaxTests/parsing/mapping_nonelementary_key_3.sol @@ -5,4 +5,4 @@ contract c { mapping(S => uint) data; } // ---- -// TypeError 7804: (49-50): Only elementary types, contract types or enums are allowed as mapping keys. +// TypeError 7804: (49-50): Only elementary types, user defined value types, contract types or enums are allowed as mapping keys. diff --git a/test/libsolidity/syntaxTests/types/struct_mapping_recursion.sol b/test/libsolidity/syntaxTests/types/struct_mapping_recursion.sol index 5dc39266f..be4e1dde0 100644 --- a/test/libsolidity/syntaxTests/types/struct_mapping_recursion.sol +++ b/test/libsolidity/syntaxTests/types/struct_mapping_recursion.sol @@ -6,4 +6,4 @@ contract C { function g (S calldata) external view {} } // ---- -// TypeError 7804: (56-57): Only elementary types, contract types or enums are allowed as mapping keys. +// TypeError 7804: (56-57): Only elementary types, user defined value types, contract types or enums are allowed as mapping keys. diff --git a/test/libsolidity/syntaxTests/userDefinedValueType/mapping_key.sol b/test/libsolidity/syntaxTests/userDefinedValueType/mapping_key.sol new file mode 100644 index 000000000..ae83ae4c7 --- /dev/null +++ b/test/libsolidity/syntaxTests/userDefinedValueType/mapping_key.sol @@ -0,0 +1,4 @@ +type MyInt is int; +contract C { + mapping(MyInt => int) m; +}