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

erroneous dependency loop with root import #7979

Closed
marler8997 opened this issue Feb 7, 2021 · 1 comment
Closed

erroneous dependency loop with root import #7979

marler8997 opened this issue Feb 7, 2021 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@marler8997
Copy link
Contributor

marler8997 commented Feb 7, 2021

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

@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 Feb 7, 2021
@Vexu Vexu added this to the 0.9.0 milestone Feb 7, 2021
@marler8997 marler8997 changed the title erroneous dependency loop erroneous dependency loop with root import Feb 7, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@mikdusan
Copy link
Member

mikdusan commented Jan 4, 2023

no more compiler errors

$ zig version
0.11.0-dev.1095+8bd734d60

$ zig build-obj main.zig
# no stdout/stderr (good)

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

4 participants