From 075d300342afa72f5328c0ee4232151dd0968264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20=C3=85stholm?= Date: Fri, 5 Sep 2025 17:45:22 +0200 Subject: [PATCH] Remove `std.builtin.subsystem` The subsystem detection was flaky and often incorrect and was not actually needed by the compiler or standard library. The actual subsystem won't be known until at link time, so it doesn't make sense to try to determine it at compile time. --- lib/std/builtin.zig | 26 -------------------------- lib/std/start.zig | 21 +++++++++------------ 2 files changed, 9 insertions(+), 38 deletions(-) diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index a646e9ad80..7a0284220c 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -6,32 +6,6 @@ const root = @import("root"); pub const assembly = @import("builtin/assembly.zig"); -/// `explicit_subsystem` is missing when the subsystem is automatically detected, -/// so Zig standard library has the subsystem detection logic here. This should generally be -/// used rather than `explicit_subsystem`. -/// On non-Windows targets, this is `null`. -pub const subsystem: ?std.Target.SubSystem = blk: { - if (@hasDecl(builtin, "explicit_subsystem")) break :blk builtin.explicit_subsystem; - switch (builtin.os.tag) { - .windows => { - if (builtin.is_test) { - break :blk std.Target.SubSystem.Console; - } - if (@hasDecl(root, "main") or - @hasDecl(root, "WinMain") or - @hasDecl(root, "wWinMain") or - @hasDecl(root, "WinMainCRTStartup") or - @hasDecl(root, "wWinMainCRTStartup")) - { - break :blk std.Target.SubSystem.Windows; - } else { - break :blk std.Target.SubSystem.Console; - } - }, - else => break :blk null, - } -}; - /// This data structure is used by the Zig language code generation and /// therefore must be kept in sync with the compiler implementation. pub const StackTrace = struct { diff --git a/lib/std/start.zig b/lib/std/start.zig index 7f7cc2f083..c22a36f24f 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -734,24 +734,21 @@ pub fn call_wWinMain() std.os.windows.INT { // - u32 in PEB.ProcessParameters.dwShowWindow // Since STARTUPINFO is the bottleneck for the allowed values, we use `u16` as the // type which can coerce into i32/c_int/u32 depending on how the user defines their wWinMain - // (the Win32 docs show wWinMain with `int` as the type for nCmdShow). - const nCmdShow: u16 = nCmdShow: { - // This makes Zig match the nCmdShow behavior of a C program with a WinMain symbol: + // (the Win32 docs show wWinMain with `int` as the type for nShowCmd). + const nShowCmd: u16 = nShowCmd: { + // This makes Zig match the nShowCmd behavior of a C program with a WinMain symbol: // - With STARTF_USESHOWWINDOW set in STARTUPINFO.dwFlags of the CreateProcess call: - // - Compiled with subsystem:console -> nCmdShow is always SW_SHOWDEFAULT - // - Compiled with subsystem:windows -> nCmdShow is STARTUPINFO.wShowWindow from - // the parent CreateProcess call + // - nShowCmd is STARTUPINFO.wShowWindow from the parent CreateProcess call // - With STARTF_USESHOWWINDOW unset: - // - nCmdShow is always SW_SHOWDEFAULT + // - nShowCmd is always SW_SHOWDEFAULT const SW_SHOWDEFAULT = 10; const STARTF_USESHOWWINDOW = 1; - // root having a wWinMain means that std.builtin.subsystem will always have a non-null value. - if (std.builtin.subsystem.? == .Windows and peb.ProcessParameters.dwFlags & STARTF_USESHOWWINDOW != 0) { - break :nCmdShow @truncate(peb.ProcessParameters.dwShowWindow); + if (peb.ProcessParameters.dwFlags & STARTF_USESHOWWINDOW != 0) { + break :nShowCmd @truncate(peb.ProcessParameters.dwShowWindow); } - break :nCmdShow SW_SHOWDEFAULT; + break :nShowCmd SW_SHOWDEFAULT; }; // second parameter hPrevInstance, MSDN: "This parameter is always NULL" - return root.wWinMain(hInstance, null, lpCmdLine, nCmdShow); + return root.wWinMain(hInstance, null, lpCmdLine, nShowCmd); }