Skip to content

local variable is never mutated regression for field and array accesses #25540

Description

@squeek502

Zig Version

0.16.0-dev.699+529aa9f27

Steps to Reproduce and Observed Behavior

I've run into this a few times now and thought I was going crazy. Locally, everything would be fine but CI would fail immediately with local variable is never mutated. One reproduction:

fn foo() void {
    var target_path_w: []const u8 = "foo";
    _ = target_path_w.len;
}

With zig1, it hits this error:

> zig1.exe ..\lib build-obj -ofmt=c -Mroot=test.zig
test.zig:2:9: error: local variable is never mutated
    var target_path_w: []const u8 = "foo";
        ^~~~~~~~~~~~~
test.zig:2:9: note: consider using 'const'

With a stage3 compiler, it does not:

> zig build-obj -ofmt=c -Mroot=test.zig

Note that it's not related to the function being unused or something like that. If you add a test block:

test "foo" {
    foo();
}

it passes:

> zig test test.zig
All 1 tests passed.

Somewhat surprisingly, zig1 seems to have the more correct behavior, as in each case I've hit, the error seems applicable.

Expected Behavior

zig1 and the stage3 compiler to agree on when things are never mutated.

Activity

  1. added
    bugObserved behavior contradicts documented or intended behavior
    on Oct 11, 2025
  2. squeek502 commented on Oct 11, 2025

    @squeek502
    MemberAuthor

    This is actually #25154 (review), so it regressed in #25154 and zig1.wasm hasn't been updated since.

    If there was a follow-up issue made, then this should be closed as a duplicate of that. If not, then I guess this can serve as that follow-up issue.

    cc @mlugg

  3. changed the title [-]`local variable is never mutated` mismatch between zig1 and zig (stage3)[/-] [+]`local variable is never mutated` regression, mismatch between zig1 and zig (stage3)[/+] on Oct 11, 2025
  4. added
    frontendTokenization, parsing, AstGen, Sema, and Liveness.
    regressionIt worked in a previous version of Zig, but stopped working.
    on Oct 11, 2025
  5. mlugg commented on Oct 11, 2025

    @mlugg
    Member

    Ah yes, thank you @squeek502; indeed I forgot to make that issue, so this can serve as it.

  6. added this to the 0.16.0 milestone on Oct 11, 2025
  7. changed the title [-]`local variable is never mutated` regression, mismatch between zig1 and zig (stage3)[/-] [+]`local variable is never mutated` regression for field and array accesses[/+] on Oct 11, 2025
  8. self-assigned this
    on Oct 11, 2025
  9. mlugg commented on Jan 30, 2026

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

Metadata

Metadata

Assignees

Labels

bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.regressionIt worked in a previous version of Zig, but stopped working.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions