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

Block label can't be used on switch #8226

Closed
rgreenblatt opened this issue Mar 12, 2021 · 5 comments
Closed

Block label can't be used on switch #8226

rgreenblatt opened this issue Mar 12, 2021 · 5 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@rgreenblatt
Copy link
Contributor

rgreenblatt commented Mar 12, 2021

EDITED:

pub fn main() void {
    var k : i32 = 0;
    var s : i32 = 1;
    const v = switch (s) blk: {
        1 => {
            if (k == 0) {
                break :blk 8;
            }
            // some code here...
            //
            break :blk 2;
        },
        // another case
        2 => {
            if (k == 2) {
                break :blk 8;
            }
            // some code here...
            //
            break :blk 2;
        },
        else => 9,
    };
}

https://zig.godbolt.org/z/K76eae

@SpexGuy
Copy link
Contributor

SpexGuy commented Mar 12, 2021

That isn't a normal block, it wouldn't make sense to break it. What do you expect this to do?

@rgreenblatt
Copy link
Contributor Author

That isn't a normal block, it wouldn't make sense to break it. What do you expect this to do?

I've edited my post to show a better example. It would basically be a shorthand for labeling all of the cases.

@SpexGuy
Copy link
Contributor

SpexGuy commented Mar 12, 2021

Hmm, so this would be a workaround:

    const v = blk: {
      break :blk switch (s) {
        ...
      }
    };

I do think we could potentially allow blk: switch (s) { ... }, similar to labeled for and while expressions. But IMO it's weird to allow labeling the switch scope itself, since it's not an expression. It feels the same to me as allowing one of these things:

const x = switch (y) y;
const T = struct blk: { f: usize };
const v = T blk: { .f = 4 };
fn foo() T blk: {}

@SpexGuy SpexGuy added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Mar 16, 2021
@Vexu Vexu added this to the 0.8.0 milestone Mar 19, 2021
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@Mouvedia
Copy link

Mouvedia commented Nov 2, 2021

I do think we could potentially allow blk: switch (s) { ... },

Are you talking about #8220?

@andrewrk
Copy link
Member

Superseded by #8220 which does incorporate this use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

5 participants