Skip to content

Evaluate shadowing initializers before publishing the new local binding #679

Closed

Description

@LunaStev

Problem

Semantic validation and LLVM generation expose a newly declared local at different times.

For an ordinary initializer, Validator::validate_node validates the initializer before inserting the new binding. A reference to the same name therefore resolves to an existing outer binding. In contrast, gen_variable_ir inserts the new alloca into variables before generating an ordinary initializer. A name lookup in that initializer can load the new, still-uninitialized allocation instead of the outer value selected by the frontend.

Code references:

Source-derived regression

Save as shadow_initializer.wave:

fun calculate() -> i32 {
    var value: i32 = 41;

    if (true) {
        var value: i32 = value + 1;
        return value;
    }

    return 0;
}

fun main() -> i32 {
    var result: i32 = calculate();
    if (result != 42) {
        return 1;
    }
    return 0;
}

Expected: calculate() returns 42 and the program exits 0. The initializer's value denotes the outer variable; subsequent reads denote the initialized inner variable.

The current source order is:

allocate inner storage
publish value -> inner storage
generate value + 1, loading inner storage before its first store
store the computed result into inner storage

This is a source-level mismatch, not a recorded execution of the Wave input. A hand-written LLVM model of the premature load optimized to an undefined return value; no particular runtime number is a valid expected failure result. The real regression must inspect Wave-generated IR as well as the corrected runtime result.

Expected behavior and scope

Generate an ordinary initializer using the bindings visible before the declaration, then expose the new binding once its initializer has been evaluated. Creating the alloca in the entry block can remain unchanged; storage placement and name visibility are separate decisions.

Preserve the existing special treatment of inline-assembly initializers, which semantic validation deliberately pre-registers for their output-binding contract. Do not remove that exception by applying the ordinary-initializer rule indiscriminately. Keep array and aggregate initialization consistent with the frontend's visibility rules.

This does not introduce definite-assignment analysis, zero-initialize every local, rename user variables, or forbid legal shadowing.

Completion criteria

  • wavec check shadow_initializer.wave accepts the input, and actual unoptimized IR reads the outer allocation when evaluating the initializer.
  • The program exits 0 at unoptimized and optimized settings with the project's supported LLVM toolchain.
  • Cover an outer local and a function parameter shadowed by an inner local, including nested scopes.
  • Confirm the new local is visible after initialization and that leaving its scope restores the applicable outer binding.
  • Initializer side effects occur exactly once and in the established evaluation order.
  • Preserve the supported inline-assembly output initializer behavior and ordinary non-shadowing and aggregate initializers.
  • Add compiler and runnable regression tests; do not treat an arbitrary pre-fix runtime value as proof of correct name resolution.

Difficulty: medium. This issue owns declaration-time binding visibility. Local-versus-global-constant lookup precedence is a separate defect; the regression above uses no global constant.

Activity

  1. added
    bug
    help wanted
    needs testing
    on Sep 21, 2026
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 wantedneeds testing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions