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

if without braces in switch case is parsed weirdly #3749

Closed
ghost opened this issue Nov 23, 2019 · 7 comments
Closed

if without braces in switch case is parsed weirdly #3749

ghost opened this issue Nov 23, 2019 · 7 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.

Comments

@ghost
Copy link

ghost commented Nov 23, 2019

I'm not really sure what the parser is trying to do here. I think this should work. I don't think the comma is overloaded that much in Zig so I don't see any ambiguity.

pub fn main() void {
    var blah = false;
    var x = false;

    switch (x) {
        false => if (blah) x = true,
        true => if (blah) x = false,
    }
}
Semantic Analysis [719/735] /home/dbandstra/host/asdf.zig:6:18: error: incompatible types: '*bool' and 'void'
        false => if (blah) x = true,
                 ^
/home/dbandstra/host/asdf.zig:6:28: note: type '*bool' here
        false => if (blah) x = true,
                           ^
/home/dbandstra/host/asdf.zig:6:18: note: type 'void' here
        false => if (blah) x = true,
                 ^

It works if I add curly braces to the if's.

        false => if (blah) { x = true; },

And this:

pub fn main() void {
    var blah = false;
    var x = false;

    switch (x) {
        false => if (blah) x = true else x = false,
        true => if (blah) x = false else x = true,
    }
}

Yields this:

/home/dbandstra/host/asdf.zig:6:37: error: expected token '}', found 'else'
        false => if (blah) x = true else x = false,
                                    ^
@mikdusan
Copy link
Member

also works:

pub fn main() void {
    var blah = false;
    var x = false;

    switch (x) {
        false => { if (blah) x = true; },
        true => { if (blah) x = false; },
    }
}

@fengb
Copy link
Contributor

fengb commented Nov 26, 2019

I'm guessing that without braces, the switch is looking for a return value and convinces the if to be an expression instead of a statement.

@andrewrk andrewrk added this to the 0.6.0 milestone Nov 27, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Mar 4, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 17, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@SpexGuy SpexGuy added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Mar 21, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@ghost
Copy link
Author

ghost commented Jul 6, 2021

This works fine, so maybe the problem is limited to when you have an assignment in the if clause?

fn func() void {
    var x = false;
}

pub fn main() void {
    var blah = false;
    var x = false;

    switch (x) {
        false => if (blah) func(),
        true => if (blah) func() else func(),
    }
}

@Vexu
Copy link
Member

Vexu commented Jul 6, 2021

Related #5731.

@mrjbq7
Copy link
Contributor

mrjbq7 commented Jan 11, 2022

I humbly suggest that if without braces should not be allowed.

It's one of the biggest footguns in C where the first line is in the if statement and the second line is not.

if (true)
    std.debug.writeAll("it's true!\n")
    std.debug.writeAll("no really, it's true\n")

@SpexGuy
Copy link
Contributor

SpexGuy commented Jan 11, 2022

@mrjbq7 We are already planning to make that a compile error, but through a different mechanism: #35

Edit: We've also previously rejected the idea of requiring braces on if statements in #4294

@Vexu
Copy link
Member

Vexu commented Dec 7, 2022

Self-hosted gives a nicer error for this:

error: invalid left-hand side to assignment
        false => if (blah) x = true,
                 ^~~~~~~~~~~

Proposal to change the grammar in #5731

@Vexu Vexu closed this as completed Dec 7, 2022
@Vexu Vexu removed this from the 0.12.0 milestone Dec 7, 2022
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 stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

6 participants