Disallow declarations to have names "this", "super" and "_".

There will be a declaration error 3726 if these names are used in the contract. Note that there is
an existing warning about shadowing a built-in for "this" and "super".
This commit is contained in:
hrkrshnn 2020-11-11 12:10:25 +01:00
parent fb2d071b70
commit 1a6f0fe52f
2 changed files with 34 additions and 0 deletions

View File

@ -509,6 +509,33 @@ bool DeclarationRegistrationHelper::registerDeclaration(
// We use "invisible" for both inactive variables in blocks and for members invisible in contracts.
// They cannot both be true at the same time.
solAssert(!(_inactive && !_declaration.isVisibleInContract()), "");
static set<string> illegalNames{"_", "super", "this"};
if (illegalNames.count(name))
{
auto isPublicFunctionOrEvent = [](Declaration const* _d) -> bool
{
if (auto functionDefinition = dynamic_cast<FunctionDefinition const*>(_d))
{
if (!functionDefinition->isFree() && functionDefinition->isPublic())
return true;
}
else if (dynamic_cast<EventDefinition const*>(_d))
return true;
return false;
};
// We allow an exception for public functions or events.
if (!isPublicFunctionOrEvent(&_declaration))
_errorReporter.declarationError(
3726_error,
*_errorLocation,
"The name \"" + name + "\" is reserved."
);
}
if (!_container.registerDeclaration(_declaration, _name, _errorLocation, !_declaration.isVisibleInContract() || _inactive, false))
{
SourceLocation firstDeclarationLocation;

View File

@ -380,4 +380,11 @@ void ReferencesResolver::validateYulIdentifierName(yul::YulString _name, SourceL
_location,
"User-defined identifiers in inline assembly cannot contain '.'."
);
if (set<string>{"this", "super", "_"}.count(_name.str()))
m_errorReporter.declarationError(
4113_error,
_location,
"The identifier name \"" + _name.str() + "\" is reserved."
);
}