Error out when contracts collide on name

The previous behaviour, courtesy of the [] operator in std::map, would
uncritically store a new ContractDefinition in m_contracts even when a
ContractDefinition already existed.  This "resolved" collissions on contract
names by clobbering the original one with the new one, and could lead to
scenarios where the clobber would only be discovered when the original
ContractDefinition could not be found or referred to, which was an unhelpful
InternalCompilerError.

This change checks the m_contracts map for a collision first and will not let
the ContractDefinition be changed to a new one once it's set, throwing a
CompilerError with information about the conflict.
This commit is contained in:
Rhett Aultman 2016-11-18 07:37:15 -08:00 committed by Rhett Aultman
parent 79e5772b8a
commit f3a84eab91

View File

@ -180,6 +180,15 @@ bool CompilerStack::parse()
if (!resolver.updateDeclaration(*m_globalContext->currentThis())) return false;
if (!resolver.updateDeclaration(*m_globalContext->currentSuper())) return false;
if (!resolver.resolveNamesAndTypes(*contract)) return false;
if (m_contracts.find(contract->name()) != m_contracts.end())
{
const ContractDefinition* existingContract = m_contracts.find(contract->name())->second.contract;
if (contract != existingContract)
BOOST_THROW_EXCEPTION(CompilerError() <<
errinfo_sourceLocation(contract->location()) <<
errinfo_comment(contract->name() + " conflicts with contract at "
+ *(existingContract->location().sourceName)));
}
m_contracts[contract->name()].contract = contract;
}
@ -201,6 +210,16 @@ bool CompilerStack::parse()
else
noErrors = false;
if (m_contracts.find(contract->name()) != m_contracts.end())
{
const ContractDefinition* existingContract = m_contracts.find(contract->name())->second.contract;
if (contract != existingContract)
BOOST_THROW_EXCEPTION(CompilerError() <<
errinfo_sourceLocation(contract->location()) <<
errinfo_comment(contract->name() + " conflicts with!!! contract at "
+ *(existingContract->location().sourceName)));
}
m_contracts[contract->name()].contract = contract;
}