Merge pull request #9740 from ethereum/issue_9629

ViewPureChecker::reportMutability: don't check visibility on constructors.
This commit is contained in:
chriseth 2020-09-07 21:58:33 +02:00 committed by GitHub
commit 38e6f2723d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 5 deletions

View File

@ -9,7 +9,7 @@ Compiler Features:
Bugfixes: Bugfixes:
* Type Checker: Disallow ``virtual`` for modifiers in libraries. * Type Checker: Disallow ``virtual`` for modifiers in libraries.
* ViewPureChecker: Prevent visibility check on constructors.
### 0.7.1 (2020-09-02) ### 0.7.1 (2020-09-02)

View File

@ -261,21 +261,24 @@ void ViewPureChecker::reportMutability(
{ {
// We do not warn for library functions because they cannot be payable anyway. // We do not warn for library functions because they cannot be payable anyway.
// Also internal functions should be allowed to use `msg.value`. // Also internal functions should be allowed to use `msg.value`.
if (m_currentFunction->isPublic() && !m_currentFunction->libraryFunction()) if ((m_currentFunction->isConstructor() || m_currentFunction->isPublic()) && !m_currentFunction->libraryFunction())
{ {
if (_nestedLocation) if (_nestedLocation)
m_errorReporter.typeError( m_errorReporter.typeError(
4006_error, 4006_error,
_location, _location,
SecondarySourceLocation().append("\"msg.value\" or \"callvalue()\" appear here inside the modifier.", *_nestedLocation), SecondarySourceLocation().append("\"msg.value\" or \"callvalue()\" appear here inside the modifier.", *_nestedLocation),
"This modifier uses \"msg.value\" or \"callvalue()\" and thus the function has to be payable or internal." m_currentFunction->isConstructor() ?
"This modifier uses \"msg.value\" or \"callvalue()\" and thus the constructor has to be payable."
: "This modifier uses \"msg.value\" or \"callvalue()\" and thus the function has to be payable or internal."
); );
else else
m_errorReporter.typeError( m_errorReporter.typeError(
5887_error, 5887_error,
_location, _location,
"\"msg.value\" and \"callvalue()\" can only be used in payable public functions. Make the function " m_currentFunction->isConstructor() ?
"\"payable\" or use an internal function to avoid this error." "\"msg.value\" and \"callvalue()\" can only be used in payable constructors. Make the constructor \"payable\" to avoid this error."
: "\"msg.value\" and \"callvalue()\" can only be used in payable public functions. Make the function \"payable\" or use an internal function to avoid this error."
); );
m_errors = true; m_errors = true;
} }

View File

@ -0,0 +1,8 @@
contract C {
uint256 value;
constructor() {
value = msg.value;
}
}
// ----
// TypeError 5887: (68-77): "msg.value" and "callvalue()" can only be used in payable constructors. Make the constructor "payable" to avoid this error.

View File

@ -0,0 +1,7 @@
contract C {
function get() public view returns(uint256) {
return msg.value;
}
}
// ----
// TypeError 5887: (78-87): "msg.value" and "callvalue()" can only be used in payable public functions. Make the function "payable" or use an internal function to avoid this error.