Skip to content

Result type lost when nesting multiple expressions #7324

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
SpexGuy opened this issue Dec 7, 2020 · 2 comments
Closed

Result type lost when nesting multiple expressions #7324

SpexGuy opened this issue Dec 7, 2020 · 2 comments
Milestone

Comments

@SpexGuy
Copy link
Contributor

SpexGuy commented Dec 7, 2020

This was found by @devzero. I'm not sure if this is a bug report or a proposal, but it seems pretty clear to me that this should compile:

const Enum = enum { a, b };

pub fn main() !void {
    var x: usize = 4;
    var y = false;
    var e: Enum = switch(x) {
        else => blk: {
            if (y) {
                break :blk .a;
            }
            break :blk .b;
        }
    };
}

Instead it reports this error:

.\src\day08.zig:7:22: error: values of type '(enum literal)' must be comptime known
        else => blk: {
                     ^
C:\Program Files\Zig\Current\lib\zig\std\start.zig:334:40: note: referenced here
            const result = root.main() catch |err| {
                                       ^

It seems like the problem is that the named block in the switch is not inferring the result type from the switch expression. So when the compiler tries to compile the break statements, it does peer type resolution instead of result type inference and comes up with EnumLiteral. Since EnumLiterals can't be runtime values, compilation fails.

The specific behavior I think should work is:

  • result types should be inferred through if, else, and switch expressions into their sub-expressions.
  • result types should be inferred through while, for, and block expressions into their associated break statements.
@SpexGuy SpexGuy changed the title Compile error when nesting multiple expressions Result type lost when nesting multiple expressions Dec 7, 2020
@ghost
Copy link

ghost commented Dec 7, 2020

I think this is #5557

@Vexu
Copy link
Member

Vexu commented Dec 7, 2020

Duplicate of #5557, #3750, #4255 and. #4629, already fixed in stage2.

@Vexu Vexu closed this as completed Dec 7, 2020
@andrewrk andrewrk added this to the 0.8.0 milestone Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants