diff options
author | Alex Beregszaszi <alex@rtfs.hu> | 2017-07-20 04:24:26 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-20 04:24:26 +0800 |
commit | 1274108ac7f209247683c56d75cf7a1a56db7f60 (patch) | |
tree | 133a649aa1c2f25545aa81262bf6f72042bee403 | |
parent | 84f8e7a46712f7da46ac15d378f0de0491278363 (diff) | |
parent | a2a759f79288c95467baf965871891bc59864340 (diff) | |
download | dexon-solidity-1274108ac7f209247683c56d75cf7a1a56db7f60.tar.gz dexon-solidity-1274108ac7f209247683c56d75cf7a1a56db7f60.tar.zst dexon-solidity-1274108ac7f209247683c56d75cf7a1a56db7f60.zip |
Merge pull request #2605 from roadriverrail/this-in-constructor
This in constructor
-rw-r--r-- | Changelog.md | 1 | ||||
-rw-r--r-- | libsolidity/analysis/StaticAnalyzer.cpp | 9 | ||||
-rw-r--r-- | libsolidity/analysis/StaticAnalyzer.h | 6 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 16 |
4 files changed, 31 insertions, 1 deletions
diff --git a/Changelog.md b/Changelog.md index 91ec7d0e..36186462 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,6 +7,7 @@ Features: * Type Checker: Disallow value transfers to contracts without a payable fallback function. * Type Checker: Include types in explicit conversion error message. * Type Checker: Raise proper error for arrays too large for ABI encoding. + * Type checker: Warn if using ``this`` in a constructor. Bugfixes: * Type Checker: Fix invalid "specify storage keyword" warning for reference members of structs. diff --git a/libsolidity/analysis/StaticAnalyzer.cpp b/libsolidity/analysis/StaticAnalyzer.cpp index b1b31163..46477e1e 100644 --- a/libsolidity/analysis/StaticAnalyzer.cpp +++ b/libsolidity/analysis/StaticAnalyzer.cpp @@ -38,12 +38,14 @@ bool StaticAnalyzer::analyze(SourceUnit const& _sourceUnit) bool StaticAnalyzer::visit(ContractDefinition const& _contract) { m_library = _contract.isLibrary(); + m_currentContract = &_contract; return true; } void StaticAnalyzer::endVisit(ContractDefinition const&) { m_library = false; + m_currentContract = nullptr; } bool StaticAnalyzer::visit(FunctionDefinition const& _function) @@ -54,6 +56,7 @@ bool StaticAnalyzer::visit(FunctionDefinition const& _function) solAssert(!m_currentFunction, ""); solAssert(m_localVarUseCount.empty(), ""); m_nonPayablePublic = _function.isPublic() && !_function.isPayable(); + m_constructor = _function.isConstructor(); return true; } @@ -61,6 +64,7 @@ void StaticAnalyzer::endVisit(FunctionDefinition const&) { m_currentFunction = nullptr; m_nonPayablePublic = false; + m_constructor = false; for (auto const& var: m_localVarUseCount) if (var.second == 0) m_errorReporter.warning(var.first->location(), "Unused local variable"); @@ -131,6 +135,11 @@ bool StaticAnalyzer::visit(MemberAccess const& _memberAccess) "\"callcode\" has been deprecated in favour of \"delegatecall\"." ); + if (m_constructor && m_currentContract) + if (ContractType const* type = dynamic_cast<ContractType const*>(_memberAccess.expression().annotation().type.get())) + if (type->contractDefinition() == *m_currentContract) + m_errorReporter.warning(_memberAccess.location(), "\"this\" used in constructor."); + return true; } diff --git a/libsolidity/analysis/StaticAnalyzer.h b/libsolidity/analysis/StaticAnalyzer.h index cd6913b5..21a487df 100644 --- a/libsolidity/analysis/StaticAnalyzer.h +++ b/libsolidity/analysis/StaticAnalyzer.h @@ -77,6 +77,12 @@ private: std::map<VariableDeclaration const*, int> m_localVarUseCount; FunctionDefinition const* m_currentFunction = nullptr; + + /// Flag that indicates a constructor. + bool m_constructor = false; + + /// Current contract. + ContractDefinition const* m_currentContract = nullptr; }; } diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 649e7970..37d0f107 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -4506,7 +4506,7 @@ BOOST_AUTO_TEST_CASE(var_handle_divided_integers) } } )"; - CHECK_SUCCESS(text); + CHECK_SUCCESS(text); } BOOST_AUTO_TEST_CASE(rational_bitnot_unary_operation) @@ -6373,6 +6373,20 @@ BOOST_AUTO_TEST_CASE(modifiers_access_storage_pointer) CHECK_SUCCESS_NO_WARNINGS(text); } +BOOST_AUTO_TEST_CASE(using_this_in_constructor) +{ + char const* text = R"( + contract C { + function C() { + this.f(); + } + function f() { + } + } + )"; + CHECK_WARNING(text, "\"this\" used in constructor"); +} + BOOST_AUTO_TEST_SUITE_END() } |