Compile‑time error when reassigning an immutable variable to its current value in Vyper
0 reputation · 30 Oct 2021, 15:20 UTC
0 reputation · 30 Oct 2021, 15:20 UTC
Goal: Determine whether the Vyper compiler treats a reassignment that gives an immutable variable the same value it already holds as a permissible no‑op or as a violation of the immutability rule.
Constraint: The language specification states that immutables cannot be modified after deployment, but it does not explicitly say whether assigning an identical value counts as a modification.
Uncertainty: Developers have reported mixed behavior when using patterns such as conditional initialization that re‑assign an immutable to its current value, and it is unclear if the compiler’s error message includes the variable name and source location or if this behavior is stable across recent Vyper releases.
Questions: Does the compiler emit an error when reassigning an immutable to its current value? Does the error message contain the variable name and line number? Is this behavior consistent across Vyper 0.2.x, 0.3.x and later versions?
33275 reputation · 31 Oct 2021, 01:44 UTC
Yes. The Vyper compiler emits a compile-time error whenever you attempt to assign a value to an immutable variable after its initial initialization, even if the assigned value is identical to the value the variable already holds. The compiler does not treat such a reassignment as a permissible no-op.
The immutability check is performed syntactically at compile time: any assignment statement targeting an immutable after its initialization is treated as a modification attempt. The compiler does not compare the assigned value against the current value, so conditional-initialization patterns that re-assign an immutable to its current value will still trigger the same error.
test.vy:# test.vy
VALUE: public(immutable(uint256))
@external
def __init__():
VALUE = 42
@external
def test():
# Attempt to reassign the same value
VALUE = 42
pip install vyper==0.3.10 (repeat with 0.2.x or 0.4.x to compare).vyper test.vy.VALUE and pointing at the reassignment line.Note that immutable syntax differs across versions (0.2.x uses VALUE: constant(...)-style declarations in some contexts and 0.4.x uses the immutable(...) form shown above), so adjust the declaration to match the version under test. If your observed error output does not include the variable name or line number, report the exact compiler version, since that would contradict the expected behavior described here.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.