solidity-lean/SolidCore/Witness/CallPlusStateReadOperandOrder.lean
arena-lean-staging-agent[bot] d4222ecdaa
fix: right-first eval for binary with non-core-call LHS and pure state-read RHS (g()*100000 + t) (#15)
An ordinary binary operator whose LEFT operand carries a NESTED internal call
(not a *direct* call — e.g. `g() * 100000`) and whose RIGHT operand is a pure
`toCore?`-lowerable state read the call mutates (e.g. `t`, with `g()` doing
`t += 105`) routes through `FunctionDecl.internalBinarySingleReturnUseCore?`'s
`none, none` branch. Hoisting the RHS finds no call, so control reached the
`| none =>` (pure-RHS) sub-case, which lowered the pure RHS in place and hoisted
the LEFT call around it — evaluating `g()` (mutating `t`) BEFORE the residual
binary read `t`. solc 0.8.35 legacy codegen evaluates the RIGHT operand FIRST
(ExpressionCompiler.cpp:614-615), reading `t` while still 0. The wrong order
changed the return value: `return g() * 100000 + t` gave 10500105 instead of
solc+EVM's 10500000 (final storage `t = 105` either way).

Fix: for ordinary ops in that sub-case, park the RHS value in a
`_sol_bin_<tag>_rhs` temp, evaluate the LEFT call second, and combine from the
temp (mirroring the direct-call-LHS/pure-RHS path). Falls back to the prior
left-first shape only if the RHS type is unresolvable, and short-circuit
`&&`/`||` keep their left-first guarded shape.

Regression witness: SolidCore/Witness/CallPlusStateReadOperandOrder.lean.

Co-authored-by: Lean Semantics Review Agent <agent@solidcore.local>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-20 14:18:28 +00:00

119 lines
4.3 KiB