Skip to content

erroneous dependency loop with root import #7979

Closed
@marler8997

Description

@marler8997

Reproduce the issue with these 2 files:

main.zig:

pub const foo = true;

usingnamespace if (@import("lib.zig").root_foo) struct {} else struct {};
usingnamespace if (@import("lib.zig").root_foo) struct {} else struct {};

pub fn main() void { }

lib.zig:

pub const root_foo = @import("root").foo;
./main.zig:4:38: error: dependency loop detected
usingnamespace if (@import("lib.zig").root_foo) struct {} else struct {};
                                     ^
./lib.zig:1:37: note: referenced here
pub const root_foo = @import("root").foo;
                                    ^
./main.zig:3:38: note: referenced here
usingnamespace if (@import("lib.zig").root_foo) struct {} else struct {};
                                     ^
/home/marler8997/zig/0.8.0-dev.1120+300ebbd56/files/lib/std/builtin.zig:669:73: note: referenced here
pub const panic: PanicFn = if (@hasDecl(root, "panic")) root.panic else default_panic;

What makes the dependency loop go away?

  1. the error goes away if you comment out one of the usingnamespace lines in main.zig
usingnamespace if (@import("lib.zig").root_foo) struct {} else struct {};
//usingnamespace if (@import("lib.zig").root_foo) struct {} else struct {};
  1. the error goes away if @import("lib.zig") is aliased and reused:
const lib = @import("lib.zig");
usingnamespace if (lib.root_foo) struct {} else struct {};
usingnamespace if (lib.root_foo) struct {} else struct {};
  1. the error goes away if lib.zig is removed and replaced with a struct:
const lib = struct { pub const root_foo = @import("root").foo; };
usingnamespace if (lib.root_foo) struct {} else struct {};
usingnamespace if (lib.root_foo) struct {} else struct {};

Use Case

This error was found when trying to use a root variable UNICODE to select different win32 symbol aliases, like CreateFile is set to CreateFileA or CreateFileW based on @import("root").UNICODE. The affected project is https://github.com/marlersoft/zigwin32

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