Skip to content

Support allocating 0 bit types #6239

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

Merged
merged 2 commits into from
Sep 6, 2020
Merged

Support allocating 0 bit types #6239

merged 2 commits into from
Sep 6, 2020

Conversation

pfgithub
Copy link
Contributor

@pfgithub pfgithub commented Sep 3, 2020

This is useful for generic functions so 0 bit types don't have to be special cased every time. The current code errors because *const T cannot be assigned to *T.

@andrewrk
Copy link
Member

andrewrk commented Sep 3, 2020

I think this used to work, and regressed with some recent changes. Would you mind adding a small test to make sure it doesn't regress again?

@pfgithub
Copy link
Contributor Author

pfgithub commented Sep 3, 2020

stage1 doesn't really seem to like having a slice of 0 bit pointers very much

    var zero_bit_slice = try allocator.alloc(*u0, 0);
    testing.expect(zero_bit_slice.len == 0);

    zero_bit_slice = try allocator.realloc(zero_bit_slice, 100);
    testing.expect(zero_bit_slice.len == 100);

    for (zero_bit_slice) |*item| {
        item.* = try allocator.create(u0);
        item.*.* = 0;
    }
    for (zero_bit_slice) |item| {
        allocator.destroy(item);
    }
    allocator.free(zero_bit_slice);
Assertion failed at /deps/zig/src/analyze.cpp:615 in get_pointer_to_type_extra2. This is a bug in the Zig compiler.
Unable to dump stack trace: debug info stripped
fish: “zig test test.zig” terminated by signal SIGABRT (Abort)
            assert(byte_alignment == 0);
            entry->abi_size = 0;
            entry->size_in_bits = 0;
            entry->abi_align = 0;

I guess the byte alignment of *u0 is being set to 1, and it can't be manually set

./test.zig:36:53: error: alignment must be >= 1
    var zero_bit_slice = try allocator.alloc(*align(0) u0, 0);
                                                    ^

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