Skip to content

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

@joegm

Description

@joegm

Zig Version

0.14.1

Steps to Reproduce and Observed Behavior

From std/array_list.zig:

const init_capacity = @as(comptime_int, @max(1, std.atomic.cache_line / @sizeOf(T)));

When @sizeOf(T) == 0 this causes a compile error:

const std = @import("std");

const Unit = enum { one };
const List = std.ArrayListUnmanaged(Unit);

pub fn main() !void {
    var l = List.empty;
    try l.append(std.heap.page_allocator, .one);
}
$ zig run test.zig
/home/joe/.local/lib/zig/std/array_list.zig:1220:81: error: division by zero here causes undefined behavior
        const init_capacity = @as(comptime_int, @max(1, std.atomic.cache_line / @sizeOf(T)));
                                                                                ^~~~~~~~~~

I encountered this with v0.14.1 but the code is still there on master AFAICT.

Expected Behavior

The comptime calculation should handle a size of 0 and not compile error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions