Skip to content

Lower indexing of computed pointers and returned array values #954

Closed

Description

@LunaStev

Problem

Semantically valid indexing of a computed pointer or returned fixed array fails in LLVM lowering. The index base is always sent through lvalue address recovery even when the base is a function result or pointer arithmetic expression.

Audited canonical master d59bdab376bdc04edacdc8029b256cb8d55b5adf on Linux amd64 using a checkout-built Wave 0.0.1-alpha-dev, Rust 1.89.0 and LLVM 21.1.8.

Reproduction

fun make() -> array<i32, 2> {
    return [11, 22];
}
fun main() -> i32 {
    return make()[0];
}
target/debug/wavec check repro.wave
target/debug/wavec build repro.wave --emit=ir -O0 --out-dir /tmp/wave-rvalue-index-o0
target/debug/wavec build repro.wave --emit=ir -O2 --out-dir /tmp/wave-rvalue-index-o2

check succeeds. Both builds fail with E9001 and Cannot take address of this expression: FunctionCall ... at llvm/src/codegen/address.rs:313.

The pointer-only form reproduces independently of array returns:

fun pick(p: ptr<i32>) -> ptr<i32> { return p; }
fun main() -> i32 {
    var values: array<i32, 2> = [11, 22];
    return pick(&values[0])[1];
}

(p + 1)[0] with p = &values[0], and indexing an array field of a returned struct, produce the same failure. Saving make() into a typed local before indexing builds at O0/O2 as a control.

Scope

Use the Typed HIR base type to distinguish an array value, pointer value, and existing lvalue storage. Lower the computed base once; materialize temporary aggregate storage only where needed. Preserve existing pointee strides, source evaluation order, pointer-to-array projection semantics, and ordinary lvalue reads/writes. This does not introduce bounds checks or change pointer arithmetic policy.

Starting points: index address recovery, indexed rvalue lowering.

Related #436 fixed arbitrary integer expressions in the index operand, not computed index bases. #914 fixed array return materialization; its local-return behavior remains valid. #409 tracks the general typed backend error channel, while this issue concerns lowering already accepted valid programs. The open Whale array/pointer adaptation issues #879/#880 are separate.

Difficulty: medium; help wanted. Not marked good first issue because value/address handling and evaluation order span multiple expression paths.

Completion criteria

  • Returned arrays, computed pointers, grouped pointer arithmetic and array fields of returned structs can be indexed at O0/O2.
  • Observable base/index side effects execute once in the established source order.
  • Existing array variable, raw-pointer, pointer-to-array and nested-index cases retain correct values and strides.
  • Semantic errors remain source diagnostics; failed lowering preserves existing outputs.

Activity

  1. added
    bug
    help wanted
    on Oct 18, 2026
  2. added a commit that references this issue on Nov 13, 2026
    be6a7a8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions