#For loop getting skipped for seemingly no reason

1 messages · Page 1 of 1 (latest)

midnight blade
#

I have this code right here

pub fn parse(str: []const u8) usize {
    _ = str;
    for (0..20) |_| {
        return 1;
    }
}

calling it gives an error

src/uci.zig:8:31: error: function with non-void return type 'usiz
e' implicitly returns
pub fn parse(str: []const u8) usize {
                              ^~~~~
src/uci.zig:13:1: note: control flow reaches end of body here
}```
stoic holly
#

I think zig doesn't see the implicit unreachable after the return for some reason

#
pub fn parse(str: []const u8) usize {
    _ = str;
    for (0..20) |_| {
        return 1;
    }
    unreachable;
}

midnight blade
#

owww

#

thanks

#

I spent like an hour scratching my head over this

stoic holly
#

the unreachable keyword is cool

karmic sphinx
#

make sure to raise an issue about this

midnight blade
#

this is the first time I have to explicitly say it

midnight blade
bright tide
#

Zig doesn't "know" whether a non-inline for loops zero or non-zero times

stoic holly
#

yeah, but this is a case of comptime knowness