Skip to content

Typing and constant folding are intertwined #5213

Description

@Sporarum

Because types can contain constants (uint256[MY_CONST + 1]), constant folding is run before typing, this causes issues:

  1. We check that the arguments to built-ins are correctly typed after we have already folded them. This means we must manually check the arguments in the folding logic, see for example fix[lang]: fix pow_mod256 raising raw python exception #5180
  2. user-defined types like structs are analyzed after constant folding (since they have members with types), this means they are not available during constant folding, making it for example impossible to constant-fold empty(MyStruct) (empty does not constant fold #5153)

Note that this inter-dependency is inherent in the semantics

The one way I see of addressing this is to proceed in steps:

  1. Extract a dependency graph, which identifiers refer to which other identifiers, and make sure there are no loops
  2. Type-check all symbols with no dependencies (aka leaves)
  3. Constant-fold them
  4. Type-check all symbols with only type-checked/folded dependencies
  5. Consant-fold them
  6. Go to step 4 until there are no more symbols (guaranteed to terminate by 1.)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions