Skip to content

Struct containing generic member over slice of Self fails to compile #5414

Closed
@ifreund

Description

@ifreund

This code fails to compile with the following error:

const std = @import("std");

fn Frog(comptime T: type) type {
    return struct { fish: T };
}

const Bear = struct {
    grizzle: Frog([]Bear),
};

pub fn main() !void {
    const my_precious = Bear{ .grizzle = undefined };
    std.debug.warn("{}", .{my_precious});
}
./src/main.zig:9:14: error: struct 'Bear' depends on itself
const Bear = struct {
             ^
./src/main.zig:10:19: note: referenced here
    grizzle: Frog([]Bear),
                  ^
./src/main.zig:10:18: note: referenced here
    grizzle: Frog([]Bear),
                 ^
./src/main.zig:15:29: note: referenced here
    const my_precious = Bear{
                            ^
/usr/lib/zig/std/start.zig:253:40: note: referenced here
            const result = root.main() catch |err| {
                                       ^

However, a small change which should produce identical code allows it to compile

const std = @import("std");

fn Frog(comptime T: type) type {
    return struct { fish: []T };
}

const Bear = struct {
    grizzle: Frog(Bear),
};

pub fn main() !void {
    const my_precious = Bear{ .grizzle = undefined };
    std.debug.warn("{}", .{my_precious});
}

In the second example Instead of passing a slice type to Frog, the struct returned by Frog always contains a slice. The first example which fails to compile is valid code as only a pointer to Bear is stored, not the struct itself, so it should be allowed to compile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorstage1The process of building from source via WebAssembly and the C backend.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions