Skip to content

Compiler crashes with SIGSEGV when appending to ArrayList(*const T) with the comptime_allocator from #25198 #25214

@tristanpemble

Description

@tristanpemble

Zig Version

0.15.1

Steps to Reproduce and Observed Behavior

Related to discussions about comptime allocators in #1291 and #25198. I was able to reproduce this with any pointer, which is why the repro has *const void; it seems to have nothing to do with the fact that it was a function pointer like I thought in #25198.

https://zigbin.io/a142dd

or

const std = @import("std");
const Allocator = std.mem.Allocator;
const Alignment = std.mem.Alignment;

pub const comptime_allocator: Allocator = .{
    .ptr = undefined,
    .vtable = &.{
        .alloc = &comptimeAlloc,
        .resize = &comptimeResize,
        .remap = &comptimeRemap,
        .free = &Allocator.noFree,
    },
};

fn comptimeAlloc(_: *anyopaque, len: usize, alignment: Alignment, ra: usize) ?[*]u8 {
    _ = ra;
    var buf: [len]u8 align(alignment.toByteUnits()) = undefined;
    return &buf;
}

fn comptimeResize(_: *anyopaque, mem: []u8, alignment: Alignment, new_len: usize, ra: usize) bool {
    _ = alignment;
    _ = ra;
    return new_len <= mem.len; // allow shrinking in-place
}

fn comptimeRemap(_: *anyopaque, mem: []u8, alignment: Alignment, new_len: usize, ra: usize) ?[*]u8 {
    _ = alignment;
    _ = ra;
    return if (new_len <= mem.len) mem.ptr else null; // allow shrinking in-place
}

comptime {
    var fns: std.ArrayList(*const void) = .empty;
    fns.append(comptime_allocator, &{});
}

Expected Behavior

Either it compiles successfully or fails with a compiler 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