Merge pull request #10240 from ethereum/immutablesPureValue

Immutables with literal values are pure.
This commit is contained in:
Leonardo 2020-11-10 15:23:33 +00:00 committed by GitHub
commit 55ac5de472
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 12 additions and 6 deletions

View File

@ -2,6 +2,7 @@
Language Features: Language Features:
* Ability to select the abi coder using ``pragma abicoder v1`` and ``pragma abicoder v2``. * 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: Compiler Features:
* Command Line Interface: Report error if file could not be read in ``--standard-json`` mode. * Command Line Interface: Report error if file could not be read in ``--standard-json`` mode.

View File

@ -186,7 +186,13 @@ void ViewPureChecker::endVisit(Identifier const& _identifier)
bool writes = _identifier.annotation().willBeWrittenTo; bool writes = _identifier.annotation().willBeWrittenTo;
if (VariableDeclaration const* varDecl = dynamic_cast<VariableDeclaration const*>(declaration)) 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; mutability = writes ? StateMutability::NonPayable : StateMutability::View;
} }
else if (MagicVariableDeclaration const* magicVar = dynamic_cast<MagicVariableDeclaration const*>(declaration)) else if (MagicVariableDeclaration const* magicVar = dynamic_cast<MagicVariableDeclaration const*>(declaration))

View File

@ -2,7 +2,7 @@
"language": "Solidity", "language": "Solidity",
"sources": { "sources": {
"a.sol": { "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": { "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}}} ","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 { contract C {
uint immutable x = 1; uint immutable x = 1;
function readX() internal view returns(uint) { function readX() internal pure returns(uint) {
return x + 3; 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".