Skip to content

fix: allow keccak256() and uint() in compile-time context for layout base expressions#16439

Open
JPier34 wants to merge 4 commits intoargotorg:developfrom
JPier34:feature/keccak256-comptime-layout
Open

fix: allow keccak256() and uint() in compile-time context for layout base expressions#16439
JPier34 wants to merge 4 commits intoargotorg:developfrom
JPier34:feature/keccak256-comptime-layout

Conversation

@JPier34
Copy link
Contributor

@JPier34 JPier34 commented Feb 3, 2026

Summary

Implements evaluation of keccak256() and explicit uint() conversion in a compile-time context. This allows using keccak256() in storage layout base expressions:

contract C layout at uint(keccak256("my.contract.id")) {}

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. Since keccak256() returns bytes32 while the layout base must be an integer, we also evaluate explicit uint() conversions at compile time so that uint(keccak256("...")) is valid.

Changes

  • ConstantEvaluator.cpp/h

    • Evaluate uint(...) type conversion at compile time when the target type is uint256.
    • Evaluate builtin keccak256(...) at compile time when the argument is a string or hex literal.
  • Tests

    • literal_cast.sol now expects success for uint(42).
    • New test layout_keccak256_string_literal.sol for layout at uint(keccak256("...")).

Backwards compatibility

Fully backwards-compatible; no change to runtime behaviour.

@github-actions
Copy link

github-actions bot commented Feb 3, 2026

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.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 uint256 type 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.

JPier34 and others added 3 commits February 18, 2026 09:25
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

keccak256() in comptime context

3 participants

Comments