Skip to content

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

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

Closed
ifreund opened this issue May 23, 2020 · 1 comment
Closed
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@ifreund
Copy link
Member

ifreund commented May 23, 2020

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.

@Vexu Vexu added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels May 24, 2020
@Vexu Vexu added this to the 0.7.0 milestone May 24, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@Vexu
Copy link
Member

Vexu commented Jan 21, 2023

Fixed in self-hosted, tested in test/behavior/struct.zig:test "no dependency loop on optional field wrapped in generic function".

@Vexu Vexu closed this as completed Jan 21, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.11.0 Jan 22, 2023
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 stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

3 participants