Skip to content
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

The while continue block does not enforce error handling. #957

Closed
bheads opened this Issue Apr 26, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@bheads
Copy link

bheads commented Apr 26, 2018

The While continue does not enforce error handling.
Also (not really a bug) but you cannot break the loop from the continue, but that maybe intended.

Sample code that should fail (but compiles and runs in an infinite loop):

const warn = @import("std").debug.warn;

const Range = struct {
    data: [10]i32,
    index: i32,

    pub const Error = error{EmptyRange};

    pub fn init() Range {
        var r = Range{
            .data = undefined,
            .index = 0,
        };

        for (r.data) |*value, i| {
            value.* = @intCast(i32, i) + 1;
            r.index = @intCast(i32, i);
        }
        return r;
    }

    pub fn empty(self: *Range) bool {
        return self.index < 0;
    }

    pub fn front(self: *Range) Error!i32 {
        if (self.empty()) return Error.EmptyRange;
        return self.data[@intCast(usize, self.index)];
    }

    pub fn popFront(self: *Range) Error!void {
        if (!self.empty()) return Error.EmptyRange; // << intential BUG, always returns an error
        self.index -= 1;
    }
};

pub fn main() void {
    var r: Range = Range.init();
    while (!r.empty()) : (r.popFront()) { // << popFront is returning an error here (try does work okay here when main returns !void)
        //while(!r.empty()) : (r.popFront() catch |e| { break; }) { // <<< error: break expression outside loop
        warn("{} ", r.front() catch |e| {
            break;
        });
    }
    warn("\n");
}

@andrewrk andrewrk added this to the 0.3.0 milestone Apr 28, 2018

@andrewrk andrewrk added the bug label Apr 28, 2018

@andrewrk andrewrk modified the milestones: 0.3.0, 0.4.0 Sep 3, 2018

@andrewrk andrewrk closed this in 4a5cd0b Mar 16, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.