solidity-parser / parser

A Solidity parser for JS built on top of a robust ANTLR4 grammar
MIT License
157 stars 44 forks source link

ArrayTypeName.length incorrect type #52

Closed naddison36 closed 3 years ago

naddison36 commented 3 years ago

ArrayTypeName is currently defined with length having type Expression | null

export interface ArrayTypeName extends BaseASTNode {
  type: 'ArrayTypeName'
  baseTypeName: TypeName
  length: Expression | null
}

At runtime, the length for a fixed length array is returning a length of type NumberLiteral. So the type should be

export interface ArrayTypeName extends BaseASTNode {
  type: 'ArrayTypeName'
  baseTypeName: TypeName
  length: NumberLiteral | null
}

Some example fixed length arrays are

bool[2] flags = [true, false];
uint256[2][3] multiDim2x3;
address[3][2] multiDim3x2;

I tried to create a PR for this but I got lost in the required ASTBuilder changes in the visitTypeName function.

fvictorio commented 3 years ago

Hey @naddison36, I'm not sure this is correct. It's possible to use something that is not a NumberLiteral there. An obvious case is:

uint[3 + 3] x;

and a more realistic one is:


contract Foo {
  uint constant length = 3;

  function f() public {
    uint[length] memory x;
  }
}

Please re-open if I'm missing something here!