From 0819e31de88fa9f99dcffc70fd6cd6de8ee8ae4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Mon, 18 Sep 2023 14:01:32 +0200 Subject: [PATCH] Reject duplicate built-in type definitions --- .../analysis/TypeRegistration.cpp | 20 ++++++++++++++++++- .../experimental/analysis/TypeRegistration.h | 1 + .../builtin_type_definition_duplicate.sol | 17 ++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/libsolidity/syntaxTests/experimental/builtin/builtin_type_definition_duplicate.sol diff --git a/libsolidity/experimental/analysis/TypeRegistration.cpp b/libsolidity/experimental/analysis/TypeRegistration.cpp index 78f3a8131..43fd8d117 100644 --- a/libsolidity/experimental/analysis/TypeRegistration.cpp +++ b/libsolidity/experimental/analysis/TypeRegistration.cpp @@ -172,7 +172,25 @@ void TypeRegistration::endVisit(TypeDefinition const& _typeDefinition) return; if (auto const* builtin = dynamic_cast(_typeDefinition.typeExpression())) - annotation(_typeDefinition).typeConstructor = annotation(*builtin).typeConstructor; + { + auto [previousDefinitionIt, inserted] = annotation().builtinTypeDefinitions.try_emplace( + builtin->nameParameter(), + &_typeDefinition + ); + + if (inserted) + annotation(_typeDefinition).typeConstructor = annotation(*builtin).typeConstructor; + else + { + auto const& [builtinName, previousDefinition] = *previousDefinitionIt; + m_errorReporter.typeError( + 9609_error, + _typeDefinition.location(), + SecondarySourceLocation{}.append("Previous definition:", previousDefinition->location()), + "Duplicate builtin type definition." + ); + } + } else annotation(_typeDefinition).typeConstructor = m_typeSystem.declareTypeConstructor( _typeDefinition.name(), diff --git a/libsolidity/experimental/analysis/TypeRegistration.h b/libsolidity/experimental/analysis/TypeRegistration.h index e8b087daa..9bc49a29c 100644 --- a/libsolidity/experimental/analysis/TypeRegistration.h +++ b/libsolidity/experimental/analysis/TypeRegistration.h @@ -42,6 +42,7 @@ public: { std::map primitiveClassInstantiations; std::map builtinClassInstantiations; + std::map builtinTypeDefinitions; }; TypeRegistration(Analysis& _analysis); diff --git a/test/libsolidity/syntaxTests/experimental/builtin/builtin_type_definition_duplicate.sol b/test/libsolidity/syntaxTests/experimental/builtin/builtin_type_definition_duplicate.sol new file mode 100644 index 000000000..28f6c39fd --- /dev/null +++ b/test/libsolidity/syntaxTests/experimental/builtin/builtin_type_definition_duplicate.sol @@ -0,0 +1,17 @@ +pragma experimental solidity; + +type void1 = __builtin("void"); +type void2 = __builtin("void"); + +type word1 = __builtin("word"); +type word2 = __builtin("word"); + +type fun1(T, U) = __builtin("fun"); +type fun2(T, U) = __builtin("fun"); +// ==== +// EVMVersion: >=constantinople +// ---- +// Warning 2264: (0-29): Experimental features are turned on. Do not use experimental features on live deployments. +// TypeError 9609: (63-94): Duplicate builtin type definition. +// TypeError 9609: (128-159): Duplicate builtin type definition. +// TypeError 9609: (197-232): Duplicate builtin type definition.