windranger-io / windranger-solidity-template

Project template for Solidity smart contract with TypeScript tests
Apache License 2.0
10 stars 9 forks source link

[Bug] Constant within struct - solc / solhint conflict #139

Closed aodhgan closed 2 years ago

aodhgan commented 2 years ago

Bug Description

Solhint is not allowing the definition of constant size arrays within a struct, giving error Function order is incorrect, struct definition can not go after state variable declaration (line 15) ordering

Consider:

   uint256 internal constant _CONST_SIZE = 5;
    struct StructWithConstantSizeArray{
        uint256[_CONST_SIZE] array;  
    }

which is valid solidity, but Solhint rejects

While


    struct StructWithConstantSizeArray{
        uint256[_CONST_SIZE] array;  
    }
   uint256 internal constant _CONST_SIZE = 5;

solc rejects (maybe as it cannot see _CONST_SIZE yet). Non magic number fixed size arrays are good for packed gas optimised structs. Is there a workaround such as place in a library or change the solhint rules for constants?

@CjHare

CjHare commented 2 years ago

@aodhgan can you place the constant definition inside the struct?

aodhgan commented 2 years ago

Doesnt seem like you can!

CjHare commented 2 years ago

Interesting, although expected 😞

CjHare commented 2 years ago

Well then, are the SolHint rule is generally sensible, there's are two clear choices for this specific case:

  1. An ignore statement
  2. Use a library to store the constant

I'd lean towards using a library, as it splits up contracts into smaller pieces, but wdyt?

CjHare commented 2 years ago

Workaround applied. 👍