zig icon indicating copy to clipboard operation
zig copied to clipboard

(Windows only) Putting "std.io.getStdOut().writer()" variable globally leads to compile error

Open poypoyan opened this issue 1 year ago • 0 comments

Zig Version

0.12.0

Steps to Reproduce and Observed Behavior

In a brand new Windows 11 laptop, this code leads to compile error:

const std = @import("std");
const stdout = std.io.getStdOut().writer(); // <--- relevant

pub fn main() !void {
    try stdout.print("Hello, {s}!\n", .{"world"});
}

Here's the error:

zig-windows-x86_64-0.12.0\lib\std\os\windows.zig:2026:28: error: unable to evaluate comptime expression
                break :blk asm (
                           ^~~
zig-windows-x86_64-0.12.0\lib\std\os\windows.zig:2041:15: note: called from here
    return teb().ProcessEnvironmentBlock;
           ~~~^~
zig-windows-x86_64-0.12.0\lib\std\io.zig:23:27: note: called from here
        return windows.peb().ProcessParameters.hStdOutput;
               ~~~~~~~~~~~^~
zig-windows-x86_64-0.12.0\lib\std\io.zig:34:40: note: called from here
    return .{ .handle = getStdOutHandle() };
                        ~~~~~~~~~~~~~~~^~
hello.zig:2:32: note: called from here
const stdout = std.io.getStdOut().writer();
               ~~~~~~~~~~~~~~~~^~
referenced by:
    main: hello.zig:5:9
    callMain: zig-windows-x86_64-0.12.0\lib\std\start.zig:511:32
    WinStartup: zig-windows-x86_64-0.12.0\lib\std\start.zig:350:45

In my Linux mint laptop, this compiles successfully.

For now, the fix in Windows is to put the const stdout inside the main().

Expected Behavior

Code should compile successfully (I guess).

poypoyan avatar May 05 '24 11:05 poypoyan