From e07274a96f6e53a995d18cc92526239b9f20eb01 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Thu, 6 Feb 2020 09:08:28 +0100 Subject: [PATCH] Disallow libraries as mapping keys. --- libsolidity/analysis/TypeChecker.cpp | 13 +++++++++---- .../mapping}/contract_mapping.sol | 0 .../mapping}/contract_mapping_invalid.sol | 0 .../{mappings => types/mapping}/enum_mapping.sol | 0 .../mapping}/enum_mapping_invalid.sol | 0 .../syntaxTests/types/mapping/library_mapping.sol | 4 ++++ 6 files changed, 13 insertions(+), 4 deletions(-) rename test/libsolidity/syntaxTests/{mappings => types/mapping}/contract_mapping.sol (100%) rename test/libsolidity/syntaxTests/{mappings => types/mapping}/contract_mapping_invalid.sol (100%) rename test/libsolidity/syntaxTests/{mappings => types/mapping}/enum_mapping.sol (100%) rename test/libsolidity/syntaxTests/{mappings => types/mapping}/enum_mapping_invalid.sol (100%) create mode 100644 test/libsolidity/syntaxTests/types/mapping/library_mapping.sol diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index f09c38832..4590bcdc2 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -2880,10 +2880,15 @@ bool TypeChecker::visit(Mapping const& _mapping) { if (auto const* keyType = dynamic_cast(&_mapping.keyType())) { - if ( - keyType->annotation().type->category() != Type::Category::Contract && - keyType->annotation().type->category() != Type::Category::Enum - ) + if (auto const* contractType = dynamic_cast(keyType->annotation().type)) + { + if (contractType->contractDefinition().isLibrary()) + m_errorReporter.typeError( + keyType->location(), + "Library types cannot be used as mapping keys." + ); + } + else if (keyType->annotation().type->category() != Type::Category::Enum) m_errorReporter.typeError( keyType->location(), "Only elementary types, contract types or enums are allowed as mapping keys." diff --git a/test/libsolidity/syntaxTests/mappings/contract_mapping.sol b/test/libsolidity/syntaxTests/types/mapping/contract_mapping.sol similarity index 100% rename from test/libsolidity/syntaxTests/mappings/contract_mapping.sol rename to test/libsolidity/syntaxTests/types/mapping/contract_mapping.sol diff --git a/test/libsolidity/syntaxTests/mappings/contract_mapping_invalid.sol b/test/libsolidity/syntaxTests/types/mapping/contract_mapping_invalid.sol similarity index 100% rename from test/libsolidity/syntaxTests/mappings/contract_mapping_invalid.sol rename to test/libsolidity/syntaxTests/types/mapping/contract_mapping_invalid.sol diff --git a/test/libsolidity/syntaxTests/mappings/enum_mapping.sol b/test/libsolidity/syntaxTests/types/mapping/enum_mapping.sol similarity index 100% rename from test/libsolidity/syntaxTests/mappings/enum_mapping.sol rename to test/libsolidity/syntaxTests/types/mapping/enum_mapping.sol diff --git a/test/libsolidity/syntaxTests/mappings/enum_mapping_invalid.sol b/test/libsolidity/syntaxTests/types/mapping/enum_mapping_invalid.sol similarity index 100% rename from test/libsolidity/syntaxTests/mappings/enum_mapping_invalid.sol rename to test/libsolidity/syntaxTests/types/mapping/enum_mapping_invalid.sol diff --git a/test/libsolidity/syntaxTests/types/mapping/library_mapping.sol b/test/libsolidity/syntaxTests/types/mapping/library_mapping.sol new file mode 100644 index 000000000..b2512b47b --- /dev/null +++ b/test/libsolidity/syntaxTests/types/mapping/library_mapping.sol @@ -0,0 +1,4 @@ +library L {} +contract C { mapping(L => bool) i; } +// ---- +// TypeError: (34-35): Library types cannot be used as mapping keys.