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

Else capture allowed for boolean condition and it forces true branch #12040

Closed
leecannon opened this issue Jul 7, 2022 · 1 comment
Closed
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@leecannon
Copy link
Contributor

Zig Version

0.10.0-dev.2860+c9006d947

Steps to Reproduce

const std = @import("std");

pub fn main() void {
    var the_answer: usize = 42;

    if (the_answer == 0) {
        std.debug.print("first: it's zero\n", .{});
    } else |n| { // with capture, it is broken
        std.debug.print("first: {}\n", .{n});
    }

    if (the_answer == 0) {
        std.debug.print("second: it's zero\n", .{});
    } else { // without capture, it is fine
        std.debug.print("second: {}\n", .{the_answer});
    }
}

Expected Behavior

first: 42
second: 42

Actual Behavior

first: it's zero
second: 42

It does not make sense for an if on a boolean expression to allow a capture on the else.

And even worse than that it causes the true branch to be executed even if the expression is false 😰

@leecannon leecannon added the bug Observed behavior contradicts documented or intended behavior label Jul 7, 2022
@leecannon leecannon changed the title Else capture allowed for boolean condition and forces true branch Else capture allowed for boolean condition and it forces true branch Jul 7, 2022
@Vexu Vexu added frontend Tokenization, parsing, AstGen, Sema, and Liveness. stage1 The process of building from source via WebAssembly and the C backend. labels Jul 7, 2022
@Vexu Vexu added this to the 0.11.0 milestone Jul 7, 2022
@Vexu Vexu removed the stage1 The process of building from source via WebAssembly and the C backend. label Dec 8, 2022
@Vexu
Copy link
Member

Vexu commented May 9, 2023

Fixed by 58caed1

@Vexu Vexu closed this as completed May 9, 2023
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 frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

No branches or pull requests

2 participants