Merge pull request #4443 from ethereum/double_var_decl_fix

Fix crash when declaring the same var twice in the same scope
This commit is contained in:
chriseth 2018-07-12 00:05:32 +02:00 committed by GitHub
commit 99bee7e233
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 1 deletions

View File

@ -96,6 +96,11 @@ void DeclarationContainer::activateVariable(ASTString const& _name)
m_invisibleDeclarations.erase(_name);
}
bool DeclarationContainer::isInvisible(ASTString const& _name) const
{
return m_invisibleDeclarations.count(_name);
}
bool DeclarationContainer::registerDeclaration(
Declaration const& _declaration,
ASTString const* _name,

View File

@ -62,6 +62,9 @@ public:
/// VariableDeclarationStatements.
void activateVariable(ASTString const& _name);
/// @returns true if declaration is currently invisible.
bool isInvisible(ASTString const& _name) const;
/// @returns existing declaration names similar to @a _name.
/// Searches this and all parent containers.
std::vector<ASTString> similarNames(ASTString const& _name) const;

View File

@ -156,6 +156,12 @@ bool NameAndTypeResolver::updateDeclaration(Declaration const& _declaration)
void NameAndTypeResolver::activateVariable(string const& _name)
{
solAssert(m_currentScope, "");
// Scoped local variables are invisible before activation.
// When a local variable is activated, its name is removed
// from a scope's invisible variables.
// This is used to avoid activation of variables of same name
// in the same scope (an error is returned).
if (m_currentScope->isInvisible(_name))
m_currentScope->activateVariable(_name);
}

View File

@ -0,0 +1,10 @@
contract test {
function f() pure public {
uint x;
{ uint x; }
uint x;
}
}
// ----
// Warning: (73-79): This declaration shadows an existing declaration.
// DeclarationError: (91-97): Identifier already declared.

View File

@ -0,0 +1,8 @@
contract test {
function f() pure public {
uint x;
uint x;
}
}
// ----
// DeclarationError: (71-77): Identifier already declared.

View File

@ -0,0 +1,16 @@
contract test {
function f() pure public {
uint x;
uint x;
uint x;
uint x;
uint x;
uint x;
}
}
// ----
// DeclarationError: (71-77): Identifier already declared.
// DeclarationError: (87-93): Identifier already declared.
// DeclarationError: (103-109): Identifier already declared.
// DeclarationError: (119-125): Identifier already declared.
// DeclarationError: (135-141): Identifier already declared.