Open LucasSte opened 1 year ago
bb
declared with bytes
is a dynamic byte array and it doesn't have a push method like arrays in some other programming languages. To do that, you gotta manage the length of the array manually and use memory allocation functions.
memory
variable. This would allow you to modify data(array in this case) inside of the bar function as it is declared as pure. It aligns with the function's read-only nature and preventing errors related to modifying storage in a read-only context.pragma solidity ^0.8.0;
contract Foo {
struct My {
bytes bb;
}
function bar() public pure returns (uint256) {
My memory s; //declare s as a memory variable
s.bb = new bytes(0); // Initialize the bytes array
s.bb.push(0x02);
return s.bb.length;
}
}
Thanks @Shiva953, you are right. solc does not allow us to push to a bytes array that is not in memory. The bug, however, still exists. The compiler must not panic because we are trying to build incorrect code. It should raise an error.
yeah sire, that's a solidity compiler limitation and I honestly don't have the slightest of the clues about anything related to compilers
Solang does support push/pop on memory arrays, even if solc does not. However, it should not panic.
This contract:
causes this error: