diff --git a/libsolidity/ExpressionCompiler.cpp b/libsolidity/ExpressionCompiler.cpp index 6a2e185c5..470fd7c58 100644 --- a/libsolidity/ExpressionCompiler.cpp +++ b/libsolidity/ExpressionCompiler.cpp @@ -48,12 +48,23 @@ void ExpressionCompiler::appendStateVariableInitialization(VariableDeclaration c { if (!_varDecl.getValue()) return; - solAssert(!!_varDecl.getValue()->getType(), "Type information not available."); + TypePointer type = _varDecl.getValue()->getType(); + solAssert(!!type, "Type information not available."); CompilerContext::LocationSetter locationSetter(m_context, _varDecl); _varDecl.getValue()->accept(*this); - utils().convertType(*_varDecl.getValue()->getType(), *_varDecl.getType(), true); - StorageItem(m_context, _varDecl).storeValue(*_varDecl.getType(), _varDecl.getLocation(), true); + if (_varDecl.getType()->dataStoredIn(DataLocation::Storage)) + { + // reference type, only convert value to mobile type and do final conversion in storeValue. + utils().convertType(*type, *type->mobileType()); + type = type->mobileType(); + } + else + { + utils().convertType(*type, *_varDecl.getType()); + type = _varDecl.getType(); + } + StorageItem(m_context, _varDecl).storeValue(*type, _varDecl.getLocation(), true); } void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& _varDecl) diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 9f806347e..113c76a08 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -5033,6 +5033,21 @@ BOOST_AUTO_TEST_CASE(literal_strings) BOOST_CHECK(callContractFunction("empty()") == encodeDyn(string())); } +BOOST_AUTO_TEST_CASE(initialise_string_constant) +{ + char const* sourceCode = R"( + contract Test { + string public short = "abcdef"; + string public long = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789001234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789001234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; + } + )"; + compileAndRun(sourceCode, 0, "Test"); + string longStr = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789001234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789001234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; + string shortStr = "abcdef"; + + BOOST_CHECK(callContractFunction("long()") == encodeDyn(longStr)); + BOOST_CHECK(callContractFunction("short()") == encodeDyn(shortStr)); +} BOOST_AUTO_TEST_SUITE_END()