mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #10240 from ethereum/immutablesPureValue
Immutables with literal values are pure.
This commit is contained in:
commit
55ac5de472
@ -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:
|
||||
* Command Line Interface: Report error if file could not be read in ``--standard-json`` mode.
|
||||
|
@ -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))
|
||||
|
@ -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": {
|
||||
|
@ -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}}}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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".
|
||||
|
Loading…
Reference in New Issue
Block a user