Skip to content

Conversation

@FnControlOption
Copy link
Contributor

Fix resolved type of tag capture in inline switch case

const Enum = enum {
    foo,
    bar,
    baz,
    qux,
};

const TaggedUnion = union(Enum) {
    foo: i32,
    bar: bool,
    baz: f64,
    qux: void,
};

const some_tagged_union: TaggedUnion = .{ .baz = 3.14 };

const switch_tagged_union_inline = switch (some_tagged_union) {
    inline .foo => |a, b| .{ a, b },
    //                 ^ Enum
    inline .bar => |a, b| .{ a, b },
    //                 ^ Enum
    inline else => |a, b| .{ a, b },
    //                 ^ Enum
};

Fix resolved type of switch capture for non-enum/union types

const some_u8: u8 = 'e';

const switch_u8 = switch (some_u8) {
    'x' => |a| a,
    //      ^ u8
    'y' => |a| a,
    //      ^ u8
    else => |a| a,
    //       ^ u8
};

Resolve type of else capture in tagged union switch

const switch_tagged_union = switch (some_tagged_union) {
    .foo => |a| a,
    .bar => |a| a,
    else => |a| a,
    //       ^ TaggedUnion
};

Copy link
Member

@Techatrix Techatrix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cases should ideally also cover the _ prong on non-exhaustive enums.

const E = enum(u8) {
    foo,
    bar,
    _,
};
const some_enum: E = .foo;
const what = switch (some_enum) {
    .foo, .bar => {},
    _ => |tag| {},
};

@Techatrix Techatrix merged commit 61d3526 into zigtools:master Jun 27, 2025
6 checks passed
@FnControlOption FnControlOption deleted the switch branch June 30, 2025 22:47
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

Successfully merging this pull request may close these issues.

2 participants