Skip to content

Unexpected error when using if / else if in return expression. #5769

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
dgottner opened this issue Jul 1, 2020 · 1 comment
Closed

Unexpected error when using if / else if in return expression. #5769

dgottner opened this issue Jul 1, 2020 · 1 comment

Comments

@dgottner
Copy link

dgottner commented Jul 1, 2020

Trying to test this code:

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

inline fn sgn(n: i64) i8 {
    return if (n < 0)
        -1
    else if (n == 0)
        0
    else
        1;
}

test "sign function" {
    assert(sgn(49) == 1);
    assert(sgn(-49) == -1);
    assert(sgn(0) == 0);
}

results in a compilation error:

$ zig test bug.zig
./bug.zig:6:10: error: values of type 'comptime_int' must be comptime known
    else if (n == 0)

Note that rewriting the above to not use an expression-based if statement does not provoke an error:

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

inline fn sgn(n: i64) i8 {
    if (n < 0)
        return -1
    else if (n == 0)
        return 0
    else
        return 1;
}

test "sign function" {
    assert(sgn(49) == 1);
    assert(sgn(-49) == -1);
    assert(sgn(0) == 0);
}
zig test nobug.zig
All 1 tests passed.
@Vexu
Copy link
Member

Vexu commented Jul 1, 2020

Duplicate of #5557, #3750, #4255 and. #4629

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants