solidity-lean/SolidCore/Witness/NewContractLocalStructArray.lean
Dan Robinson 001ff554ef Fix #163 NEW-CONTRACT-LOCAL-STRUCT-ARRAY: qualify new-array element type of a contract-local struct
solc 0.8.35 accepts and executes `new P[](n)` where `P` is a CONTRACT-LOCAL
struct (`contract C { struct P { uint256 x; } ... P[] memory pa = new P[](1); ... }`).
The model's checker rejected it (over-reject, replay-reachable): the
`Ty.array _ none` branch of the `Expr.newExpr` type-check
(`SolidCore/Solidity/TypeCheck.lean`) returned the RAW AST element type `["P"]`,
while every TARGET type formed for a declared local, return, or parameter
qualifies a contract-local struct reference to `["C","P"]` (function sigs are
pre-qualified via `Ty.qualifyLocalUserTypes`; declared locals via
`CheckEnv.qualifyCurrentLocalUserTypes`). The unqualified `["P"]` then never
matched the qualified target, so `expectAssignableTo` failed with
`TypeError.expectedType (array ["C","P"]) (array ["P"])`.

Fix: run the `new`-array RESULT element type through the same
`CheckEnv.qualifyCurrentLocalUserTypes`, which only qualifies to a path actually
known in the current/ancestor scope (elementary-element and file-level-struct
arrays are untouched; a same-named struct in another contract is qualified only
to its own scope, so there is no mirror over-accept).

Struct-constructor field targets are the one TARGET context that was NOT
pre-qualified (`checkStructConstructorArgs` read `StructDecl.fieldTys` raw), so
they previously matched the unqualified new-array by accident (e.g. the #160
recursive witness `Node(1, new Node[](0))`). To keep both operands normalized
identically, the struct-constructor comparison now qualifies the field target
types through the SAME `qualifyCurrentLocalUserTypes` before comparing; a
genuinely different element type still mismatches (verified: `new Q[]` into a
`P[]` local is still rejected, matching solc).

Adds witness `SolidCore/Witness/NewContractLocalStructArray.lean` (accept +
execute the isolation ladder) and a replay-gated Forge lane +
`manifest.json` case pinning real solc 0.8.35 + EVM values (localVarLen=3,
returnArr.length=1, multiLen=2, arr2Len=4, uintLen=5). Full `lake build`
(1122 jobs) green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
(cherry picked from commit 047618b60c6dde0dc54e4894249d9b8009dee5df)
2026-07-10 23:48:52 -07:00

173 lines
8.3 KiB