Skip to content

Switch branch of noreturn union field with mutable capture is incorrectly semantically analyzed #13655

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
ifreund opened this issue Nov 26, 2022 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@ifreund
Copy link
Member

ifreund commented Nov 26, 2022

Zig Version

0.11.0-dev.168+b2ffe113d

Steps to Reproduce and Observed Behavior

This test should pass, but it fails due to the @compileError() being triggered.

test "mutable capture of noreturn union field" {
    const U = union(enum) {
        a: u32,
        b: noreturn,
    };
    var a = U{ .a = 1 };
    switch (a) {
        .a => {},
        .b => |*val| {
            _ = val;
            @compileError("Shouldn't be triggered");
        },
    }
}

Expected Behavior

The test should pass. Note that we have test coverage in place for immutable captures and the given test passes if |val| is used instead of |*val|.

@ifreund ifreund added the bug Observed behavior contradicts documented or intended behavior label Nov 26, 2022
@ifreund ifreund added this to the 0.10.1 milestone Nov 26, 2022
@ifreund ifreund added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Nov 26, 2022
@nektro
Copy link
Contributor

nektro commented Nov 26, 2022

should the compile error instead then be that a .b case was added since the compiler can know it should be unreachable in this case? related #12863

@ifreund
Copy link
Member Author

ifreund commented Nov 26, 2022

No, there should be no compile error. See the existing noreturn union field behavior tests.

This example doesn't demonstrate the motivating use-case for this behavior, namely "disabling" a tagged union field based on a comptime-known value. For example

const View = union(enum) {
   xdg_toplevel: XdgToplevel,
   xwayland_view: if (build_options.xwayland) XwaylandView else noreturn,
}

@Vexu Vexu closed this as completed in 71937f7 Nov 26, 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 frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

No branches or pull requests

2 participants