Skip to content

Spurious "dependency loop detected" and "missing struct field" errors #17255

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

Open
dweiller opened this issue Sep 24, 2023 · 4 comments
Open

Spurious "dependency loop detected" and "missing struct field" errors #17255

dweiller opened this issue Sep 24, 2023 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. regression It worked in a previous version of Zig, but stopped working.
Milestone

Comments

@dweiller
Copy link
Contributor

dweiller commented Sep 24, 2023

Zig Version

0.12.0-dev.494+a8d2ed806

Steps to Reproduce and Observed Behavior

Clone https://github.com/dweiller/zimalloc and run

git checkout d577c4a3
zig build

I will try to make a smaller repro case and bisect the Zig commit introducing it when I get time (and llvm17 finishes compiling). A minimal repro case is below in #17255 (comment).

Here is the full error message:

Build Summary: 1/4 steps succeeded; 1 failed (disable with --summary none)
test transitive failure
└─ run test transitive failure
   └─ zig test Debug native 3 errors
src/Segment.zig:9:5: error: dependency loop detected
pub const Ptr = *align(segment_alignment) @This();
~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/zimalloc.zig:50:32: error: missing struct field: thread_heaps
    var gpa = Allocator(config){};
              ~~~~~~~~~~~~~~~~~^~
src/zimalloc.zig:50:32: note: missing struct field: thread_heaps_lock
src/allocator.zig:8:12: note: struct 'allocator.Allocator(.{.thread_data_prealloc = 128, .thread_safe = true, .safety_checks = false})' declared here
    return struct {
           ^~~~~~
src/allocator.zig:10:79: error: missing struct field: prealloc_segment
        thread_heaps: std.SegmentedList(Heap, config.thread_data_prealloc) = .{},
                                                                             ~^~
src/allocator.zig:10:79: note: missing struct field: dynamic_segments
src/allocator.zig:10:79: note: missing struct field: len
/path/to/zig/zig-linux-x86_64-0.12.0-dev.494+a8d2ed806/lib/std/segmented_list.zig:79:12: note: struct 'segmented_list.SegmentedList(Heap,128)' declared here
    return struct {
           ^~~~~~

These errors are not encountered when compiling with 0.12.0-dev.464+a63a1c5cb (the commit that merged llvm17, though I can't find the tarball for this to test locally now, but my CI worked using it), so it seems the issue was introduced between that commit and when #17172 got merged.

The dependency loop issue shouldn't be the same issue as other dependency issue in the issue tracker (e.g. #16932, #16419, #14353) which are all about function pointers and affect earlier compiler versions than 0.12.0-dev.464+a63a1c5cb.

Expected Behavior

The code should compile as far as I can tell - the dependency loop is (according to the compile error) caused by a decl that is creates a type alias like pub const Ptr = *align(alignment) @This(); and the missing struct fields all have default values.

@dweiller dweiller added the bug Observed behavior contradicts documented or intended behavior label Sep 24, 2023
@Vexu
Copy link
Member

Vexu commented Sep 25, 2023

a8d2ed8 merged #17172 which is likely to be the cause.

@Vexu Vexu added regression It worked in a previous version of Zig, but stopped working. frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Sep 25, 2023
@Vexu Vexu added this to the 0.13.0 milestone Sep 25, 2023
@dweiller

This comment was marked as outdated.

@dweiller

This comment was marked as outdated.

@dweiller
Copy link
Contributor Author

dweiller commented Oct 16, 2023

I have managed to make a minimal repro case:

const Slab = struct {
    pub const Ptr = *align(1 << 16) Slab;

    pub const List = struct {
        head: ?Ptr = null,
    };

    next: Ptr,
};

const MeshingPool = struct {
    partial_slabs: Slab.List = .{},
};

test {
    _ = MeshingPool{};
}

If you run zig test on the above the compiler reports:

repro.zig:2:9: error: dependency loop detected
    pub const Ptr = *align(1 << 16) Slab;
    ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I have 6 different 1-line diffs that allow the above case to compile successfully:

@@ -1,5 +1,5 @@
 const Slab = struct {
-    pub const Ptr = *align(1 << 16) Slab;
+    pub const Ptr = *Slab;
 
     pub const List = struct {
         head: ?Ptr = null,
@@ -2,7 +2,6 @@
     pub const Ptr = *align(1 << 16) Slab;
 
     pub const List = struct {
-        head: ?Ptr = null,
     };
@@ -2,7 +2,7 @@
     pub const Ptr = *align(1 << 16) Slab;
 
     pub const List = struct {
-        head: ?Ptr = null,
+        head: ?*align(1 << 16) Slab = null,
     };
 
     next: Ptr,
@@ -5,7 +5,6 @@
         head: ?Ptr = null,
     };
 
-    next: Ptr,
 };
@@ -5,7 +5,7 @@
         head: ?Ptr = null,
     };
 
-    next: Ptr,
+    next: *align(1 << 16) Slab,
 };
 
 const MeshingPool = struct {
@@ -13,5 +13,5 @@
 };
 
 test {
-    _ = MeshingPool{};
+    _ = MeshingPool;
 }

(I wasn't sure if the it's surprising that the last diff changes whether it compiles or not).

dweiller added a commit to dweiller/zig-mesh that referenced this issue Nov 2, 2023
This is a workaround and should be reverted when
ziglang/zig#17255 is resolved.
@andrewrk andrewrk modified the milestones: 0.14.0, 0.14.1 Mar 1, 2025
@andrewrk andrewrk modified the milestones: 0.14.1, 0.15.0 Apr 15, 2025
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 frontend Tokenization, parsing, AstGen, Sema, and Liveness. regression It worked in a previous version of Zig, but stopped working.
Projects
None yet
Development

No branches or pull requests

3 participants