Skip to content

std.ArrayListUnamanged fix compile error when @sizeOf(T) == 0 #24888

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

pmovzxdq
Copy link

Fixes #24886

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

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

I think there's a better way to implement this, and arguably a unit test is the more valuable deliverable here anyway.

@pmovzxdq
Copy link
Author

I have some questions:

var list: ArrayList(void) = .empty;
try testing.expect(list.items.len == 0);
try testing.expect(list.capacity == 0); // ???

I am not sure if this is right. I think it makes more sense if list.capacity is initialized to math.maxInt(usize):

var list: ArrayList(void) = .empty;
try testing.expect(list.items.len == 0);
try testing.expect(list.capacity == math.maxInt(usize));

initCapacity() currently is like the second example:

var list = try ArrayList(void).initCapacity(testing.allocator, 200);
defer list.deinit(testing.allocator);

try testing.expect(list.items.len == 0);
try testing.expect(list.capacity == math.maxInt(usize));

Which one should be used?

You said that there is a better way to implement this.
I actually like the current fix because:

  • init_capacity is only used by growCapacity().
  • It makes sense, because init_capacity is equal to the number of T that can fit in a std.atomic.cache_line. An unlimited amount (math.maxInt(usize)) of 0 bytes (like @sizeOf(void) or @sizeOf(enum { one })) can fit into any amount of memory.

I am open to any suggestions.

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.

std.ArrayListUnamanged: init_capacity causes compile error when @sizeOf(T) == 0
2 participants