Immutables with literal values are pure.

This commit is contained in:
chriseth 2020-11-09 17:12:29 +01:00
parent 41f5036507
commit 765ed13814
6 changed files with 12 additions and 6 deletions

View File

@ -2,6 +2,7 @@
Language Features:
* Ability to select the abi coder using ``pragma abicoder v1`` and ``pragma abicoder v2``.
* Immutable variables with literal number values are considered pure.
Compiler Features:
* SMTChecker: Add division by zero checks in the CHC engine.

View File

@ -186,7 +186,13 @@ void ViewPureChecker::endVisit(Identifier const& _identifier)
bool writes = _identifier.annotation().willBeWrittenTo;
if (VariableDeclaration const* varDecl = dynamic_cast<VariableDeclaration const*>(declaration))
{
if (varDecl->isStateVariable() && !varDecl->isConstant())
if (varDecl->immutable())
{
// Immutables that are assigned literals are pure.
if (!(varDecl->value() && varDecl->value()->annotation().type->category() == Type::Category::RationalNumber))
mutability = StateMutability::View;
}
else if (varDecl->isStateVariable() && !varDecl->isConstant())
mutability = writes ? StateMutability::NonPayable : StateMutability::View;
}
else if (MagicVariableDeclaration const* magicVar = dynamic_cast<MagicVariableDeclaration const*>(declaration))
@ -291,7 +297,7 @@ void ViewPureChecker::reportMutability(
m_currentFunction->stateMutability() == StateMutability::Pure ||
m_currentFunction->stateMutability() == StateMutability::NonPayable,
""
);
);
}
ViewPureChecker::MutabilityAndLocation const& ViewPureChecker::modifierMutability(

View File

@ -2,7 +2,7 @@
"language": "Solidity",
"sources": {
"a.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0\ncontract A { uint256 immutable x = 1; function f() public view returns (uint256) { return x; } }"
"content": "// SPDX-License-Identifier: GPL-3.0\ncontract A { uint256 immutable x = 1 + 3; function f() public pure returns (uint256) { return x; } }"
}
},
"settings": {

View File

@ -1,2 +1,2 @@
{"contracts":{"a.sol":{"A":{"evm":{"deployedBytecode":{"generatedSources":[],"immutableReferences":{"3":[{"length":32,"start":77}]},"linkReferences":{},"object":"<BYTECODE REMOVED>","opcodes":"<OPCODES REMOVED>","sourceMap":"36:96:0:-:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;74:56;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;108:7;126:1;119:8;;74:56;:::o"}}}}},"errors":[{"component":"general","errorCode":"3420","formattedMessage":"a.sol: Warning: Source file does not specify required compiler version!
{"contracts":{"a.sol":{"A":{"evm":{"deployedBytecode":{"generatedSources":[],"immutableReferences":{"5":[{"length":32,"start":77}]},"linkReferences":{},"object":"<BYTECODE REMOVED>","opcodes":"<OPCODES REMOVED>","sourceMap":"36:100:0:-:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;78:56;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;112:7;130:1;123:8;;78:56;:::o"}}}}},"errors":[{"component":"general","errorCode":"3420","formattedMessage":"a.sol: Warning: Source file does not specify required compiler version!
","message":"Source file does not specify required compiler version!","severity":"warning","sourceLocation":{"end":-1,"file":"a.sol","start":-1},"type":"Warning"}],"sources":{"a.sol":{"id":0}}}

View File

@ -1,7 +1,7 @@
contract C {
uint immutable x = 1;
function readX() internal view returns(uint) {
function readX() internal pure returns(uint) {
return x + 3;
}
}

View File

@ -5,4 +5,3 @@ contract B {
}
}
// ----
// TypeError 2527: (100-101): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".