Skip to content

Allow assignment to fixed-size arrays from typed expressions, not only identifiers #61

@michaelsutton

Description

@michaelsutton

Assignment to fixed-size arrays is currently stricter than initialization.

This works:

byte[32] selected = prev_state.a;

but this fails:

selected = prev_state.b;

with:

array assignment only supports array identifiers

This becomes blocking when combined with two other current restrictions:

  • return must be the last statement
  • you cannot return separately from if/else

That means a branch-dependent fixed-size array value can become impossible to express.

Exact example

contract C(byte[32] initA, byte[32] initB) {
    byte[32] a = initA;
    byte[32] b = initB;

    function choose(State prev_state) : (State) {
        byte[32] selected = prev_state.a;

        if (selected[0] == 1) {
            selected = prev_state.b;
        }

        return({
            a: selected,
            b: prev_state.b
        });
    }

    entrypoint function main() {
        (State next) = choose({a: a, b: b});
        require(next.b == b);
    }
}

This currently fails on:

selected = prev_state.b;

There is no real workaround here:

  • the chosen byte[32] value depends on the branch
  • the final return must happen after the branch
  • separate returns inside the branches are not allowed
  • and unlike byte, there is no scalar workaround for byte[32]

Expected behavior
At minimum, fixed-size array assignment should accept RHS expressions that are type-compatible with the target type, for example:

  • byte[32] = other_byte32
  • byte[32] = prev_state.hash
  • more generally T[N] = expr when expr matches T[N]

The compiler already knows the exact target size, and it already accepts expression initializers for fixed-size arrays at definition time. This looks like an implementation restriction in assignment rather than a necessary language rule.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions