Skip to content

Compile-time evaluation of constants of member constants #16055

Open
@cameel

Description

@cameel

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

low effortThere is not much implementation work to be done. The task is very easy or tiny.low impactChanges are not very noticeable or potential benefits are limited.must have eventuallySomething we consider essential but not enough to prevent us from releasing Solidity 1.0 without it.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @cameel@matheusaaguiar

    Issue actions

      Compile-time evaluation of constants of member constants · Issue #16055 · ethereum/solidity