assignment to struct in array
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 2d 19h
- Merged PRs (30d)
- 29
Description
I'll start with as little code as possible as likely "It's me not solc"
```solidity
struct Allocation {
uint48 stakeTime;
uint48 unlockTime;
uint32 spare;
uint128 stakeAmount;
}
mapping(address => Allocation[]) public userMap;
Allocation[] storage allocations = userMap[_account];
Allocation storage a;
while (aid > i) {
a = allocations[aid];
if (a.unlockTime <= block.timestamp) {
++foundUnlocked;
totalAmount += a.stakeAmount;
// a = allocations[allocations.length - 1]; // Why does this does not work ????
allocations[aid] = allocations[allocations.length - 1]; // this works
allocations.pop();
}
--aid;
}
```
I understand that array of struct if a reference type, but why can't I assign to `a` that it shows up in `allocations[aid]` (as it should point to the same storage slot) ?
If I assign individual variables within a struct it works, but that is of course not an efficient alternative.
```solidity
a = allocations[i];
a.stakeAmount = totalAmount.toUint128();
a.stakeTime = uint48(block.timestamp);
a.unlockTime = uint48(block.timestamp);
```
What am I missing ?
Contributor guide
Research direction
Start by reproducing the provided Solidity snippet and compare the behavior of assigning the storage reference with assigning the struct value. Trace the language's storage-reference and struct-assignment semantics; done means determining whether this is intended behavior or a compiler bug and documenting the confirmed explanation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- solidity
- Domain
- blockchain, compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100