fix: allow keccak256() and uint() in compile-time context for layout base expressions#16439
fix: allow keccak256() and uint() in compile-time context for layout base expressions#16439JPier34 wants to merge 4 commits intoargotorg:developfrom
Conversation
|
Thank you for your contribution to the Solidity compiler! A team member will follow up shortly. If you haven't read our contributing guidelines and our review checklist before, please do it now, this makes the reviewing process and accepting your contribution smoother. If you have any questions or need our help, feel free to post them in the PR or talk to us directly on the #solidity-dev channel on Matrix. |
There was a problem hiding this comment.
Pull request overview
This PR implements compile-time evaluation of keccak256() and explicit uint() type conversions, enabling storage layout base expressions like contract C layout at uint(keccak256("my.contract.id")) {}. This addresses issue #16421 and implements Stage 1 of issue #16420.
Changes:
- Adds compile-time evaluation support for
uint256type conversions from rational values - Adds compile-time evaluation support for
keccak256()with string and hex string literals - Updates test expectations to reflect the new compile-time capabilities
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| libsolidity/analysis/ConstantEvaluator.h | Adds endVisit method declaration for FunctionCall nodes |
| libsolidity/analysis/ConstantEvaluator.cpp | Implements compile-time evaluation logic for uint() conversions and keccak256() calls, with proper range checking and error handling |
| test/libsolidity/syntaxTests/storageLayoutSpecifier/literal_cast.sol | Removes error expectation since uint(42) is now supported in compile-time context |
| test/libsolidity/syntaxTests/storageLayoutSpecifier/layout_keccak256_string_literal.sol | Adds new test case demonstrating the main use case: uint(keccak256("...")) in layout expressions |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
test/libsolidity/syntaxTests/storageLayoutSpecifier/layout_keccak256_string_literal.sol
Show resolved
Hide resolved
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Summary
Implements evaluation of
keccak256()and explicituint()conversion in a compile-time context. This allows usingkeccak256()in storage layout base expressions:Fixes #16421. Implements Stage 1 of #16420.
Motivation
Layout base expressions commonly use either ERC-7201 (handled in PR #15968) or plain hashing with
keccak256(). This change supports the latter. Sincekeccak256()returnsbytes32while the layout base must be an integer, we also evaluate explicituint()conversions at compile time so thatuint(keccak256("..."))is valid.Changes
ConstantEvaluator.cpp/h
uint(...)type conversion at compile time when the target type isuint256.keccak256(...)at compile time when the argument is a string or hex literal.Tests
literal_cast.solnow expects success foruint(42).layout_keccak256_string_literal.solforlayout at uint(keccak256("...")).Backwards compatibility
Fully backwards-compatible; no change to runtime behaviour.