Skip to content

Commit 281b68b

Browse files
authored
Merge pull request #12803 from ethereum/documentsatetvarinitcahnge
Move state variable initialization to top.
2 parents 6eece81 + 1be21e2 commit 281b68b

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

docs/ir-breaking-changes.rst

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,48 @@ Semantic Only Changes
3030
This section lists the changes that are semantic-only, thus potentially
3131
hiding new and different behavior in existing code.
3232

33+
- The order of state variable initialization has changed in case of inheritance.
34+
35+
The order used to be:
36+
37+
- All state variables are zero-initialized at the beginning.
38+
- Evaluate base constructor arguments from most derived to most base contract.
39+
- Initialize all state variables in the whole inheritance hierarchy from most base to most derived.
40+
- Run the constructor, if present, for all contracts in the linearized hierarchy from most base to most derived.
41+
42+
New order:
43+
44+
- All state variables are zero-initialized at the beginning.
45+
- Evaluate base constructor arguments from most derived to most base contract.
46+
- For every contract in order from most base to most derived in the linearized hierarchy:
47+
48+
1. Initialize state variables.
49+
2. Run the constructor (if present).
50+
51+
This causes differences in contracts where the initial value of a state
52+
variable relies on the result of the constructor in another contract:
53+
54+
.. code-block:: solidity
55+
56+
// SPDX-License-Identifier: GPL-3.0
57+
pragma solidity >=0.7.1;
58+
59+
contract A {
60+
uint x;
61+
constructor() {
62+
x = 42;
63+
}
64+
function f() public view returns(uint256) {
65+
return x;
66+
}
67+
}
68+
contract B is A {
69+
uint public y = f();
70+
}
71+
72+
Previously, ``y`` would be set to 0. This is due to the fact that we would first initialize state variables: First, ``x`` is set to 0, and when initializing ``y``, ``f()`` would return 0 causing ``y`` to be 0 as well.
73+
With the new rules, ``y`` will be set to 42. We first initialize ``x`` to 0, then call A's constructor which sets ``x`` to 42. Finally, when initializing ``y``, ``f()`` returns 42 causing ``y`` to be 42.
74+
3375
- When storage structs are deleted, every storage slot that contains
3476
a member of the struct is set to zero entirely. Formerly, padding space
3577
was left untouched.
@@ -112,47 +154,6 @@ hiding new and different behavior in existing code.
112154
- New code generator: ``0`` as all parameters, including return parameters, will be re-initialized before
113155
each ``_;`` evaluation.
114156

115-
- The order of contract initialization has changed in case of inheritance.
116-
117-
The order used to be:
118-
119-
- All state variables are zero-initialized at the beginning.
120-
- Evaluate base constructor arguments from most derived to most base contract.
121-
- Initialize all state variables in the whole inheritance hierarchy from most base to most derived.
122-
- Run the constructor, if present, for all contracts in the linearized hierarchy from most base to most derived.
123-
124-
New order:
125-
126-
- All state variables are zero-initialized at the beginning.
127-
- Evaluate base constructor arguments from most derived to most base contract.
128-
- For every contract in order from most base to most derived in the linearized hierarchy execute:
129-
130-
1. If present at declaration, initial values are assigned to state variables.
131-
2. Constructor, if present.
132-
133-
This causes differences in some contracts, for example:
134-
135-
.. code-block:: solidity
136-
137-
// SPDX-License-Identifier: GPL-3.0
138-
pragma solidity >=0.7.1;
139-
140-
contract A {
141-
uint x;
142-
constructor() {
143-
x = 42;
144-
}
145-
function f() public view returns(uint256) {
146-
return x;
147-
}
148-
}
149-
contract B is A {
150-
uint public y = f();
151-
}
152-
153-
Previously, ``y`` would be set to 0. This is due to the fact that we would first initialize state variables: First, ``x`` is set to 0, and when initializing ``y``, ``f()`` would return 0 causing ``y`` to be 0 as well.
154-
With the new rules, ``y`` will be set to 42. We first initialize ``x`` to 0, then call A's constructor which sets ``x`` to 42. Finally, when initializing ``y``, ``f()`` returns 42 causing ``y`` to be 42.
155-
156157
- Copying ``bytes`` arrays from memory to storage is implemented in a different way.
157158
The old code generator always copies full words, while the new one cuts the byte
158159
array after its end. The old behaviour can lead to dirty data being copied after

0 commit comments

Comments
 (0)