Skip to content

Compile-time evaluation of constants of member constants #16055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cameel opened this issue May 16, 2025 · 0 comments · May be fixed by #16056
Open

Compile-time evaluation of constants of member constants #16055

cameel opened this issue May 16, 2025 · 0 comments · May be fixed by #16056
Assignees
Labels
low effort There is not much implementation work to be done. The task is very easy or tiny. low impact Changes are not very noticeable or potential benefits are limited. must have eventually Something we consider essential but not enough to prevent us from releasing Solidity 1.0 without it.

Comments

@cameel
Copy link
Member

cameel commented May 16, 2025

Abstract

In contexts where a compile-time constant expression is expected, we currently allow constants initialized with literal expressions, but only when the constant is referenced via a standalone identifier (e.g. X). It does not work when it's via a member access expression (e.g. C.X). Both should work.

contract D {
    uint constant LENGTH = 42;
    uint[LENGTH] x;   // OK
    uint[D.LENGTH] y; // Error: Invalid array length, expected integer literal or constant expression.
}

Motivation

This limitation is unintuitive and the error message does not make it clear what the problem is.

The problem is also likely to become more common once we allow using constants in layout specifiers (#15944).

Specification

In contexts where we use ConstantEvaluator to evaluate expressions at compilation time (array sizes or layout specifiers, etc), it should be possible to reference constants even if they are accessed via member access on contracts, modules or any other objects.

It should also be possible to use member access expressions inside constant initializers (e.g. uint constant X = m.Y).

Backwards Compatibility

Full compatibility. It will affect only situations that currently produce an error.

@cameel cameel added low effort There is not much implementation work to be done. The task is very easy or tiny. low impact Changes are not very noticeable or potential benefits are limited. must have eventually Something we consider essential but not enough to prevent us from releasing Solidity 1.0 without it. labels May 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
low effort There is not much implementation work to be done. The task is very easy or tiny. low impact Changes are not very noticeable or potential benefits are limited. must have eventually Something we consider essential but not enough to prevent us from releasing Solidity 1.0 without it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants