Commit 075d300342

Carl Åstholm <carl@astholm.se>
2025-09-05 17:45:22
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.
1 parent 74900e9
Changed files (2)
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 {
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);
 }