Merge pull request #9306 from ethereum/yul-restrict-dot

[BREAKING] Restrict dots in Yul identifiers
This commit is contained in:
chriseth 2020-07-07 15:13:14 +02:00 committed by GitHub
commit 5cfb929e49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 45 additions and 1 deletions

View File

@ -13,7 +13,7 @@ Breaking changes:
Language Features:
* Yul: Disallow EVM instruction `pc()`.
* Yul: Disallow consecutive and trailing dots in identifiers. Leading dots were already disallowed.
Compiler Features:

View File

@ -197,7 +197,10 @@ void AsmAnalyzer::operator()(VariableDeclaration const& _varDecl)
m_currentScope->insideFunction()
);
for (auto const& variable: _varDecl.variables)
{
expectValidIdentifier(variable.name, variable.location);
expectValidType(variable.type, variable.location);
}
if (_varDecl.value)
{
@ -237,11 +240,13 @@ void AsmAnalyzer::operator()(VariableDeclaration const& _varDecl)
void AsmAnalyzer::operator()(FunctionDefinition const& _funDef)
{
yulAssert(!_funDef.name.empty(), "");
expectValidIdentifier(_funDef.name, _funDef.location);
Block const* virtualBlock = m_info.virtualBlocks.at(&_funDef).get();
yulAssert(virtualBlock, "");
Scope& varScope = scope(virtualBlock);
for (auto const& var: _funDef.parameters + _funDef.returnVariables)
{
expectValidIdentifier(var.name, var.location);
expectValidType(var.type, var.location);
m_activeVariables.insert(&std::get<Scope::Variable>(varScope.identifiers.at(var.name)));
}
@ -517,6 +522,26 @@ Scope& AsmAnalyzer::scope(Block const* _block)
return *scopePtr;
}
void AsmAnalyzer::expectValidIdentifier(YulString _identifier, SourceLocation const& _location)
{
// NOTE: the leading dot case is handled by the parser not allowing it.
if (boost::ends_with(_identifier.str(), "."))
m_errorReporter.syntaxError(
3384_error,
_location,
"\"" + _identifier.str() + "\" is not a valid identifier (ends with a dot)."
);
if (_identifier.str().find("..") != std::string::npos)
m_errorReporter.syntaxError(
7771_error,
_location,
"\"" + _identifier.str() + "\" is not a valid identifier (contains consecutive dots)."
);
}
void AsmAnalyzer::expectValidType(YulString _type, SourceLocation const& _location)
{
if (!m_dialect.types.count(_type))

View File

@ -108,6 +108,7 @@ private:
void checkAssignment(Identifier const& _variable, YulString _valueType);
Scope& scope(Block const* _block);
void expectValidIdentifier(YulString _identifier, langutil::SourceLocation const& _location);
void expectValidType(YulString _type, langutil::SourceLocation const& _location);
void expectType(YulString _expectedType, YulString _givenType, langutil::SourceLocation const& _location);

View File

@ -4,3 +4,9 @@
_...(a...)
}
// ----
// SyntaxError 3384: (6-27): "_..." is not a valid identifier (ends with a dot).
// SyntaxError 7771: (6-27): "_..." is not a valid identifier (contains consecutive dots).
// SyntaxError 3384: (20-23): "$.." is not a valid identifier (ends with a dot).
// SyntaxError 7771: (20-23): "$.." is not a valid identifier (contains consecutive dots).
// SyntaxError 3384: (36-40): "a..." is not a valid identifier (ends with a dot).
// SyntaxError 7771: (36-40): "a..." is not a valid identifier (contains consecutive dots).

View File

@ -2,3 +2,4 @@
function x..y() {}
}
// ----
// SyntaxError 7771: (6-24): "x..y" is not a valid identifier (contains consecutive dots).

View File

@ -2,3 +2,4 @@
function x(a..b) {}
}
// ----
// SyntaxError 7771: (17-21): "a..b" is not a valid identifier (contains consecutive dots).

View File

@ -2,3 +2,4 @@
function x() -> a..b {}
}
// ----
// SyntaxError 7771: (22-26): "a..b" is not a valid identifier (contains consecutive dots).

View File

@ -2,3 +2,4 @@
let a..b := 1
}
// ----
// SyntaxError 7771: (10-14): "a..b" is not a valid identifier (contains consecutive dots).

View File

@ -2,3 +2,4 @@
function x...y() {}
}
// ----
// SyntaxError 7771: (6-25): "x...y" is not a valid identifier (contains consecutive dots).

View File

@ -2,3 +2,4 @@
function x(a...b) {}
}
// ----
// SyntaxError 7771: (17-22): "a...b" is not a valid identifier (contains consecutive dots).

View File

@ -2,3 +2,4 @@
function x() -> a...b {}
}
// ----
// SyntaxError 7771: (22-27): "a...b" is not a valid identifier (contains consecutive dots).

View File

@ -2,3 +2,4 @@
let a...b := 1
}
// ----
// SyntaxError 7771: (10-15): "a...b" is not a valid identifier (contains consecutive dots).

View File

@ -2,3 +2,4 @@
function x.() {}
}
// ----
// SyntaxError 3384: (6-22): "x." is not a valid identifier (ends with a dot).

View File

@ -2,3 +2,4 @@
function x(a.) {}
}
// ----
// SyntaxError 3384: (17-19): "a." is not a valid identifier (ends with a dot).

View File

@ -2,3 +2,4 @@
function x() -> a. {}
}
// ----
// SyntaxError 3384: (22-24): "a." is not a valid identifier (ends with a dot).

View File

@ -2,3 +2,4 @@
let a. := 1
}
// ----
// SyntaxError 3384: (10-12): "a." is not a valid identifier (ends with a dot).