diff --git a/libsolidity/AST.cpp b/libsolidity/AST.cpp index 683452027..0dbad433f 100644 --- a/libsolidity/AST.cpp +++ b/libsolidity/AST.cpp @@ -262,19 +262,6 @@ void StructDefinition::checkRecursion() const } } -void EnumDefinition::checkValidityOfMembers() const -{ - vector> members(getMembers()); - auto compareDecls = [](ASTPointer a, ASTPointer b) - { - return a->getName() < b->getName(); - }; - sort(begin(members), end(members), compareDecls); - for (size_t i = 0; i < members.size() - 1; ++i) - if (members[i]->getName() == members[i + 1]->getName()) - BOOST_THROW_EXCEPTION(members[i]->createTypeError("Duplicate member detected in Enum")); -} - TypePointer EnumDefinition::getType(ContractDefinition const*) const { return make_shared(make_shared(*this)); diff --git a/libsolidity/AST.h b/libsolidity/AST.h index 4167537e5..51d8031a3 100644 --- a/libsolidity/AST.h +++ b/libsolidity/AST.h @@ -333,11 +333,7 @@ public: virtual TypePointer getType(ContractDefinition const*) const override; - /// Checks that the members do not include any duplicate names - void checkValidityOfMembers() const; - private: - std::vector> m_members; }; diff --git a/libsolidity/ExpressionCompiler.cpp b/libsolidity/ExpressionCompiler.cpp index 63324fe58..29ec3d577 100644 --- a/libsolidity/ExpressionCompiler.cpp +++ b/libsolidity/ExpressionCompiler.cpp @@ -498,12 +498,10 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess) case Type::Category::TypeType: { TypeType const& type = dynamic_cast(*_memberAccess.getExpression().getType()); - ContractType const* contractType; - EnumType const* enumType; if (!type.getMembers().getMemberType(member)) - BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid member access to " + type.toString())); + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid member access to " + type.toString())); - if ((contractType = dynamic_cast(type.getActualType().get()))) + if (auto contractType = dynamic_cast(type.getActualType().get())) { ContractDefinition const& contract = contractType->getContractDefinition(); for (ASTPointer const& function: contract.getDefinedFunctions()) @@ -513,12 +511,9 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess) return; } } - else if ((enumType = dynamic_cast(type.getActualType().get()))) - { + else if (auto enumType = dynamic_cast(type.getActualType().get())) m_context << enumType->getMemberValue(_memberAccess.getMemberName()); - return; - } - + break; } case Type::Category::ByteArray: { diff --git a/libsolidity/NameAndTypeResolver.cpp b/libsolidity/NameAndTypeResolver.cpp index 859d0097c..dbe5693a8 100644 --- a/libsolidity/NameAndTypeResolver.cpp +++ b/libsolidity/NameAndTypeResolver.cpp @@ -81,8 +81,6 @@ void NameAndTypeResolver::checkTypeRequirements(ContractDefinition& _contract) { for (ASTPointer const& structDef: _contract.getDefinedStructs()) structDef->checkValidityOfMembers(); - for (ASTPointer const& enumDef: _contract.getDefinedEnums()) - enumDef->checkValidityOfMembers(); _contract.checkTypeRequirements(); } @@ -236,6 +234,12 @@ void DeclarationRegistrationHelper::endVisit(EnumDefinition&) closeCurrentScope(); } +bool DeclarationRegistrationHelper::visit(EnumValue& _value) +{ + registerDeclaration(_value, false); + return true; +} + bool DeclarationRegistrationHelper::visit(FunctionDefinition& _function) { registerDeclaration(_function, true); diff --git a/libsolidity/NameAndTypeResolver.h b/libsolidity/NameAndTypeResolver.h index 4b1b8c0cd..d9ac98ce5 100644 --- a/libsolidity/NameAndTypeResolver.h +++ b/libsolidity/NameAndTypeResolver.h @@ -100,6 +100,7 @@ private: void endVisit(StructDefinition& _struct) override; bool visit(EnumDefinition& _enum) override; void endVisit(EnumDefinition& _enum) override; + bool visit(EnumValue& _value) override; bool visit(FunctionDefinition& _function) override; void endVisit(FunctionDefinition& _function) override; bool visit(ModifierDefinition& _modifier) override; diff --git a/test/SolidityNameAndTypeResolution.cpp b/test/SolidityNameAndTypeResolution.cpp index ec49a42d1..d6e4ed516 100644 --- a/test/SolidityNameAndTypeResolution.cpp +++ b/test/SolidityNameAndTypeResolution.cpp @@ -1080,7 +1080,7 @@ BOOST_AUTO_TEST_CASE(enum_duplicate_values) enum ActionChoices { GoLeft, GoRight, GoLeft, Sit } } )"; - BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); + BOOST_CHECK_THROW(parseTextAndResolveNames(text), DeclarationError); } BOOST_AUTO_TEST_SUITE_END()