Skip to content

for loop is unusable with types larger than usize #17139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
FedericoStra opened this issue Sep 13, 2023 · 1 comment
Closed

for loop is unusable with types larger than usize #17139

FedericoStra opened this issue Sep 13, 2023 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@FedericoStra
Copy link
Contributor

Zig Version

0.12.0-dev.185+49075d205

Steps to Reproduce and Observed Behavior

The documentation for for vaguely says that

    // To iterate over consecutive integers, use the range syntax.
    // Unbounded range is always a compile error.
    var sum3 : usize = 0;
    for (0..5) |i| {
        sum3 += i;
    }
    try expect(sum3 == 10);

What it doesn't say is the important disclaimer that for only works for usize, which is a massive limitation.

For instance, the following seemingly innocent loops do not compile (in caso of tooBig assuming that usize is at most u64):

const std = @import("std");
const maxInt = std.math.maxInt;

pub fn negative() void {
    for (-1..0) |n|
        std.debug.print("{}\n", .{n});
}

pub fn tooBig() void {
    for (0..maxInt(u65)) |n|
        std.debug.print("{}\n", .{n});
}

test {
    std.testing.refAllDecls(@This());
}
❯ zig test src/for.zig 
src/for.zig:5:10: error: type 'usize' cannot represent integer value '-1'
    for (-1..0) |n|
         ^~
referenced by:
    refAllDecls__anon_1088: /.../zig/zig-linux-x86_64-0.12.0-dev.185+49075d205/lib/std/testing.zig:1129:14
    test_0: src/for.zig:15:28
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
src/for.zig:10:19: error: type 'usize' cannot represent integer value '36893488147419103231'
    for (0..maxInt(u65)) |n|
            ~~~~~~^~~~~

Expected Behavior

Unsurprisingly, the expected behavior is that the for loop should be able to handle negative numbers and arbitrarily large integers.

I know that this kind of for loop is redundant and can always be reformulated in terms of while, but then what is the point of having such a limited construct which in many cases is either completely unusable or at best very cumbersome (requiring for instance @intCast/@truncate)?

@FedericoStra FedericoStra added the bug Observed behavior contradicts documented or intended behavior label Sep 13, 2023
@mlugg
Copy link
Member

mlugg commented Sep 13, 2023

Duplicate of #17039 (I know the names imply slightly different things, but both issues really mean "types other than usize")

@Vexu Vexu closed this as not planned Won't fix, can't repro, duplicate, stale Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

3 participants