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

struct depends on itself #10659

Closed
ayende opened this issue Jan 21, 2022 · 4 comments
Closed

struct depends on itself #10659

ayende opened this issue Jan 21, 2022 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@ayende
Copy link
Contributor

ayende commented Jan 21, 2022

Zig Version

0.9

Steps to Reproduce

The following code fails to compile (can see it live here: https://godbolt.org/z/vrqj7Pz5f).

const std = @import("std");
const builtin = @import("builtin");

const os = std.os;
const mem = std.mem;
const testing = std.testing;


pub fn DoublyLinkedList(comptime T: type) type {
    return struct {
        const Self = @This();

        pub const Node = struct {
            next: ?*Node,
            prev: ?*Node,
            item: T,

        };

        head: ?*Node = null,
        tail: ?*Node = null,

    };
}
    pub const Writer = struct {
        cache: Lru,
        completed: std.atomic.Atomic(?*WriteState),

        const WriteState = struct {
            list: *Lru.List = undefined,
            writer: ? *Writer,
        };

        const Lru = struct {
            const List = struct {
                list: DoublyLinkedList(*WriteState) ,
                used: usize = 0,
                max: usize,
            };

            young: List,

            pub fn init() Lru {
                return .{.young = .{.list = .{}, .max = 1}};
            }

            pub fn deinit(self:* Lru, allocator: std.mem.Allocator) void {
                _ = allocator;
                _ = self;
            }
        };
    };


pub fn main() void {
        var lru = Writer.Lru.init();
        defer lru.deinit(testing.allocator);
}

Expected Behavior

This should compile successfully. There is only a dependency on the pointer, not the actual struct, so the size is known.
If I remove the std.atomic.Atomic, it also compiles.

Actual Behavior

./example.zig:29:28: error: struct 'WriteState' depends on itself
        const WriteState = struct {
                           ^
./example.zig:31:13: note: while checking this field
            writer: ? *Writer,
            ^
./example.zig:16:13: note: while checking this field
            item: T,
            ^
./example.zig:20:9: note: while checking this field
        head: ?*Node = null,
        ^
./example.zig:36:17: note: while checking this field
                list: DoublyLinkedList(*WriteState) ,
                ^
./example.zig:41:13: note: while checking this field
            young: List,
            ^
@ayende ayende added the bug Observed behavior contradicts documented or intended behavior label Jan 21, 2022
@nektro
Copy link
Contributor

nektro commented Jan 21, 2022

please add the zig language code to the code block and run it through zig fmt

@marler8997
Copy link
Contributor

It took a few minutes but I minified your example:

const std = @import("std");

const A = struct {
    b: std.atomic.Atomic(?*B),
};
const B = struct {
    a: ?*A,
};

pub fn main() void {
    const b: B = undefined;
    _ = b;
}

This looks like it could be a bug with the Zig compiler, or some sort of problem with circular references used in conjunction with Atomic?

@emidoots
Copy link

Here's a simpler example:

fn Atomic(comptime T: type) type {
    return T;
}

const A = struct {
    b: Atomic(?*B),
};
const B = struct {
    a: ?*A,
};

pub fn main() void {
    const b: B = undefined;
    _ = b;
}

If you change Atomic(?*B) in the above example to just ?*B the behavior changes, when logically it shouldn't. The error no longer occurs.

@ominitay
Copy link
Contributor

Duplicate of #4562?

@Vexu Vexu closed this as completed Jan 29, 2022
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

6 participants