Skip to content
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

OutOfMemory and segfault when using anytype for argument in #22363

Open
leonskidev opened this issue Dec 30, 2024 · 0 comments
Open

OutOfMemory and segfault when using anytype for argument in #22363

leonskidev opened this issue Dec 30, 2024 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@leonskidev
Copy link

leonskidev commented Dec 30, 2024

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.

@leonskidev leonskidev added the bug Observed behavior contradicts documented or intended behavior label Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

1 participant