mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #2528 from ethereum/warnNoStorage
Warn if local storage reference variable does not use "storage" explicitly.
This commit is contained in:
commit
4bde6fa961
@ -1,5 +1,8 @@
|
|||||||
### 0.4.13 (unreleased)
|
### 0.4.13 (unreleased)
|
||||||
|
|
||||||
|
Features:
|
||||||
|
* Type Checker: Warn if a local storage reference variable does not explicitly use the keyword ``storage``.
|
||||||
|
|
||||||
Bugfixes:
|
Bugfixes:
|
||||||
* Compiler Interface: Only output AST if analysis was successful.
|
* Compiler Interface: Only output AST if analysis was successful.
|
||||||
* Code Generator: Correctly unregister modifier variables.
|
* Code Generator: Correctly unregister modifier variables.
|
||||||
|
@ -289,7 +289,20 @@ void ReferencesResolver::endVisit(VariableDeclaration const& _variable)
|
|||||||
typeLoc = DataLocation::Memory;
|
typeLoc = DataLocation::Memory;
|
||||||
}
|
}
|
||||||
else if (varLoc == Location::Default)
|
else if (varLoc == Location::Default)
|
||||||
typeLoc = _variable.isCallableParameter() ? DataLocation::Memory : DataLocation::Storage;
|
{
|
||||||
|
if (_variable.isCallableParameter())
|
||||||
|
typeLoc = DataLocation::Memory;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
typeLoc = DataLocation::Storage;
|
||||||
|
if (!_variable.isStateVariable())
|
||||||
|
m_errorReporter.warning(
|
||||||
|
_variable.location(),
|
||||||
|
"Variable is declared as a storage pointer. "
|
||||||
|
"Use an explicit \"storage\" keyword to silence this warning."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
typeLoc = varLoc == Location::Memory ? DataLocation::Memory : DataLocation::Storage;
|
typeLoc = varLoc == Location::Memory ? DataLocation::Memory : DataLocation::Storage;
|
||||||
isPointer = !_variable.isStateVariable();
|
isPointer = !_variable.isStateVariable();
|
||||||
|
@ -854,10 +854,12 @@ bool TypeChecker::visit(VariableDeclarationStatement const& _statement)
|
|||||||
if (auto ref = dynamic_cast<ReferenceType const*>(type(varDecl).get()))
|
if (auto ref = dynamic_cast<ReferenceType const*>(type(varDecl).get()))
|
||||||
{
|
{
|
||||||
if (ref->dataStoredIn(DataLocation::Storage))
|
if (ref->dataStoredIn(DataLocation::Storage))
|
||||||
m_errorReporter.warning(
|
{
|
||||||
varDecl.location(),
|
string errorText{"Uninitialized storage pointer."};
|
||||||
"Uninitialized storage pointer. Did you mean '<type> memory " + varDecl.name() + "'?"
|
if (varDecl.referenceLocation() == VariableDeclaration::Location::Default)
|
||||||
);
|
errorText += " Did you mean '<type> memory " + varDecl.name() + "'?";
|
||||||
|
m_errorReporter.warning(varDecl.location(), errorText);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (dynamic_cast<MappingType const*>(type(varDecl).get()))
|
else if (dynamic_cast<MappingType const*>(type(varDecl).get()))
|
||||||
m_errorReporter.typeError(
|
m_errorReporter.typeError(
|
||||||
|
@ -2817,7 +2817,7 @@ BOOST_AUTO_TEST_CASE(uninitialized_mapping_array_variable)
|
|||||||
char const* sourceCode = R"(
|
char const* sourceCode = R"(
|
||||||
contract C {
|
contract C {
|
||||||
function f() {
|
function f() {
|
||||||
mapping(uint => uint)[] x;
|
mapping(uint => uint)[] storage x;
|
||||||
x;
|
x;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3103,7 +3103,7 @@ BOOST_AUTO_TEST_CASE(non_initialized_references)
|
|||||||
}
|
}
|
||||||
function f()
|
function f()
|
||||||
{
|
{
|
||||||
s x;
|
s storage x;
|
||||||
x.a = 2;
|
x.a = 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -6144,6 +6144,32 @@ BOOST_AUTO_TEST_CASE(shadowing_warning_can_be_removed)
|
|||||||
CHECK_SUCCESS_NO_WARNINGS(text);
|
CHECK_SUCCESS_NO_WARNINGS(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(warn_unspecified_storage)
|
||||||
|
{
|
||||||
|
char const* text = R"(
|
||||||
|
contract C {
|
||||||
|
struct S { uint a; }
|
||||||
|
S x;
|
||||||
|
function f() {
|
||||||
|
S storage y = x;
|
||||||
|
y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
CHECK_SUCCESS_NO_WARNINGS(text);
|
||||||
|
text = R"(
|
||||||
|
contract C {
|
||||||
|
struct S { uint a; }
|
||||||
|
S x;
|
||||||
|
function f() {
|
||||||
|
S y = x;
|
||||||
|
y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
CHECK_WARNING(text, "is declared as a storage pointer. Use an explicit \"storage\" keyword to silence this warning");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user