When i deleted 2 lines a Dependency Loop appeared and I cannot solve it

It would look more like this:

const A = struct { B };
const C = struct { b: B };

const B = struct {
    ptr: *anyopaque,
    d: *const D,
    pub fn do(self: B, args: A) void {
        return self.d.func(self.ptr, args);
    }
};

const D = struct {
    func: *const fn (ctx: *anyopaque, args: A) void,
};

fn funky(ctx: *anyopaque, args: A) void {
    _ = .{ ctx, args };
}

test "deploop" {
    const a: A = .{ .b = undefined };
    const c: C = .{ .b = .{ .ptr = @ptrCast(@alignCast(@constCast(&A))), .d = &.{ .func = &funky } } };
    _ = .{ a, c };
    try std.testing.expect(true);
}

const std = @import("std");

Try it and see what you think.

Made a small change const a: A = .{undefined}; as it’s a tuple.

The test was considered a circular dependency in version 0.15.2, but it passed successfully in 0.16.0-dev.1657+985a3565c. I think this issue has been fixed in the latest version of Zig.

Supplement: I found that the “equivalent code” I provided earlier is also detected as a dependency cycle in version 0.15.2, I had only tested it on the nightly version before.

So I think the OP’s issue will no longer exist after upgrading Zig.

3 Likes