Merge pull request #8574 from ethereum/immutableAssignAtDecl

Properly handle assignments of immutables at declaration.
This commit is contained in:
chriseth 2020-04-02 10:38:16 +02:00 committed by GitHub
commit c8f0629e94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 2 deletions

View File

@ -550,7 +550,7 @@ void ContractCompiler::initializeStateVariables(ContractDefinition const& _contr
{ {
solAssert(!_contract.isLibrary(), "Tried to initialize state variables of library."); solAssert(!_contract.isLibrary(), "Tried to initialize state variables of library.");
for (VariableDeclaration const* variable: _contract.stateVariables()) for (VariableDeclaration const* variable: _contract.stateVariables())
if (variable->value() && !variable->isConstant() && !variable->immutable()) if (variable->value() && !variable->isConstant())
ExpressionCompiler(m_context, m_optimiserSettings.runOrderLiterals).appendStateVariableInitialization(*variable); ExpressionCompiler(m_context, m_optimiserSettings.runOrderLiterals).appendStateVariableInitialization(*variable);
} }

View File

@ -73,7 +73,10 @@ void ExpressionCompiler::appendStateVariableInitialization(VariableDeclaration c
utils().convertType(*type, *_varDecl.annotation().type); utils().convertType(*type, *_varDecl.annotation().type);
type = _varDecl.annotation().type; type = _varDecl.annotation().type;
} }
StorageItem(m_context, _varDecl).storeValue(*type, _varDecl.location(), true); if (_varDecl.immutable())
ImmutableItem(m_context, _varDecl).storeValue(*type, _varDecl.location(), true);
else
StorageItem(m_context, _varDecl).storeValue(*type, _varDecl.location(), true);
} }
void ExpressionCompiler::appendConstStateVariableAccessor(VariableDeclaration const& _varDecl) void ExpressionCompiler::appendConstStateVariableAccessor(VariableDeclaration const& _varDecl)

View File

@ -183,6 +183,7 @@ void ImmutableItem::setToZero(SourceLocation const&, bool) const
StorageItem::StorageItem(CompilerContext& _compilerContext, VariableDeclaration const& _declaration): StorageItem::StorageItem(CompilerContext& _compilerContext, VariableDeclaration const& _declaration):
StorageItem(_compilerContext, *_declaration.annotation().type) StorageItem(_compilerContext, *_declaration.annotation().type)
{ {
solAssert(!_declaration.immutable(), "");
auto const& location = m_context.storageLocationOfVariable(_declaration); auto const& location = m_context.storageLocationOfVariable(_declaration);
m_context << location.first << u256(location.second); m_context << location.first << u256(location.second);
} }

View File

@ -0,0 +1,8 @@
contract A {
uint8 immutable a = 2;
function f() public view returns (uint) {
return a;
}
}
// ----
// f() -> 2