mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
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:
parent
fb2d071b70
commit
1a6f0fe52f
@ -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;
|
||||
|
@ -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."
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user