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

boolean-if on optional invalidly allowed at comptime #21742

Closed
rohlem opened this issue Oct 18, 2024 · 2 comments
Closed

boolean-if on optional invalidly allowed at comptime #21742

rohlem opened this issue Oct 18, 2024 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code.

Comments

@rohlem
Copy link
Contributor

rohlem commented Oct 18, 2024

Zig Version

0.14.0-dev.1924+bdd3bc056

Steps to Reproduce and Observed Behavior

fn b(arg: ?bool) bool {
    return if (arg) true else false;
}
fn u(arg: ?u0) bool {
    return if (arg) true else false;
}
test {
    _ = comptime b(true); //passes (yields false)
    _ = comptime u(0); //passes (yields false)
    //_ = b(true); //correctly issues compile error `expected type 'bool', found '?bool'`
    //_ = u(0); //correctly issues compile error `expected type 'bool', found '?u0'`
}

Expected Behavior

Optional-unwrapping if should always require a capture, as it does in runtime code.
IMO this should count as a miscompilation, since invalid code (where the intent is not clear) is processed (and yields an unintuitive result).

@rohlem rohlem added the bug Observed behavior contradicts documented or intended behavior label Oct 18, 2024
@mlugg mlugg added the miscompilation The compiler reports success but produces semantically incorrect code. label Oct 18, 2024
@mlugg
Copy link
Member

mlugg commented Oct 18, 2024

This will be solved by #21422, which I will be able to merge soon.

@rohlem
Copy link
Contributor Author

rohlem commented Oct 18, 2024

duplicate of #21405 (which has a less specific title, but I think the first is still the one kept open usually? Although I prefer better titles. Anyway now I've clicked the button already.)

@rohlem rohlem closed this as completed Oct 18, 2024
@rohlem rohlem closed this as not planned Won't fix, can't repro, duplicate, stale Oct 18, 2024
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 miscompilation The compiler reports success but produces semantically incorrect code.
Projects
None yet
Development

No branches or pull requests

2 participants