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

compile error for unreachable code from while condition #952

Open
andrewrk opened this issue Apr 25, 2018 · 3 comments
Open

compile error for unreachable code from while condition #952

andrewrk opened this issue Apr 25, 2018 · 3 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Milestone

Comments

@andrewrk
Copy link
Member

this gets past the compiler, but it should give a compile error because return null is unreachable code.

pub fn lastIndexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize {
    var i: usize = start_index;

    while (i >= 0) : (i -= 1) {
        if (slice[i] == value) {
            return i;
        }
    }

     return null;
 }
@andrewrk andrewrk added the enhancement Solving this issue will likely involve adding new logic or components to the codebase. label Apr 25, 2018
@andrewrk andrewrk added this to the 0.4.0 milestone Apr 25, 2018
@BraedonWooding
Copy link
Contributor

BraedonWooding commented Apr 25, 2018

Halting Problem however could restrict this? Since you could have cases where you have an infinite array that isn't easily seen, such as incrementing i through some other function and so on.

Also how is this code unreachable? This will only be unreachable if 'i < 0' else it could never find the value and i would decrement till it reached '0' then it would return null? Perhaps you mean i+=1?

@andrewrk
Copy link
Member Author

zig already knows that while (i >= 0) when i is unsigned is equivalent to while (true). since there is no control flow to the end of the while, the compiler can be certain that return null is unreachable code.

it's a matter of making the return type of the while expression noreturn in this situation.

@BraedonWooding
Copy link
Contributor

Oh I see what your saying, I didn't see that it was unsigned (just slipped by my mind).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Projects
None yet
Development

No branches or pull requests

2 participants