-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
immutable state variables are not considered pure in inheritance #9554
Comments
While I said in the chat that constant and immutable should have the same mutability, maybe we should think a bit harder about this - at least the value of an immutable variable can change once across its lifetime is not a compile-time constant. |
Although I of course agree that this is very annoying. If we conclude that they should be |
There seems to be a larger issue here. The compiler should not throw an error if a pure function references an immutable variable, since So either immutable variables result in inline constants (literals), or they don't. |
May be related. Immutables could be used as
I'm not sure if there is a real use-case here though i.e., using immutables as higher-order functions. |
@bshastry But the documentation I quoted gives the impression that
|
Has this been implemented for immutable variables with literal number values only? I am still getting an error compiling with 0.7.6 testing with an address type value. |
I'm sorry, but I fear that addresses do not work, only numbers that are not addresses. |
Why is this issue closed? This still occurs in Solidity 0.8.4 |
@CodeForcer we concluded that not all immutables can be pure, because their value can depend on constructor input. Can you give a specific code example? |
@chriseth What does For example, in
the Can you give an example where reading an immutable variable reads storage? Or are you suggesting that the above definition of |
|
@chriseth This is the first I hear of that definition. "Functions can be declared pure in which case they promise not to read from or modify the state." from https://docs.soliditylang.org/en/v0.8.9/contracts.html?highlight=pure#pure-functions And this has been defined this way for quite a long time. I am not sure why what is in the pre-deployed source code matters. Solidity interfaces, as used by off-chain clients and, more importantly, by other contracts, just care that Whatever the bytecode looks like before it appears on-chain, at an address, seems completely irrelevant. Since the contract only exists post-constructor, the state mutability is implied to be defined for such a deployed contract. |
It seems this was only part of the initial issue description and not added to the documentation. After all, this only became relevant after we introduced |
Added information to the documentation: #12256 |
Immutables end up as in-line litterals. The on-chain byte code and cost for the following
There 4 In #992, you state:
Thus, by that definition, the above 4 contracts all have pure It hurts no one to allow pure functions to contain immutables, and only results in objective technical accuracy, rather than subjective semantics.
Can you point me to where this is defined anywhere in Solidity/EVM literature or among Solidity developers? This is honestly the first time I ever hear of this. Pure implies that the output is dependant without any state, other than the functions themselves. |
Exactly that is what it is. In the example the value of the immutable depends on the blockchain state. A copy of the contract deployed at a different time will have a different value. The idea of a pure function is that if you compile it again and put it in a different contract, it will return the same value, which it does not if it accesses the immutable in the example above. |
Feel like there should be something in-between it would be useful to have a another modifier that ensure these function only access bytecode and nothing else. the |
With Solidity 0.7.0 a
pure
getter implemented via animmutable
state variable throwsTypeError: Overriding public state variable changes state mutability from "pure" to "view”.
This is probably coming from the "Disallow public state variables overwriting pure functions" breaking change however
immutable
s should be excluded from this as they can be consideredpure
. I tested withconstant
instead ofimmutable
and this works okay when exposed throughpure
.The text was updated successfully, but these errors were encountered: