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

langref: improve zero bit types documentation #14385

Closed
wants to merge 1 commit into from

Conversation

perillo
Copy link
Contributor

@perillo perillo commented Jan 20, 2023

In the Zero Bit Types section, add the empty enum, empty struct and empty union. Clarify that a non empty zero bit union just needs to have all fields of zero bit type, instead of only one field of zero bit type.

Add a doctest.

Copy link
Member

@Vexu Vexu left a comment

Choose a reason for hiding this comment

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

Empty enums and unions are not zero-bit types but noreturn types. Their behavior is a bit buggy currently though #13807.

@perillo
Copy link
Contributor Author

perillo commented Jan 20, 2023

The following executable:

const std = @import("std");
const debug = std.debug;

pub fn main() void {
    inline for (.{
        enum {},
        union {},
    }) |typ| {
        debug.print("typ: {}\n", .{typ});
        debug.print("@typeInfo(typ): {}\n", .{@typeInfo(typ)});
    }
}

prints:

typ: zero_bit_types.main__enum_3928
@typeInfo(typ): builtin.Type{ .Enum = builtin.Type.Enum{ .tag_type = u0, .fields = {  }, .decls = {  }, .is_exhaustive = true } }
typ: zero_bit_types.main__union_3929
@typeInfo(typ): builtin.Type{ .Union = builtin.Type.Union{ .layout = builtin.Type.ContainerLayout.Auto, .tag_type = null, .fields = {  }, .decls = {  } } }

Thanks.

@perillo
Copy link
Contributor Author

perillo commented Jan 20, 2023

I also tried the examples in #13807, with zig 0.11.0-dev.1327+b42bd759a.

The empty error set causes a segmentation fault, instead of a panic:
LLVM Emit Object... zsh: segmentation fault (core dumped) zig build-lib lib.zig

What about adding the empty error set to the list, and document the current limitations/issues?

P.S.:
What does it mean:
"Empty enums and unions are not currently recognized as noreturn types so this example compiles just fine but if recognized as such then it hits the same issues."

How can the compiler change the way it recognizes empty enums and unions?

@Vexu
Copy link
Member

Vexu commented Jan 22, 2023

What about adding the empty error set to the list, and document the current limitations/issues?

An empty error set is also a noreturn type, not a zero-bit type.

P.S.: What does it mean: "Empty enums and unions are not currently recognized as noreturn types so this example compiles just fine but if recognized as such then it hits the same issues."

How can the compiler change the way it recognizes empty enums and unions?

Noreturn types affect the semantic analysis and empty enums and unions currently behave incorrectly because I'm unsure what the proper behavior should be.

@perillo
Copy link
Contributor Author

perillo commented Jan 22, 2023

Why not just disallow empty struct, empty union and empty enum?

@Vexu
Copy link
Member

Vexu commented Jan 22, 2023

See #3257

@perillo
Copy link
Contributor Author

perillo commented Jan 23, 2023

I removed the empty struct, enum and union from the list and updated the commit message.

At the end of the section I added a clarification about problematic empty types.

@perillo perillo requested a review from Vexu January 23, 2023 08:46
doc/langref.html.in Outdated Show resolved Hide resolved
In the Zero Bit Types section, clarify that a non empty zero bit union
just needs to have all fields of zero bit type, instead of only one
field of zero bit type.

Clarify that empty struct, enum, union and error set are currently
problematic.
@andrewrk andrewrk closed this Jun 17, 2023
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.

None yet

3 participants