Follow-up to #155 (the checker accepts a library function returning a storage

reference). The interpreter/lowering could not USE that returned pointer: a
contract calling an INTERNAL library function that value-returns a `storage`
struct reference and then dereferencing it — `L.ref(s).x = v` (write) and
`return L.ref(s).x` (read) — was accepted by solc 0.8.35 + the model's checker
but failed at model runtime lowering (`toCoreContract? = none`, over-reject).
Pinned solc + real EVM (anvil, and a new Forge lane) give 42 for set(42);get().

Root cause: `L.ref(s).x` never became `L.ref(s)[0]` because the
struct-member->index rewrite (`resolveStructs`) cannot type a call result, and no
lowering arm consumed a member/index USE of a value-returned storage pointer.
The function boundary already threads a WHOLE storage-ref return back as a plain
`Value.storageRef`, and the interpreter already resolves member/index on such a
pointer to the caller's slot — only the source->core lowering was missing.

Fix (SolidCore/Solidity/Interface.lean), scoped to the WHOLE-reference return
case (`return s;` for a storage-ref param, threaded as a plain storageRef):
  * `Expr.callSingleStorageStructReturnDecl?` — recover the callee's return
    struct decl; the `expandUsing` surface pass rewrites `<call>.field` to
    `<call>[fieldIndex]`, so the existing index-assign arm lowers the WRITE.
  * a new `Stmt.returnValues (Expr.index (call) i)` arm lowers the READ, reusing
    `internalSingleStorageReturnRefCore?` to capture the returned pointer.
  * both gated on `FunctionDecl.returnsWholeStorageRefParam?`.

The gate is load-bearing: a nested sub-PATH return (`return d.m;`,
`return o.inner;`) produces a `storagePathRef` the boundary does NOT re-base onto
the caller, so its USE stays an over-reject rather than mis-resolving at run
time — verified no new wrong-value/panic is introduced (path-return cases still
cleanly over-reject). The public/external library storage-return case (external
calls cannot carry a storage pointer across the ABI boundary) and the nested
path-return case both need the boundary path-return re-basing and are left as
follow-ups.

Coverage:
  * SolidCore/Witness/LibStorageReturnUse.lean — EXECUTES `L.ref(s).x = 42;
    return L.ref(s).x` in the interpreter and `#guard`s the result is 42 (the
    lean-executing coverage the #155 witness lacked); imported from SolidCore.lean.
  * tests/forge-harness/lib-storage-return-use/ — new Forge lane pinning the
    real solc/EVM roundtrip (2 tests pass); manifest case `lib-storage-return-use`
    pairs it with the imported-Lean accept + execute (harness: forge=ok lean=ok,
    compare=pass).

Gate: full `lake build` green (1116 jobs, includes FuelMonotonicity + all
witnesses).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
(cherry picked from commit a9d168e282756a3911296cd5d7d14215b8e4be89)
This commit is contained in:
Dan Robinson 2026-07-10 13:33:02 -07:00
commit 272e76333a
7 changed files with 413 additions and 1 deletions