ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
35.07k stars 2.56k forks source link

Build options: passing structs fails #19594

Open lbirkert opened 7 months ago

lbirkert commented 7 months ago

Zig Version

0.12.0-dev.3597+d979df585

Steps to Reproduce and Observed Behavior

git clone https://github.com/lbirkert/issue_zig_options
zig build run

crashes with

$ zig build run
run
└─ run issue_zig_options
   └─ install
      └─ install issue_zig_options
         └─ zig build-exe issue_zig_options Debug native 1 errors
zig-cache/c/c7bcb4d097e4baeff8cd8d531d51090a/options.zig:1:57: error: expected expression, found 'pub'
pub const @"test": []const build.Test = &[_]build.Test {
                                                        ^
error: the following command failed with 1 compilation errors:
/usr/lib/zig/zig build-exe -ODebug --dep options -Mroot=/home/lbirkert/Projects/issue_zig_options/src/main.zig -Moptions=/home/lbirkert/Projects/issue_zig_options/zig-cache/c/c7bcb4d097e4baeff8cd8d531d51090a/options.zig --cache-dir /home/lbirkert/Projects/issue_zig_options/zig-cache --global-cache-dir /home/lbirkert/.cache/zig --name issue_zig_options --listen=-
Build Summary: 1/6 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run issue_zig_options transitive failure
   ├─ zig build-exe issue_zig_options Debug native 1 errors
   └─ install transitive failure
      └─ install issue_zig_options transitive failure
         └─ zig build-exe issue_zig_options Debug native (+1 more reused dependencies)
error: the following build command failed with exit code 1:
/home/lbirkert/Projects/issue_zig_options/zig-cache/o/0abbb8db28aae576395c817b802d44b8/build /usr/lib/zig/zig /home/lbirkert/Projects/issue_zig_options /home/lbirkert/Projects/issue_zig_options/zig-cache /home/lbirkert/.cache/zig --seed 0x52ce0923 -Zac9cc083f3c12f62 run

options.zig:

pub const @"test": []const build.Test = &[_]build.Test {
        pub const @"build.Test" = struct {
    };
};

neofetch:

$ neofetch
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢰⡆⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄  lbirkert@laptop
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢠⣿⣿⡄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄  
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢀⣾⣿⣿⣿⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄    · Archcraft x86_64
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣼⣿⣿⣿⣿⣷⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄    · LIFEBOOK A557
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣼⣿⣿⣿⣿⣿⣿⣷⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄    · 6.8.2-arch2-1
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢼⣿⣿⣿⣿⣿⣿⣿⣿⣧⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄    · 4 hours, 43 mins
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣰⣤⣈⠻⢿⣿⣿⣿⣿⣿⣿⣧⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄    · 1454 (pacman), 13 (flatpak)
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣰⣿⣿⣿⣿⣮⣿⣿⣿⣿⣿⣿⣿⣧⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄    · zsh 5.9
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄    · 1920x1080
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄    · bspwm
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄    · Arc-Dark [GTK2/3]
⠄⠄⠄⠄⠄⠄⠄⠄⠄⣼⣿⣿⣿⣿⣿⡿⣿⣿⡟⠄⠄⠸⣿⣿⡿⣿⣿⣿⣿⣿⣷⡀⠄⠄⠄⠄⠄⠄⠄⠄    · Arc-Circle [GTK2/3]
⠄⠄⠄⠄⠄⠄⠄⠄⣼⣿⣿⣿⣿⣿⡏⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠈⣿⣿⣿⣿⣿⣷⡀⠄⠄⠄⠄⠄⠄⠄    · alacritty
⠄⠄⠄⠄⠄⠄⢀⣼⣿⣿⣿⣿⣿⣿⡗⠄⠄⠄⢀⣠⣤⣀⠄⠄⠄⠸⣿⣿⣿⣿⣿⣿⣷⡀⠄⠄⠄⠄⠄⠄    · Intel i5-7200U (4) @ 3.100GHz
⠄⠄⠄⠄⠄⢀⣾⣿⣿⣿⣿⣿⡏⠁⠄⠄⠄⢠⣿⣿⣿⣿⡇⠄⠄⠄⠄⢙⣿⣿⣻⠿⣿⣷⡀⠄⠄⠄⠄⠄    · Intel HD Graphics 620
⠄⠄⠄⠄⢀⣾⣿⣿⣿⣿⣿⣿⣷⣤⡀⠄⠄⠄⠻⣿⣿⡿⠃⠄⠄⠄⢀⣼⣿⣿⣿⣿⣦⣌⠙⠄⠄⠄⠄⠄    · 3811MiB / 15894MiB
⠄⠄⠄⢠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡀⠄⠄⠄
⠄⠄⢠⣿⣿⣿⣿⣿⣿⣿⡿⠟⠋⠁⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠙⠻⣿⣿⣿⣿⣿⣿⣿⣿⡄⠄⠄
⠄⣠⣿⣿⣿⣿⠿⠛⠋⠁⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠉⠙⠻⢿⣿⣿⣿⣿⣆⠄
⡰⠟⠛⠉⠁⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠉⠙⠛⠿⢆

Expected Behavior

One should be able to pass structs as build options. The zig compiler seems to currently generate faulty code in the options.zig file. I believe the declaration of the struct should happen earlier in the file. Expected code:

pub const @"build.Test" = struct {};
pub const @"test": []const build.Test = &[_]build.Test {
    .{},
};
lbirkert commented 7 months ago

The code that puts the declaration right inside the struct lies within here: https://github.com/ziglang/zig/blob/master/lib/std/Build/Step/Options.zig#L245

I am unsure whether defining such type declarations inside a struct is planned (for that the module would work as expected and we would only have to wait for an implementation), or whether putting the declaration inside the struct is a mistake.