Yul constant substitution loses lexical scope

Open
#614 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
haskell
Domain
compilers

Research direction

Start by reading MastEval.escapingAssignsYulBlock and substYulStmt, the scope handling paths named in the issue. Trace how nested Yul declarations, loop initializers, and later assembly blocks affect constant invalidation and substitution. Done means the examples preserve the outer assignment and substitute 7 in both positions while keeping the loop-local binding.

Written by the indexing model from the issue text.

Description

A declaration in a nested Yul block hides an earlier write to an outer variable from the constant invalidation logic. A later read is then incorrectly replaced with the old constant.

contract C {
  function main() returns (word) {
    let x: word = 0;
    let y: word = 0;
    assembly {
      if calldatasize() { x := 5 { let x := 9 } }
      y := x
    }
    return y;
  }
}

Actual: the emitted Hull contains y := 0. Expected: keep y := x,
so the result can be 5 when calldata is nonempty. The nested let x must not
hide the outer assignment.

Loop initializers and later assembly blocks can also retain references to an outer comptime binding that has already been erased:

contract C {
  function main() returns (word) {
    let seed: comptime<word> = 7;
    let y: word = 0;
    assembly {
      for { let seed := seed } lt(seed, 9) { seed := add(seed, 1) } {
        y := add(y, seed)
      }
    }
    assembly { y := add(y, seed) }
    return y;
  }
}

Actual: the outer comptime seed is erased, but the loop initializer still
contains let seed := seed, and the later assembly block retains an unbound
seed. Expected: substitute 7 in both positions while preserving the
loop-local binding. MastEval.escapingAssignsYulBlock and substYulStmt appear
to be the relevant scope-handling paths.

Dominant language
Haskell
Stars
115
Forks
11
Avg merge
20h 29m
Merged PRs (30d)
2

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from argotorg/solcore

All issues in argotorg/solcore

Similar issues

More Haskell issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.