zig icon indicating copy to clipboard operation
zig copied to clipboard

Unhelpful error message on attempt to mutate for-loop over range capture payload

Open amcrjlnv opened this issue 2 years ago • 0 comments

Zig Version

0.11.0-dev.3396+cb3265a3c

Steps to Reproduce and Observed Output

This code does not compile (expected). It used to cause a segmentation fault, but now it gives an unhelpful error message.

const std = @import("std");
pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    var sum: usize = 0;
    for (0..100) |i| {
        i += 1;
        sum += i;
    }
    try std.fmt.format(stdout, "The sum is {}\n", .{sum});
}

Compiling it results in the following error:

$ zig build-exe example.zig
example.zig:8:11: error: expected pointer, found 'u1'
        i += 1;
        ~~^~~~

The interesting thing, hover, is that examples such as those in #15086 still show the correct error message:

pub fn main() !void {
    var xs: [3]u8 = .{ 0, 1, 2 };
    var sum: usize = 0;
    for (xs) |x| {
        x += 1;
        sum += x;
    }
}
example.zig:5:11: error: cannot assign to constant
        x += 1;
        ~~^~~~

If it matters, I am running Ubuntu 22.04 on x86_64 and I've compiled zig with LLVM 16.0.3

Expected Output

It should tell me that I can't modify the captured value because it's constant like the second example.

amcrjlnv avatar Jun 12 '23 23:06 amcrjlnv