Skip to content

OutOfMemory and segfault when using anytype for argument in #22363

Description

@leonskij

Zig Version

0.14.0-dev.2577+271452d22

Steps to Reproduce and Observed Behavior

Whilst slimming the code down into a smaller reproducible example, I encountered another seemingly linked issue. These both appear to be emitted during compilation, though I'm not completely sure.

All of the examples below were tested with a plain zig run main.zig on x86-64 Linux.

Example 1

This example exits with code 1 and prints an OutOfMemory error to the terminal.

pub fn main() void {
    _ = initGeneric();
}

fn Generic(comptime doStuffFn: fn (arg: anytype) void) type {
    return struct {
        pub fn doStuff(arg: anytype) void {
            return doStuffFn(arg);
        }
    };
}

fn initGeneric() Generic(doStuff) {
    return .{};
}

fn doStuff(arg: anytype) void {
    _ = arg;
}

If you change the code to use concrete types instead of anytypes, then the issue is resolved.

pub fn main() void {
    _ = initGeneric();
}

fn Generic(comptime doStuffFn: fn (arg: void) void) type {
    return struct {
        pub fn doStuff(arg: void) void {
            return doStuffFn(arg);
        }
    };
}

fn initGeneric() Generic(doStuff) {
    return .{};
}

fn doStuff(arg: void) void {
    _ = arg;
}

Example 2

This example segmentation faults. The only change here is that the initGeneric function returns a value that is incompatible with its return type.

pub fn main() void {
    _ = initGeneric();
}

fn Generic(comptime doStuffFn: fn (arg: anytype) void) type {
    return struct {
        pub fn doStuff(self: @This(), arg: anytype) void {
            _ = self;
            return doStuffFn(arg);
        }
    };
}

fn initGeneric() Generic(doStuff) {
    return .{ .context = .{} };
}

fn doStuff(arg: anytype) void {
    _ = arg;
}

If you change the code to use concrete types instead of anytypes, then the issue is resolved and you instead get a correct type error related to the return of initGeneric.

pub fn main() void {
    _ = initGeneric();
}

fn Generic(comptime doStuffFn: fn (arg: void) void) type {
    return struct {
        pub fn doStuff(self: @This(), arg: void) void {
            _ = self;
            return doStuffFn(arg);
        }
    };
}

fn initGeneric() Generic(doStuff) {
    return .{ .context = .{} };
}

fn doStuff(arg: void) void {
    _ = arg;
}

Edit 1

After more searching through this issue tracker, this could be related to or duplicate of #21099 and/or #21850. If so, feel free to close this issue.

Edit 2

I tried out the above examples in Zig 0.13.0 and these were the results.

Example 1 prints the text below and then aborts (SIGABRT).

thread 191065 panic: zig compiler bug: GenericPoison
Unable to dump stack trace: debug info stripped

Example 2 segmentation faults as it does above.

Expected Behavior

The examples should work as their concretely typed alternatives do.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions